2f25da09
Yarik
first commit
|
1
2
3
4
|
<?php
namespace artweb\artbox\ecommerce\models;
|
8a7e6ecf
Yarik
Namespaces
|
5
6
7
8
|
use artweb\artbox\behaviors\SaveImgBehavior;
use artweb\artbox\components\artboxtree\ArtboxTreeBehavior;
use artweb\artbox\language\behaviors\LanguageBehavior;
use artweb\artbox\language\models\Language;
|
2f25da09
Yarik
first commit
|
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
use Yii;
use yii\base\InvalidParamException;
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;
use yii\db\Query;
use yii\web\Request;
/**
* This is the model class for table "category".
*
* @todo Write doc for ArtboxTreeBehavior
* @property integer $id
* @property integer $remote_id
* @property integer $parent_id
* @property string $path
* @property integer $depth
* @property string $image
|
fa743cfe
Yarik
Catalog + home pa...
|
26
|
* @property string $icon
|
2f25da09
Yarik
first commit
|
27
28
29
30
31
32
|
* @property integer $product_unit_id
* @property Product[] $products
* @property ProductUnit $productUnit
* @property ProductCategory[] $productCategories
* @property Brand[] $brands
* @property TaxGroup[] $taxGroups
|
f540c70b
Yarik
Category finish
|
33
|
* @property Category[] $siblings
|
1f5140e3
Yarik
Brand
|
34
|
* @property Category $parentAR
|
2f25da09
Yarik
first commit
|
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
* * From language behavior *
* @property CategoryLang $lang
* @property CategoryLang[] $langs
* @property CategoryLang $objectLang
* @property string $ownerKey
* @property string $langKey
* @property CategoryLang[] $modelLangs
* @property bool $transactionStatus
* @method string getOwnerKey()
* @method void setOwnerKey( string $value )
* @method string getLangKey()
* @method void setLangKey( string $value )
* @method ActiveQuery getLangs()
* @method ActiveQuery getLang( integer $language_id )
* @method CategoryLang[] generateLangs()
* @method void loadLangs( Request $request )
* @method bool linkLangs()
* @method bool saveLangs()
* @method bool getTransactionStatus()
* * End language behavior *
* * From SaveImgBehavior
* @property string|null $imageFile
* @property string|null $imageUrl
* @method string|null getImageFile( int $field )
|
fa743cfe
Yarik
Catalog + home pa...
|
59
|
* @method string|null getImageUrl( int $field, bool $dummy )
|
2f25da09
Yarik
first commit
|
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
* * End SaveImgBehavior
*/
class Category extends ActiveRecord
{
public function behaviors()
{
return [
'artboxtree' => [
'class' => ArtboxTreeBehavior::className(),
'keyNameGroup' => null,
'keyNamePath' => 'path',
],
'language' => [
'class' => LanguageBehavior::className(),
],
[
'class' => SaveImgBehavior::className(),
'fields' => [
[
'name' => 'image',
'directory' => 'categories',
],
|
fa743cfe
Yarik
Catalog + home pa...
|
83
84
85
86
|
[
'name' => 'icon',
'directory' => 'categories',
],
|
2f25da09
Yarik
first commit
|
87
88
89
90
|
],
],
];
}
|
f9ff89a8
Administrator
add variantSku
|
91
92
|
|
f9ff89a8
Administrator
add variantSku
|
93
|
|
2f25da09
Yarik
first commit
|
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
/**
* @inheritdoc
*/
public static function tableName()
{
return 'category';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[
[
'parent_id',
'depth',
'product_unit_id',
],
'integer',
],
[
[
'path',
],
'string',
],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('product', 'Category ID'),
'parent_id' => Yii::t('product', 'Parent ID'),
'path' => Yii::t('product', 'Path'),
'depth' => Yii::t('product', 'Depth'),
'image' => Yii::t('product', 'Image'),
|
fa743cfe
Yarik
Catalog + home pa...
|
137
|
'icon' => Yii::t('product', 'Icon'),
|
2f25da09
Yarik
first commit
|
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
'imageUrl' => Yii::t('product', 'Image'),
'product_unit_id' => Yii::t('product', 'Product Unit ID'),
'remote_id' => Yii::t('product', 'Remote ID'),
];
}
public static function find()
{
return new CategoryQuery(get_called_class());
}
/**
* @return \yii\db\ActiveQuery
*/
public function getProductUnit()
{
return $this->hasOne(ProductUnit::className(), [ 'id' => 'product_unit_id' ]);
}
/**
* @return ActiveQuery
*/
public function getProducts()
{
return $this->hasMany(Product::className(), [ 'id' => 'product_id' ])
|
f540c70b
Yarik
Category finish
|
163
|
->viaTable('product_category', [ 'category_id' => 'id' ]);
|
2f25da09
Yarik
first commit
|
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
}
/**
* @return \yii\db\ActiveQuery
*/
public function getProductCategories()
{
return $this->hasMany(ProductCategory::className(), [ 'category_id' => 'id' ]);
}
/**
* Get all brands for Category query
*
* @return ActiveQuery
*/
public function getBrands()
{
return $this->hasMany(Brand::className(), [ 'id' => 'brand_id' ])
->via('products');
}
/**
* Get Tax Groups by level
* * 0 for Product
* * 1 for ProductVariant
*
* @param int $level
*
* @return ActiveQuery
*/
public function getTaxGroupsByLevel(int $level)
{
if ($level !== 0 || $level !== 1) {
throw new InvalidParamException('Level supports only 0 and 1 values');
}
return $this->hasMany(TaxGroup::className(), [ 'id' => 'tax_group_id' ])
->viaTable('tax_group_to_category', [ 'category_id' => 'id' ])
->andWhere([ 'level' => $level ]);
}
/**
* Леша найди путь как убрать это, мб в базу записать просто по умолчанию значение и notnull
*
* @param bool $insert
*
* @return bool
*/
public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
if (empty( $this->parent_id )) {
$this->parent_id = 0;
}
return true;
}
return false;
}
/**
* Get query for activefilter for current category
*
* @return Query
*/
public function getActiveFilters()
{
$language_id = Language::getCurrent()->id;
$query1 = ( new Query() )->distinct()
->select(
[
'option_id',
]
)
->from('tax_option')
->innerJoin(
'product_variant_option',
'tax_option.id = product_variant_option.option_id'
)
->innerJoin('tax_group', 'tax_group.id = tax_option.tax_group_id')
->innerJoin(
'product_variant',
'product_variant.id = product_variant_option.product_variant_id'
)
->innerJoin('product', 'product.id = product_variant.product_id')
->innerJoin('product_category', 'product_category.product_id = product.id')
|
01d06e32
Administrator
add variantSku
|
250
251
252
253
254
|
->innerJoin('tax_group_to_category', 'tax_group.id = tax_group_to_category.tax_group_id')
->where(['product_category.category_id' => $this->id,
'tax_group.is_filter' => TRUE,
'tax_group_to_category.category_id'=>$this->id,
])
|
2f25da09
Yarik
first commit
|
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
->andWhere(
[
'!=',
'product_variant.stock',
0,
]
);
$query2 = ( new Query() )->distinct()
->select(
[
'option_id',
]
)
->from('tax_option')
->innerJoin(
'product_option',
'tax_option.id = product_option.option_id'
)
->innerJoin('tax_group', 'tax_group.id = tax_option.tax_group_id')
->innerJoin('product', 'product.id = product_option.product_id')
->innerJoin('product_category', 'product_category.product_id = product.id')
->innerJoin('product_variant', 'product_variant.product_id = product.id')
|
01d06e32
Administrator
add variantSku
|
278
279
280
281
282
|
->innerJoin('tax_group_to_category', 'tax_group.id = tax_group_to_category.tax_group_id')
->where(['product_category.category_id' => $this->id,
'tax_group.is_filter' => TRUE,
'tax_group_to_category.category_id'=>$this->id,
])
|
2f25da09
Yarik
first commit
|
283
284
285
286
287
288
289
290
291
292
|
->andWhere(
[
'!=',
'product_variant.stock',
0,
]
);
$query3 = ( new Query() )->select(
[
'tax_option.*',
|
df08a6f4
Administrator
full commit
|
293
|
'tax_option.id as tax_option_id',
|
2f25da09
Yarik
first commit
|
294
295
|
'tax_option_lang.alias as option_alias',
'tax_group_lang.alias as group_alias',
|
5715e3de
Administrator
full commit
|
296
|
'tax_group_lang.title as title',
|
2f25da09
Yarik
first commit
|
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
'tax_option_lang.value as value',
'tax_option.sort AS tax_option_sort',
'tax_group.sort AS tax_group_sort',
]
)
->from([ 'tax_option' ])
->where([ 'tax_option.id' => $query1->union($query2) ])
->innerJoin('tax_group', 'tax_group.id = tax_option.tax_group_id')
->innerJoin('tax_option_lang', 'tax_option.id = tax_option_lang.tax_option_id')
->innerJoin('tax_group_lang', 'tax_group.id = tax_group_lang.tax_group_id')
->andWhere([ 'tax_option_lang.language_id' => $language_id ])
->andWhere([ 'tax_group_lang.language_id' => $language_id ])
->orderBy('tax_option.sort, tax_group.sort');
return $query3;
}
/**
* Get query to get all TaxGroup for current Category
*
* @return ActiveQuery
*/
public function getTaxGroups()
{
return $this->hasMany(TaxGroup::className(), [ 'id' => 'tax_group_id' ])
->viaTable('tax_group_to_category', [ 'category_id' => 'id' ]);
}
|
f540c70b
Yarik
Category finish
|
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
/**
* Get query to obtain siblings of current category (categories with same parent)
*
* @return ActiveQuery
*/
public function getSiblings()
{
return $this->hasMany($this::className(), [ 'parent_id' => 'parent_id' ])
->andWhere(
[
'not',
[ 'id' => $this->id ],
]
);
}
|
1f5140e3
Yarik
Brand
|
339
340
341
342
343
344
345
346
347
348
|
/**
* Return Active query to obtain parent category
*
* @return ActiveQuery
*/
public function getParentAR()
{
return $this->hasOne(self::className(), [ 'id' => 'parent_id' ]);
}
|
2f25da09
Yarik
first commit
|
349
|
}
|