CatalogController.php 3.83 KB
<?php

namespace frontend\controllers;

use common\modules\product\models\Brand;
use common\modules\product\models\Category;
use common\modules\product\models\CategorySearch;
use common\modules\product\models\Product;
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;
use yii\web\HttpException;

class CatalogController extends \yii\web\Controller
{
    public function actionCategory($alias)
    {
        $category = CategorySearch::findByAlias($alias);
        if (empty($category->category_id)) {
            throw new HttpException(404 ,'Page not found');
        }
        if ($category->depth < 2) {
            return $this->render(
                'categories',
                [
                    'category' => $category
                ]
            );
        } else {
//            $products = $category->getRelations('product_categories')->all();

            $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'
                ]);
            $pages = new Pagination(['totalCount' => $query->count(), 'pageSize' => $per_page]);
            $count = $query->count();

//            $priceQuery = clone $query;
            $priceMin = $query->min(ProductVariant::tableName() .'.price');
            $priceMax = $query->max(ProductVariant::tableName() .'.price');

            $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();

            return $this->render(
                'products',
                [
                    'category' => $category,
                    'products' => $products,
                    'product_count' => $count,
                    'sort' => $sort,
                    'pages' => $pages,
                    'per_page' => $per_page,
                    'priceMin' => $priceMin,
                    'priceMax' => $priceMax,
                    'brands' => $brands,
                    'brands_count' => $brands_count,
                ]
            );
        }
    }

    public function actionProduct($alias)
    {
        $product = ProductSearch::findByAlias($alias);
        if (empty($product->product_id)) {
            throw new HttpException(404 ,'Page not found');
        }
        return $this->render('product');
    }

    public function actionBrands()
    {
        return 'actionBrands';
    }

    public function actionBrand($alias)
    {
        return 'actionBrand:'. $alias;
    }

}