Blame view

frontend/controllers/AjaxController.php 2.95 KB
7f87d6f9   Yarik   Modals.
1
2
3
4
5
  <?php
  
  namespace frontend\controllers;
  
  use common\models\Feedback;
a95ce595   Administrator   big commti
6
7
8
9
10
  use common\modules\product\helpers\ProductHelper;
  use common\modules\product\models\Brand;
  use common\modules\product\models\Category;
  use common\modules\product\models\ProductVariant;
  use yii\db\ActiveQuery;
7f87d6f9   Yarik   Modals.
11
  use yii\web\Controller;
10b789ee   Administrator   big commti
12
  use \common\modules\product\widgets\specialProducts;
7f87d6f9   Yarik   Modals.
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
  class AjaxController extends Controller
  {
      public function actionFeedback() {
          $response = \Yii::$app->response;
          $response->format = $response::FORMAT_JSON;
          $request = \Yii::$app->request;
          $model = new Feedback([
              'scenario' => Feedback::SCENARIO_FEEDBACK,
          ]);
          if($model->load($request->post())) {
              if($model->validate()) {
                  $model->save(false);
                  return [
                      'result' => 'Запрос успешно отправлен.',
                  ];
              } else {
                  return [
                      'error' => 'Validation failed',
                      'result' => [
                          'errors' => $model->getFirstErrors(),
                      ],
                  ];
              }
          }
          $response->statusCode = 400;
          $response->statusText = 'Empty request';
          return ['error' => 'Empty request'];
      }
10b789ee   Administrator   big commti
41
42
43
44
45
46
47
48
49
50
51
52
53
  
  
      public function actionNew(){
          return specialProducts::widget(['type' => 'new']);
      }
  
      public function actionTop(){
          return specialProducts::widget(['type' => 'top']);
      }
  
      public function actionProm(){
          return specialProducts::widget(['type' => 'promo']);
      }
a95ce595   Administrator   big commti
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
  
  
      public function actionTest(){
          $filter = \Yii::$app->request->get('info');
  
  
          if(!empty( $filter[ 'brands' ] )) {
              $brands = Brand::find()
                  ->select('brand_id')
                  ->where([
                      'in',
                      'alias',
                      $filter[ 'brands' ],
                  ])
                  ->all();
              $filter[ 'brands' ] = [ ];
              foreach($brands as $brand) {
                  $filter[ 'brands' ][] = $brand->brand_id;
              }
          }
  
          if(!empty($filter)){
              $category = Category::findOne(176);
              return $this->findItem($category,$filter);
          } else {
              return 'test';
          }
  
      }
  
      public function getSearchQuery($category = null, $params = []) {
  
  
          /** @var ActiveQuery $query */
          /**@var Category $category **/
          $query = $category->getProducts();
  
  
          $query->select(['product.*']);
          $query->joinWith(['enabledVariants','brand','options', 'category']);
  
          $query->groupBy(['product.product_id', 'product_variant.price']);
  
          ProductHelper::_setQueryParams($query, $params);
  
          $query->andWhere(['!=', ProductVariant::tableName() .'.status', 1]);
  
  
  
          return $query;
  
      }
  
  
  
      public function findItem($category,$params){
          return $this->getSearchQuery($category,$params)->count();
      }
7f87d6f9   Yarik   Modals.
112
  }