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