/
CategoryQuery.php
326 lines (296 loc) · 9.17 KB
/
CategoryQuery.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\elements\db;
use Craft;
use craft\db\Query;
use craft\db\QueryAbortedException;
use craft\db\Table;
use craft\elements\Category;
use craft\helpers\ArrayHelper;
use craft\helpers\Db;
use craft\helpers\StringHelper;
use craft\models\CategoryGroup;
use yii\db\Connection;
/**
* CategoryQuery represents a SELECT SQL statement for categories in a way that is independent of DBMS.
*
* @property-write string|string[]|CategoryGroup|null $group The category group(s) that resulting categories must belong to
* @method Category[]|array all($db = null)
* @method Category|array|null one($db = null)
* @method Category|array|null nth(int $n, ?Connection $db = null)
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0.0
* @doc-path categories.md
* @supports-structure-params
* @supports-site-params
* @supports-title-param
* @supports-slug-param
* @supports-uri-param
* @supports-status-param
* @replace {element} category
* @replace {elements} categories
* @replace {twig-method} craft.categories()
* @replace {myElement} myCategory
* @replace {element-class} \craft\elements\Category
*/
class CategoryQuery extends ElementQuery
{
// General parameters
// -------------------------------------------------------------------------
/**
* @var bool Whether to only return categories that the user has permission to edit.
* @used-by editable()
*/
public bool $editable = false;
/**
* @var mixed The category group ID(s) that the resulting categories must be in.
* @used-by group()
* @used-by groupId()
*/
public mixed $groupId = null;
/**
* @inheritdoc
*/
public function __set($name, $value)
{
if ($name === 'group') {
$this->group($value);
} else {
parent::__set($name, $value);
}
}
/**
* @inheritdoc
*/
public function init(): void
{
if (!isset($this->withStructure)) {
$this->withStructure = true;
}
parent::init();
}
/**
* Sets the [[$editable]] property.
*
* @param bool $value The property value (defaults to true)
* @return self self reference
* @uses $editable
*/
public function editable(bool $value = true): self
{
$this->editable = $value;
return $this;
}
/**
* Narrows the query results based on the category groups the categories belong to.
*
* Possible values include:
*
* | Value | Fetches categories…
* | - | -
* | `'foo'` | in a group with a handle of `foo`.
* | `'not foo'` | not in a group with a handle of `foo`.
* | `['foo', 'bar']` | in a group with a handle of `foo` or `bar`.
* | `['not', 'foo', 'bar']` | not in a group with a handle of `foo` or `bar`.
* | a [[CategoryGroup|CategoryGroup]] object | in a group represented by the object.
*
* ---
*
* ```twig
* {# Fetch categories in the Foo group #}
* {% set {elements-var} = {twig-method}
* .group('foo')
* .all() %}
* ```
*
* ```php
* // Fetch categories in the Foo group
* ${elements-var} = {php-method}
* ->group('foo')
* ->all();
* ```
*
* @param mixed $value The property value
* @return self self reference
* @uses $groupId
*/
public function group(mixed $value): self
{
if ($value instanceof CategoryGroup) {
// Special case for a single category group, since we also want to capture the structure ID
$this->structureId = ($value->structureId ?: false);
$this->groupId = [$value->id];
} elseif (Db::normalizeParam($value, function($item) {
if (is_string($item)) {
$item = Craft::$app->getCategories()->getGroupByHandle($item);
}
return $item instanceof CategoryGroup ? $item->id : null;
})) {
$this->groupId = $value;
} else {
$this->groupId = (new Query())
->select(['id'])
->from(Table::CATEGORYGROUPS)
->where(Db::parseParam('handle', $value))
->column();
}
return $this;
}
/**
* Narrows the query results based on the category groups the categories belong to, per the groups’ IDs.
*
* Possible values include:
*
* | Value | Fetches categories…
* | - | -
* | `1` | in a group with an ID of 1.
* | `'not 1'` | not in a group with an ID of 1.
* | `[1, 2]` | in a group with an ID of 1 or 2.
* | `['not', 1, 2]` | not in a group with an ID of 1 or 2.
*
* ---
*
* ```twig
* {# Fetch categories in the group with an ID of 1 #}
* {% set {elements-var} = {twig-method}
* .groupId(1)
* .all() %}
* ```
*
* ```php
* // Fetch categories in the group with an ID of 1
* ${elements-var} = {php-method}
* ->groupId(1)
* ->all();
* ```
*
* @param mixed $value The property value
* @return self self reference
* @uses $groupId
*/
public function groupId(mixed $value): self
{
$this->groupId = $value;
return $this;
}
/**
* @inheritdoc
*/
protected function beforePrepare(): bool
{
$this->_normalizeGroupId();
// See if 'group' was set to an invalid handle
if ($this->groupId === []) {
return false;
}
$this->joinElementTable(Table::CATEGORIES);
$this->query->select([
'categories.groupId',
]);
$this->_applyEditableParam();
$this->_applyGroupIdParam();
$this->_applyRefParam();
return parent::beforePrepare();
}
/**
* Applies the 'editable' param to the query being prepared.
*
* @throws QueryAbortedException
*/
private function _applyEditableParam(): void
{
if ($this->editable) {
// Limit the query to only the category groups the user has permission to edit
$this->subQuery->andWhere([
'categories.groupId' => Craft::$app->getCategories()->getEditableGroupIds(),
]);
}
}
/**
* Applies the 'groupId' param to the query being prepared.
*/
private function _applyGroupIdParam(): void
{
if ($this->groupId) {
$this->subQuery->andWhere(['categories.groupId' => $this->groupId]);
// Should we set the structureId param?
if (!isset($this->structureId) && count($this->groupId) === 1) {
$structureId = (new Query())
->select(['structureId'])
->from([Table::CATEGORYGROUPS])
->where(Db::parseNumericParam('id', $this->groupId))
->scalar();
$this->structureId = (int)$structureId ?: false;
}
}
}
/**
* Normalizes the groupId param to an array of IDs or null
*/
private function _normalizeGroupId(): void
{
if (empty($this->groupId)) {
$this->groupId = is_array($this->groupId) ? [] : null;
} elseif (is_numeric($this->groupId)) {
$this->groupId = [$this->groupId];
} elseif (!is_array($this->groupId) || !ArrayHelper::isNumeric($this->groupId)) {
$this->groupId = (new Query())
->select(['id'])
->from([Table::CATEGORYGROUPS])
->where(Db::parseNumericParam('id', $this->groupId))
->column();
}
}
/**
* Applies the 'ref' param to the query being prepared.
*/
private function _applyRefParam(): void
{
if (!$this->ref) {
return;
}
$refs = $this->ref;
if (!is_array($refs)) {
$refs = is_string($refs) ? StringHelper::split($refs) : [$refs];
}
$condition = ['or'];
$joinCategoryGroups = false;
foreach ($refs as $ref) {
$parts = array_filter(explode('/', $ref));
if (!empty($parts)) {
if (count($parts) == 1) {
$condition[] = Db::parseParam('elements_sites.slug', $parts[0]);
} else {
$condition[] = [
'and',
Db::parseParam('categorygroups.handle', $parts[0]),
Db::parseParam('elements_sites.slug', $parts[1]),
];
$joinCategoryGroups = true;
}
}
}
$this->subQuery->andWhere($condition);
if ($joinCategoryGroups) {
$this->subQuery->innerJoin(['categorygroups' => Table::CATEGORYGROUPS], '[[categorygroups.id]] = [[categories.groupId]]');
}
}
/**
* @inheritdoc
* @since 3.5.0
*/
protected function cacheTags(): array
{
$tags = [];
if ($this->groupId) {
foreach ($this->groupId as $groupId) {
$tags[] = "group:$groupId";
}
}
return $tags;
}
}