Blame view

models/Category.php 14.5 KB
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
      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

41349ad2   Alexey Boroda   -Category sort field
21
       * @property integer           $sort

2f25da09   Yarik   first commit
22
23
24
25
26
       * @property integer           $remote_id

       * @property integer           $parent_id

       * @property string            $path

       * @property integer           $depth

       * @property string            $image

fa743cfe   Yarik   Catalog + home pa...
27
       * @property string            $icon

2f25da09   Yarik   first commit
28
29
30
31
32
33
       * @property integer           $product_unit_id

       * @property Product[]         $products

       * @property ProductUnit       $productUnit

       * @property ProductCategory[] $productCategories

       * @property Brand[]           $brands

       * @property TaxGroup[]        $taxGroups

f540c70b   Yarik   Category finish
34
       * @property Category[]        $siblings

1f5140e3   Yarik   Brand
35
       * @property Category          $parentAR

2f25da09   Yarik   first commit
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
       *       * 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...
60
       * @method string|null getImageUrl( int $field, bool $dummy )

2f25da09   Yarik   first commit
61
62
63
64
65
66
67
68
69
70
       *       * End SaveImgBehavior

       */

      class Category extends ActiveRecord

      {

          

          public function behaviors()

          {

              return [

                  'artboxtree' => [

                      'class'        => ArtboxTreeBehavior::className(),

41349ad2   Alexey Boroda   -Category sort field
71
                      'keyNameGroup' => NULL,

2f25da09   Yarik   first commit
72
73
74
75
76
77
78
79
80
81
82
83
                      'keyNamePath'  => 'path',

                  ],

                  'language'   => [

                      'class' => LanguageBehavior::className(),

                  ],

                  [

                      'class'  => SaveImgBehavior::className(),

                      'fields' => [

                          [

                              'name'      => 'image',

                              'directory' => 'categories',

                          ],

fa743cfe   Yarik   Catalog + home pa...
84
85
86
87
                          [

                              'name'      => 'icon',

                              'directory' => 'categories',

                          ],

2f25da09   Yarik   first commit
88
89
90
91
                      ],

                  ],

              ];

          }

2f25da09   Yarik   first commit
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
          

          /**

           * @inheritdoc

           */

          public static function tableName()

          {

              return 'category';

          }

          

          /**

           * @inheritdoc

           */

          public function rules()

          {

              return [

                  [

                      [

                          'parent_id',

                          'depth',

                          'product_unit_id',

41349ad2   Alexey Boroda   -Category sort field
112
                          'sort',

2f25da09   Yarik   first commit
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
                      ],

                      '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...
136
                  'icon'            => Yii::t('product', 'Icon'),

2f25da09   Yarik   first commit
137
138
139
                  'imageUrl'        => Yii::t('product', 'Image'),

                  'product_unit_id' => Yii::t('product', 'Product Unit ID'),

                  'remote_id'       => Yii::t('product', 'Remote ID'),

41349ad2   Alexey Boroda   -Category sort field
140
                  'sort'            => Yii::t('product', 'Порядок вывода'),

2f25da09   Yarik   first commit
141
142
143
144
145
146
147
148
149
150
151
152
153
              ];

          }

          

          public static function find()

          {

              return new CategoryQuery(get_called_class());

          }

          

          /**

           * @return \yii\db\ActiveQuery

           */

          public function getProductUnit()

          {

02b8486e   Administrator   add variantSku
154
              return $this->hasOne(ProductUnit::className(), [ 'id' => 'product_unit_id' ]);

2f25da09   Yarik   first commit
155
156
157
158
159
160
161
162
          }

          

          /**

           * @return ActiveQuery

           */

          public function getProducts()

          {

              return $this->hasMany(Product::className(), [ 'id' => 'product_id' ])

ad0eb49e   Administrator   add variantSku
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
          }

          

          /**

           * @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)) {

                  

41349ad2   Alexey Boroda   -Category sort field
215
                  if (empty($this->parent_id)) {

2f25da09   Yarik   first commit
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
                      $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')

41349ad2   Alexey Boroda   -Category sort field
250
251
252
253
254
255
256
257
258
259
260
                                       ->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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
                                       ->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')

41349ad2   Alexey Boroda   -Category sort field
284
285
286
287
288
289
290
291
292
293
294
                                       ->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
295
296
297
298
299
300
301
302
303
304
                                       ->andWhere(

                                           [

                                               '!=',

                                               'product_variant.stock',

                                               0,

                                           ]

                                       );

              $query3 = ( new Query() )->select(

                  [

                      'tax_option.*',

df08a6f4   Administrator   full commit
305
                      'tax_option.id as tax_option_id',

2f25da09   Yarik   first commit
306
307
                      'tax_option_lang.alias as option_alias',

                      'tax_group_lang.alias as group_alias',

5715e3de   Administrator   full commit
308
                      'tax_group_lang.title as title',

2f25da09   Yarik   first commit
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
                      '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' ])

d77e7532   Alexey Boroda   -sitemap try
333
                          ->viaTable('tax_group_to_category', [ 'category_id' => 'id' ]);

2f25da09   Yarik   first commit
334
          }

f540c70b   Yarik   Category finish
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
          

          /**

           * 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
351
352
353
354
355
356
357
358
359
360
          

          /**

           * Return Active query to obtain parent category

           *

           * @return ActiveQuery

           */

          public function getParentAR()

          {

              return $this->hasOne(self::className(), [ 'id' => 'parent_id' ]);

          }

2f25da09   Yarik   first commit
361
      }