AjaxController.php
3 KB
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
<?php
namespace frontend\controllers;
use common\models\Feedback;
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;
use yii\web\Controller;
use \common\modules\product\widgets\specialProducts;
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'];
    }
    public function actionNew(){
        return specialProducts::widget(['type' => 'new']);
    }
    public function actionTop(){
        return specialProducts::widget(['type' => 'top']);
    }
    public function actionProm(){
        return specialProducts::widget(['type' => 'promo']);
    }
    public function actionCount(){
        $filter = \Yii::$app->request->get('info');
        $id = \Yii::$app->request->get('category');
        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($id);
            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();
    }
}