Blame view

frontend/controllers/CatalogController.php 8.7 KB
550eac02   Administrator   second
1
2
3
4
  <?php
  
  namespace frontend\controllers;
  
055ecc3b   Karnovsky A   Karnovsky 11052016
5
6
7
8
  use common\modules\product\Filter;
  use common\modules\product\helpers\ProductHelper;
  use common\modules\rubrication\models\TaxOptionSearch;
  use frontend\models\ProductFrontendSearch;
550eac02   Administrator   second
9
  use Yii;
055ecc3b   Karnovsky A   Karnovsky 11052016
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  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;
550eac02   Administrator   second
26
27
  use yii\web\HttpException;
  
055ecc3b   Karnovsky A   Karnovsky 11052016
28
  class CatalogController extends \yii\web\Controller
550eac02   Administrator   second
29
  {
055ecc3b   Karnovsky A   Karnovsky 11052016
30
31
32
      public function actionSearch() {
          // @todo
      }
550eac02   Administrator   second
33
  
055ecc3b   Karnovsky A   Karnovsky 11052016
34
      public function actionCategory()
550eac02   Administrator   second
35
      {
055ecc3b   Karnovsky A   Karnovsky 11052016
36
37
38
39
40
41
42
43
44
          /** @var Category $category */
          $category = Yii::$app->request->get('category');
          $filter = Yii::$app->request->get('filter', []);
          $word = trim(Yii::$app->request->get('word', ''));
  
          if (empty($category->category_id) && empty($word)) {
              throw new HttpException(404 ,'Page not found');
          }
  
055ecc3b   Karnovsky A   Karnovsky 11052016
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
          if (!empty($word)) {
              $params = [];
  
              $params['keywords'] = explode(' ', preg_replace("|[\s,.!:&?~();-]|i", " ", $word));
              foreach($params['keywords'] as $i => &$keyword) {
                  $keyword = trim($keyword);
                  if (empty($keyword)) {
                      unset($params['keywords'][$i]);
                  }
              }
  
              $productModel = new ProductFrontendSearch();
              $productProvider = $productModel->search($category, $params);
  
              $categoriesQuery = Category::find()
                  ->innerJoin(ProductCategory::tableName(), ProductCategory::tableName() .'.category_id = '. Category::tableName() .'.category_id')
                  ->innerJoin(Product::tableName(), Product::tableName() .'.product_id = '. ProductCategory::tableName() .'.product_id');
              foreach ($params['keywords'] as $keyword) {
                  $categoriesQuery->andWhere(['ilike', 'product.name', $keyword]);
              }
              $categories = $categoriesQuery->all();
  
              return $this->render(
                  'search',
                  [
                      'keywords'          => $params['keywords'],
                      'category'          => $category,
                      'productModel'      => $productModel,
                      'productProvider'   => $productProvider,
055ecc3b   Karnovsky A   Karnovsky 11052016
74
75
76
77
                      'categories'        => $categories,
                  ]
              );
  
055ecc3b   Karnovsky A   Karnovsky 11052016
78
79
80
81
82
83
84
85
86
87
88
          } else {
              $params = [];
  
              if ( !empty($filter['brands']) ) {
                  $brands = Brand::find()->select('brand_id')->where(['in', 'alias', $filter['brands']])->all();
                  $params['brands'] = [];
                  foreach ($brands as $brand) {
                      $params['brands'][] = $brand->brand_id;
                  }
              }
  
e2651dcd   Karnovsky A   Special-product-f...
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
              if ( !empty($filter['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;
                  }
              }
  
055ecc3b   Karnovsky A   Karnovsky 11052016
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
              if ( !empty($filter['options']) ) {
                  $params['options'] = $filter['options'];
                  /*$optionQuery = TaxOption::find();
                  $optionQuery->select('tax_option_id');
                  $optionQuery->innerJoinWith('group');
                  foreach ($filter['options'] as $option_key => $option_values) {
                      $optionQuery->orWhere([
                          'tax_group_id' => $option_key,
                          'alias' => $filter['options']
                          ]);
                  }
                  $options = ->where(['in', 'alias', $filter['options']])->all();
                  $params['options'] = [];
                  foreach ($options as $option) {
                      $params['options'][] = $option->tax_option_id;
                  }*/
              }
  
              if ( !empty($filter['prices']) ) {
                  $params['prices'] = $filter['prices'];
              }
  
              $productModel = new ProductFrontendSearch();
              $productProvider = $productModel->search($category, $params);
  
              $brandModel = new BrandSearch();
              $brandProvider = $brandModel->getBrands($category, $params);
  
              $optionsProvider = $productModel->optionsForCategory($category, $params);
              $groups = [];
              foreach ($optionsProvider->models as $option) {
                  if (!isset($groups[$option->tax_group_id])) {
                      $groups[$option->tax_group_id] = $option->taxGroup;
                      $groups[$option->tax_group_id]->_options = [];
                  }
                  $groups[$option->tax_group_id]->_options[] = $option;
              }
              foreach($groups as $i => $group) {
                  if (empty($group->_options))
                      unset($groups[$i]);
              }
  
              $priceLimits = $productModel->priceLimits($category, $params);
  
              return $this->render(
                  'products',
                  [
                      'category'          => $category,
                      'brandModel'        => $brandModel,
                      'brandProvider'     => $brandProvider,
                      'filter'            => $filter,
                      'productModel'      => $productModel,
                      'productProvider'   => $productProvider,
                      'optionsProvider'   => $optionsProvider,
                      'groups'            => $groups,
                      'priceLimits'       => $priceLimits,
055ecc3b   Karnovsky A   Karnovsky 11052016
160
161
162
163
164
165
166
                  ]
              );
          }
      }
  
      public function actionProduct()
      {
ccc7a9d3   Karnovsky A   Karnovsky 12052016
167
          /** @var Product $product */
055ecc3b   Karnovsky A   Karnovsky 11052016
168
169
170
171
172
173
174
175
176
177
178
179
180
          $product = Yii::$app->request->get('product');
  
          $groups = [];
          foreach($product->category->getTaxGroups()->all() as $_group) {
              $groups[$_group->tax_group_id] = $_group;
          }
          foreach ($product->options as $option) {
              $groups[$option->tax_group_id]->_options[] = $option;
          }
          foreach($groups as $i => $group) {
              if (empty($group->_options))
                  unset($groups[$i]);
          }
ccc7a9d3   Karnovsky A   Karnovsky 12052016
181
          $category = $product->category;
055ecc3b   Karnovsky A   Karnovsky 11052016
182
  
055ecc3b   Karnovsky A   Karnovsky 11052016
183
184
185
186
          ProductHelper::addLastProsucts($product->product_id);
  
          return $this->render('product', [
              'product' => $product,
ccc7a9d3   Karnovsky A   Karnovsky 12052016
187
              'category' => $category,
055ecc3b   Karnovsky A   Karnovsky 11052016
188
              'properties' => $groups,
055ecc3b   Karnovsky A   Karnovsky 11052016
189
          ]);
550eac02   Administrator   second
190
      }
055ecc3b   Karnovsky A   Karnovsky 11052016
191
192
  
      public function actionBrands()
550eac02   Administrator   second
193
      {
38828295   Karnovsky A   -
194
195
196
197
198
199
200
201
202
203
          $dataProvider = new ActiveDataProvider([
              'query' => Brand::find()->joinWith('brandName')->orderBy('brand_name.value'),
              'pagination' => [
                  'pageSize' => -1,
              ]
          ]);
  
          return $this->render('brands', [
              'dataProvider' => $dataProvider,
          ]);
055ecc3b   Karnovsky A   Karnovsky 11052016
204
205
      }
  
868680ca   Karnovsky A   -
206
      public function actionBrand($brand)
055ecc3b   Karnovsky A   Karnovsky 11052016
207
      {
868680ca   Karnovsky A   -
208
209
210
211
212
213
214
215
216
217
          $filter = Yii::$app->request->get('filter', []);
  
          $params = [
              'brand_id' => $brand->brand_id,
          ];
  
          if ( !empty($filter['prices']) ) {
              $params['prices'] = $filter['prices'];
          }
  
c9d723d7   Karnovsky A   -
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
          if ( !empty($filter['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;
              }
          }
  
868680ca   Karnovsky A   -
233
234
235
236
237
238
239
240
241
242
243
244
          $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,
              'filter'            => $filter,
          ]);
055ecc3b   Karnovsky A   Karnovsky 11052016
245
246
247
      }
  
  }