Blame view

frontend/models/ProductFrontendSearch.php 7.63 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();

          }

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

4902c747   Karnovsky A   -
58
59
60
  

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

  

3f2bc3d0   Administrator   first commit
61
62
63
          $dataProvider = new ActiveDataProvider([

              'query' => $query,

              'pagination' => [

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

3f2bc3d0   Administrator   first commit
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
              ],

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

  

3147aab1   Karnovsky A   Fix search with s...
90
91
          $query->andWhere(['!=', ProductVariant::tableName() .'.stock', 0]);

  

3f2bc3d0   Administrator   first commit
92
93
94
95
          return $dataProvider;

      }

  

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

c34b3aa0   Karnovsky A   -
96
97
  //        ,

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

3f2bc3d0   Administrator   first commit
98
99
          $query = TaxOption::find()

              ->select([

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

3f2bc3d0   Administrator   first commit
101
102
103
104
105
106
107
108
109
              ])

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

163914fd   Karnovsky A   -
110
          $query->orderBy(TaxOption::tableName() .'.sort', SORT_ASC);

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

3f2bc3d0   Administrator   first commit
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
          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   -
128
          return $query;

3f2bc3d0   Administrator   first commit
129
130
131
132
133
      }

  

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

          if (!empty($category)) {

              /** @var ActiveQuery $query */

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

              $query = $category->getProducts();

3f2bc3d0   Administrator   first commit
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
          } 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   -
160
161
162
                  $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
163
164
              }

          }

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

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

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

              }

          }

3f2bc3d0   Administrator   first commit
170
171
172
173
174
175
176
177
178
179
180
181
182
          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...
183
184
185
  

  

  

3f2bc3d0   Administrator   first commit
186
187
188
189
190
191
192
193
194
195
          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']]);

              }

          }

      }

  }