Blame view

frontend/controllers/CatalogController.php 4.82 KB
85261b14   Karnovsky A   not fixed commite
1
2
3
4
  <?php
  
  namespace frontend\controllers;
  
b519af22   Karnovsky A   Base-product func...
5
  use common\modules\product\models\Brand;
85261b14   Karnovsky A   not fixed commite
6
7
8
  use common\modules\product\models\Category;
  use common\modules\product\models\CategorySearch;
  use common\modules\product\models\Product;
b519af22   Karnovsky A   Base-product func...
9
10
11
12
13
14
15
  use common\modules\product\models\ProductCategory;
  use common\modules\product\models\ProductSearch;
  use common\modules\product\models\ProductVariant;
  use yii\data\ActiveDataProvider;
  use yii\data\Pagination;
  use yii\data\Sort;
  use yii\db\ActiveQuery;
85261b14   Karnovsky A   not fixed commite
16
17
18
19
20
21
22
  use yii\web\HttpException;
  
  class CatalogController extends \yii\web\Controller
  {
      public function actionCategory($alias)
      {
          $category = CategorySearch::findByAlias($alias);
b519af22   Karnovsky A   Base-product func...
23
24
25
          if (empty($category->category_id)) {
              throw new HttpException(404 ,'Page not found');
          }
85261b14   Karnovsky A   not fixed commite
26
27
28
29
30
31
32
33
          if ($category->depth < 2) {
              return $this->render(
                  'categories',
                  [
                      'category' => $category
                  ]
              );
          } else {
b519af22   Karnovsky A   Base-product func...
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
              $per_page = 24;
  
              $sort = new Sort([
                  'attributes' => [
                      'name' => [
                          'asc' => ['name' => SORT_ASC],
                          'desc' => ['name' => SORT_DESC],
                          'default' => SORT_DESC,
                          'label' => 'имени',
                      ],
                      'price' => [
                          'asc' => [ProductVariant::tableName() .'.price' => SORT_ASC],
                          'desc' => [ProductVariant::tableName() .'.price' => SORT_DESC],
                          'default' => SORT_DESC,
                          'label' => 'цене',
                      ],
                  ],
              ]);
              /** @var ActiveQuery $query */
              $query = $category->getRelations('product_categories')
                  ->joinWith([
                      'variants'
                  ]);
b15c889e   Karnovsky A   Base-product#2 fu...
57
              $all_count = $query->count();
b519af22   Karnovsky A   Base-product func...
58
  
b15c889e   Karnovsky A   Base-product#2 fu...
59
60
61
              $priceQuery = clone $query;
              $priceMin = $priceMinCurr = $priceQuery->min(ProductVariant::tableName() .'.price');
              $priceMax = $priceMaxCurr = $priceQuery->max(ProductVariant::tableName() .'.price');
b519af22   Karnovsky A   Base-product func...
62
  
b15c889e   Karnovsky A   Base-product#2 fu...
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
              if (($price_interval = \Yii::$app->request->get('price_interval')) != false) {
                  $price_interval = explode(';', $price_interval);
                  $price_interval = [
                      floatval($price_interval[0]),
                      floatval($price_interval[1]),
                  ];
                  if ($price_interval[0] > 0) {
                      $query->andWhere(['>=', ProductVariant::tableName() .'.price', $price_interval[0]]);
                      $priceMinCurr = $price_interval[0];
                  }
                  if ($price_interval[1] > 0) {
                      $query->andWhere(['<=', ProductVariant::tableName() .'.price', $price_interval[1]]);
                      $priceMaxCurr = $price_interval[1];
                  }
              }
              $count = $query->count();
              $pages = new Pagination(['totalCount' => $count, 'pageSize' => $per_page]);
b519af22   Karnovsky A   Base-product func...
80
81
82
83
84
85
86
87
88
89
              $query->offset($pages->offset)
                  ->orderBy($sort->orders)
                  ->limit($pages->limit);
              $products = $query->all();
  
              $brandsQuery = Brand::find()->innerJoinWith('products')->innerJoin(ProductCategory::tableName(), ProductCategory::tableName() .'.product_id='. Product::tableName() .'.product_id')->where([
                  ProductCategory::tableName() .'.category_id' => $category->category_id
              ])->groupBy(Brand::tableName() .'.brand_id');
              $brands = $brandsQuery->all();
              $brands_count = $brandsQuery->count();
85261b14   Karnovsky A   not fixed commite
90
  
b15c889e   Karnovsky A   Base-product#2 fu...
91
92
              $groups = $category->getTaxGroups()->all();
  
85261b14   Karnovsky A   not fixed commite
93
94
95
96
              return $this->render(
                  'products',
                  [
                      'category' => $category,
b519af22   Karnovsky A   Base-product func...
97
                      'products' => $products,
b15c889e   Karnovsky A   Base-product#2 fu...
98
                      'all_count' => $all_count,
b519af22   Karnovsky A   Base-product func...
99
100
101
102
103
104
                      'product_count' => $count,
                      'sort' => $sort,
                      'pages' => $pages,
                      'per_page' => $per_page,
                      'priceMin' => $priceMin,
                      'priceMax' => $priceMax,
b15c889e   Karnovsky A   Base-product#2 fu...
105
106
                      'priceMinCurr' => $priceMinCurr,
                      'priceMaxCurr' => $priceMaxCurr,
b519af22   Karnovsky A   Base-product func...
107
108
                      'brands' => $brands,
                      'brands_count' => $brands_count,
b15c889e   Karnovsky A   Base-product#2 fu...
109
                      'groups' => $groups,
85261b14   Karnovsky A   not fixed commite
110
111
112
113
114
115
116
                  ]
              );
          }
      }
  
      public function actionProduct($alias)
      {
b519af22   Karnovsky A   Base-product func...
117
          $product = ProductSearch::findByAlias($alias);
85261b14   Karnovsky A   not fixed commite
118
          if (empty($product->product_id)) {
b519af22   Karnovsky A   Base-product func...
119
              throw new HttpException(404 ,'Page not found');
85261b14   Karnovsky A   not fixed commite
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
          }
          return $this->render('product');
      }
  
      public function actionBrands()
      {
          return 'actionBrands';
      }
  
      public function actionBrand($alias)
      {
          return 'actionBrand:'. $alias;
      }
  
  }