Blame view

frontend/controllers/CategoryController.php 8.18 KB
950817c6   Alex Savenko   first commit
1
2
3
4
5
6
7
8
  <?php
      
      namespace frontend\controllers;
      
      use artbox\catalog\helpers\FilterHelper;
      use artbox\catalog\models\Category;
      use artbox\core\components\SeoComponent;
      use artbox\core\models\Alias;
2e452ecc   mzavalniuk   added SeoCategory...
9
      use frontend\components\SeoCategory;
950817c6   Alex Savenko   first commit
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
      use yii\data\ActiveDataProvider;
      use yii\db\ActiveQuery;
      use yii\web\Controller;
      use yii\web\NotFoundHttpException;
      use Yii;
      
      /**
       * Class CategoryController
       *
       * @package frontend\controllers
       */
      class CategoryController extends Controller
      {
          /**
           * Show category by ID
           *
           * @param string $category
           * @param string $filter
           *
           * @return string
           */
          public function actionView($category, $filter = '')
          {
              $model = $this->findModel($category);
              /**
               * @var SeoComponent $seo
               * @var FilterHelper $filterHelper
               */
              $seo = Yii::$app->get('seo');
              $seo->setModel($model->lang);
              $filterHelper = \Yii::$app->get('filter');
              $filterHelper->setFilter($filter);
2e452ecc   mzavalniuk   added SeoCategory...
42
43
44
45
46
47
48
49
50
              $seoData = new SeoCategory(
                  [
                      'options'    => $filterHelper->getActiveOptions(),
                      'category'   => $model,
                      'categoryAlias' => $seo->alias,
                  ]
              );
              $seo->loaded = false;
              $seo->setAlias($seoData);
950817c6   Alex Savenko   first commit
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
              $query = $filterHelper->buildQuery()
                                    ->innerJoinWith('category', false)
                                    ->andWhere([ 'product_to_category.category_id' => $model->id ])
                                    ->innerJoinWith(
                                        [
                                            'lang' => function ($query) {
                                                /**
                                                 * @var ActiveQuery $query
                                                 */
                                                $query->with('alias');
                                            },
                                        ]
                                    )
                                    ->innerJoinWith('variant')
                                    ->andWhere('variant.price > 0')
                                    ->with('image', 'variants.image');
              $query->orderBy('CASE WHEN(stock > 0 and variant.price > 0) then 10 WHEN(stock = 0 and variant.price > 0) then 8 ELSE 0 END DESC');
              $dataProvider = new ActiveDataProvider(
                  [
                      'query'      => $query,
                      'pagination' => [
                          'pageSize' => 18,
                      ],
                      'sort'       => [
                          'attributes'   => [
                              'relevant'   => [
                                  'asc'   => [
                                      'product.id' => SORT_DESC,
                                  ],
                                  'desc'  => [
                                      'product.id' => SORT_DESC,
                                  ],
10e74468   mzavalniuk   translate to ua r...
83
                                  'label' => \Yii::t('app', Yii::t('app','default')),
950817c6   Alex Savenko   first commit
84
85
86
87
88
89
90
91
                              ],
                              'title_asc'  => [
                                  'asc'   => [
                                      'product_lang.title' => SORT_ASC,
                                  ],
                                  'desc'  => [
                                      'product_lang.title' => SORT_ASC,
                                  ],
10e74468   mzavalniuk   translate to ua r...
92
                                  'label' => \Yii::t('app', Yii::t('app','a-z')),
950817c6   Alex Savenko   first commit
93
94
95
96
97
98
99
100
                              ],
                              'title_desc' => [
                                  'asc'   => [
                                      'product_lang.title' => SORT_DESC,
                                  ],
                                  'desc'  => [
                                      'product_lang.title' => SORT_DESC,
                                  ],
10e74468   mzavalniuk   translate to ua r...
101
                                  'label' => \Yii::t('app', Yii::t('app','z-a')),
950817c6   Alex Savenko   first commit
102
103
104
105
106
107
108
109
                              ],
                              'price_asc'  => [
                                  'asc'   => [
                                      'variant.price' => SORT_ASC,
                                  ],
                                  'desc'  => [
                                      'variant.price' => SORT_ASC,
                                  ],
10e74468   mzavalniuk   translate to ua r...
110
                                  'label' => \Yii::t('app', Yii::t('app','price asc')),
950817c6   Alex Savenko   first commit
111
112
113
114
115
116
117
118
                              ],
                              'price_desc' => [
                                  'asc'   => [
                                      'variant.price' => SORT_DESC,
                                  ],
                                  'desc'  => [
                                      'variant.price' => SORT_DESC,
                                  ],
10e74468   mzavalniuk   translate to ua r...
119
                                  'label' => \Yii::t('app', Yii::t('app','price desc')),
950817c6   Alex Savenko   first commit
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
                              ],
                          ],
                          'defaultOrder' => [
                              'relevant' => SORT_DESC,
                          ],
                      ],
                  ]
              );
              $queryClone = clone $query;
              $queryClone->orderBy = [];
              $queryClone->where = [];
              $queryClone->limit = false;
              $priceMin = $queryClone->select('min(price)')
                                     ->where([ 'product_to_category.category_id' => $model->id ])
                                     ->andWhere('variant.price > 0')
                                     ->scalar();
              $priceMax = $queryClone->select('max(price)')
                                     ->where([ 'product_to_category.category_id' => $model->id ])
                                      ->andWhere('variant.price > 0')
                                      ->scalar();
              return $this->render(
                  'view',
                  [
                      'model'        => $model,
                      'dataProvider' => $dataProvider,
                      'priceMin'     => $priceMin,
                      'priceMax'     => $priceMax,
                  ]
              );
          }
          
          /**
           * Find category by ID
           *
           * @param $category
           *
           * @return \artbox\catalog\models\Category
           * @throws \yii\web\NotFoundHttpException
           */
          protected function findModel($category)
          {
              /**
               * @var SeoComponent $seo
               */
              $seo = Yii::$app->get('seo');
              /**
               * @var FilterHelper $filter
               */
              $filter = \Yii::$app->get('filter');
              /**
               * @var Alias $alias
               */
              $alias = Alias::find()
                            ->where([ 'value' => $category ])
                            ->one();
              if (empty($alias)) {
                  throw new NotFoundHttpException('Category not found');
              }
              $id = $filter->getIdFromRoute($alias->route);
              /**
               * @var Category $model
               */
              $model = Category::findWithFilters($id)
                               ->with('lang.alias')
                               ->with('categories.lang.alias')
                               ->with(
                                   [
                                       'parent' => function ($query) {
                                           /**
                                            * @var ActiveQuery $query
                                            */
                                           $query->with('lang.alias', 'categories.lang.alias');
                                       },
                                   ]
                               )
                               ->one();
              $seo->setAlias($model->lang->alias);
              if (!empty($model)) {
                  if ($model->lang->alias_id !== $seo->aliasId) {
                      throw new NotFoundHttpException('Wrong language');
                  }
                  return $model;
              } else {
                  throw new NotFoundHttpException('Model not found');
              }
          }
      }