1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\models; |
4
|
|
|
|
5
|
|
|
use yii\helpers\ArrayHelper; |
6
|
|
|
use Itstructure\AdminModule\models\{MultilanguageTrait, Language}; |
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 $created_at |
21
|
|
|
* @property string $updated_at |
22
|
|
|
* @property int $pageId |
23
|
|
|
* @property string $icon |
24
|
|
|
* @property int $active |
25
|
|
|
* |
26
|
|
|
* @property Page $page |
27
|
|
|
* @property ProductLanguage[] $productsLanguages |
28
|
|
|
* @property Language[] $languages |
29
|
|
|
* |
30
|
|
|
* @package app\models |
31
|
|
|
*/ |
32
|
|
|
class Product extends ActiveRecord |
33
|
|
|
{ |
34
|
|
|
use MultilanguageTrait, ThumbnailTrait; |
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var int|string thumbnail(mediafile id or url). |
38
|
|
|
*/ |
39
|
|
|
public $thumbnail; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var array image(array of 'mediafile id' or 'mediafile url'). |
43
|
|
|
*/ |
44
|
|
|
public $image; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
public $albums = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Initialize. |
53
|
|
|
* Set albums, that product has. |
54
|
|
|
*/ |
55
|
|
|
public function init() |
56
|
|
|
{ |
57
|
|
|
$this->albums = $this->getAlbums(); |
58
|
|
|
|
59
|
|
|
parent::init(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public static function tableName() |
66
|
|
|
{ |
67
|
|
|
return 'products'; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
public function rules() |
74
|
|
|
{ |
75
|
|
|
return [ |
76
|
|
|
[ |
77
|
|
|
[ |
78
|
|
|
'pageId', |
79
|
|
|
'active' |
80
|
|
|
], |
81
|
|
|
'required' |
82
|
|
|
], |
83
|
|
|
[ |
84
|
|
|
[ |
85
|
|
|
'pageId', |
86
|
|
|
'active' |
87
|
|
|
], |
88
|
|
|
'integer' |
89
|
|
|
], |
90
|
|
|
[ |
91
|
|
|
[ |
92
|
|
|
'created_at', |
93
|
|
|
'updated_at' |
94
|
|
|
], |
95
|
|
|
'safe' |
96
|
|
|
], |
97
|
|
|
[ |
98
|
|
|
[ |
99
|
|
|
'icon' |
100
|
|
|
], |
101
|
|
|
'string', |
102
|
|
|
'max' => 64 |
103
|
|
|
], |
104
|
|
|
[ |
105
|
|
|
[ |
106
|
|
|
'pageId' |
107
|
|
|
], |
108
|
|
|
'exist', |
109
|
|
|
'skipOnError' => true, |
110
|
|
|
'targetClass' => Page::class, |
111
|
|
|
'targetAttribute' => ['pageId' => 'id'] |
112
|
|
|
], |
113
|
|
|
[ |
114
|
|
|
UploadModelInterface::FILE_TYPE_THUMB, |
115
|
|
|
function($attribute){ |
116
|
|
|
if (!is_numeric($this->{$attribute}) && !is_string($this->{$attribute})){ |
117
|
|
|
$this->addError($attribute, 'Tumbnail content must be a numeric or string.'); |
118
|
|
|
} |
119
|
|
|
}, |
120
|
|
|
'skipOnError' => false, |
121
|
|
|
], |
122
|
|
|
[ |
123
|
|
|
UploadModelInterface::FILE_TYPE_IMAGE, |
124
|
|
|
function($attribute) { |
125
|
|
|
if (!is_array($this->{$attribute})) { |
126
|
|
|
$this->addError($attribute, 'Image field content must be an array.'); |
127
|
|
|
} |
128
|
|
|
}, |
129
|
|
|
'skipOnError' => false, |
130
|
|
|
], |
131
|
|
|
[ |
132
|
|
|
'albums', |
133
|
|
|
'each', |
134
|
|
|
'rule' => ['integer'], |
135
|
|
|
], |
136
|
|
|
]; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @inheritdoc |
141
|
|
|
*/ |
142
|
|
|
public function behaviors() |
143
|
|
|
{ |
144
|
|
|
return ArrayHelper::merge(parent::behaviors(), [ |
145
|
|
|
'mediafile' => [ |
146
|
|
|
'class' => BehaviorMediafile::class, |
147
|
|
|
'name' => static::tableName(), |
148
|
|
|
'attributes' => [ |
149
|
|
|
UploadModelInterface::FILE_TYPE_THUMB, |
150
|
|
|
UploadModelInterface::FILE_TYPE_IMAGE, |
151
|
|
|
], |
152
|
|
|
], |
153
|
|
|
'albums' => [ |
154
|
|
|
'class' => BehaviorAlbum::class, |
155
|
|
|
'name' => static::tableName(), |
156
|
|
|
'attributes' => [ |
157
|
|
|
'albums', |
158
|
|
|
], |
159
|
|
|
], |
160
|
|
|
]); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return array |
165
|
|
|
*/ |
166
|
|
|
public function attributes(): array |
167
|
|
|
{ |
168
|
|
|
return [ |
169
|
|
|
UploadModelInterface::FILE_TYPE_THUMB, |
170
|
|
|
UploadModelInterface::FILE_TYPE_IMAGE, |
171
|
|
|
'albums', |
172
|
|
|
'id', |
173
|
|
|
'pageId', |
174
|
|
|
'icon', |
175
|
|
|
'active', |
176
|
|
|
'created_at', |
177
|
|
|
'updated_at' |
178
|
|
|
]; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* {@inheritdoc} |
183
|
|
|
*/ |
184
|
|
|
public function attributeLabels() |
185
|
|
|
{ |
186
|
|
|
return [ |
187
|
|
|
'id' => 'ID', |
188
|
|
|
'pageId' => 'Page ID', |
189
|
|
|
'icon' => 'Icon', |
190
|
|
|
'active' => 'Active', |
191
|
|
|
'created_at' => 'Created At', |
192
|
|
|
'updated_at' => 'Updated At', |
193
|
|
|
]; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @return \yii\db\ActiveQuery |
198
|
|
|
*/ |
199
|
|
|
public function getPage() |
200
|
|
|
{ |
201
|
|
|
return $this->hasOne(Page::class, [ |
202
|
|
|
'id' => 'pageId' |
203
|
|
|
]); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return \yii\db\ActiveQuery |
208
|
|
|
*/ |
209
|
|
|
public function getProductsLanguages() |
210
|
|
|
{ |
211
|
|
|
return $this->hasMany(ProductLanguage::class, [ |
212
|
|
|
'products_id' => 'id' |
213
|
|
|
]); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @return \yii\db\ActiveQuery |
218
|
|
|
*/ |
219
|
|
|
public function getLanguages() |
220
|
|
|
{ |
221
|
|
|
return $this->hasMany(Language::class, [ |
222
|
|
|
'id' => 'language_id' |
223
|
|
|
])->viaTable('products_language', [ |
224
|
|
|
'products_id' => 'id' |
225
|
|
|
]); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Get albums, that product has. |
230
|
|
|
* |
231
|
|
|
* @return Album[] |
232
|
|
|
*/ |
233
|
|
|
public function getAlbums() |
234
|
|
|
{ |
235
|
|
|
return OwnerAlbum::getAlbumsQuery([ |
236
|
|
|
'owner' => $this->tableName(), |
237
|
|
|
'ownerId' => $this->id, |
238
|
|
|
'ownerAttribute' => 'albums', |
239
|
|
|
])->all(); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|