Blame view

common/modules/product/models/Category.php 11.7 KB
4ca21c3e   Alexey Boroda   first commit
1
2
3
4
5
6
7
8
9
  <?php

  

  namespace common\modules\product\models;

  

  use common\components\artboxtree\ArtboxTreeBehavior;

  use common\components\artboxtree\ArtboxTreeHelper;

  use common\modules\rubrication\models\TaxGroup;

  use Yii;

  

4556b430   Alexey Boroda   Changes:
10
  use yii\db\ActiveQuery;

4ca21c3e   Alexey Boroda   first commit
11
  use yii\db\Query;

4556b430   Alexey Boroda   Changes:
12
13
  use yii\helpers\VarDumper;

  

4ca21c3e   Alexey Boroda   first commit
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  /**

   * This is the model class for table "category".

   *

   * @property integer $category_id

   * @property string $remote_id

   * @property integer $parent_id

   * @property string $path

   * @property integer $depth

   * @property string $image

   * @property string $brand_image

   * @property string $meta_title

   * @property string $meta_desc

   * @property string $meta_robots

   * @property string $seo_text

   * @property string $first_text

   * @property string $second_text

   * @property integer $product_unit_id

   * @property string $alias

   * @property string $h1

   * @property boolean $name

   * @property boolean $new_collection

   * @property Category $parent

   * @property Product[] $products

   * @property ProductUnit $productUnit

   * @property ProductCategory[] $productCategories

6fb4a732   Alexey Boroda   Changes:
39
40
   * @property boolean $stock_program

   * @property boolean $on_order

4ca21c3e   Alexey Boroda   first commit
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
   */

  class Category extends \yii\db\ActiveRecord

  {

      public $imageUpload;

  

      public $brandImageUpload;

  

      public function behaviors()

      {

          return [

              'artboxtree' => [

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

                  'keyNameGroup' => null,

                  'keyNamePath' => 'path',

              ],

              'slug' => [

                  'class' => 'common\behaviors\Slug',

                  'in_attribute' => 'name',

                  'out_attribute' => 'alias',

                  'translit' => true

              ],

  

          ];

      }

  

      /**

       * @inheritdoc

       */

      public static function tableName()

      {

          return 'category';

      }

  

      /**

       * @inheritdoc

       */

      public function rules()

      {

          return [

              [['name'], 'string'],

              [['parent_id', 'depth', 'product_unit_id'], 'integer'],

              [['path', 'meta_desc', 'h1', 'seo_text'], 'string'],

              [['meta_title', 'image', 'brand_image'], 'string', 'max' => 255],

              [['meta_robots'], 'string', 'max' => 50],

              [['alias', 'name'], 'string', 'max' => 250],

              [['imageUpload', 'brandImageUpload'], 'safe'],

              [['imageUpload', 'brandImageUpload'], 'file', 'extensions' => 'jpg, gif, png'],

              [['first_text', 'second_text'], 'string', 'max' => 255],

6fb4a732   Alexey Boroda   Changes:
89
              [['new_collection', 'stock_program', 'on_order'], 'boolean'],

4ca21c3e   Alexey Boroda   first commit
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
          ];

      }

  

      /**

       * @inheritdoc

       */

      public function attributeLabels()

      {

          return [

              'category_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'),

              'imageUrl' => Yii::t('product', 'Image'),

              'brandImageUrl' => 'Картинка брэнда',

              'meta_title' => Yii::t('product', 'Meta Title'),

              'meta_desc' => Yii::t('product', 'Meta Desc'),

              'meta_robots' => Yii::t('product', 'Meta Robots'),

              'h1' => Yii::t('product', 'h1'),

              'seo_text' => Yii::t('product', 'Seo Text'),

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

5a29abd6   Alexey Boroda   -Admin panel pret...
112
              'alias' => 'url',

4ca21c3e   Alexey Boroda   first commit
113
114
115
116
              'name' => Yii::t('product', 'Name'),

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

              'first_text' => 'Заголовок',

              'second_text' => 'Подзаголовок',

5a29abd6   Alexey Boroda   -Admin panel pret...
117
              'new_collection' => 'Новая коллекция (для коллекций)',

4ca21c3e   Alexey Boroda   first commit
118
              'brand_image' => 'Картинка бренда',

5a29abd6   Alexey Boroda   -Admin panel pret...
119
120
121
122
123
              'stock_program' => 'Складская программа (для брендов)',

              'on_order' => 'Под заказ (для брендов)',

              'imageUpload' => 'Картинка',

              'brandImageUpload' => 'Картинка (бренда)'

              

4ca21c3e   Alexey Boroda   first commit
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
          ];

      }

  

      public static function find()

      {

          return new CategoryQuery(get_called_class());

      }

  

      /**

       * @return \yii\db\ActiveQuery

       */

      public function getProductUnit()

      {

          return $this->hasOne(ProductUnit::className(), ['product_unit_id' => 'product_unit_id']);

      }

  

      public function getProducts() {

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

              ->viaTable('product_category', ['category_id' => 'category_id']);

      }

  

4556b430   Alexey Boroda   Changes:
145
146
147
148
149
150
151
152
153
154
155
156
      /**

       * Custom method for baccara

       *

       * @return ActiveQuery

       */

      public function getBaccaraProducts() {

          return Product::find()->joinWith('categories')

              ->where([

                  'category.category_id' => $this->getChildrenByDepth(2)->column(),

              ]);

      }

  

4ca21c3e   Alexey Boroda   first commit
157
158
159
160
161
162
163
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
  

      /**

       * @return \yii\db\ActiveQuery

       */

      public function getProductCategories()

      {

          return $this->hasMany(ProductCategory::className(), ['category_id' => 'category_id']);

      }

  

      public function getBrands(){

          return $this->getProducts()->select('brand.*')->joinWith('brand')->groupBy('brand.brand_id');

  

      }

  

      public function getTaxGroupsByLevel($level)

      {

          return $this->hasMany(TaxGroup::className(), ['tax_group_id' => 'tax_group_id'])

              ->viaTable('tax_group_to_category', ['category_id' => 'category_id'])

              ->andWhere(['level' => $level]);

      }

  

      public function getRemote_category()

      {

          return ArtboxTreeHelper::getArrayField($this->remote_id);

      }

  

      public function setRemote_category($value)

      {

          if (!empty($value) && is_array($value)) {

              $this->remote_id = ArtboxTreeHelper::setArrayField($value, false);

          }

      }

  

      public function getImageFile() {

          return empty($this->image) ? '/images/no_photo.png' : Yii::getAlias('@imagesDir/categories/'. $this->image);

      }

  

      public function getImageUrl()

      {

          return empty($this->image) ? '/images/no_photo.png' : Yii::getAlias('@imagesUrl/categories/' . $this->image);

      }

  

      /**

       * Gets path to brand's image file

       * @return string

       */

      public function getBrandImageFile() {

          return empty($this->brand_image) ? '/images/no_photo.png' : Yii::getAlias('@imagesDir/categories/'. $this->brand_image);

      }

  

      /**

       * Gets url of brand's image

       * @return string

       */

      public function getBrandImageUrl()

      {

          return empty($this->brand_image) ? '/images/no_photo.png' : Yii::getAlias('@imagesUrl/categories/' . $this->brand_image);

      }

  

      public function beforeSave($insert)

      {

          if (parent::beforeSave($insert)) {

  

              if (empty($this->parent_id))

                  $this->parent_id = 0;

  

              return true;

          }

          return false;

      }

      

      public function getActiveFilters() {

4556b430   Alexey Boroda   Changes:
229
230
  

  

4ca21c3e   Alexey Boroda   first commit
231
232
233
234
235
236
237
238
239
240
241
          $query1 = (new Query())

              ->distinct()

              ->select([

                  'option_id'

              ])

              ->from('tax_option')

              ->innerJoin('product_variant_option', 'tax_option.tax_option_id = product_variant_option.option_id')

              ->innerJoin('tax_group', 'tax_group.tax_group_id = tax_option.tax_group_id')

              ->innerJoin('product_variant', 'product_variant.product_variant_id = product_variant_option.product_variant_id')

              ->innerJoin('product', 'product.product_id = product_variant.product_id')

              ->innerJoin('product_category', 'product_category.product_id = product.product_id')

4556b430   Alexey Boroda   Changes:
242
              ->where(['product_category.category_id' => $this->getChildrenByDepth(2)->asArray()->column(), 'tax_group.is_filter' => TRUE])

4ca21c3e   Alexey Boroda   first commit
243
244
245
246
247
248
249
250
251
252
253
254
255
              ->andWhere(['!=', 'product_variant.stock', 0]);

  

          $query2 = (new Query())

              ->distinct()

              ->select([

                  'option_id'

              ])

              ->from('tax_option')

              ->innerJoin('product_option', 'tax_option.tax_option_id = product_option.option_id')

              ->innerJoin('tax_group', 'tax_group.tax_group_id = tax_option.tax_group_id')

              ->innerJoin('product', 'product.product_id = product_option.product_id')

              ->innerJoin('product_category', 'product_category.product_id = product.product_id')

              ->innerJoin('product_variant', 'product_variant.product_id = product.product_id')

4556b430   Alexey Boroda   Changes:
256
              ->where(['product_category.category_id' => $this->getChildrenByDepth(2)->asArray()->column(), 'tax_group.is_filter' => TRUE])

4ca21c3e   Alexey Boroda   first commit
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
              ->andWhere(['!=', 'product_variant.stock', 0]);

          $query3 = (new Query())

              ->select([

                  'tax_option.*',

                  'tax_group.*',

                  'tax_option.alias as option_alias',

                  'tax_group.alias as group_alias',

                  'tax_option.value as value',

                  'tax_option.sort AS tax_option_sort',

                  'tax_group.sort AS tax_group_sort',

              ])

              ->from(['tax_option' ])

              ->where(['tax_option.tax_option_id'=>$query1->union($query2)])

              ->innerJoin('tax_group','tax_group.tax_group_id = tax_option.tax_group_id')

              ->orderBy('tax_option.sort, tax_group.sort');

          return $query3;

      }

  

      public function getTaxGroupsForMenu()

      {

  

          $connection = Yii::$app->getDb();

          $command = $connection->createCommand('

                  SELECT ton.alias as option_alias, *

                  FROM tax_option as ton

                  RIGHT JOIN tax_group ON ton.tax_group_id = tax_group.tax_group_id

                  RIGHT JOIN tax_group_to_category ON tax_group.tax_group_id = tax_group_to_category.tax_group_id

                  WHERE  ton.tax_option_id IN (

                  SELECT po.option_id FROM product_option as po

                  WHERE po.product_id IN (

                          SELECT product_id FROM product_category WHERE category_id = :category_id

                      )

                  )

                  AND tax_group.is_menu = true AND tax_group_to_category.category_id = :category_id',

              [

                  ':category_id' => $this->category_id

  

              ]);

          return $command->queryAll();

  

  

  

      }

  

4556b430   Alexey Boroda   Changes:
301
302
      public function getChildrenByDepth($depth)

      {

55371b6c   Alexey Boroda   -Get all children...
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
  //        VarDumper::dump($this->getAllChildren($depth)

  //

  //            ->where([

  //            'depth' => $depth,

  //            'parent_id' => $this->getChildrenAL()

  //        ])->asArray()->select(['category_id'])->createCommand()->rawSql, 10, true);

  //

          

       

          return $this->getAllChildren($depth)

  

                      ->where([

                          'depth' => $depth,

                          'parent_id' => $this->getChildrenAL()->select('category_id')->column()

                      ])->asArray()->select(['category_id']);

4556b430   Alexey Boroda   Changes:
318
      }

4ca21c3e   Alexey Boroda   first commit
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
  

      public function setTaxGroup($value)

      {

          return $this->tax_group = $value;

      }

  

      public function getTaxGroup()

      {

          return $this->hasMany(TaxGroup::className(), ['tax_group_id' => 'tax_group_id'])

              ->viaTable('tax_group_to_category', ['category_id' => 'category_id']);

      }

  

      public function getParent()

      {

          return $this->hasOne(Category::className(), [

              'category_id' => 'parent_id',

          ]);

      }

2d7ef064   Alexey Boroda   added menu and pr...
337
338
339
340
341
342
343
344
345
  

      public function getGrandParent()

      {

          return $this->hasOne(Category::className(), [

              'category_id' => 'parent_id',

          ])->viaTable('category', [

              'category_id' => 'parent_id',

          ], function($query) { $query->from('category c2'); });

      }

4ca21c3e   Alexey Boroda   first commit
346
  }