Blame view

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

  

  namespace frontend\models;

  

a0be9a4d   Karnovsky A   30062016
5
  use common\modules\product\helpers\ProductHelper;

3f2bc3d0   Administrator   first commit
6
  use common\modules\product\models\Brand;

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

  use common\modules\product\models\CategoryName;

3f2bc3d0   Administrator   first commit
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
  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 = []) {

aa5c63f4   Yarik   Filter fixing
51
          

3f2bc3d0   Administrator   first commit
52
          $dataProvider = new ActiveDataProvider([

09af7db9   Karnovsky A   Fix filter
53
              'query' => $this->getSearchQuery($category, $params),

3f2bc3d0   Administrator   first commit
54
              'pagination' => [

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

3f2bc3d0   Administrator   first commit
56
57
58
59
60
61
62
63
64
65
66
67
68
              ],

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

49c47c76   Karnovsky A   -
69
                          'label' => 'по цене',

3f2bc3d0   Administrator   first commit
70
71
72
73
74
75
76
77
78
                      ],

                  ],

              ]

          ]);

  

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

              return $dataProvider;

          }

  

09af7db9   Karnovsky A   Fix filter
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  

  

          return $dataProvider;

      }

  

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

          if (!empty($category)) {

              /** @var ActiveQuery $query */

  //            $query = $category->getRelations('product_categories');

              $query = $category->getProducts();

          } else {

              $query = Product::find();

          }

          $query->select(['product.*']);

1b898c16   Administrator   20.07.16
93
          $query->joinWith(['enabledVariants','brand', 'brand.brandName', 'category', 'category.categoryName']);

09af7db9   Karnovsky A   Fix filter
94
95
96
  

          $query->groupBy(['product.product_id', 'product_variant.price']);

  

a0be9a4d   Karnovsky A   30062016
97
          ProductHelper::_setQueryParams($query, $params);

3f2bc3d0   Administrator   first commit
98
  

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

  

09af7db9   Karnovsky A   Fix filter
101
          return $query;

3f2bc3d0   Administrator   first commit
102
103
104
      }

  

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

3f2bc3d0   Administrator   first commit
105
106
          $query = TaxOption::find()

              ->select([

9d539ac3   Karnovsky A   Brands and option...
107
                  TaxOption::tableName() .'.*',

3f2bc3d0   Administrator   first commit
108
              ])

09af7db9   Karnovsky A   Fix filter
109
              ->leftJoin(ProductOption::tableName(), ProductOption::tableName() .'.option_id='. TaxOption::tableName() .'.tax_option_id')

1b898c16   Administrator   20.07.16
110
              ->joinWith('taxGroup')

9d539ac3   Karnovsky A   Brands and option...
111
112
              ->where([TaxGroup::tableName() .'.is_filter' => true]);

  

09af7db9   Karnovsky A   Fix filter
113
          $query->innerJoin('product_variant', 'product_variant.product_id = '. ProductOption::tableName() .'.product_id');

9d539ac3   Karnovsky A   Brands and option...
114
115
116
117
          $query->andWhere(['!=', 'product_variant.stock', 0]);

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

  //        $query->having(['>', 'COUNT(product_variant.product_variant_id)', 0]);

  

3f2bc3d0   Administrator   first commit
118
          if (!empty($category)) {

09af7db9   Karnovsky A   Fix filter
119
              $query->innerJoin(ProductCategory::tableName(), ProductCategory::tableName() .'.product_id='. ProductOption::tableName() .'.product_id');

3f2bc3d0   Administrator   first commit
120
121
              $query->andWhere([ProductCategory::tableName() .'.category_id' => $category->category_id]);

          }

9d539ac3   Karnovsky A   Brands and option...
122
  

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

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

09af7db9   Karnovsky A   Fix filter
125
  

1b898c16   Administrator   20.07.16
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
  //        $queryCount = ProductOption::find()

  //            ->select(['COUNT('. ProductOption::tableName() .'.product_id)'])

  //            ->where(ProductOption::tableName() .'.option_id = '. TaxOption::tableName() .'.tax_option_id');

  //        $queryCount->andWhere('(SELECT COUNT(pv.product_variant_id) FROM "product_variant" "pv" WHERE pv.stock != 0 AND pv.product_id = '. ProductOption::tableName() .'.product_id) > 0');

  //        if (!empty($category)) {

  //            $queryCount->andWhere('(SELECT COUNT(pc.product_id) FROM product_category pc WHERE pc.product_id = '. ProductOption::tableName() .'.product_id AND pc.category_id = '. $category->category_id .') > 0');

  //        }

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

  //            $queryCount->innerJoin('tax_option', 'tax_option.tax_option_id = product_option.option_id');

  //            $queryCount->innerJoin('tax_group', 'tax_group.tax_group_id = tax_option.tax_group_id');

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

  //                $queryCount->andWhere([

  //                    'tax_group.alias' => $group,

  //                    'tax_option.alias' => $options

  //                ]);

  //            }

  //        }

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

  //            $queryCount->innerJoin(Product::tableName(), 'product.product_id='. ProductOption::tableName() .'.product_id');

  //            $queryCount->andWhere(['product.brand_id' => $params['brands']]);

  //        }

  //        if (!empty($params['prices'])) {

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

  //                $queryCount->andWhere(['>=', 'pv.price', $params['prices']['min']]);

  //            }

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

  //                $queryCount->andWhere(['<=', 'pv.price', $params['prices']['max']]);

  //            }

  //        }

103e8126   Karnovsky A   Remove ITEMS_COUN...
155
  //        $query->addSelect(['_items_count' => $queryCount]);

3f2bc3d0   Administrator   first commit
156
  

8724ec1f   Karnovsky A   -
157
          return $query;

3f2bc3d0   Administrator   first commit
158
159
160
161
162
      }

  

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

          if (!empty($category)) {

              /** @var ActiveQuery $query */

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

              $query = $category->getProducts();

3f2bc3d0   Administrator   first commit
165
166
167
168
169
          } else {

              $query = Product::find();

          }

          $query->joinWith('variant');

  

d4859b79   Yarik   Price slider fix
170
171
172
          // Price filter fix

          unset($params['prices']);

          

a0be9a4d   Karnovsky A   30062016
173
          ProductHelper::_setQueryParams($query, $params, false);

3f2bc3d0   Administrator   first commit
174
175
176
177
178
179
180
181
182
183
184
  

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

          ];

      }

3f2bc3d0   Administrator   first commit
185
  }