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