Blame view

frontend/controllers/CatalogController.php 7.79 KB
b89bd6a3   Alexey Boroda   26.09.16 filters ...
1
2
3
4
5
6
7
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
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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
  <?php
  
  namespace frontend\controllers;
  
  use common\modules\product\Filter;
  use common\modules\product\helpers\ProductHelper;
  use common\modules\rubrication\models\TaxOptionSearch;
  use frontend\models\ProductFrontendSearch;
  use Yii;
  use common\modules\product\models\Brand;
  use common\modules\product\models\BrandSearch;
  use common\modules\product\models\Category;
  use common\modules\product\models\CategorySearch;
  use common\modules\product\models\Product;
  use common\modules\product\models\ProductCategory;
  use common\modules\product\models\ProductOption;
  use common\modules\product\models\ProductSearch;
  use common\modules\product\models\ProductVariant;
  use common\modules\rubrication\models\TaxGroup;
  use common\modules\rubrication\models\TaxOption;
  use common\modules\rubrication\models\TaxValueString;
  use yii\data\ActiveDataProvider;
  use yii\data\Pagination;
  use yii\data\Sort;
  use yii\db\ActiveQuery;
  use yii\helpers\ArrayHelper;
  use yii\web\HttpException;
  use yii\helpers\VarDumper;
  
  class CatalogController extends \yii\web\Controller
  {
      public function actionSearch() {
          // @todo
      }
  
      public function actionCategory()
      {
  
          /** @var Category $category */
          $category = Yii::$app->request->get('category');
          $filter = Yii::$app->request->get('filters', [ ]);
          $filter_check = $filter;
  
          if(empty( $category->category_id ) && empty( $word )) {
              return $this->render('catalog');
          }
  
          ProductHelper::addLastCategory($category->category_id);
  
          $params = [ ];
  
          $optionsList = ArrayHelper::getColumn(TaxGroup::find()
              ->where([ 'is_filter' => 'TRUE' ])
              ->all(), 'alias');
  
          if(!empty( $filter[ 'brands' ] )) {
              unset( $filter_check[ 'brands' ] );
              $brands = Brand::find()
                  ->select('brand_id')
                  ->where([
                      'in',
                      'alias',
                      $filter[ 'brands' ],
                  ])
                  ->all();
              $params[ 'brands' ] = [ ];
              foreach($brands as $brand) {
                  $params[ 'brands' ][] = $brand->brand_id;
              }
          }
  
          if(!empty( $filter[ 'special' ] )) {
              unset( $filter_check[ 'special' ] );
              if(!is_array($filter[ 'special' ])) {
                  $filter[ 'special' ] = [ $filter[ 'special' ] ];
              }
              if(in_array('new', $filter[ 'special' ])) {
                  $params[ 'special' ][ 'is_new' ] = true;
              }
              if(in_array('top', $filter[ 'special' ])) {
                  $params[ 'special' ][ 'is_top' ] = true;
              }
              if(in_array('promo', $filter[ 'special' ])) {
                  $params[ 'special' ][ 'akciya' ] = true;
              }
          }
  
          if(!empty( $filter[ 'prices' ] )) {
              unset( $filter_check[ 'prices' ] );
              $params[ 'prices' ] = $filter[ 'prices' ];
          }
  
          foreach($optionsList as $optionList) {
  
              if(isset( $filter[ $optionList ] )) {
                  unset( $filter_check[ $optionList ] );
                  $params[ $optionList ] = $filter[ $optionList ];
              }
  
          }
  
          if(!empty( $filter_check )) {
              $filter = array_diff_key($filter, $filter_check);
              Yii::$app->response->redirect([
                  'catalog/category',
                  'category' => $category,
                  'filters'  => $filter,
              ], 301);
          }
  
          $productModel = new ProductFrontendSearch();
          //$productQuery = $productModel->getSearchQuery($category, $params);
          $productProvider = $productModel->search($category, $params);
  
          $brandModel = new BrandSearch();
          $brands = $brandModel->getBrands($category, $params)
              ->all();
  
          $groups = $category->getActiveFilters()->all();
  
          $groups = ArrayHelper::index($groups, null, 'name');
  
          $priceLimits = $productModel->priceLimits($category, $params);
  
          /*
           * Greedy search for comments and rating
           */
          $query = $productProvider->query;
          $query->with([
              'variant',
              'comments',
              'averageRating',
          ]);
          /*
           * End of greedy search for rating and comments
           */
          $dataProvider = new ActiveDataProvider([
              'query' => $query,
              'pagination' => [
                  'pageSize' => 15,
              ],
          ]);
  
b89bd6a3   Alexey Boroda   26.09.16 filters ...
144
145
146
147
148
149
150
151
152
153
          $products = Product::find()->joinWith('categories')
              ->where([
                  'category.category_id' => $category->getChildrenByDepth(2)->column(),
              ]);
          $productsProvider = new ActiveDataProvider([
              'query' => $products,
              'pagination' => [
                  'pageSize' => 15,
              ],
          ]);
6fb4a732   Alexey Boroda   Changes:
154
155
156
          $stockProgram = Category::find()
              ->where([
                  'stock_program' => true,
b224f0ae   Alexey Boroda   Changes:
157
                  'parent_id' => $category->category_id,
6fb4a732   Alexey Boroda   Changes:
158
159
160
161
              ])->all();
          $onOrder = Category::find()
              ->where([
                  'on_order' => true,
b224f0ae   Alexey Boroda   Changes:
162
                  'parent_id' => $category->category_id,
6fb4a732   Alexey Boroda   Changes:
163
              ])->all();
b89bd6a3   Alexey Boroda   26.09.16 filters ...
164
165
166
167
168
169
170
171
172
173
174
          return $this->render('products', [
              'category'        => $category,
              'brandModel'      => $brandModel,
              'dataProvider' => $dataProvider,
              'brands'          => $brands,
              'filter'          => $filter,
              'params'          => $params,
              'productModel'    => $productModel,
              'productsProvider' => $dataProvider,
              'groups'            => $groups,
              'priceLimits'     => $priceLimits,
6fb4a732   Alexey Boroda   Changes:
175
176
              'stockProgram' => $stockProgram,
              'onOrder' => $onOrder,
b89bd6a3   Alexey Boroda   26.09.16 filters ...
177
178
179
180
181
182
183
184
185
186
187
          ]);
  
      }
  
      /**
       * @param string $product
       * @param string $variant
       * @return string
       */
      public function actionProduct($product, $variant)
      {
ea8fd6e6   Alexey Boroda   -Product attributes
188
          
b89bd6a3   Alexey Boroda   26.09.16 filters ...
189
190
191
192
          $product = Product::find()
              ->joinWith([
                  'variants' => function($query) {
                      $query->indexBy('sku');
ea8fd6e6   Alexey Boroda   -Product attributes
193
                  },
b89bd6a3   Alexey Boroda   26.09.16 filters ...
194
195
196
197
              ], true, 'INNER JOIN')
              ->where([
                  'product.alias' => $product,
                  'product_variant.sku' => $variant,
987739f1   Alexey Boroda   -Simple spoiler
198
199
200
201
              ])->with([
                  'category.parent',
                  'options.group',
              ])->one();
ea8fd6e6   Alexey Boroda   -Product attributes
202
203
          
          $attributes = [];
987739f1   Alexey Boroda   -Simple spoiler
204
205
206
207
208
209
210
          if(!empty($product->options)) {
              foreach($product->options as $option) {
                  if(!$option->group->is_menu) {
                      continue;
                  }
                  $attributes[ $option->group->name ][] = $option->value;
              }
ea8fd6e6   Alexey Boroda   -Product attributes
211
          }
987739f1   Alexey Boroda   -Simple spoiler
212
          
b89bd6a3   Alexey Boroda   26.09.16 filters ...
213
214
215
216
217
218
          $variant = $product->variants[$variant];
          $variants = $product->variants;
          return $this->render('view', [
              'variants' => $variants,
              'product' => $product,
              'variant' => $variant,
ea8fd6e6   Alexey Boroda   -Product attributes
219
              'attributes' => $attributes,
b89bd6a3   Alexey Boroda   26.09.16 filters ...
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
          ]);
      }
  
      public function actionBrands()
      {
          $dataProvider = new ActiveDataProvider([
              'query' => Brand::find()->orderBy('name'),
              'pagination' => [
                  'pageSize' => -1,
              ]
          ]);
  
          return $this->render('brands', [
              'dataProvider' => $dataProvider,
          ]);
      }
  
      public function actionBrand($brand)
      {
          $brand = BrandSearch::findByAlias($brand);
  
          $params = [
              'brands' => $brand->brand_id,
          ];
  
          $productModel = new ProductFrontendSearch();
          $productProvider = $productModel->search(null, $params);
  
          $priceLimits = $productModel->priceLimits(null, $params);
  
          return $this->render('brand', [
              'productModel'      => $productModel,
              'productProvider'   => $productProvider,
              'brand'             => $brand,
              'priceLimits'       => $priceLimits,
          ]);
      }
  
  }