Blame view

frontend/controllers/CategoryController.php 2.37 KB
4ca21c3e   Alexey Boroda   first commit
1
2
3
4
5
6
7
8
9
  <?php
  
  namespace frontend\controllers;
  
  use common\modules\product\models\Category;
  use common\modules\product\models\Product;
  use yii\data\ActiveDataProvider;
  use yii\web\Controller;
  use yii\web\NotFoundHttpException;
4556b430   Alexey Boroda   Changes:
10
  use yii\helpers\VarDumper;
4ca21c3e   Alexey Boroda   first commit
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
  
  /**
   * Category controller
   */
  class CategoryController extends Controller
  {
      /**
       * Displays
       *
       * @param integer $id
       * @return string
       */
      public function actionIndex($id)
      {
          $category = $this->findModel($id, 0);
          $query = Category::find()
              ->where([
                  'parent_id' => $category->category_id
              ]);
          $dataProvider = new ActiveDataProvider([
              'query' => $query,
              'pagination' => false,
          ]);
          return $this->render('index', [
              'dataProvider' => $dataProvider,
              'category' => $category,
          ]);
      }
  
      public function actionBrand($id)
      {
          $category = $this->findModel($id, 1);
          $query = Category::find()
              ->with('parent')
              ->where([
                  'parent_id' => $category->category_id
              ]);
          $dataProvider = new ActiveDataProvider([
              'query' => $query,
              'pagination' => false,
          ]);
          return $this->render('brand', [
              'dataProvider' => $dataProvider,
              'category' => $category,
          ]);
      }
  
      public function actionCollection($id)
      {
          $category = $this->findModel($id, 2);
          $i = 0;
          foreach ($category->products as $product) {
              $products[$i][] = $product;
              $i++;
              if ($i === 3) $i = 0;
          }
          if (!empty($products)) {
              return $this->render('collection', [
                  'products' => $products,
                  'category' => $category,
              ]);
          } else {
              throw new NotFoundHttpException("В этой коллекции нет товаров");
          }
      }
  
      /**
       * Find model by id.
       *
       * @param integer $id
       * @return Category
       * @throws NotFoundHttpException
       */
      private function findModel($id, $depth) {
          $model = Category::find()->where([
              'category_id' => $id,
              'depth' => $depth,
          ])->one();
          if(!empty($model)) {
              return $model;
          } else {
              throw new NotFoundHttpException();
          }
      }
  }