-
Notifications
You must be signed in to change notification settings - Fork 638
/
Copy pathNestedElementTrait.php
368 lines (323 loc) · 9.82 KB
/
NestedElementTrait.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\base;
use Craft;
use craft\db\Query;
use craft\db\Table;
use craft\elements\db\EagerLoadPlan;
use craft\helpers\Db;
use yii\base\InvalidConfigException;
/**
* NestedElementTrait
*
* @property ElementInterface|null $primaryOwner the primary owner element
* @property ElementInterface|null $owner the owner element
* @property int|null $primaryOwnerId the primary owner element’s ID
* @property int|null $ownerId the owner element’s ID
* @property ElementContainerFieldInterface|null $field the element’s field
* @mixin Element
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 5.0.0
*/
trait NestedElementTrait
{
/**
* @inheritdoc
*/
public static function eagerLoadingMap(array $sourceElements, string $handle): array|null|false
{
switch ($handle) {
case 'owner':
case 'primaryOwner':
/** @var NestedElementInterface[] $sourceElements */
return [
'elementType' => get_class(reset($sourceElements)),
'map' => array_map(fn(NestedElementInterface $element) => [
'source' => $element->id,
'target' => match ($handle) {
'owner' => $element->getOwnerId(),
'primaryOwner' => $element->getPrimaryOwnerId(),
},
], $sourceElements),
'criteria' => [
'status' => null,
],
];
default:
return parent::eagerLoadingMap($sourceElements, $handle);
}
}
/**
* @var int|null Primary owner ID
*/
private ?int $primaryOwnerId = null;
/**
* @var int|null Owner ID
*/
private ?int $ownerId = null;
/**
* @var int|null Field ID
*/
public ?int $fieldId = null;
/**
* @var int|null Sort order
*/
public ?int $sortOrder = null;
/**
* @var bool Whether to save the element’s row in the `elements_owners` table from `afterSave()`.
*/
public bool $saveOwnership = true;
/**
* @var bool Whether the search index should be updated for the owner element, alongside this element.
*
* This will only be checked if [[fieldId]] is set, and `false` isn’t passed to the `updateSearchIndex`
* argument of [[\craft\services\Elements::saveElement()]].
*
* @since 5.2.0
*/
public bool $updateSearchIndexForOwner = false;
/**
* @var ElementInterface|false The primary owner element, or false if [[primaryOwnerId]] is invalid
* @see getPrimaryOwner()
* @see setPrimaryOwner()
*/
private ElementInterface|false $_primaryOwner;
/**
* @var ElementInterface|false The owner element, or false if [[ownerId]] is invalid
* @see getOwner()
* @see setOwner()
*/
private ElementInterface|false $_owner;
/**
* @inheritdoc
*/
public function attributes(): array
{
$names = parent::attributes();
$names[] = 'primaryOwnerId';
$names[] = 'ownerId';
return $names;
}
/**
* @inheritdoc
*/
public function extraFields(): array
{
$names = parent::extraFields();
$names[] = 'primaryOwner';
$names[] = 'owner';
return $names;
}
/**
* @inheritdoc
*/
public function getPrimaryOwnerId(): ?int
{
return $this->primaryOwnerId ?? $this->ownerId;
}
/**
* @inheritdoc
*/
public function setPrimaryOwnerId(?int $id): void
{
$this->primaryOwnerId = $id;
}
/**
* @inheritdoc
*/
public function getPrimaryOwner(): ?ElementInterface
{
if (!isset($this->_primaryOwner)) {
$primaryOwnerId = $this->getPrimaryOwnerId();
if (!$primaryOwnerId) {
return null;
}
$this->_primaryOwner = Craft::$app->getElements()->getElementById($primaryOwnerId, null, $this->siteId, [
'trashed' => null,
]) ?? false;
if (!$this->_primaryOwner) {
throw new InvalidConfigException("Invalid owner ID: $primaryOwnerId");
}
}
return $this->_primaryOwner ?: null;
}
/**
* @inheritdoc
*/
public function setPrimaryOwner(?ElementInterface $owner): void
{
$this->_primaryOwner = $owner ?? false;
$this->primaryOwnerId = $owner->id ?? null;
}
/**
* @inheritdoc
*/
public function getOwnerId(): ?int
{
return $this->ownerId ?? $this->primaryOwnerId;
}
/**
* @inheritdoc
*/
public function setOwnerId(?int $id): void
{
$this->ownerId = $id;
}
/**
* @inheritdoc
*/
public function getOwner(): ?ElementInterface
{
if (!isset($this->_owner)) {
$ownerId = $this->getOwnerId();
if (!$ownerId) {
return null;
}
// If ownerId and primaryOwnerId are the same, return the primary owner
if ($ownerId === $this->getPrimaryOwnerId()) {
return $this->getPrimaryOwner();
}
$this->_owner = Craft::$app->getElements()->getElementById($ownerId, null, $this->siteId, [
'trashed' => null,
]) ?? false;
if (!$this->_owner) {
throw new InvalidConfigException("Invalid owner ID: $ownerId");
}
}
return $this->_owner ?: null;
}
/**
* @inheritdoc
*/
public function setOwner(?ElementInterface $owner): void
{
$this->_owner = $owner ?? false;
$this->ownerId = $owner->id ?? null;
}
/**
* @inheritdoc
*/
public function getField(): ?ElementContainerFieldInterface
{
if (!isset($this->fieldId)) {
return null;
}
$field = $this->getOwner()?->getFieldLayout()->getFieldById($this->fieldId)
?? Craft::$app->getFields()->getFieldById($this->fieldId);
if (!$field instanceof ElementContainerFieldInterface) {
throw new InvalidConfigException("Invalid field ID: $this->fieldId");
}
return $field;
}
/**
* @inheritdoc
*/
public function getSortOrder(): ?int
{
return $this->sortOrder;
}
/**
* @inheritdoc
*/
public function setSortOrder(?int $sortOrder): void
{
$this->sortOrder = $sortOrder;
}
/**
* @inheritdoc
*/
public function setSaveOwnership(bool $saveOwnership): void
{
$this->saveOwnership = $saveOwnership;
}
/**
* @inheritdoc
*/
public function addInvalidNestedElementIds(array $ids): void
{
parent::addInvalidNestedElementIds($ids);
if (isset($this->_owner)) {
$this->_owner->addInvalidNestedElementIds($ids);
}
}
/**
* @inheritdoc
*/
public function setEagerLoadedElements(string $handle, array $elements, EagerLoadPlan $plan): void
{
switch ($plan->handle) {
case 'owner':
$this->setOwner(reset($elements));
break;
case 'primaryOwner':
$this->setPrimaryOwner(reset($elements));
break;
default:
parent::setEagerLoadedElements($handle, $elements, $plan);
}
}
/**
* Saves the element’s ownership data, if it belongs to a field + owner element
*/
private function saveOwnership(bool $isNew, string $elementTable, string $fieldIdColumn = 'fieldId'): void
{
if (!$this->saveOwnership || !isset($this->fieldId)) {
return;
}
$ownerId = $this->getOwnerId();
if (!$ownerId) {
return;
}
if (!isset($this->sortOrder) && (!$isNew || $this->duplicateOf)) {
// figure out if we should proceed this way
// if we're dealing with an element that's being duplicated, and it has a draftId
// it means we're creating a draft of something
// if we're duplicating element via duplicate action - draftId would be empty
$elementId = null;
if ($this->duplicateOf) {
if ($this->draftId) {
$elementId = $this->duplicateOf->id;
}
} else {
// if we're not duplicating, use this element's id
$elementId = $this->id;
}
if ($elementId) {
$this->sortOrder = (new Query())
->select('sortOrder')
->from(Table::ELEMENTS_OWNERS)
->where([
'elementId' => $elementId,
'ownerId' => $ownerId,
])
->scalar() ?: null;
}
}
if (!isset($this->sortOrder)) {
$max = (new Query())
->from(['eo' => Table::ELEMENTS_OWNERS])
->innerJoin(['e' => $elementTable], '[[e.id]] = [[eo.elementId]]')
->where([
'eo.ownerId' => $ownerId,
"e.$fieldIdColumn" => $this->fieldId,
])
->max('[[eo.sortOrder]]');
$this->sortOrder = $max ? $max + 1 : 1;
}
if (!$isNew) {
Db::delete(Table::ELEMENTS_OWNERS, [
'elementId' => $this->id,
'ownerId' => $ownerId,
]);
}
Db::insert(Table::ELEMENTS_OWNERS, [
'elementId' => $this->id,
'ownerId' => $ownerId,
'sortOrder' => $this->sortOrder,
]);
}
}