Blame view

frontend/controllers/FilterController.php 12.2 KB
d8c1a2e0   Yarik   Big commit artbox
1
2
3
4
  <?php
      
      namespace frontend\controllers;
      
99e2f4e7   Yarik   Category to purpo...
5
      use common\models\CategoryToPurpose;
cc658b4c   Yarik   Big commit
6
7
8
9
      use common\modules\product\models\Brand;
      use common\modules\product\models\Category;
      use common\modules\product\models\ProductSearch;
      use common\modules\rubrication\models\TaxOption;
c67585d7   Yarik   Category path fixes
10
      use yii\db\ActiveQuery;
d8c1a2e0   Yarik   Big commit artbox
11
      use yii\web\Controller;
cc658b4c   Yarik   Big commit
12
      use yii\web\NotFoundHttpException;
d8c1a2e0   Yarik   Big commit artbox
13
14
15
16
17
18
19
      
      /**
       * Filter controller
       */
      class FilterController extends Controller
      {
          
cc658b4c   Yarik   Big commit
20
21
22
23
24
          public function actionIndex($category_id, $purpose_id)
          {
              $category = $this->findCategory($category_id);
              $purpose = $this->findPurpose($purpose_id);
              $brands = Brand::find()
d55d2fe0   Yarik   Multilanguage
25
26
27
28
                             ->joinWith('lang', true, 'INNER JOIN')
                             ->joinWith('products.lang')
                             ->joinWith('products.categories.lang')
                             ->joinWith('products.options.lang')
99e2f4e7   Yarik   Category to purpo...
29
30
31
32
33
34
                             ->where(
                                 [
                                     'category.category_id'     => $category->category_id,
                                     'tax_option.tax_option_id' => $purpose->tax_option_id,
                                 ]
                             )
cc658b4c   Yarik   Big commit
35
                             ->all();
99e2f4e7   Yarik   Category to purpo...
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
              $image = CategoryToPurpose::find()
                                        ->where(
                                            [
                                                'category_id' => $category_id,
                                                'purpose_id'  => $purpose_id,
                                            ]
                                        )
                                        ->andWhere(
                                            [
                                                'not',
                                                [ 'image' => null ],
                                            ]
                                        )
                                        ->one();
              return $this->render(
                  'index',
                  [
                      'category' => $category,
                      'purpose'  => $purpose,
                      'brands'   => $brands,
                      'image'    => $image,
                  ]
              );
cc658b4c   Yarik   Big commit
59
60
          }
          
d8c1a2e0   Yarik   Big commit artbox
61
62
          /**
           * Filter by type.
c67585d7   Yarik   Category path fixes
63
           *
d8c1a2e0   Yarik   Big commit artbox
64
65
           * @return string
           */
cc658b4c   Yarik   Big commit
66
          public function actionCategory($id)
d8c1a2e0   Yarik   Big commit artbox
67
          {
cc658b4c   Yarik   Big commit
68
              $category = $this->findCategory($id);
c67585d7   Yarik   Category path fixes
69
              $brandsAll = $category->activeBrands;
cc658b4c   Yarik   Big commit
70
              $purposes = TaxOption::find()
d55d2fe0   Yarik   Multilanguage
71
                                   ->joinWith('lang', true, 'INNER JOIN')
99e2f4e7   Yarik   Category to purpo...
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
                                   ->joinWith(
                                       [
                                           'products' => function ($query) use ($category) {
                                               $query->joinWith(
                                                   [
                                                       'categories' => function ($query) use ($category) {
                                                           $query->andWhere(
                                                               [ 'category.category_id' => $category->category_id ]
                                                           );
                                                       },
                                                   ]
                                               );
                                           },
                                       ]
                                   )
d55d2fe0   Yarik   Multilanguage
87
                                   ->joinWith('products.categories.lang')
887985c7   Yarik   Filter fix
88
                                   ->joinWith('products.lang')
d55d2fe0   Yarik   Multilanguage
89
                                   ->joinWith('products.brand.lang')
cc658b4c   Yarik   Big commit
90
                                   ->joinWith('taxGroup')
99e2f4e7   Yarik   Category to purpo...
91
92
93
94
95
96
                                   ->where(
                                       [
                                           'category.category_id'   => $category->category_id,
                                           'tax_group.tax_group_id' => 5,
                                       ]
                                   )
cc658b4c   Yarik   Big commit
97
                                   ->all();
cc658b4c   Yarik   Big commit
98
              $brands = [];
99e2f4e7   Yarik   Category to purpo...
99
              foreach ($purposes as $purpose) {
cc658b4c   Yarik   Big commit
100
101
102
103
                  /**
                   * @var TaxOption $purpose
                   */
                  $brands[ $purpose->tax_option_id ] = [];
99e2f4e7   Yarik   Category to purpo...
104
                  foreach ($purpose->products as $product) {
cc658b4c   Yarik   Big commit
105
106
107
108
                      /**
                       * @var Brand $brand
                       */
                      $brand = $product->brand;
99e2f4e7   Yarik   Category to purpo...
109
                      if (!empty( $brand )) {
cc658b4c   Yarik   Big commit
110
111
112
113
                          $brands[ $purpose->tax_option_id ][ $brand->brand_id ] = $brand;
                      }
                  }
              }
99e2f4e7   Yarik   Category to purpo...
114
115
116
117
118
119
120
121
122
              return $this->render(
                  'category',
                  [
                      'category'  => $category,
                      'purposes'  => $purposes,
                      'brands'    => $brands,
                      'brandsAll' => $brandsAll,
                  ]
              );
d8c1a2e0   Yarik   Big commit artbox
123
124
125
126
          }
          
          /**
           * Filter by purpose.
c67585d7   Yarik   Category path fixes
127
           *
d8c1a2e0   Yarik   Big commit artbox
128
129
           * @return string
           */
cc658b4c   Yarik   Big commit
130
131
132
133
          public function actionPurpose($id)
          {
              $purpose = $this->findPurpose($id);
              $categories = Category::find()
d55d2fe0   Yarik   Multilanguage
134
135
                                    ->joinWith('products.lang')
                                    ->joinWith('products.brand.lang')
99e2f4e7   Yarik   Category to purpo...
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
                                    ->joinWith(
                                        [
                                            'products' => function ($query) use ($purpose) {
                                                $query->joinWith(
                                                    [
                                                        'options' => function ($query) use ($purpose) {
                                                            $query->andWhere(
                                                                [ 'tax_option.tax_option_id' => $purpose->tax_option_id ]
                                                            );
                                                        },
                                                    ]
                                                );
                                            },
                                        ]
                                    )
d55d2fe0   Yarik   Multilanguage
151
152
                                    ->joinWith('products.options.lang')
                                    ->where([ 'tax_option.tax_option_id' => $purpose->tax_option_id ])
cc658b4c   Yarik   Big commit
153
154
                                    ->all();
              $brands = [];
99e2f4e7   Yarik   Category to purpo...
155
              foreach ($categories as $category) {
cc658b4c   Yarik   Big commit
156
                  $brands[ $category->category_id ] = [];
99e2f4e7   Yarik   Category to purpo...
157
                  foreach ($category->products as $product) {
cc658b4c   Yarik   Big commit
158
159
160
161
                      /**
                       * @var Brand $brand
                       */
                      $brand = $product->brand;
99e2f4e7   Yarik   Category to purpo...
162
                      if (!empty( $brand )) {
cc658b4c   Yarik   Big commit
163
164
165
166
                          $brands[ $category->category_id ][ $brand->brand_id ] = $brand;
                      }
                  }
              }
99e2f4e7   Yarik   Category to purpo...
167
168
169
170
171
172
173
174
              return $this->render(
                  'purpose',
                  [
                      'purpose'    => $purpose,
                      'categories' => $categories,
                      'brands'     => $brands,
                  ]
              );
cc658b4c   Yarik   Big commit
175
176
177
178
179
180
181
182
183
184
          }
          
          public function actionBrand($category_id, $purpose_id, $brand_id)
          {
              $category = $this->findCategory($category_id);
              $purpose = $this->findPurpose($purpose_id);
              $brand = $this->findBrand($brand_id);
              $searchModel = new ProductSearch();
              $dataProvider = $searchModel->search(\Yii::$app->request->queryParams);
              $dataProvider->pagination = false;
c67585d7   Yarik   Category path fixes
185
186
187
              /**
               * @var ActiveQuery $query
               */
cc658b4c   Yarik   Big commit
188
              $query = $dataProvider->query;
d55d2fe0   Yarik   Multilanguage
189
              $query->with('variants.lang')
d55d2fe0   Yarik   Multilanguage
190
191
192
                    ->joinWith('brand.lang')
                    ->joinWith('options.lang')
                    ->joinWith('categories.lang')
99e2f4e7   Yarik   Category to purpo...
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
                    ->andWhere(
                        [
                            'category.category_id'     => $category->category_id,
                            'tax_option.tax_option_id' => $purpose->tax_option_id,
                            'brand.brand_id'           => $brand->brand_id,
                        ]
                    );
              return $this->render(
                  'brand',
                  [
                      'category'     => $category,
                      'purpose'      => $purpose,
                      'brand'        => $brand,
                      'searchModel'  => $searchModel,
                      'dataProvider' => $dataProvider,
                  ]
              );
cc658b4c   Yarik   Big commit
210
211
          }
          
c67585d7   Yarik   Category path fixes
212
213
214
215
216
217
218
219
220
221
222
223
224
225
          public function actionCategoryBrand($category_id, $brand_id)
          {
              $category = $this->findCategory($category_id);
              $brand = $this->findBrand($brand_id);
              $searchModel = new ProductSearch();
              $dataProvider = $searchModel->search(\Yii::$app->request->queryParams);
              $dataProvider->pagination = false;
              /**
               * @var ActiveQuery $query
               */
              $query = $dataProvider->query;
              $query->with('variants.lang')
                    ->joinWith('brand.lang')
                    ->joinWith('categories.lang')
99e2f4e7   Yarik   Category to purpo...
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
                    ->andWhere(
                        [
                            'category.category_id' => $category->category_id,
                            'brand.brand_id'       => $brand->brand_id,
                        ]
                    );
              return $this->render(
                  'category-brand',
                  [
                      'category'     => $category,
                      'brand'        => $brand,
                      'searchModel'  => $searchModel,
                      'dataProvider' => $dataProvider,
                  ]
              );
c67585d7   Yarik   Category path fixes
241
242
243
244
245
246
          }
          
          public function actionCategoryBrands($category_id)
          {
              $category = $this->findCategory($category_id);
              $brands = $category->activeBrands;
99e2f4e7   Yarik   Category to purpo...
247
248
249
250
251
252
253
              return $this->render(
                  'category-brands',
                  [
                      'category' => $category,
                      'brands'   => $brands,
                  ]
              );
c67585d7   Yarik   Category path fixes
254
255
          }
          
cc658b4c   Yarik   Big commit
256
257
258
259
260
261
262
263
264
265
          /**
           * @param $id
           *
           * @return TaxOption
           * @throws NotFoundHttpException
           */
          private function findPurpose($id)
          {
              $model = TaxOption::find()
                                ->joinWith('taxGroup')
99e2f4e7   Yarik   Category to purpo...
266
267
268
269
270
271
                                ->where(
                                    [
                                        'tax_option.tax_option_id' => $id,
                                        'tax_group.tax_group_id'   => 5,
                                    ]
                                )
cc658b4c   Yarik   Big commit
272
                                ->one();
99e2f4e7   Yarik   Category to purpo...
273
              if (empty( $model )) {
cc658b4c   Yarik   Big commit
274
275
276
277
278
279
280
281
282
283
284
285
286
287
                  throw new NotFoundHttpException();
              }
              return $model;
          }
          
          /**
           * @param $id
           *
           * @return Category
           * @throws NotFoundHttpException
           */
          private function findCategory($id)
          {
              $model = Category::find()
99e2f4e7   Yarik   Category to purpo...
288
289
290
291
292
                               ->where(
                                   [
                                       'category.category_id' => $id,
                                   ]
                               )
cc658b4c   Yarik   Big commit
293
                               ->one();
99e2f4e7   Yarik   Category to purpo...
294
              if (empty( $model )) {
cc658b4c   Yarik   Big commit
295
296
297
298
299
300
301
302
303
304
305
306
                  throw new NotFoundHttpException();
              }
              return $model;
          }
          
          /**
           * @param $id
           *
           * @return Brand
           * @throws NotFoundHttpException
           */
          private function findBrand($id)
d8c1a2e0   Yarik   Big commit artbox
307
          {
cc658b4c   Yarik   Big commit
308
309
310
311
              /**
               * @var Brand $model
               */
              $model = Brand::find()
99e2f4e7   Yarik   Category to purpo...
312
313
314
315
316
                            ->where(
                                [
                                    'brand.brand_id' => $id,
                                ]
                            )
cc658b4c   Yarik   Big commit
317
                            ->one();
99e2f4e7   Yarik   Category to purpo...
318
              if (empty( $model )) {
cc658b4c   Yarik   Big commit
319
320
321
                  throw new NotFoundHttpException();
              }
              return $model;
d8c1a2e0   Yarik   Big commit artbox
322
323
          }
      }