Blame view

frontend/models/ProductFrontendSearch.php 7.64 KB
3f2bc3d0   Administrator   first commit
1
2
3
4
5
  <?php

  

  namespace frontend\models;

  

  use common\modules\product\models\Brand;

8724ec1f   Karnovsky A   -
6
7
  use common\modules\product\models\BrandName;

  use common\modules\product\models\CategoryName;

3f2bc3d0   Administrator   first commit
8
9
10
11
12
13
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
39
40
41
42
43
44
45
46
47
48
49
50
51
  use common\modules\product\models\ProductCategory;

  use common\modules\product\models\ProductOption;

  use common\modules\product\models\ProductSearch;

  use common\modules\rubrication\models\TaxGroup;

  use common\modules\rubrication\models\TaxOption;

  use Yii;

  use yii\base\Model;

  use yii\data\ActiveDataProvider;

  use yii\db\ActiveQuery;

  use yii\db\ActiveRecord;

  use yii\web\NotFoundHttpException;

  use common\modules\product\models\Product;

  use common\modules\product\models\ProductVariant;

  

  class ProductFrontendSearch extends Product {

      /**

       * @inheritdoc

       */

      public function rules()

      {

          return [

              [['price_interval', 'brands'], 'safe'],

          ];

      }

  

      /**

       * @inheritdoc

       */

      public function scenarios()

      {

          // bypass scenarios() implementation in the parent class

          return Model::scenarios();

      }

  

      /**

       * Creates data provider instance with search query applied for frontend

       *

       * @param array $params

       *

       * @return ActiveDataProvider

       */

      public function search($category = null, $params = []) {

          if (!empty($category)) {

              /** @var ActiveQuery $query */

8724ec1f   Karnovsky A   -
52
53
  //            $query = $category->getRelations('product_categories');

              $query = $category->getProducts();

3f2bc3d0   Administrator   first commit
54
55
56
          } else {

              $query = Product::find();

          }

8724ec1f   Karnovsky A   -
57
          $query->joinWith(['variant', 'brand', 'brand.brandName', 'category', 'category.categoryName']);

4902c747   Karnovsky A   -
58
59
60
61
62
63
  

          $query->groupBy('product.product_id');

  

  //        if (empty($_GET['sort']) || ($_GET['sort'] != 'price' && $_GET['sort'] != '-price')) {

  //            $query->groupBy('product.product_id');

  //        }

3f2bc3d0   Administrator   first commit
64
65
66
67
  

          $dataProvider = new ActiveDataProvider([

              'query' => $query,

              'pagination' => [

4902c747   Karnovsky A   -
68
                  'pageSize' => 16,

3f2bc3d0   Administrator   first commit
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
              ],

              'sort' => [

                  'attributes' => [

                      'name' => [

                          'asc' => ['name' => SORT_ASC],

                          'desc' => ['name' => SORT_DESC],

                          'default' => SORT_DESC,

                          'label' => 'имени',

                      ],

                      'price' => [

                          'asc' => [ProductVariant::tableName() .'.price' => SORT_ASC],

                          'desc' => [ProductVariant::tableName() .'.price' => SORT_DESC],

                          'default' => SORT_DESC,

                          'label' => 'цене',

                      ],

                  ],

              ]

          ]);

  

          if (!$this->validate()) {

              return $dataProvider;

          }

  

          $this->_setParams($query, $params);

  

          return $dataProvider;

      }

  

      public function optionsForCategory($category = null, $params = []) {

c34b3aa0   Karnovsky A   -
98
99
  //        ,

  //        'COUNT('. ProductOption::tableName() .'.product_id) AS _items_count'

3f2bc3d0   Administrator   first commit
100
101
          $query = TaxOption::find()

              ->select([

c34b3aa0   Karnovsky A   -
102
                  TaxOption::tableName() .'.*'

3f2bc3d0   Administrator   first commit
103
104
105
106
107
108
109
110
111
              ])

              ->innerJoin(ProductOption::tableName(), ProductOption::tableName() .'.option_id='. TaxOption::tableName() .'.tax_option_id')

              ->innerJoin(ProductCategory::tableName(), ProductCategory::tableName() .'.product_id='. ProductOption::tableName() .'.product_id')

              ->innerJoin(TaxGroup::tableName(), TaxGroup::tableName() .'.tax_group_id='. TaxOption::tableName() .'.tax_group_id')

              ->andWhere([TaxGroup::tableName() .'.is_filter' => true]);

          if (!empty($category)) {

              $query->andWhere([ProductCategory::tableName() .'.category_id' => $category->category_id]);

          }

          $query->groupBy(TaxOption::tableName() .'.tax_option_id');

c34b3aa0   Karnovsky A   -
112
          $query->limit(null);

3f2bc3d0   Administrator   first commit
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
          if (!empty($params['prices'])) {

              if ($params['prices']['min'] > 0 || $params['prices']['max'] > 0) {

                  $query->innerJoin(ProductVariant::tableName(), ProductVariant::tableName() .'.product_id='. ProductCategory::tableName() .'.product_id');

              }

              if ($params['prices']['min'] > 0) {

                  $query->andWhere(['>=', ProductVariant::tableName() .'.price', $params['prices']['min']]);

              }

              if ($params['prices']['max'] > 0) {

                  $query->andWhere(['<=', ProductVariant::tableName() .'.price', $params['prices']['max']]);

              }

          }

          if (!empty($params['brands'])) {

              $query->innerJoin(Product::tableName(), Product::tableName() .'.product_id='. ProductCategory::tableName() .'.product_id');

              $query->andWhere([Product::tableName() .'.brand_id' => $params['brands']]);

          }

  

8724ec1f   Karnovsky A   -
129
          return $query;

3f2bc3d0   Administrator   first commit
130
131
132
133
134
      }

  

      public function priceLimits($category = null, $params = []) {

          if (!empty($category)) {

              /** @var ActiveQuery $query */

8724ec1f   Karnovsky A   -
135
136
  //            $query = $category->getRelations('product_categories');

              $query = $category->getProducts();

3f2bc3d0   Administrator   first commit
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
          } else {

              $query = Product::find();

          }

          $query->joinWith('variant');

  

          $this->_setParams($query, $params, false);

  

  //        $query->select([

  //            'MIN('. ProductVariant::tableName() .'.price) AS priceMIN',

  //            'MAX('. ProductVariant::tableName() .'.price) AS priceMAX',

  //        ]);

  

          return [

              'min' => $query->min(ProductVariant::tableName() .'.price'),

              'max' => $query->max(ProductVariant::tableName() .'.price'),

          ];

      }

  

      protected function _setParams(&$query, $params, $setPriceLimits = true) {

          if (!empty($params['keywords'])) {

              if (!is_array($params['keywords'])) {

                  $params['keywords'] = [$params['keywords']];

              }

              foreach ($params['keywords'] as $keyword) {

8724ec1f   Karnovsky A   -
161
162
163
                  $query->orFilterWhere(['ilike', Product::tableName() .'.name', $keyword]);

                  $query->orFilterWhere(['ilike', BrandName::tableName() .'.value', $keyword]);

                  $query->orFilterWhere(['ilike', CategoryName::tableName() .'.value', $keyword]);

3f2bc3d0   Administrator   first commit
164
165
              }

          }

e2651dcd   Karnovsky A   Special-product-f...
166
167
168
169
170
          if (!empty($params['special'])) {

              foreach($params['special'] as $key => $value) {

                  $query->orFilterWhere([Product::tableName() .'.'. $key => $value]);

              }

          }

3f2bc3d0   Administrator   first commit
171
172
173
174
175
176
177
178
179
180
181
182
183
          if (!empty($params['brands'])) {

              $query->andFilterWhere([Product::tableName() .'.brand_id' => $params['brands']]);

          }

          if (!empty($params['options'])) {

              foreach ($params['options'] as $group => $options) {

                  foreach ($options as &$option) {

                      $option = "'$option'";

                  }

                  $query->andWhere(

                      Product::tableName() . '.product_id IN (SELECT product_id AS products FROM product_option INNER JOIN tax_option ON tax_option.tax_option_id = product_option.option_id INNER JOIN tax_group ON tax_group.tax_group_id = tax_option.tax_group_id WHERE tax_group.alias LIKE \''. $group .'\' AND tax_option.alias IN (' . implode(',', $options) . '))'

                  );

              }

          }

e2651dcd   Karnovsky A   Special-product-f...
184
185
186
  

  

  

3f2bc3d0   Administrator   first commit
187
188
189
190
191
192
193
194
195
196
          if ($setPriceLimits && !empty($params['prices'])) {

              if ($params['prices']['min'] > 0) {

                  $query->andWhere(['>=', ProductVariant::tableName() .'.price', $params['prices']['min']]);

              }

              if ($params['prices']['max'] > 0) {

                  $query->andWhere(['<=', ProductVariant::tableName() .'.price', $params['prices']['max']]);

              }

          }

      }

  }