Completed
Push — master ( a20c7b...d885ca )
by Nate
02:36
created

AssociationQuery::applySiteConditions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-element-lists/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-element-lists/
7
 */
8
9
namespace flipbox\craft\element\lists\queries;
10
11
use Craft;
12
use craft\base\ElementInterface;
13
use craft\db\QueryAbortedException;
14
use craft\helpers\Db;
15
use flipbox\craft\element\lists\records\Association;
16
use flipbox\craft\ember\helpers\QueryHelper;
17
use flipbox\craft\ember\queries\AuditAttributesTrait;
18
use flipbox\craft\ember\queries\CacheableActiveQuery;
19
use flipbox\craft\ember\queries\FieldAttributeTrait;
20
use flipbox\craft\ember\queries\SiteAttributeTrait;
21
use yii\db\Query;
22
23
/**
24
 * @author Flipbox Factory <[email protected]>
25
 * @since 2.0.0
26
 *
27
 * @method Association[] getCachedResult()
28
 * @method Association[] all()
29
 * @method Association one()
30
 */
31
class AssociationQuery extends CacheableActiveQuery
32
{
33
    use AuditAttributesTrait,
34
        FieldAttributeTrait,
35
        SiteAttributeTrait;
36
37
    /**
38
     * @var int|null Sort order
39
     */
40
    public $sortOrder;
41
42
    /**
43
     * @var string|string[]|null
44
     */
45
    public $target;
46
47
    /**
48
     * @var string|string[]|null
49
     */
50
    public $source;
51
52
    /**
53
     * @param $value
54
     * @return $this
55
     */
56
    public function sortOrder($value)
57
    {
58
        $this->sortOrder = $value;
59
        return $this;
60
    }
61
62
    /**
63
     * @param $value
64
     * @return $this
65
     */
66
    public function setSortOrder($value)
67
    {
68
        return $this->sortOrder($value);
69
    }
70
71
    /**
72
     * @param int|int[]|string|string[]|ElementInterface|ElementInterface[]|false|null $value
73
     * @return static
74
     */
75
    public function setTargetId($value)
76
    {
77
        return $this->setTarget($value);
78
    }
79
80
    /**
81
     * @param int|int[]|string|string[]|ElementInterface|ElementInterface[]|false|null $value
82
     * @return static
83
     */
84
    public function targetId($value)
85
    {
86
        return $this->setTarget($value);
87
    }
88
89
    /**
90
     * @param int|int[]|string|string[]|ElementInterface|ElementInterface[]|false|null $value
91
     * @return static
92
     */
93
    public function setTarget($value)
94
    {
95
        $this->target = $value;
96
        return $this;
97
    }
98
99
    /**
100
     * @param int|int[]|string|string[]|ElementInterface|ElementInterface[]|false|null $value
101
     * @return static
102
     */
103
    public function target($value)
104
    {
105
        return $this->setTarget($value);
106
    }
107
108
109
    /**
110
     * @param int|int[]|string|string[]|ElementInterface|ElementInterface[]|false|null $value
111
     * @return static
112
     */
113
    public function setSourceId($value)
114
    {
115
        return $this->setSource($value);
116
    }
117
118
    /**
119
     * @param int|int[]|string|string[]|ElementInterface|ElementInterface[]|false|null $value
120
     * @return static
121
     */
122
    public function sourceId($value)
123
    {
124
        return $this->setSource($value);
125
    }
126
127
    /**
128
     * @param int|int[]|string|string[]|ElementInterface|ElementInterface[]|false|null $value
129
     * @return static
130
     */
131
    public function setSource($value)
132
    {
133
        $this->source = $value;
134
        return $this;
135
    }
136
137
    /**
138
     * @param int|int[]|string|string[]|ElementInterface|ElementInterface[]|false|null $value
139
     * @return static
140
     */
141
    public function source($value)
142
    {
143
        return $this->setSource($value);
144
    }
145
146
    /**
147
     * @inheritdoc
148
     */
149
    public function init()
150
    {
151
        parent::init();
152
153
        if ($this->select === null) {
154
            $this->select = ['*'];
155
        }
156
157
        if ($this->orderBy === null) {
158
            $this->orderBy = ['sortOrder' => SORT_ASC];
159
        }
160
    }
161
162
    /**
163
     * @inheritdoc
164
     * @throws QueryAbortedException
165
     */
166
    public function prepare($builder)
167
    {
168
        // Is the query already doomed?
169
        if (($this->field !== null && empty($this->field)) ||
170
            ($this->target !== null && empty($this->target)) ||
171
            ($this->source !== null && empty($this->source))
172
        ) {
173
            throw new QueryAbortedException();
174
        }
175
176
        $this->applyFieldConditions();
177
        $this->applySiteConditions();
178
        $this->applyAuditAttributeConditions();
179
180
        if ($this->sortOrder !== null) {
181
            $this->andWhere(Db::parseParam('sortOrder', $this->sortOrder));
182
        }
183
184
        if ($this->target !== null) {
185
            $this->andWhere(Db::parseParam('targetId', $this->parseElementValue($this->target)));
186
        }
187
188
        if ($this->source !== null) {
189
            $this->andWhere(Db::parseParam('sourceId', $this->parseElementValue($this->source)));
190
        }
191
192
        return parent::prepare($builder);
193
    }
194
195
    /**
196
     * Apply attribute conditions
197
     */
198
    protected function applyFieldConditions()
199
    {
200
        if ($this->field !== null) {
201
            $this->andWhere(Db::parseParam('fieldId', $this->parseFieldValue($this->field)));
202
        }
203
    }
204
205
    /**
206
     * Apply attribute conditions
207
     */
208
    protected function applySiteConditions()
209
    {
210
        if ($this->site !== null) {
211
            $this->andWhere(Db::parseParam('siteId', $this->parseSiteValue($this->site)));
212
        } else {
213
            $this->andWhere(Db::parseParam('siteId', Craft::$app->getSites()->currentSite->id));
214
        }
215
    }
216
217
    /**
218
     * @param $value
219
     * @return array|string
220
     */
221
    protected function parseElementValue($value)
222
    {
223
        return QueryHelper::prepareParam(
224
            $value,
225
            function (string $uri) {
226
                $value = (new Query())
227
                    ->select(['id'])
228
                    ->from(['{{%elements_sites}} elements_sites'])
229
                    ->where(['uri' => $uri])
230
                    ->scalar();
231
                return empty($value) ? false : $value;
232
            }
233
        );
234
    }
235
}
236