1 | <?php |
||
2 | |||
3 | namespace app\models; |
||
4 | |||
5 | use Yii; |
||
6 | use yii\helpers\ArrayHelper; |
||
7 | use Itstructure\MultiLevelMenu\MenuWidget; |
||
8 | use Itstructure\AdminModule\models\{MultilanguageTrait, Language, ActiveRecord}; |
||
9 | use Itstructure\AdminModule\interfaces\ModelInterface; |
||
10 | use Itstructure\MFUploader\behaviors\{BehaviorMediafile, BehaviorAlbum}; |
||
11 | use Itstructure\MFUploader\models\OwnerAlbum; |
||
12 | use Itstructure\MFUploader\models\album\Album; |
||
13 | use Itstructure\MFUploader\interfaces\UploadModelInterface; |
||
14 | use app\traits\ThumbnailTrait; |
||
15 | |||
16 | /** |
||
17 | * This is the model class for table "pages". |
||
18 | * |
||
19 | * @property int|string $thumbnail thumbnail(mediafile id or url). |
||
20 | * @property array $albums Existing album ids. |
||
21 | * @property int $id |
||
22 | * @property string $created_at |
||
23 | * @property string $updated_at |
||
24 | * @property int $parentId |
||
25 | * @property string $icon |
||
26 | * @property string $alias |
||
27 | * @property int $active |
||
28 | * |
||
29 | * @property PageLanguage[] $pagesLanguages |
||
30 | * @property Language[] $languages |
||
31 | * |
||
32 | * @package app\models |
||
33 | */ |
||
34 | class Page extends ActiveRecord |
||
35 | { |
||
36 | use MultilanguageTrait, ThumbnailTrait; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
37 | |||
38 | /** |
||
39 | * @var int|string thumbnail(mediafile id or url). |
||
40 | */ |
||
41 | public $thumbnail; |
||
42 | |||
43 | /** |
||
44 | * @var array image(array of 'mediafile id' or 'mediafile url'). |
||
45 | */ |
||
46 | public $image; |
||
47 | |||
48 | /** |
||
49 | * @var array |
||
50 | */ |
||
51 | public $albums = []; |
||
52 | |||
53 | /** |
||
54 | * Init albums. |
||
55 | */ |
||
56 | public function afterFind() |
||
57 | { |
||
58 | $this->albums = $this->getAlbums(); |
||
59 | |||
60 | parent::afterFind(); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @inheritdoc |
||
65 | */ |
||
66 | public static function tableName() |
||
67 | { |
||
68 | return 'pages'; |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * @inheritdoc |
||
73 | */ |
||
74 | public function rules() |
||
75 | { |
||
76 | return [ |
||
77 | [ |
||
78 | [ |
||
79 | 'created_at', |
||
80 | 'updated_at', |
||
81 | ], |
||
82 | 'safe', |
||
83 | ], |
||
84 | [ |
||
85 | [ |
||
86 | 'active', |
||
87 | ], |
||
88 | 'required', |
||
89 | ], |
||
90 | [ |
||
91 | [ |
||
92 | 'parentId', |
||
93 | 'active' |
||
94 | ], |
||
95 | 'integer', |
||
96 | ], |
||
97 | [ |
||
98 | [ |
||
99 | 'icon', |
||
100 | 'alias', |
||
101 | ], |
||
102 | 'string', |
||
103 | 'max' => 128, |
||
104 | ], |
||
105 | [ |
||
106 | 'parentId', |
||
107 | 'filter', |
||
108 | 'filter' => function ($value) { |
||
109 | if (empty($value)) { |
||
110 | return null; |
||
111 | } else { |
||
112 | return MenuWidget::checkNewParentId($this, $value) ? $value : $this->getOldAttribute('parentId'); |
||
113 | } |
||
114 | } |
||
115 | ], |
||
116 | [ |
||
117 | 'alias', |
||
118 | 'filter', |
||
119 | 'filter' => function ($value) { |
||
120 | return preg_replace( '/[^a-z0-9_]+/', '-', strtolower(trim($value))); |
||
121 | } |
||
122 | ], |
||
123 | [ |
||
124 | 'alias', |
||
125 | 'unique', |
||
126 | 'skipOnError' => true, |
||
127 | 'targetClass' => static::class, |
||
128 | 'filter' => $this->getScenario() == ModelInterface::SCENARIO_UPDATE ? 'id != '.$this->id : '' |
||
129 | ], |
||
130 | [ |
||
131 | UploadModelInterface::FILE_TYPE_THUMB, |
||
132 | function($attribute){ |
||
133 | if (!is_numeric($this->{$attribute}) && !is_string($this->{$attribute})){ |
||
134 | $this->addError($attribute, 'Tumbnail content must be a numeric or string.'); |
||
135 | } |
||
136 | }, |
||
137 | 'skipOnError' => false, |
||
138 | ], |
||
139 | [ |
||
140 | UploadModelInterface::FILE_TYPE_IMAGE, |
||
141 | function($attribute) { |
||
142 | if (!is_array($this->{$attribute})) { |
||
143 | $this->addError($attribute, 'Image field content must be an array.'); |
||
144 | } |
||
145 | }, |
||
146 | 'skipOnError' => false, |
||
147 | ], |
||
148 | [ |
||
149 | 'albums', |
||
150 | 'each', |
||
151 | 'rule' => ['integer'], |
||
152 | ], |
||
153 | ]; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @inheritdoc |
||
158 | */ |
||
159 | public function behaviors() |
||
160 | { |
||
161 | return ArrayHelper::merge(parent::behaviors(), [ |
||
162 | 'mediafile' => [ |
||
163 | 'class' => BehaviorMediafile::class, |
||
164 | 'name' => static::tableName(), |
||
165 | 'attributes' => [ |
||
166 | UploadModelInterface::FILE_TYPE_THUMB, |
||
167 | UploadModelInterface::FILE_TYPE_IMAGE, |
||
168 | ], |
||
169 | ], |
||
170 | 'albums' => [ |
||
171 | 'class' => BehaviorAlbum::class, |
||
172 | 'name' => static::tableName(), |
||
173 | 'attributes' => [ |
||
174 | 'albums', |
||
175 | ], |
||
176 | ], |
||
177 | ]); |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * @return array |
||
182 | */ |
||
183 | public function attributes(): array |
||
184 | { |
||
185 | return [ |
||
186 | UploadModelInterface::FILE_TYPE_THUMB, |
||
187 | UploadModelInterface::FILE_TYPE_IMAGE, |
||
188 | 'albums', |
||
189 | 'id', |
||
190 | 'parentId', |
||
191 | 'icon', |
||
192 | 'alias', |
||
193 | 'active', |
||
194 | 'created_at', |
||
195 | 'updated_at', |
||
196 | ]; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * @inheritdoc |
||
201 | */ |
||
202 | public function attributeLabels() |
||
203 | { |
||
204 | return [ |
||
205 | 'id' => 'ID', |
||
206 | 'parentId' => Yii::t('app', 'Parent object'), |
||
207 | 'icon' => Yii::t('app', 'Icon'), |
||
208 | 'active' => Yii::t('app', 'Active status'), |
||
209 | 'alias' => Yii::t('app', 'URL Alias'), |
||
210 | 'created_at' => Yii::t('app', 'Created date'), |
||
211 | 'updated_at' => Yii::t('app', 'Updated date'), |
||
212 | ]; |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Reassigning child objects to their new parent after delete the main model record. |
||
217 | */ |
||
218 | public function afterDelete() |
||
219 | { |
||
220 | MenuWidget::afterDeleteMainModel($this); |
||
221 | |||
222 | parent::afterDelete(); |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * @return array|\yii\db\ActiveRecord[] |
||
227 | */ |
||
228 | public static function getMenu() |
||
229 | { |
||
230 | return static::find()->select([ |
||
231 | 'id', 'parentId' |
||
232 | ])->all(); |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * @return array|\yii\db\ActiveRecord[] |
||
237 | */ |
||
238 | public static function getActiveMenu() |
||
239 | { |
||
240 | return static::find()->select([ |
||
241 | 'id', 'parentId', 'alias' |
||
242 | ])->where([ |
||
243 | 'active' => 1 |
||
244 | ])->all(); |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * @return \yii\db\ActiveQuery |
||
249 | */ |
||
250 | public function getPagesLanguages() |
||
251 | { |
||
252 | return $this->hasMany(PageLanguage::class, [ |
||
253 | 'pages_id' => 'id' |
||
254 | ]); |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * @return \yii\db\ActiveQuery |
||
259 | */ |
||
260 | public function getLanguages() |
||
261 | { |
||
262 | return $this->hasMany(Language::class, [ |
||
263 | 'id' => 'language_id' |
||
264 | ])->viaTable('pages_language', [ |
||
265 | 'pages_id' => 'id' |
||
266 | ]); |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * Get albums, that catalog has. |
||
271 | * |
||
272 | * @return Album[] |
||
273 | */ |
||
274 | public function getAlbums() |
||
275 | { |
||
276 | return OwnerAlbum::getAlbumsQuery([ |
||
277 | 'owner' => static::tableName(), |
||
278 | 'ownerId' => $this->id, |
||
279 | 'ownerAttribute' => 'albums', |
||
280 | ])->all(); |
||
281 | } |
||
282 | } |
||
283 |