Blame view

frontend/controllers/CatalogController.php 6.26 KB
4253cbec   root   first commit
1
  <?php
e337d04a   Administrator   liniya first commit
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
  
  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;
37415bee   Administrator   big commti
27
  use yii\helpers\VarDumper;
e337d04a   Administrator   liniya first commit
28
29
30
31
32
33
34
35
36
  use yii\web\HttpException;
  
  class CatalogController extends \yii\web\Controller
  {
      public function actionSearch() {
          // @todo
      }
  
      public function actionCategory()
4253cbec   root   first commit
37
      {
e337d04a   Administrator   liniya first commit
38
39
40
41
42
43
44
45
  
          /** @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
46
          }
e337d04a   Administrator   liniya first commit
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  
          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;
4253cbec   root   first commit
69
              }
e337d04a   Administrator   liniya first commit
70
71
72
73
74
75
          }
  
          if(!empty( $filter[ 'special' ] )) {
              unset( $filter_check[ 'special' ] );
              if(!is_array($filter[ 'special' ])) {
                  $filter[ 'special' ] = [ $filter[ 'special' ] ];
4253cbec   root   first commit
76
              }
e337d04a   Administrator   liniya first commit
77
78
              if(in_array('new', $filter[ 'special' ])) {
                  $params[ 'special' ][ 'is_new' ] = true;
4253cbec   root   first commit
79
              }
e337d04a   Administrator   liniya first commit
80
81
              if(in_array('top', $filter[ 'special' ])) {
                  $params[ 'special' ][ 'is_top' ] = true;
4253cbec   root   first commit
82
              }
e337d04a   Administrator   liniya first commit
83
84
              if(in_array('promo', $filter[ 'special' ])) {
                  $params[ 'special' ][ 'akciya' ] = true;
4253cbec   root   first commit
85
              }
4253cbec   root   first commit
86
          }
e337d04a   Administrator   liniya first commit
87
88
89
90
91
92
93
94
95
96
97
  
          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 ];
4253cbec   root   first commit
98
              }
e337d04a   Administrator   liniya first commit
99
  
4253cbec   root   first commit
100
          }
e337d04a   Administrator   liniya first commit
101
102
103
104
105
106
107
108
  
          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
109
          }
e337d04a   Administrator   liniya first commit
110
111
112
113
114
115
116
117
118
  
          $productModel = new ProductFrontendSearch();
          //$productQuery = $productModel->getSearchQuery($category, $params);
          $productProvider = $productModel->search($category, $params);
  
          $brandModel = new BrandSearch();
          $brands = $brandModel->getBrands($category, $params)
              ->all();
  
0bfe5701   Administrator   big commti
119
          $groups = $category->getActiveFilters();
e337d04a   Administrator   liniya first commit
120
          $groups = ArrayHelper::index($groups, null, 'name');
0bfe5701   Administrator   big commti
121
          $priceLimits = $productModel->priceLimits($category, $params);
e337d04a   Administrator   liniya first commit
122
123
124
125
126
127
  
          /*
           * Greedy search for comments and rating
           */
          $query = $productProvider->query;
          $query->with([
3523d488   Administrator   big commti
128
              'events',
fe7b6960   Yarik   Basket
129
              'variant',
e337d04a   Administrator   liniya first commit
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
              'comments',
              'averageRating',
          ]);
          /*
           * End of greedy search for rating and comments
           */
  
          return $this->render('products', [
              'category'        => $category,
              'brandModel'      => $brandModel,
              'brands'          => $brands,
              'filter'          => $filter,
              'params'          => $params,
              'productModel'    => $productModel,
              'productProvider' => $productProvider,
              'groups'            => $groups,
0bfe5701   Administrator   big commti
146
              'priceLimits'     => $priceLimits,
e337d04a   Administrator   liniya first commit
147
148
149
150
151
152
153
154
155
          ]);
  
      }
  
      public function actionProduct()
      {
          /** @var Product $product */
          $product = Yii::$app->request->get('product');
  
e337d04a   Administrator   liniya first commit
156
157
158
  
          ProductHelper::addLastProsucts($product->product_id);
  
9dcd66fc   Administrator   big commti
159
160
161
162
163
164
165
166
167
          $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
168
169
          return $this->render('product', [
              'product' => $product,
9dcd66fc   Administrator   big commti
170
              'category' => $category,
e337d04a   Administrator   liniya first commit
171
172
173
          ]);
      }
  
2cbb3bdc   Виталий   test commit
174
175
176
177
178
179
180
181
182
183
184
185
186
      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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
  
      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
207
      }
e337d04a   Administrator   liniya first commit
208
209
  
  }