Blame view

frontend/controllers/CatalogController.php 8.85 KB
85261b14   Karnovsky A   not fixed commite
1
2
3
4
  <?php
  
  namespace frontend\controllers;
  
b519af22   Karnovsky A   Base-product func...
5
  use common\modules\product\models\Brand;
85261b14   Karnovsky A   not fixed commite
6
7
8
  use common\modules\product\models\Category;
  use common\modules\product\models\CategorySearch;
  use common\modules\product\models\Product;
b519af22   Karnovsky A   Base-product func...
9
  use common\modules\product\models\ProductCategory;
5aa7418e   Karnovsky A   Base-product#3 fu...
10
  use common\modules\product\models\ProductOption;
b519af22   Karnovsky A   Base-product func...
11
12
  use common\modules\product\models\ProductSearch;
  use common\modules\product\models\ProductVariant;
5aa7418e   Karnovsky A   Base-product#3 fu...
13
14
  use common\modules\rubrication\models\TaxGroup;
  use common\modules\rubrication\models\TaxOption;
14eadb86   Karnovsky A   Eager loading for...
15
  use common\modules\rubrication\models\TaxValueString;
b519af22   Karnovsky A   Base-product func...
16
17
18
19
  use yii\data\ActiveDataProvider;
  use yii\data\Pagination;
  use yii\data\Sort;
  use yii\db\ActiveQuery;
85261b14   Karnovsky A   not fixed commite
20
21
22
23
  use yii\web\HttpException;
  
  class CatalogController extends \yii\web\Controller
  {
020c69f5   Karnovsky A   Base-product#5 fu...
24
25
26
27
      public function actionSearch() {
  
      }
  
85261b14   Karnovsky A   not fixed commite
28
29
30
      public function actionCategory($alias)
      {
          $category = CategorySearch::findByAlias($alias);
b519af22   Karnovsky A   Base-product func...
31
32
33
          if (empty($category->category_id)) {
              throw new HttpException(404 ,'Page not found');
          }
85261b14   Karnovsky A   not fixed commite
34
35
36
37
38
39
40
41
          if ($category->depth < 2) {
              return $this->render(
                  'categories',
                  [
                      'category' => $category
                  ]
              );
          } else {
b519af22   Karnovsky A   Base-product func...
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
              $per_page = 24;
  
              $sort = new 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' => 'цене',
                      ],
                  ],
              ]);
              /** @var ActiveQuery $query */
              $query = $category->getRelations('product_categories')
                  ->joinWith([
                      'variants'
                  ]);
b15c889e   Karnovsky A   Base-product#2 fu...
65
              $all_count = $query->count();
b519af22   Karnovsky A   Base-product func...
66
  
5aa7418e   Karnovsky A   Base-product#3 fu...
67
              $brandsQuery = Brand::find()
14eadb86   Karnovsky A   Eager loading for...
68
                  ->innerJoinWith('brandName')
5aa7418e   Karnovsky A   Base-product#3 fu...
69
70
71
72
73
74
75
                  ->innerJoinWith('products')
                  ->innerJoin(ProductCategory::tableName(), ProductCategory::tableName() .'.product_id='. Product::tableName() .'.product_id')
                  ->where([
                      ProductCategory::tableName() .'.category_id' => $category->category_id
                  ])
                  ->groupBy(Brand::tableName() .'.brand_id');
              $brands = $brandsQuery->all();
14eadb86   Karnovsky A   Eager loading for...
76
              $brands_count = count($brands); // $brandsQuery->count();
5aa7418e   Karnovsky A   Base-product#3 fu...
77
78
  
              $optionsQuery = TaxOption::find()
a1860416   Karnovsky A   Items count for o...
79
80
81
82
                  ->select([
                      TaxOption::tableName() .'.*',
                      'COUNT('. ProductOption::tableName() .'.product_id) AS _items_count'
                  ])
5aa7418e   Karnovsky A   Base-product#3 fu...
83
84
                  ->innerJoin(ProductOption::tableName(), ProductOption::tableName() .'.option_id='. TaxOption::tableName() .'.tax_option_id')
                  ->innerJoin(ProductCategory::tableName(), ProductCategory::tableName() .'.product_id='. ProductOption::tableName() .'.product_id')
14eadb86   Karnovsky A   Eager loading for...
85
                  ->innerJoinWith('group')
5aa7418e   Karnovsky A   Base-product#3 fu...
86
87
88
89
90
91
92
93
94
                  ->where([
                      ProductCategory::tableName() .'.category_id' => $category->category_id
                  ])
                  ->groupBy(TaxOption::tableName() .'.tax_option_id');
              $all_options = [];
              foreach($optionsQuery->all() as $_option) {
                  $all_options[] = $_option;
              }
  
b15c889e   Karnovsky A   Base-product#2 fu...
95
96
97
              $priceQuery = clone $query;
              $priceMin = $priceMinCurr = $priceQuery->min(ProductVariant::tableName() .'.price');
              $priceMax = $priceMaxCurr = $priceQuery->max(ProductVariant::tableName() .'.price');
b519af22   Karnovsky A   Base-product func...
98
  
5aa7418e   Karnovsky A   Base-product#3 fu...
99
              // Prices
b15c889e   Karnovsky A   Base-product#2 fu...
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
              if (($price_interval = \Yii::$app->request->get('price_interval')) != false) {
                  $price_interval = explode(';', $price_interval);
                  $price_interval = [
                      floatval($price_interval[0]),
                      floatval($price_interval[1]),
                  ];
                  if ($price_interval[0] > 0) {
                      $query->andWhere(['>=', ProductVariant::tableName() .'.price', $price_interval[0]]);
                      $priceMinCurr = $price_interval[0];
                  }
                  if ($price_interval[1] > 0) {
                      $query->andWhere(['<=', ProductVariant::tableName() .'.price', $price_interval[1]]);
                      $priceMaxCurr = $price_interval[1];
                  }
              }
5aa7418e   Karnovsky A   Base-product#3 fu...
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
  
              $groups = [];
              foreach($category->getTaxGroups()->all() as $_group) {
                  $groups[$_group->tax_group_id] = $_group;
              }
              foreach ($all_options as $option) {
                  $groups[$option->tax_group_id]->_options[] = $option;
              }
              foreach($groups as $i => $group) {
                  if (empty($group->_options))
                      unset($groups[$i]);
              }
  
              // Options
              if (($options = \Yii::$app->request->get('option')) != false) {
2c4d173e   Karnovsky A   Base-product#6 fu...
130
131
132
133
134
135
136
137
138
  //                $query->innerJoin(ProductOption::tableName(), ProductOption::tableName() .'.product_id='. Product::tableName() .'.product_id');
  //                $query->innerJoin(TaxOption::tableName(), TaxOption::tableName() .'.tax_option_id='. ProductOption::tableName() .'.option_id');
                  foreach($options as $group_alias => $options_alias) {
                      if (!is_array($options_alias)) {
                          $options_alias = [$options_alias];
                      }
                      foreach($options_alias as &$option_alias) {
                          $option_alias = "'". $option_alias ."'";
                      }
14eadb86   Karnovsky A   Eager loading for...
139
                      /*$group = TaxGroup::find()->where(['like', 'alias', $group_alias])->one();
5aa7418e   Karnovsky A   Base-product#3 fu...
140
141
                      if (!$group) {
                          continue;
14eadb86   Karnovsky A   Eager loading for...
142
                      }*/
5aa7418e   Karnovsky A   Base-product#3 fu...
143
                  }
14eadb86   Karnovsky A   Eager loading for...
144
                  $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 WHERE tax_option.alias IN ('. implode(',', $options_alias) .'))');
5aa7418e   Karnovsky A   Base-product#3 fu...
145
              }
020c69f5   Karnovsky A   Base-product#5 fu...
146
147
148
149
150
151
152
153
154
155
156
  
              if (($_brands = \Yii::$app->request->get('brand')) != false && is_array($_brands) && count($_brands) > 0) {
                  $_brands = Brand::find()->where(['in', 'alias', $_brands])->all();
                  $bids = [];
                  foreach ($_brands as $brand) {
                      $bids[] = $brand->brand_id;
                  }
                  if (count($bids)) {
                      $query->andWhere([Product::tableName() .'.brand_id' => $bids]);
                  }
              }
5aa7418e   Karnovsky A   Base-product#3 fu...
157
  
14eadb86   Karnovsky A   Eager loading for...
158
159
160
161
              $query->with('variant');
              $query->with('brand');
              $query->with('categories');
              $query->with('image');
5aa7418e   Karnovsky A   Base-product#3 fu...
162
  
14eadb86   Karnovsky A   Eager loading for...
163
              $count = $query->count();
b15c889e   Karnovsky A   Base-product#2 fu...
164
              $pages = new Pagination(['totalCount' => $count, 'pageSize' => $per_page]);
b519af22   Karnovsky A   Base-product func...
165
166
167
              $query->offset($pages->offset)
                  ->orderBy($sort->orders)
                  ->limit($pages->limit);
14eadb86   Karnovsky A   Eager loading for...
168
  
b519af22   Karnovsky A   Base-product func...
169
170
              $products = $query->all();
  
85261b14   Karnovsky A   not fixed commite
171
172
173
174
              return $this->render(
                  'products',
                  [
                      'category' => $category,
b519af22   Karnovsky A   Base-product func...
175
                      'products' => $products,
b15c889e   Karnovsky A   Base-product#2 fu...
176
                      'all_count' => $all_count,
b519af22   Karnovsky A   Base-product func...
177
178
179
180
181
182
                      'product_count' => $count,
                      'sort' => $sort,
                      'pages' => $pages,
                      'per_page' => $per_page,
                      'priceMin' => $priceMin,
                      'priceMax' => $priceMax,
b15c889e   Karnovsky A   Base-product#2 fu...
183
184
                      'priceMinCurr' => $priceMinCurr,
                      'priceMaxCurr' => $priceMaxCurr,
b519af22   Karnovsky A   Base-product func...
185
186
                      'brands' => $brands,
                      'brands_count' => $brands_count,
b15c889e   Karnovsky A   Base-product#2 fu...
187
                      'groups' => $groups,
5aa7418e   Karnovsky A   Base-product#3 fu...
188
                      'options' => $options,
85261b14   Karnovsky A   not fixed commite
189
190
191
192
193
194
195
                  ]
              );
          }
      }
  
      public function actionProduct($alias)
      {
b519af22   Karnovsky A   Base-product func...
196
          $product = ProductSearch::findByAlias($alias);
85261b14   Karnovsky A   not fixed commite
197
          if (empty($product->product_id)) {
b519af22   Karnovsky A   Base-product func...
198
              throw new HttpException(404 ,'Page not found');
85261b14   Karnovsky A   not fixed commite
199
          }
5aa7418e   Karnovsky A   Base-product#3 fu...
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
          $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]);
          }
  
          return $this->render('product', [
              'product' => $product,
              'properties' => $groups,
          ]);
85261b14   Karnovsky A   not fixed commite
216
217
218
219
220
221
222
223
224
225
226
227
228
      }
  
      public function actionBrands()
      {
          return 'actionBrands';
      }
  
      public function actionBrand($alias)
      {
          return 'actionBrand:'. $alias;
      }
  
  }