Blame view

frontend/controllers/CatalogController.php 6.42 KB
4253cbec   root   first commit
1
  <?php
e337d04a   Administrator   liniya first commit
2
3
4
  
  namespace frontend\controllers;
  
a95ce595   Administrator   big commti
5
  
e337d04a   Administrator   liniya first commit
6
  use common\modules\product\helpers\ProductHelper;
e337d04a   Administrator   liniya first commit
7
8
9
10
11
  use frontend\models\ProductFrontendSearch;
  use Yii;
  use common\modules\product\models\Brand;
  use common\modules\product\models\BrandSearch;
  use common\modules\product\models\Category;
e337d04a   Administrator   liniya first commit
12
  use common\modules\product\models\Product;
e337d04a   Administrator   liniya first commit
13
  use common\modules\rubrication\models\TaxGroup;
e337d04a   Administrator   liniya first commit
14
  use yii\data\ActiveDataProvider;
e337d04a   Administrator   liniya first commit
15
  use yii\helpers\ArrayHelper;
e337d04a   Administrator   liniya first commit
16
17
18
  
  class CatalogController extends \yii\web\Controller
  {
a95ce595   Administrator   big commti
19
20
  
  
e337d04a   Administrator   liniya first commit
21
22
23
24
25
      public function actionSearch() {
          // @todo
      }
  
      public function actionCategory()
4253cbec   root   first commit
26
      {
e337d04a   Administrator   liniya first commit
27
  
a95ce595   Administrator   big commti
28
  
e337d04a   Administrator   liniya first commit
29
30
31
32
33
34
35
          /** @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');
4253cbec   root   first commit
36
          }
e337d04a   Administrator   liniya first commit
37
38
39
40
41
42
43
44
45
  
          ProductHelper::addLastCategory($category->category_id);
  
          $params = [ ];
  
          $optionsList = ArrayHelper::getColumn(TaxGroup::find()
              ->where([ 'is_filter' => 'TRUE' ])
              ->all(), 'alias');
  
a95ce595   Administrator   big commti
46
  
e337d04a   Administrator   liniya first commit
47
48
49
50
51
  
          if(!empty( $filter[ 'special' ] )) {
              unset( $filter_check[ 'special' ] );
              if(!is_array($filter[ 'special' ])) {
                  $filter[ 'special' ] = [ $filter[ 'special' ] ];
4253cbec   root   first commit
52
              }
e337d04a   Administrator   liniya first commit
53
54
              if(in_array('new', $filter[ 'special' ])) {
                  $params[ 'special' ][ 'is_new' ] = true;
4253cbec   root   first commit
55
              }
e337d04a   Administrator   liniya first commit
56
57
              if(in_array('top', $filter[ 'special' ])) {
                  $params[ 'special' ][ 'is_top' ] = true;
4253cbec   root   first commit
58
              }
e337d04a   Administrator   liniya first commit
59
60
              if(in_array('promo', $filter[ 'special' ])) {
                  $params[ 'special' ][ 'akciya' ] = true;
4253cbec   root   first commit
61
              }
4253cbec   root   first commit
62
          }
e337d04a   Administrator   liniya first commit
63
64
65
66
67
68
  
          if(!empty( $filter[ 'prices' ] )) {
              unset( $filter_check[ 'prices' ] );
              $params[ 'prices' ] = $filter[ 'prices' ];
          }
  
a95ce595   Administrator   big commti
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
          $activeFiltersParams = $filter_check;
  
          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;
              }
          }
  
e337d04a   Administrator   liniya first commit
87
88
89
90
91
          foreach($optionsList as $optionList) {
  
              if(isset( $filter[ $optionList ] )) {
                  unset( $filter_check[ $optionList ] );
                  $params[ $optionList ] = $filter[ $optionList ];
4253cbec   root   first commit
92
              }
e337d04a   Administrator   liniya first commit
93
  
4253cbec   root   first commit
94
          }
e337d04a   Administrator   liniya first commit
95
96
97
98
99
100
101
102
  
          if(!empty( $filter_check )) {
              $filter = array_diff_key($filter, $filter_check);
              Yii::$app->response->redirect([
                  'catalog/category',
                  'category' => $category,
                  'filters'  => $filter,
              ], 301);
4253cbec   root   first commit
103
          }
e337d04a   Administrator   liniya first commit
104
105
  
          $productModel = new ProductFrontendSearch();
a95ce595   Administrator   big commti
106
  
e337d04a   Administrator   liniya first commit
107
108
          $productProvider = $productModel->search($category, $params);
  
a95ce595   Administrator   big commti
109
  
e337d04a   Administrator   liniya first commit
110
111
112
113
          $brandModel = new BrandSearch();
          $brands = $brandModel->getBrands($category, $params)
              ->all();
  
a95ce595   Administrator   big commti
114
  
0bfe5701   Administrator   big commti
115
          $groups = $category->getActiveFilters();
a95ce595   Administrator   big commti
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
144
145
146
147
  
          foreach($brands as $brand){
              array_unshift($groups , [
                  'group_alias' => 'brands',
                  'option_alias' => $brand->alias,
                  'tax_option_id' => $brand->brand_id,
                  'value' => $brand->name,
                  'alias' => 'brands',
                  'name' => 'Бренды'
              ]);
          }
  
  
  
          foreach ($groups as $key=> $group) {
              $param = $activeFiltersParams;
              if(isset($param[$group['alias']])){
                  if(!in_array($group['option_alias'],$param[$group['alias']])){
                      $param[$group['alias']][] = $group['option_alias'];
                  } else {
                      continue;
                  }
              }else {
                  $param = array_merge($param, [$group['alias']=>[$group['option_alias']]]);
              }
  
              $groups[$key] = array_merge($groups[$key],['cacheKey' => json_encode($param)] );
  
          }
  
  
  
e337d04a   Administrator   liniya first commit
148
          $groups = ArrayHelper::index($groups, null, 'name');
a95ce595   Administrator   big commti
149
150
151
152
  
  
  
  
82b54963   Administrator   big commti
153
          $cacheKey = ['ActiveProperties','id' => $category];
a95ce595   Administrator   big commti
154
155
156
  
          if(!$priceLimits = Yii::$app->cache->get($cacheKey)){
  
82b54963   Administrator   big commti
157
              $priceLimits = $productModel->priceLimits($category);
a95ce595   Administrator   big commti
158
159
160
  
              Yii::$app->cache->set($cacheKey,$priceLimits,3600*24);
          }
e337d04a   Administrator   liniya first commit
161
  
e337d04a   Administrator   liniya first commit
162
163
164
  
          return $this->render('products', [
              'category'        => $category,
e337d04a   Administrator   liniya first commit
165
166
167
168
              'filter'          => $filter,
              'params'          => $params,
              'productModel'    => $productModel,
              'productProvider' => $productProvider,
a95ce595   Administrator   big commti
169
              'groups'          => $groups,
e688b471   Administrator   big commti
170
              'priceLimits'     => $priceLimits,
e337d04a   Administrator   liniya first commit
171
172
173
174
175
176
177
178
179
          ]);
  
      }
  
      public function actionProduct()
      {
          /** @var Product $product */
          $product = Yii::$app->request->get('product');
  
e337d04a   Administrator   liniya first commit
180
181
182
  
          ProductHelper::addLastProsucts($product->product_id);
  
9dcd66fc   Administrator   big commti
183
184
185
186
187
188
189
190
191
          $category = null;
          $last_category_id = ProductHelper::getLastCategory();
          if(!empty($last_category_id)) {
              $category = $product->getCategory()->andWhere(['category_id' => $last_category_id])->one();
          }
          if(empty($category)) {
              $category = $product->category;
          }
  
e337d04a   Administrator   liniya first commit
192
193
          return $this->render('product', [
              'product' => $product,
9dcd66fc   Administrator   big commti
194
              'category' => $category,
e337d04a   Administrator   liniya first commit
195
196
197
          ]);
      }
  
2cbb3bdc   Виталий   test commit
198
199
200
201
202
203
204
205
206
207
208
209
210
      public function actionBrands()
      {
          $dataProvider = new ActiveDataProvider([
              'query' => Brand::find()->orderBy('name'),
              'pagination' => [
                  'pageSize' => -1,
              ]
          ]);
  
          return $this->render('brands', [
              'dataProvider' => $dataProvider,
          ]);
      }
e337d04a   Administrator   liniya first commit
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
  
      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,
          ]);
4253cbec   root   first commit
231
      }
e337d04a   Administrator   liniya first commit
232
233
  
  }