/
Section.php
398 lines (349 loc) · 11.3 KB
/
Section.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\models;
use Craft;
use craft\base\Model;
use craft\db\Query;
use craft\db\Table;
use craft\elements\Entry;
use craft\helpers\ArrayHelper;
use craft\helpers\Db;
use craft\helpers\ProjectConfig as ProjectConfigHelper;
use craft\helpers\StringHelper;
use craft\records\Section as SectionRecord;
use craft\validators\HandleValidator;
use craft\validators\UniqueValidator;
/**
* Section model class.
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0.0
* @property Section_SiteSettings[] $siteSettings Site-specific settings
* @property EntryType[] $entryTypes Entry types
* @property bool $hasMultiSiteEntries Whether entries in this section support multiple sites
*/
class Section extends Model
{
public const TYPE_SINGLE = 'single';
public const TYPE_CHANNEL = 'channel';
public const TYPE_STRUCTURE = 'structure';
public const PROPAGATION_METHOD_NONE = 'none';
public const PROPAGATION_METHOD_SITE_GROUP = 'siteGroup';
public const PROPAGATION_METHOD_LANGUAGE = 'language';
public const PROPAGATION_METHOD_ALL = 'all';
/** @since 3.5.0 */
public const PROPAGATION_METHOD_CUSTOM = 'custom';
/** @since 3.7.0 */
public const DEFAULT_PLACEMENT_BEGINNING = 'beginning';
/** @since 3.7.0 */
public const DEFAULT_PLACEMENT_END = 'end';
/**
* @var int|null ID
*/
public ?int $id = null;
/**
* @var int|null Structure ID
*/
public ?int $structureId = null;
/**
* @var string|null Name
*/
public ?string $name = null;
/**
* @var string|null Handle
*/
public ?string $handle = null;
/**
* @var string|null Type
*/
public ?string $type = null;
/**
* @var int|null Max levels
*/
public ?int $maxLevels = null;
/**
* @var bool Enable versioning
*/
public bool $enableVersioning = true;
/**
* @var string Propagation method
* @phpstan-var self::PROPAGATION_METHOD_NONE|self::PROPAGATION_METHOD_SITE_GROUP|self::PROPAGATION_METHOD_LANGUAGE|self::PROPAGATION_METHOD_ALL|self::PROPAGATION_METHOD_CUSTOM
*
* This will be set to one of the following:
*
* - `none` – Only save entries in the site they were created in
* - `siteGroup` – Save entries to other sites in the same site group
* - `language` – Save entries to other sites with the same language
* - `all` – Save entries to all sites enabled for this section
*
* @since 3.2.0
*/
public string $propagationMethod = self::PROPAGATION_METHOD_ALL;
/**
* @var string Default placement
* @phpstan-var self::DEFAULT_PLACEMENT_BEGINNING|self::DEFAULT_PLACEMENT_END
* @since 3.7.0
*/
public string $defaultPlacement = self::DEFAULT_PLACEMENT_END;
/**
* @var array|null Preview targets
*/
public ?array $previewTargets = null;
/**
* @var string|null Section's UID
*/
public ?string $uid = null;
/**
* @var Section_SiteSettings[]|null
*/
private ?array $_siteSettings = null;
/**
* @var EntryType[]|null
*/
private ?array $_entryTypes = null;
/**
* @inheritdoc
*/
public function init(): void
{
if (!isset($this->previewTargets)) {
$this->previewTargets = [
[
'label' => Craft::t('app', 'Primary {type} page', [
'type' => StringHelper::toLowerCase(Entry::displayName()),
]),
'urlFormat' => '{url}',
],
];
}
parent::init();
}
/**
* @inheritdoc
*/
public function attributeLabels(): array
{
return [
'handle' => Craft::t('app', 'Handle'),
'name' => Craft::t('app', 'Name'),
'type' => Craft::t('app', 'Section Type'),
];
}
/**
* @inheritdoc
*/
protected function defineRules(): array
{
$rules = parent::defineRules();
$rules[] = [['id', 'structureId', 'maxLevels'], 'number', 'integerOnly' => true];
$rules[] = [['handle'], HandleValidator::class, 'reservedWords' => ['id', 'dateCreated', 'dateUpdated', 'uid', 'title']];
$rules[] = [
['type'], 'in', 'range' => [
self::TYPE_SINGLE,
self::TYPE_CHANNEL,
self::TYPE_STRUCTURE,
],
];
$rules[] = [
['propagationMethod'], 'in', 'range' => [
self::PROPAGATION_METHOD_NONE,
self::PROPAGATION_METHOD_SITE_GROUP,
self::PROPAGATION_METHOD_LANGUAGE,
self::PROPAGATION_METHOD_ALL,
self::PROPAGATION_METHOD_CUSTOM,
],
];
$rules[] = [['name', 'handle'], UniqueValidator::class, 'targetClass' => SectionRecord::class];
$rules[] = [['name', 'handle', 'type', 'propagationMethod', 'siteSettings'], 'required'];
$rules[] = [['name', 'handle'], 'string', 'max' => 255];
$rules[] = [['siteSettings'], 'validateSiteSettings'];
$rules[] = [['defaultPlacement'], 'in', 'range' => [self::DEFAULT_PLACEMENT_BEGINNING, self::DEFAULT_PLACEMENT_END]];
$rules[] = [['previewTargets'], 'validatePreviewTargets'];
return $rules;
}
/**
* Validates the site settings.
*/
public function validateSiteSettings(): void
{
// If this is an existing section, make sure they aren't moving it to a
// completely different set of sites in one fell swoop
if ($this->id) {
$currentSiteIds = (new Query())
->select(['siteId'])
->from([Table::SECTIONS_SITES])
->where(['sectionId' => $this->id])
->column();
if (empty(array_intersect($currentSiteIds, array_keys($this->getSiteSettings())))) {
$this->addError('siteSettings', Craft::t('app', 'At least one currently-enabled site must remain enabled.'));
}
}
foreach ($this->getSiteSettings() as $i => $siteSettings) {
if (!$siteSettings->validate()) {
$this->addModelErrors($siteSettings, "siteSettings[$i]");
}
}
}
/**
* Validates the preview targets.
*/
public function validatePreviewTargets(): void
{
$hasErrors = false;
foreach ($this->previewTargets as &$target) {
$target['label'] = trim($target['label']);
$target['urlFormat'] = trim($target['urlFormat']);
if ($target['label'] === '') {
$target['label'] = ['value' => $target['label'], 'hasErrors' => true];
$hasErrors = true;
}
}
unset($target);
if ($hasErrors) {
$this->addError('previewTargets', Craft::t('app', 'All targets must have a label.'));
}
}
/**
* Use the translated section name as the string representation.
*
* @return string
*/
public function __toString(): string
{
return Craft::t('site', $this->name) ?: static::class;
}
/**
* Returns the section's site-specific settings, indexed by site ID.
*
* @return Section_SiteSettings[]
*/
public function getSiteSettings(): array
{
if (isset($this->_siteSettings)) {
return $this->_siteSettings;
}
if (!$this->id) {
return [];
}
// Set them with setSiteSettings() so they get indexed by site ID and setSection() gets called on them
$this->setSiteSettings(Craft::$app->getSections()->getSectionSiteSettings($this->id));
return $this->_siteSettings;
}
/**
* Sets the section's site-specific settings.
*
* @param Section_SiteSettings[] $siteSettings Array of Section_SiteSettings objects.
*/
public function setSiteSettings(array $siteSettings): void
{
$this->_siteSettings = ArrayHelper::index($siteSettings, 'siteId');
foreach ($this->_siteSettings as $settings) {
$settings->setSection($this);
}
}
/**
* Returns the site IDs that are enabled for the section.
*
* @return int[]
*/
public function getSiteIds(): array
{
return array_keys($this->getSiteSettings());
}
/**
* Adds site-specific errors to the model.
*
* @param array $errors
* @param int $siteId
*/
public function addSiteSettingsErrors(array $errors, int $siteId): void
{
foreach ($errors as $attribute => $siteErrors) {
$key = $attribute . '-' . $siteId;
foreach ($siteErrors as $error) {
$this->addError($key, $error);
}
}
}
/**
* Returns the section's entry types.
*
* @return EntryType[]
*/
public function getEntryTypes(): array
{
if (isset($this->_entryTypes)) {
return $this->_entryTypes;
}
if (!$this->id) {
return [];
}
$this->_entryTypes = Craft::$app->getSections()->getEntryTypesBySectionId($this->id);
return $this->_entryTypes;
}
/**
* Sets the section's entry types.
*
* @param EntryType[] $entryTypes
* @since 3.1.0
*/
public function setEntryTypes(array $entryTypes): void
{
$this->_entryTypes = $entryTypes;
}
/**
* Returns whether entries in this section support multiple sites.
*
* @return bool
* @since 3.0.35
*/
public function getHasMultiSiteEntries(): bool
{
return (
Craft::$app->getIsMultiSite() &&
count($this->getSiteSettings()) > 1 &&
$this->propagationMethod !== self::PROPAGATION_METHOD_NONE
);
}
/**
* Returns the section’s config.
*
* @return array
* @since 3.5.0
*/
public function getConfig(): array
{
$config = [
'name' => $this->name,
'handle' => $this->handle,
'type' => $this->type,
'enableVersioning' => $this->enableVersioning,
'propagationMethod' => $this->propagationMethod,
'siteSettings' => [],
'defaultPlacement' => $this->defaultPlacement ?? self::DEFAULT_PLACEMENT_END,
];
if (!empty($this->previewTargets)) {
$config['previewTargets'] = ProjectConfigHelper::packAssociativeArray($this->previewTargets);
}
if ($this->type === self::TYPE_STRUCTURE) {
$config['structure'] = [
'uid' => $this->structureId ? Db::uidById(Table::STRUCTURES, $this->structureId) : StringHelper::UUID(),
'maxLevels' => (int)$this->maxLevels ?: null,
];
}
foreach ($this->getSiteSettings() as $siteId => $siteSettings) {
$siteUid = Db::uidById(Table::SITES, $siteId);
$config['siteSettings'][$siteUid] = [
'enabledByDefault' => (bool)$siteSettings['enabledByDefault'],
'hasUrls' => (bool)$siteSettings['hasUrls'],
'uriFormat' => $siteSettings['uriFormat'] ?: null,
'template' => $siteSettings['template'] ?: null,
];
}
return $config;
}
}