-
Notifications
You must be signed in to change notification settings - Fork 692
Expand file tree
/
Copy pathCategoryGroup_SiteSettings.php
More file actions
139 lines (118 loc) Β· 3.12 KB
/
CategoryGroup_SiteSettings.php
File metadata and controls
139 lines (118 loc) Β· 3.12 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
<?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\validators\SiteIdValidator;
use craft\validators\UriFormatValidator;
use yii\base\InvalidConfigException;
/**
* CategoryGroup_SiteSettings model class.
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0.0
*/
class CategoryGroup_SiteSettings extends Model
{
/**
* @var int|null ID
*/
public ?int $id = null;
/**
* @var int|null Group ID
*/
public ?int $groupId = null;
/**
* @var int|null Site ID
*/
public ?int $siteId = null;
/**
* @var bool|null Has URLs?
*/
public ?bool $hasUrls = null;
/**
* @var string|null URI format
*/
public ?string $uriFormat = null;
/**
* @var string|null Entry template
*/
public ?string $template = null;
/**
* @var CategoryGroup|null
*/
private ?CategoryGroup $_group = null;
/**
* Returns the group.
*
* @return CategoryGroup
* @throws InvalidConfigException if [[groupId]] is missing or invalid
*/
public function getGroup(): CategoryGroup
{
if (isset($this->_group)) {
return $this->_group;
}
if (!$this->groupId) {
throw new InvalidConfigException('Category is missing its group ID');
}
if (($this->_group = Craft::$app->getCategories()->getGroupById($this->groupId)) === null) {
throw new InvalidConfigException('Invalid group ID: ' . $this->groupId);
}
return $this->_group;
}
/**
* Sets the group.
*
* @param CategoryGroup $group
*/
public function setGroup(CategoryGroup $group): void
{
$this->_group = $group;
}
/**
* Returns the site.
*
* @return Site
* @throws InvalidConfigException if [[siteId]] is missing or invalid
*/
public function getSite(): Site
{
if (!$this->siteId) {
throw new InvalidConfigException('Category group site settings model is missing its site ID');
}
if (($site = Craft::$app->getSites()->getSiteById($this->siteId)) === null) {
throw new InvalidConfigException('Invalid site ID: ' . $this->siteId);
}
return $site;
}
/**
* @inheritdoc
*/
public function attributeLabels(): array
{
return [
'template' => Craft::t('app', 'Template'),
'uriFormat' => Craft::t('app', 'URI Format'),
];
}
/**
* @inheritdoc
*/
protected function defineRules(): array
{
$rules = parent::defineRules();
$rules[] = [['id', 'groupId', 'siteId'], 'number', 'integerOnly' => true];
$rules[] = [['siteId'], SiteIdValidator::class];
$rules[] = [['template'], 'string', 'max' => 500];
$rules[] = [['uriFormat'], UriFormatValidator::class];
if ($this->hasUrls) {
$rules[] = [['uriFormat'], 'required'];
}
return $rules;
}
}