Blame view

app/controllers/ProductsController.php 4.61 KB
bf807468   Alex Savenko   first commit
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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  <?php

  

  namespace app\controllers;

  

  use Yii;

  use yii\web\Controller;

  use yii\web\HttpException;

  use yii\data\Pagination;

  use app\models\Catalog;

  use app\models\Products;

  use app\models\Mod;

  use app\models\ProductsFasovka;

  use app\models\ProductsType;

  use app\models\ProductsBrends;

  

  class ProductsController extends Controller

  {

  

      public function actionIndex()

      {

              $modelProducts = new Products;

              

              $modelProducts->load($_GET);

              

              

              if(!$catalog = Catalog::find()->where(['translit'=>$_GET['translit']])->with('parent')->one())

                      throw new HttpException(404, 'Данной странице не существует!');

              

              

                  $query = Products::find()

                                   ->where(

                                       'catalog_id=:catalog_id',

                                       [':catalog_id' => $catalog->id]

                                   )->with(['catalog']) ;

                  if(!empty($modelProducts->fasovka)){

                     $query->innerJoinWith(['fasovka'])->andWhere([ProductsFasovka::tableName().'.fasovka_id'=>$modelProducts->fasovka]); 

                  }

                  if(!empty($modelProducts->type)){

                     $query->innerJoinWith(['type'])->andWhere([ProductsType::tableName().'.type_id'=>$modelProducts->type]); 

                  }

                  if(!empty($modelProducts->brends)){

                     $query->innerJoinWith(['brends'])->andWhere([ProductsBrends::tableName().'.brend_id'=>$modelProducts->brends]); 

                  }                

                  $query->groupBy(['id']);

                  $query->innerJoinWith(['minCost']);

                  if(!empty($_GET['order'])){

                      switch($_GET['order']){

                          case 1 : $query->orderBy('mod.cost ASC'); break;

                          case 2 : $query->orderBy('mod.cost DESC'); break;

                          case 3 : $query->orderBy('name ASC'); break;

                      }

                  }else $query->orderBy('new DESC, top DESC, akciya DESC, name ASC');

                  $countQuery = clone $query;

                  $pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize'=>18]);

                  $pages->forcePageParam = false;

                  $pages->pageSizeParam = false;

                  $products = $query->offset($pages->offset)

                      ->limit($pages->limit)

                      ->orderBy(

                          [

                              'out_of_stock' => SORT_ASC,

                          ]

                      )

                      ->all();

  

              return $this->render('index', [

                  'modelProducts'=>$modelProducts,    

                  'catalog'=>$catalog,

                  'pages'=>$pages,

                  'products'=>$products,

              ]);

      }

      

      public function actionSearch(){

                  $query = Products::find()->innerJoinWith(['catalog','mods']) ;

                  if (!empty($_GET['search_str'])) {

                      $query->andWhere(['like', 'products.name', $_GET['search_str']]);

                      $query->orWhere(['like', 'catalog.name', $_GET['search_str']]);

                      $query->orWhere(['like', 'products.art', $_GET['search_str']]);

                      $query->orWhere(['like', 'mod.art', $_GET['search_str']]);

                  }

                  $query->groupBy(['id']);

                  $countQuery = clone $query;

                  $pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize'=>18]);

                  $pages->forcePageParam = false;

                  $pages->pageSizeParam = false;

                  $products = $query->offset($pages->offset)

                      ->limit($pages->limit)

                      ->all();

  

              return $this->render('search', [

                  'pages'=>$pages,

                  'products'=>$products,

              ]);        

      }

      

      public function actionShow(){

  

          if(!$catalog = Catalog::find()->where(['translit'=>$_GET['translit_rubric']])->with('parent')->one())

                      throw new HttpException(404, 'Данной странице не существует!');

            

          

          if(!$product = Products::find()->where(['id'=>$_GET['id']])->one())

                      throw new HttpException(404, 'Данной странице не существует!');        

             

  

          return $this->render('show', [

                  'catalog'=>$catalog,

                  'product'=>$product,

              ]);

      }

      

      

    

      

  }