AjaxController.php 3 KB
<?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();
    }
}