a8370482
Alexander Karnovsky
init project
|
1
2
3
4
|
<?php
namespace common\modules\product\models;
|
5aa7418e
Karnovsky A
Base-product#3 fu...
|
5
|
use common\behaviors\Slug;
|
a8370482
Alexander Karnovsky
init project
|
6
7
8
9
|
use common\modules\rubrication\models\TaxOption;
use Yii;
use common\modules\relation\relationBehavior;
use yii\db\ActiveQuery;
|
e2128676
Karnovsky A
Karnovsky 0505201...
|
10
11
|
use yii\helpers\Html;
use yii\web\UploadedFile;
|
a8370482
Alexander Karnovsky
init project
|
12
13
14
15
16
|
/**
* This is the model class for table "{{%product}}".
*
* @property string $name
|
b519af22
Karnovsky A
Base-product func...
|
17
|
* @property integer $brand_id
|
a8370482
Alexander Karnovsky
init project
|
18
|
* @property integer $product_id
|
b519af22
Karnovsky A
Base-product func...
|
19
20
21
22
23
|
* @property Category $category
* @property array $categories
* @property ProductVariant $variant
* @property ProductImage $image
* @property array $images
|
550f051a
Karnovsky A
Karnovsky-0505201...
|
24
25
|
* @property boolean $is_top
* @property boolean $is_new
|
a8370482
Alexander Karnovsky
init project
|
26
27
28
|
*/
class Product extends \yii\db\ActiveRecord
{
|
14eadb86
Karnovsky A
Eager loading for...
|
29
|
/** @var array $_variants */
|
b519af22
Karnovsky A
Base-product func...
|
30
|
public $_variants = [];
|
5aa7418e
Karnovsky A
Base-product#3 fu...
|
31
32
33
|
/** @var array $_images */
public $imagesUpload = [];
|
a8370482
Alexander Karnovsky
init project
|
34
35
36
37
38
39
40
41
42
|
/**
* @inheritdoc
*/
public function behaviors()
{
return [
[
'class' => relationBehavior::className(),
'relations' => [
|
5aa7418e
Karnovsky A
Base-product#3 fu...
|
43
44
|
'product_categories' => 'entity1', // Product category
'product_option' => 'entity1' // Product category
|
a8370482
Alexander Karnovsky
init project
|
45
46
|
]
],
|
5aa7418e
Karnovsky A
Base-product#3 fu...
|
47
48
49
50
51
52
|
[
'class' => Slug::className(),
'in_attribute' => 'name',
'out_attribute' => 'alias',
'translit' => true
]
|
a8370482
Alexander Karnovsky
init project
|
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
];
}
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%product}}';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
|
b519af22
Karnovsky A
Base-product func...
|
70
|
[['brand_id'], 'integer'],
|
a8370482
Alexander Karnovsky
init project
|
71
|
[['name'], 'string', 'max' => 150],
|
5aa7418e
Karnovsky A
Base-product#3 fu...
|
72
|
[['alias'], 'string', 'max' => 250],
|
e2128676
Karnovsky A
Karnovsky 0505201...
|
73
74
|
[['categories', 'variants', 'options', 'imagesUpload'], 'safe'],
[['imagesUpload'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg, gif', 'maxFiles' => 50],
|
5aa7418e
Karnovsky A
Base-product#3 fu...
|
75
|
[['description', 'video'], 'safe'],
|
550f051a
Karnovsky A
Karnovsky-0505201...
|
76
|
[['is_top', 'is_new'], 'boolean'],
|
a8370482
Alexander Karnovsky
init project
|
77
78
79
80
81
82
83
84
85
86
|
// [['product_id'], 'exist', 'skipOnError' => true, 'targetClass' => Product::className(), 'targetAttribute' => ['product_id' => 'product_id']],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
|
b519af22
Karnovsky A
Base-product func...
|
87
|
'product_id' => Yii::t('product', 'ID'),
|
a8370482
Alexander Karnovsky
init project
|
88
|
'name' => Yii::t('product', 'Name'),
|
b519af22
Karnovsky A
Base-product func...
|
89
|
'brand_id' => Yii::t('product', 'Brand'),
|
a8370482
Alexander Karnovsky
init project
|
90
|
'categories' => Yii::t('product', 'Categories'), // relation behavior field
|
b519af22
Karnovsky A
Base-product func...
|
91
|
'category' => Yii::t('product', 'Category'), // relation behavior field
|
5aa7418e
Karnovsky A
Base-product#3 fu...
|
92
93
|
'image' => Yii::t('product', 'Image'),
'images' => Yii::t('product', 'Images'),
|
550f051a
Karnovsky A
Karnovsky-0505201...
|
94
95
96
97
98
|
'description' => Yii::t('product', 'Description'),
'video' => Yii::t('product', 'Video embeded'),
'variants' => Yii::t('product', 'Variants'),
'is_top' => Yii::t('product', 'Is top'),
'is_new' => Yii::t('product', 'Is new'),
|
a8370482
Alexander Karnovsky
init project
|
99
100
101
102
103
104
105
106
|
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getBrand()
{
|
b519af22
Karnovsky A
Base-product func...
|
107
|
return $this->hasOne(Brand::className(), ['brand_id' => 'brand_id']);
|
a8370482
Alexander Karnovsky
init project
|
108
109
|
}
|
b519af22
Karnovsky A
Base-product func...
|
110
111
112
113
114
115
116
117
118
119
120
121
|
/**
* @return \yii\db\ActiveQuery
*/
public function getImage()
{
return $this->hasOne(ProductImage::className(), ['product_id' => 'product_id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getImages()
|
a8370482
Alexander Karnovsky
init project
|
122
|
{
|
b519af22
Karnovsky A
Base-product func...
|
123
|
return $this->hasMany(ProductImage::className(), ['product_id' => 'product_id']);
|
a8370482
Alexander Karnovsky
init project
|
124
125
|
}
|
5aa7418e
Karnovsky A
Base-product#3 fu...
|
126
127
128
129
|
public function setImages($images) {
$this->_images = $images;
}
|
b519af22
Karnovsky A
Base-product func...
|
130
131
132
133
|
/**
* @return \yii\db\ActiveQuery
*/
public function getVariant()
|
a8370482
Alexander Karnovsky
init project
|
134
|
{
|
b519af22
Karnovsky A
Base-product func...
|
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
return $this->hasOne(ProductVariant::className(), ['product_id' => 'product_id']);
}
public function getVariantPrice() {
return $this->variant->price;
}
/**
* @return \yii\db\ActiveQuery
*/
public function getVariants()
{
return $this->hasMany(ProductVariant::className(), ['product_id' => 'product_id']);
}
public function setVariants($variants) {
$this->_variants = $variants;
}
public function getFullName()
{
|
7cdb0894
Karnovsky A
Some fixes
|
155
|
return empty($this->brand) ? $this->name : $this->brand->name .' '. $this->name;
|
b519af22
Karnovsky A
Base-product func...
|
156
157
158
|
}
public function getCategories() {
|
550f051a
Karnovsky A
Karnovsky-0505201...
|
159
160
|
return $this->hasMany(Category::className(), ['category_id' => 'category_id'])->viaTable('product_category', ['product_id' => 'product_id']);
// return $this->getRelations('product_categories');
|
b519af22
Karnovsky A
Base-product func...
|
161
162
163
164
165
166
167
168
|
}
public function getCategoriesNames() {
$result = [];
foreach($this->categories as $category) {
$result[] = $category->name;
}
return $result;
|
a8370482
Alexander Karnovsky
init project
|
169
170
171
|
}
public function getCategory() {
|
550f051a
Karnovsky A
Karnovsky-0505201...
|
172
|
return $this->hasOne(Category::className(), ['category_id' => 'category_id'])->viaTable('product_category', ['product_id' => 'product_id']);
|
a8370482
Alexander Karnovsky
init project
|
173
174
|
}
|
5aa7418e
Karnovsky A
Base-product#3 fu...
|
175
176
177
178
|
public function getOptions() {
return $this->getRelations('product_option');
}
|
a8370482
Alexander Karnovsky
init project
|
179
180
181
182
183
184
185
186
|
/**
* @inheritdoc
* @return ProductQuery the active query used by this AR class.
*/
public static function find()
{
return new ProductQuery(get_called_class());
}
|
b519af22
Karnovsky A
Base-product func...
|
187
188
189
190
191
|
public function afterSave($insert, $changedAttributes)
{
parent::afterSave($insert, $changedAttributes);
|
e2128676
Karnovsky A
Karnovsky 0505201...
|
192
193
194
195
196
197
198
199
200
201
202
203
|
// $images = UploadedFile::getInstance($this, 'imagesUpload');
// var_dump($images);exit;
// if (!empty($this->imagesUpload)) {
// if (!is_array($this->imagesUpload)) {
// $this->imagesUpload = [$this->imagesUpload];
// }
// foreach($this->imagesUpload as $image) {
// $image->saveAs((Yii::getAlias('@frontend/web/images/products/original/' . $image->baseName .'_'. uniqid() . '.' . $image->extension)));
// }
//
//
|
0cc2a675
Karnovsky A
Base-product#4 fu...
|
204
|
// }
|
5aa7418e
Karnovsky A
Base-product#3 fu...
|
205
|
|
b519af22
Karnovsky A
Base-product func...
|
206
207
208
209
210
|
$todel = [];
foreach ($this->variants ? : [] as $_variant) {
$todel[$_variant->product_variant_id] = $_variant->product_variant_id;
}
foreach ($this->_variants as $_variant) {
|
5aa7418e
Karnovsky A
Base-product#3 fu...
|
211
212
213
|
if (!is_array($_variant)) {
return;
}
|
b519af22
Karnovsky A
Base-product func...
|
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
if (!empty($_variant['product_variant_id'])) {
unset($todel[$_variant['product_variant_id']]);
$model = ProductVariant::findOne($_variant['product_variant_id']);
} else {
$model = new ProductVariant();
}
$_variant['product_id'] = $this->product_id;
$model->load(['ProductVariant' => $_variant]);
$model->save();
}
if (!empty($todel)) {
ProductVariant::deleteAll(['product_variant_id' => $todel]);
}
}
|
e2128676
Karnovsky A
Karnovsky 0505201...
|
228
229
230
231
|
public function imagesUpload()
{
if ($this->validate()) {
|
cf07505d
Karnovsky A
Karnovsky 0605201...
|
232
|
$images = [];
|
e2128676
Karnovsky A
Karnovsky 0505201...
|
233
|
foreach ($this->imagesUpload as $image) {
|
cf07505d
Karnovsky A
Karnovsky 0605201...
|
234
235
236
237
238
239
240
241
242
|
$imageName = $image->baseName .'.'. $image->extension;
$i = 0;
while(file_exists(Yii::getAlias('@frontend/web/images/products/' . $imageName))) {
$i++;
$imageName = $image->baseName .'_'. $i .'.'. $image->extension;
}
$image->saveAs(Yii::getAlias('@frontend/web/images/products/' .$imageName));
$images[] = $imageName;
|
e2128676
Karnovsky A
Karnovsky 0505201...
|
243
|
}
|
cf07505d
Karnovsky A
Karnovsky 0605201...
|
244
|
return $images;
|
e2128676
Karnovsky A
Karnovsky 0505201...
|
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
} else {
return false;
}
}
public function getImagesHTML() {
$op = [];
if ($this->images) {
foreach ($this->images as $image) {
$op[] = Html::img($image->imageUrl);
}
}
return $op;
}
|
cf07505d
Karnovsky A
Karnovsky 0605201...
|
259
260
261
262
263
264
265
266
|
public function getImagesConfig() {
$op = [];
if ($this->images) {
foreach ($this->images as $image) {
$op[] = [
'caption' => $image->image,
'width' => '120px',
|
dc50f607
Karnovsky A
Some fixes
|
267
268
269
270
271
|
'url' => \yii\helpers\Url::to(['/product/manage/delimg', 'id' => $image->product_image_id]),
'key' => $image->product_image_id,
'extra' => [
'id' => $image->product_image_id,
],
|
cf07505d
Karnovsky A
Karnovsky 0605201...
|
272
273
274
275
276
|
];
}
}
return $op;
}
|
a8370482
Alexander Karnovsky
init project
|
277
|
}
|