-
Notifications
You must be signed in to change notification settings - Fork 690
Expand file tree
/
Copy pathTag.php
More file actions
373 lines (328 loc) · 8.15 KB
/
Tag.php
File metadata and controls
373 lines (328 loc) · 8.15 KB
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
369
370
371
372
373
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\elements;
use Craft;
use craft\base\Element;
use craft\db\Table;
use craft\elements\conditions\ElementConditionInterface;
use craft\elements\conditions\tags\TagCondition;
use craft\elements\db\TagQuery;
use craft\gql\interfaces\elements\Tag as TagInterface;
use craft\helpers\Db;
use craft\models\FieldLayout;
use craft\models\TagGroup;
use craft\records\Tag as TagRecord;
use GraphQL\Type\Definition\Type;
use yii\base\InvalidConfigException;
use yii\validators\InlineValidator;
/**
* Tag represents a tag element.
*
* @property TagGroup $group the tag's group
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 3.0.0
*/
class Tag extends Element
{
/**
* @inheritdoc
*/
public static function displayName(): string
{
return Craft::t('app', 'Tag');
}
/**
* @inheritdoc
*/
public static function lowerDisplayName(): string
{
return Craft::t('app', 'tag');
}
/**
* @inheritdoc
*/
public static function pluralDisplayName(): string
{
return Craft::t('app', 'Tags');
}
/**
* @inheritdoc
*/
public static function pluralLowerDisplayName(): string
{
return Craft::t('app', 'tags');
}
/**
* @inheritdoc
*/
public static function refHandle(): ?string
{
return 'tag';
}
/**
* @inheritdoc
*/
public static function hasTitles(): bool
{
return true;
}
/**
* @inheritdoc
*/
public static function hasUris(): bool
{
return true;
}
/**
* @inheritdoc
*/
public static function isLocalized(): bool
{
return true;
}
/**
* @inheritdoc
* @return TagQuery The newly created [[TagQuery]] instance.
*/
public static function find(): TagQuery
{
return new TagQuery(static::class);
}
/**
* @inheritdoc
* @return TagCondition
*/
public static function createCondition(): ElementConditionInterface
{
return Craft::createObject(TagCondition::class, [static::class]);
}
/**
* @inheritdoc
*/
protected static function defineSources(string $context): array
{
$sources = [];
foreach (Craft::$app->getTags()->getAllTagGroups() as $tagGroup) {
$sources[] = [
'key' => 'taggroup:' . $tagGroup->uid,
'label' => Craft::t('site', $tagGroup->name),
'criteria' => ['groupId' => $tagGroup->id],
];
}
return $sources;
}
/**
* Returns the GraphQL type name that tags should use, based on their tag group.
*
* @since 5.0.0
*/
public static function gqlTypeName(TagGroup $tagGroup): string
{
return sprintf('%s_Tag', $tagGroup->handle);
}
/**
* @inheritdoc
*/
public static function baseGqlType(): Type
{
return TagInterface::getType();
}
/**
* @inheritdoc
* @since 3.3.0
*/
public static function gqlScopesByContext(mixed $context): array
{
/** @var TagGroup $context */
return ['taggroups.' . $context->uid];
}
/**
* @inheritdoc
*/
protected static function defineFieldLayouts(?string $source): array
{
if ($source !== null) {
$groups = [];
if (preg_match('/^taggroup:(.+)$/', $source, $matches)) {
$group = Craft::$app->getTags()->getTagGroupByUid($matches[1]);
if ($group) {
$groups[] = $group;
}
}
} else {
$groups = Craft::$app->getTags()->getAllTagGroups();
}
return array_map(fn(TagGroup $group) => $group->getFieldLayout(), $groups);
}
/**
* @var int|null Group ID
*/
public ?int $groupId = null;
/**
* @var bool Whether the tag was deleted along with its group
* @see beforeDelete()
*/
public bool $deletedWithGroup = false;
/**
* @inheritdoc
*/
public function extraFields(): array
{
$names = parent::extraFields();
$names[] = 'group';
return $names;
}
/**
* @inheritdoc
*/
protected function defineRules(): array
{
$rules = parent::defineRules();
$rules[] = [['groupId'], 'number', 'integerOnly' => true];
$rules[] = [
['title'],
'validateTitle',
'when' => fn(): bool => !$this->hasErrors('groupId') && !$this->hasErrors('title'),
'on' => [self::SCENARIO_DEFAULT, self::SCENARIO_LIVE],
];
return $rules;
}
/**
* Validates the tag title.
*
* @param string $attribute
* @param array|null $params
* @param InlineValidator $validator
* @since 3.4.12
*/
public function validateTitle(string $attribute, ?array $params, InlineValidator $validator): void
{
$query = self::find()
->groupId($this->groupId)
->siteId($this->siteId)
->title(Db::escapeParam($this->title));
if ($this->id) {
$query->andWhere(['not', ['elements.id' => $this->id]]);
}
if ($query->exists()) {
$validator->addError($this, $attribute, Craft::t('yii', '{attribute} "{value}" has already been taken.'));
}
}
/**
* @inheritdoc
* @since 3.5.0
*/
protected function cacheTags(): array
{
return [
"group:$this->groupId",
];
}
/**
* @inheritdoc
*/
public function canView(User $user): bool
{
return true;
}
/**
* @inheritdoc
*/
public function canSave(User $user): bool
{
return true;
}
/**
* @inheritdoc
*/
public function canDuplicate(User $user): bool
{
return true;
}
/**
* @inheritdoc
*/
public function canDelete(User $user): bool
{
return true;
}
/**
* @inheritdoc
*/
public function getFieldLayout(): ?FieldLayout
{
try {
return $this->getGroup()->getFieldLayout();
} catch (InvalidConfigException) {
return null;
}
}
/**
* Returns the tag's group.
*
* @return TagGroup
* @throws InvalidConfigException if [[groupId]] is missing or invalid
*/
public function getGroup(): TagGroup
{
if (!isset($this->groupId)) {
throw new InvalidConfigException('Tag is missing its group ID');
}
if (($group = Craft::$app->getTags()->getTagGroupById($this->groupId)) === null) {
throw new InvalidConfigException('Invalid tag group ID: ' . $this->groupId);
}
return $group;
}
/**
* @inheritdoc
* @since 3.3.0
*/
public function getGqlTypeName(): string
{
return static::gqlTypeName($this->getGroup());
}
// Events
// -------------------------------------------------------------------------
/**
* @inheritdoc
* @throws InvalidConfigException
*/
public function afterSave(bool $isNew): void
{
if (!$this->propagating) {
// Get the tag record
if (!$isNew) {
$record = TagRecord::findOne($this->id);
if (!$record) {
throw new InvalidConfigException("Invalid tag ID: $this->id");
}
} else {
$record = new TagRecord();
$record->id = (int)$this->id;
}
$record->groupId = (int)$this->groupId;
$record->save(false);
}
parent::afterSave($isNew);
}
/**
* @inheritdoc
*/
public function beforeDelete(): bool
{
if (!parent::beforeDelete()) {
return false;
}
// Update the tag record
Db::update(Table::TAGS, [
'deletedWithGroup' => $this->deletedWithGroup,
], [
'id' => $this->id,
], [], false);
return true;
}
}