Blame view

common/modules/product/models/Category.php 9.64 KB
4253cbec   root   first commit
1
2
3
4
  <?php

  

  namespace common\modules\product\models;

  

871a7349   Administrator   big commti
5
  

4253cbec   root   first commit
6
7
  use common\components\artboxtree\ArtboxTreeBehavior;

  use common\components\artboxtree\ArtboxTreeHelper;

871a7349   Administrator   big commti
8
  

4253cbec   root   first commit
9
10
  use common\modules\rubrication\models\TaxGroup;

  use Yii;

871a7349   Administrator   big commti
11
  

4253cbec   root   first commit
12
  use yii\db\Query;

e60b727a   Administrator   big commti
13
  use common\behaviors\Slug;

4253cbec   root   first commit
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  /**

   * 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 $meta_title

   * @property string $meta_desc

   * @property string $meta_robots

   * @property string $seo_text

   * @property integer $category_name_id

   * @property integer $product_unit_id

   * @property string $alias

   * @property boolean $populary

871a7349   Administrator   big commti
31
   * @property boolean $name

4253cbec   root   first commit
32
33
34
35
36
37
38
39
40
41
   *

   * @property CategoryName $categoryName

   * @property Product[] $products

   * @property ProductUnit $productUnit

   * @property CategoryName[] $categoryNames

   * @property ProductCategory[] $productCategories

   */

  class Category extends \yii\db\ActiveRecord

  {

      public $imageUpload;

4253cbec   root   first commit
42
43
44
45
46
47
48
49
  

      public function behaviors()

      {

          return [

              'artboxtree' => [

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

                  'keyNameGroup' => null,

                  'keyNamePath' => 'path',

e60b727a   Administrator   big commti
50
              ],

5a0c03c7   Administrator   big commti
51
52
              'slug' => [

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

e60b727a   Administrator   big commti
53
54
55
                  'in_attribute' => 'name',

                  'out_attribute' => 'alias',

                  'translit' => true

5a0c03c7   Administrator   big commti
56
              ],

771f8d54   Administrator   big commti
57
  

4253cbec   root   first commit
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
          ];

      }

  

      /**

       * @inheritdoc

       */

      public static function tableName()

      {

          return 'category';

      }

  

      /**

       * @inheritdoc

       */

      public function rules()

      {

          return [

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

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

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

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

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

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

              [['populary'], 'boolean'],

aefe93aa   Administrator   big commti
82
              [['imageUpload','taxGroup'], 'safe'],

4253cbec   root   first commit
83
84
85
86
87
88
89
90
91
92
93
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
              [['imageUpload'], 'file', 'extensions' => 'jpg, gif, png'],

          ];

      }

  

      /**

       * @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'),

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

              'alias' => Yii::t('product', 'Alias'),

              'populary' => Yii::t('product', 'Populary'),

              'name' => Yii::t('product', 'Name'),

              '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(), ['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']);

  //        return $this->getRelations('product_categories');

      }

  

4253cbec   root   first commit
131
132
133
134
135
136
137
138
139
  

      /**

       * @return \yii\db\ActiveQuery

       */

      public function getProductCategories()

      {

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

      }

  

9ffea880   Administrator   big commti
140
141
142
143
      public function getBrands(){

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

  

      }

4253cbec   root   first commit
144
145
146
  

      public function getTaxGroupsByLevel($level)

      {

d09f430f   Administrator   big commti
147
148
          return $this->hasMany(TaxGroup::className(), ['tax_group_id' => 'tax_group_id'])

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

4253cbec   root   first commit
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
              ->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);

          }

      }

  

4253cbec   root   first commit
164
  

871a7349   Administrator   big commti
165
  

4253cbec   root   first commit
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
  

      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);

      }

  

      public function beforeSave($insert)

      {

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

  

              if (empty($this->parent_id))

                  $this->parent_id = 0;

  

              return true;

          }

          return false;

      }

  

      public function beforeDelete()

      {

  

          if(!empty($this->products)){

              foreach($this->products as $product){

                  $product->delete();

              }

          }

          ProductCategory::deleteAll(['category_id' => $this->category_id]);

4253cbec   root   first commit
197
198
          return true;

      }

4253cbec   root   first commit
199
200
201
202
      public function getActiveFilters() {

          $query1 = (new Query())

              ->distinct()

              ->select([

871a7349   Administrator   big commti
203
                  'option_id'

4253cbec   root   first commit
204
205
206
207
208
209
210
              ])

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

37415bee   Administrator   big commti
211
212
              ->innerJoin('tax_group_to_category', 'tax_group.tax_group_id = tax_group_to_category.tax_group_id')

              ->where(['product_category.category_id' => $this->category_id, 'tax_group.is_filter' => TRUE,'tax_group_to_category.category_id'=>$this->category_id])

4253cbec   root   first commit
213
              ->andWhere(['!=', 'product_variant.stock', 0]);

871a7349   Administrator   big commti
214
  

4253cbec   root   first commit
215
216
217
          $query2 = (new Query())

              ->distinct()

              ->select([

871a7349   Administrator   big commti
218
                  'option_id'

4253cbec   root   first commit
219
220
221
222
223
224
225
              ])

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

37415bee   Administrator   big commti
226
227
              ->innerJoin('tax_group_to_category', 'tax_group.tax_group_id = tax_group_to_category.tax_group_id')

              ->where(['product_category.category_id' => $this->category_id, 'tax_group.is_filter' => TRUE,'tax_group_to_category.category_id'=>$this->category_id])

4253cbec   root   first commit
228
229
              ->andWhere(['!=', 'product_variant.stock', 0]);

          $query3 = (new Query())

871a7349   Administrator   big commti
230
231
232
233
234
              ->select([

                  'tax_option.*',

                  'tax_group.*',

                  'tax_option.alias as option_alias',

                  'tax_group.alias as group_alias',

b905419d   Administrator   big commti
235
                  'tax_option.name as value',

871a7349   Administrator   big commti
236
237
238
239
240
241
                  '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')

871a7349   Administrator   big commti
242
              ->orderBy('tax_option.sort, tax_group.sort');

4253cbec   root   first commit
243
244
245
          return $query3;

      }

  

c4015198   Administrator   big commti
246
      public function getTaxGroupsForMenu()

e337d04a   Administrator   liniya first commit
247
248
249
250
      {

  

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

          $command = $connection->createCommand('

b905419d   Administrator   big commti
251
                  SELECT ton.alias as option_alias,ton.name as value, *

e337d04a   Administrator   liniya first commit
252
                  FROM tax_option as ton

e337d04a   Administrator   liniya first commit
253
                  RIGHT JOIN tax_group ON ton.tax_group_id = tax_group.tax_group_id

c93dd110   Administrator   big commti
254
                  RIGHT JOIN tax_group_to_category ON tax_group.tax_group_id = tax_group_to_category.tax_group_id

e337d04a   Administrator   liniya first commit
255
256
257
258
259
260
                  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

                      )

                  )

c93dd110   Administrator   big commti
261
                  AND tax_group.is_menu = true AND tax_group_to_category.category_id = :category_id',

e337d04a   Administrator   liniya first commit
262
              [

e337d04a   Administrator   liniya first commit
263
264
265
266
267
268
269
270
                  ':category_id' => $this->category_id

  

              ]);

          return $command->queryAll();

  

  

  

      }

d09f430f   Administrator   big commti
271
  

aefe93aa   Administrator   big commti
272
273
274
  

      public function setTaxGroup($value)

      {

b905419d   Administrator   big commti
275
          return $this->taxgroup = $value;

aefe93aa   Administrator   big commti
276
277
      }

  

d09f430f   Administrator   big commti
278
279
280
281
282
      public function getTaxGroup()

      {

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

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

      }

4253cbec   root   first commit
283
  }