Blame view

frontend/controllers/CollectionController.php 2.49 KB
d8c1a2e0   Yarik   Big commit artbox
1
2
3
4
  <?php
      
      namespace frontend\controllers;
      
cc658b4c   Yarik   Big commit
5
6
7
8
      use common\models\Project;
      use common\modules\product\models\Product;
      use common\modules\product\models\ProductVariant;
      use yii\helpers\ArrayHelper;
d8c1a2e0   Yarik   Big commit artbox
9
      use yii\web\Controller;
cc658b4c   Yarik   Big commit
10
      use yii\web\NotFoundHttpException;
d8c1a2e0   Yarik   Big commit artbox
11
12
      
      /**
36d1807a   Yarik   Big commit.
13
       * Article controller
d8c1a2e0   Yarik   Big commit artbox
14
       */
36d1807a   Yarik   Big commit.
15
      class CollectionController extends Controller
d8c1a2e0   Yarik   Big commit artbox
16
17
18
      {
          
          /**
36d1807a   Yarik   Big commit.
19
           * Displays article list.
d8c1a2e0   Yarik   Big commit artbox
20
21
           * @return string
           */
cc658b4c   Yarik   Big commit
22
          public function actionIndex($id)
d8c1a2e0   Yarik   Big commit artbox
23
          {
cc658b4c   Yarik   Big commit
24
25
26
27
              /**
               * @var ProductVariant $variant
               */
              $collection = $this->findCollection($id);
f95ddf61   Yarik   "current" functio...
28
29
              $variants = $collection->variants;
              $variant = current($variants);
cc658b4c   Yarik   Big commit
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
              return $this->redirect([
                  'view',
                  'collection_id' => $collection->product_id,
                  'variant_id'    => $variant->product_variant_id,
              ]);
          }
          
          public function actionView($collection_id, $variant_id)
          {
              $collection = $this->findCollection($collection_id, $variant_id);
              $variants = $collection->variants;
              $variants = ArrayHelper::index($variants, 'product_variant_id');
              $variant = $variants[ $variant_id ];
              $projects = Project::find()
                                 ->joinWith('productToProject')
                                 ->with('image')
                                 ->where([ 'product_to_project.product_variant_id' => $variant->product_variant_id ])
                                 ->all();
              return $this->render('view', [
                  'collection' => $collection,
                  'variants'   => $variants,
                  'variant'    => $variant,
                  'projects'   => $projects,
              ]);
          }
          
          /**
           * @param $id
           *
           * @return Product
           * @throws NotFoundHttpException
           */
          private function findCollection($id, $variant_id = NULL)
          {
              $model = Product::find()
                              ->joinWith('variants', true, 'INNER JOIN')
                              ->with('productSpec')
                              ->where([ 'product.product_id' => $id ])
                              ->andFilterWhere([ 'product_variant_id' => $variant_id ])
                              ->one();
              if(empty( $model )) {
                  throw new NotFoundHttpException();
              }
              return $model;
d8c1a2e0   Yarik   Big commit artbox
74
          }
d8c1a2e0   Yarik   Big commit artbox
75
      }