Blame view

frontend/controllers/ProductController.php 2.44 KB
4ca21c3e   Alexey Boroda   first commit
1
2
3
  <?php
      
      namespace frontend\controllers;
2d7ef064   Alexey Boroda   added menu and pr...
4
5
  
      use common\modules\product\models\Category;
4ca21c3e   Alexey Boroda   first commit
6
      use common\modules\product\models\Product;
2d7ef064   Alexey Boroda   added menu and pr...
7
      use yii\data\ActiveDataProvider;
4ca21c3e   Alexey Boroda   first commit
8
9
10
11
12
13
14
15
16
17
18
19
20
      use yii\web\Controller;
      use yii\web\NotFoundHttpException;
  
      /**
       * Products controller
       */
      class ProductController extends Controller
      {
          
          /**
           * Displays product list.
           * @return string
           */
2d7ef064   Alexey Boroda   added menu and pr...
21
          public function actionIndex($id)
4ca21c3e   Alexey Boroda   first commit
22
          {
2d7ef064   Alexey Boroda   added menu and pr...
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
              $category = Category::find()
                  ->where([
                      'category_id' => $id,
                  ])->one();
              $query = Product::find()
                  ->joinWith(['categories.grandParent c3'])
                  ->with('variant')
                  ->where(['c3.category_id' => $id]);
              $dataProvider = new ActiveDataProvider([
                  'query' => $query,
                  'pagination' => [
                      'pageSize' => 15,
                  ],
              ]);
              return $this->render('index', [
                  'dataProvider' => $dataProvider,
                  'category' => $category,
              ]);
4ca21c3e   Alexey Boroda   first commit
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
          }
  
          /**
           * @param int $product_id
           * @param int $variant_id
           * @return string
           */
          public function actionView($product_id, $variant_id)
          {
              $product = Product::find()
                  ->joinWith([
                      'variants' => function($query) {
                          $query->indexBy('product_variant_id');
                      }
                  ], true, 'INNER JOIN')
                  ->where([
                      'product.product_id' => $product_id,
                      'product_variant.product_variant_id' => $variant_id,
                  ])->with('category.parent')->one();
              $variant = $product->variants[$variant_id];
              $variants = $product->variants;
              return $this->render('view', [
                  'variants' => $variants,
                  'product' => $product,
                  'variant' => $variant,
              ]);
          }
  
          /**
           * @param int $id
           * @return Product
           * @throws NotFoundHttpException
           */
          private function findProduct($id)
          {
              $model = Product::find()->where(['product_id' => (int) $id])
                  ->one();
              if(empty($model)) {
                  throw new NotFoundHttpException();
              }
              return $model;
          }
      }