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