Blame view

frontend/controllers/SpecialController.php 3.56 KB
8cd678be   Yarik   Special pages
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
  <?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') {
8e13c18c   Yarik   Search
32
                  $query->is('product.mask', 2);
8cd678be   Yarik   Special pages
33
                  $productQuery = Product::find()
8e13c18c   Yarik   Search
34
                                         ->is('product.mask', 2);
8cd678be   Yarik   Special pages
35
              } else {
8e13c18c   Yarik   Search
36
                  $query->is('product.mask', 4);
8cd678be   Yarik   Special pages
37
                  $productQuery = Product::find()
8e13c18c   Yarik   Search
38
                                         ->is('product.mask', 4);
8cd678be   Yarik   Special pages
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
              }
              $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') {
8e13c18c   Yarik   Search
77
                  $query->is('product.mask', 2);
8cd678be   Yarik   Special pages
78
              } elseif ($type === 'sale') {
8e13c18c   Yarik   Search
79
                  $query->is('product.mask', 4);
8cd678be   Yarik   Special pages
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
              }
              $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');
              }
          }
      }