Blame view

frontend/controllers/SpecialController.php 3.56 KB
950817c6   Alex Savenko   first commit
1
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  <?php
      
      namespace frontend\controllers;
      
      use artbox\catalog\models\Category;
      use artbox\catalog\models\Product;
      use yii\data\ActiveDataProvider;
      use yii\web\Controller;
      use yii\web\NotFoundHttpException;
      
      /**
       * Class SpecialController
       *
       * @package frontend\controllers
       */
      class SpecialController extends Controller
      {
          /**
           * Show all specials by type
           *
           * @param string $type
           *
           * @return string
           */
          public function actionIndex($type)
          {
              $query = Category::find()
                               ->with('lang.alias')
                               ->innerJoinWith('products', false)
                               ->groupBy('category.id');
              if ($type === 'new') {
                  $query->is('product.mask', 2);
                  $productQuery = Product::find()
                                         ->is('product.mask', 2);
              } else {
                  $query->is('product.mask', 4);
                  $productQuery = Product::find()
                                         ->is('product.mask', 4);
              }
              $categories = $query->all();
              $dataProvider = new ActiveDataProvider(
                  [
                      'query' => $productQuery,
                  ]
              );
              return $this->render(
                  'index',
                  [
                      'categories'   => $categories,
                      'dataProvider' => $dataProvider,
                  ]
              );
          }
          
          /**
           * Show specials by type and category
           *
           * @param $type
           * @param $category
           *
           * @return string
           */
          public function actionCategory($type, $category)
          {
              $model = $this->findCategory($category);
              $query = Product::find()
                              ->with('variants', 'image')
                              ->innerJoinWith('categories', 'false')
                              ->where([ 'product_to_category.category_id' => $model->id ])
                              ->orderBy(
                                  [
                                      'product.sort'       => SORT_ASC,
                                      'product.created_at' => SORT_DESC,
                                  ]
                              );
              if ($type === 'new') {
                  $query->is('product.mask', 2);
              } elseif ($type === 'sale') {
                  $query->is('product.mask', 4);
              }
              $dataProvider = new ActiveDataProvider(
                  [
                      'query' => $query,
                  ]
              );
              return $this->render(
                  'category',
                  [
                      'model'        => $model,
                      'dataProvider' => $dataProvider,
                  ]
              );
          }
          
          /**
           * @param string $category
           *
           * @return Category
           * @throws \yii\web\NotFoundHttpException
           */
          protected function findCategory(string $category)
          {
              /**
               * @var Category $model
               */
              $model = Category::find()
                               ->innerJoinWith('lang.alias', false)
                               ->where([ 'alias.value' => $category ])
                               ->one();
              if (!empty($model)) {
                  return $model;
              } else {
                  throw new NotFoundHttpException('Model not found');
              }
          }
      }