1 | <?php |
||
2 | |||
3 | namespace app\models; |
||
4 | |||
5 | use Yii; |
||
6 | use yii\helpers\ArrayHelper; |
||
7 | use Itstructure\AdminModule\models\{MultilanguageTrait, Language, ActiveRecord}; |
||
8 | use Itstructure\AdminModule\interfaces\ModelInterface; |
||
9 | use Itstructure\MFUploader\behaviors\{BehaviorMediafile, BehaviorAlbum}; |
||
10 | use Itstructure\MFUploader\models\OwnerAlbum; |
||
11 | use Itstructure\MFUploader\models\album\Album; |
||
12 | use Itstructure\MFUploader\interfaces\UploadModelInterface; |
||
13 | use app\traits\ThumbnailTrait; |
||
14 | |||
15 | /** |
||
16 | * This is the model class for table "articles". |
||
17 | * |
||
18 | * @property int|string $thumbnail thumbnail(mediafile id or url). |
||
19 | * @property array $image image(array of 'mediafile id' or 'mediafile 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 $pageId |
||
25 | * @property string $icon |
||
26 | * @property string $alias |
||
27 | * @property int $active |
||
28 | * |
||
29 | * @property Page $page |
||
30 | * @property ArticleLanguage[] $articlesLanguages |
||
31 | * @property Language[] $languages |
||
32 | * |
||
33 | * @package app\models |
||
34 | */ |
||
35 | class Article extends ActiveRecord |
||
36 | { |
||
37 | use MultilanguageTrait, ThumbnailTrait; |
||
0 ignored issues
–
show
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 'articles'; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | */ |
||
75 | public function rules() |
||
76 | { |
||
77 | return [ |
||
78 | [ |
||
79 | [ |
||
80 | 'pageId', |
||
81 | 'active', |
||
82 | 'alias', |
||
83 | ], |
||
84 | 'required' |
||
85 | ], |
||
86 | [ |
||
87 | [ |
||
88 | 'pageId', |
||
89 | 'active', |
||
90 | ], |
||
91 | 'integer' |
||
92 | ], |
||
93 | [ |
||
94 | [ |
||
95 | 'created_at', |
||
96 | 'updated_at' |
||
97 | ], |
||
98 | 'safe' |
||
99 | ], |
||
100 | [ |
||
101 | [ |
||
102 | 'icon', |
||
103 | 'alias', |
||
104 | ], |
||
105 | 'string', |
||
106 | 'max' => 128 |
||
107 | ], |
||
108 | [ |
||
109 | 'alias', |
||
110 | 'filter', |
||
111 | 'filter' => function ($value) { |
||
112 | return preg_replace( '/[^a-z0-9_]+/', '-', strtolower(trim($value))); |
||
113 | } |
||
114 | ], |
||
115 | [ |
||
116 | 'alias', |
||
117 | 'unique', |
||
118 | 'skipOnError' => true, |
||
119 | 'targetClass' => static::class, |
||
120 | 'filter' => $this->getScenario() == ModelInterface::SCENARIO_UPDATE ? 'id != '.$this->id : '' |
||
121 | ], |
||
122 | [ |
||
123 | [ |
||
124 | 'pageId' |
||
125 | ], |
||
126 | 'exist', |
||
127 | 'skipOnError' => true, |
||
128 | 'targetClass' => Page::class, |
||
129 | 'targetAttribute' => ['pageId' => 'id'] |
||
130 | ], |
||
131 | [ |
||
132 | UploadModelInterface::FILE_TYPE_THUMB, |
||
133 | function($attribute){ |
||
134 | if (!is_numeric($this->{$attribute}) && !is_string($this->{$attribute})){ |
||
135 | $this->addError($attribute, 'Tumbnail content must be a numeric or string.'); |
||
136 | } |
||
137 | }, |
||
138 | 'skipOnError' => false, |
||
139 | ], |
||
140 | [ |
||
141 | UploadModelInterface::FILE_TYPE_IMAGE, |
||
142 | function($attribute) { |
||
143 | if (!is_array($this->{$attribute})) { |
||
144 | $this->addError($attribute, 'Image field content must be an array.'); |
||
145 | } |
||
146 | }, |
||
147 | 'skipOnError' => false, |
||
148 | ], |
||
149 | [ |
||
150 | 'albums', |
||
151 | 'each', |
||
152 | 'rule' => ['integer'], |
||
153 | ], |
||
154 | ]; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @inheritdoc |
||
159 | */ |
||
160 | public function behaviors() |
||
161 | { |
||
162 | return ArrayHelper::merge(parent::behaviors(), [ |
||
163 | 'mediafile' => [ |
||
164 | 'class' => BehaviorMediafile::class, |
||
165 | 'name' => static::tableName(), |
||
166 | 'attributes' => [ |
||
167 | UploadModelInterface::FILE_TYPE_THUMB, |
||
168 | UploadModelInterface::FILE_TYPE_IMAGE, |
||
169 | ], |
||
170 | ], |
||
171 | 'albums' => [ |
||
172 | 'class' => BehaviorAlbum::class, |
||
173 | 'name' => static::tableName(), |
||
174 | 'attributes' => [ |
||
175 | 'albums', |
||
176 | ], |
||
177 | ], |
||
178 | ]); |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * @return array |
||
183 | */ |
||
184 | public function attributes(): array |
||
185 | { |
||
186 | return [ |
||
187 | UploadModelInterface::FILE_TYPE_THUMB, |
||
188 | UploadModelInterface::FILE_TYPE_IMAGE, |
||
189 | 'albums', |
||
190 | 'id', |
||
191 | 'pageId', |
||
192 | 'icon', |
||
193 | 'alias', |
||
194 | 'active', |
||
195 | 'created_at', |
||
196 | 'updated_at' |
||
197 | ]; |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * {@inheritdoc} |
||
202 | */ |
||
203 | public function attributeLabels() |
||
204 | { |
||
205 | return [ |
||
206 | 'id' => 'ID', |
||
207 | 'pageId' => Yii::t('articles', 'Parent page'), |
||
208 | 'icon' => Yii::t('app', 'Icon'), |
||
209 | 'active' => Yii::t('app', 'Active status'), |
||
210 | 'alias' => Yii::t('app', 'URL Alias'), |
||
211 | 'created_at' => Yii::t('app', 'Created date'), |
||
212 | 'updated_at' => Yii::t('app', 'Updated date'), |
||
213 | ]; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * @return \yii\db\ActiveQuery |
||
218 | */ |
||
219 | public function getPage() |
||
220 | { |
||
221 | return $this->hasOne(Page::class, [ |
||
222 | 'id' => 'pageId' |
||
223 | ]); |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * @return \yii\db\ActiveQuery |
||
228 | */ |
||
229 | public function getArticlesLanguages() |
||
230 | { |
||
231 | return $this->hasMany(ArticleLanguage::class, [ |
||
232 | 'articles_id' => 'id' |
||
233 | ]); |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * @return \yii\db\ActiveQuery |
||
238 | */ |
||
239 | public function getLanguages() |
||
240 | { |
||
241 | return $this->hasMany(Language::class, [ |
||
242 | 'id' => 'language_id' |
||
243 | ])->viaTable('articles_language', [ |
||
244 | 'articles_id' => 'id' |
||
245 | ]); |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Get albums, that article has. |
||
250 | * |
||
251 | * @return Album[] |
||
252 | */ |
||
253 | public function getAlbums() |
||
254 | { |
||
255 | return OwnerAlbum::getAlbumsQuery([ |
||
256 | 'owner' => static::tableName(), |
||
257 | 'ownerId' => $this->id, |
||
258 | 'ownerAttribute' => 'albums', |
||
259 | ])->all(); |
||
260 | } |
||
261 | } |
||
262 |