request->get('category'); $filter = Yii::$app->request->get('filter', []); $word = trim(Yii::$app->request->get('word', '')); if (empty($category->category_id) && empty($word)) { return $this->render( 'catalog' ); } if (!empty($word)) { $params = []; $params['keywords'] = explode(' ', preg_replace("|[\s,.!:&?~();-]|i", " ", $word)); foreach($params['keywords'] as $i => &$keyword) { $keyword = trim($keyword); if (empty($keyword)) { unset($params['keywords'][$i]); } } $productModel = new ProductFrontendSearch(); $productProvider = $productModel->search($category, $params); $categoriesQuery = Category::find() ->innerJoin(ProductCategory::tableName(), ProductCategory::tableName() .'.category_id = '. Category::tableName() .'.category_id') ->innerJoin(Product::tableName(), Product::tableName() .'.product_id = '. ProductCategory::tableName() .'.product_id') ->innerJoin(ProductVariant::tableName(), ProductVariant::tableName() .'.product_id = '. ProductCategory::tableName() .'.product_id'); foreach ($params['keywords'] as $keyword) { $categoriesQuery->andWhere(['ilike', 'product.name', $keyword]); } $categoriesQuery->andWhere(['!=', ProductVariant::tableName() .'.stock', 0]); $categories = $categoriesQuery->all(); return $this->render( 'search', [ 'keywords' => $params['keywords'], 'category' => $category, 'productModel' => $productModel, 'productProvider' => $productProvider, 'categories' => $categories, ] ); } else { $params = []; if ( !empty($filter['brands']) ) { $brands = Brand::find()->select('brand_id')->where(['in', 'alias', $filter['brands']])->all(); $params['brands'] = []; foreach ($brands as $brand) { $params['brands'][] = $brand->brand_id; } } if ( !empty($filter['special']) ) { if (!is_array($filter['special'])) { $filter['special'] = [$filter['special']]; } if (in_array('new', $filter['special'])) { $params['special']['is_new'] = true; } if (in_array('top', $filter['special'])) { $params['special']['is_top'] = true; } if (in_array('promo', $filter['special'])) { $params['special']['akciya'] = true; } } if ( !empty($filter['options']) ) { $params['options'] = $filter['options']; } if ( !empty($filter['prices']) ) { $params['prices'] = $filter['prices']; } $productModel = new ProductFrontendSearch(); $productProvider = $productModel->search($category, $params); $brandModel = new BrandSearch(); $brands = $brandModel->getBrands($category, $params)->all(); $optionsQuery = $productModel->optionsForCategory($category, $params); $groups = []; foreach ($optionsQuery->all() as $option) { if (!isset($groups[$option->tax_group_id])) { $groups[$option->tax_group_id] = $option->taxGroup; $groups[$option->tax_group_id]->_options = []; } $groups[$option->tax_group_id]->_options[] = $option; } foreach($groups as $i => $group) { if (empty($group->_options)) unset($groups[$i]); } $priceLimits = $productModel->priceLimits($category, $params); return $this->render( 'products', [ 'category' => $category, 'brandModel' => $brandModel, 'brands' => $brands, 'filter' => $filter, 'productModel' => $productModel, 'productProvider' => $productProvider, 'groups' => $groups, 'priceLimits' => $priceLimits, ] ); } } public function actionProduct() { /** @var Product $product */ $product = Yii::$app->request->get('product'); if(!$product->enabledVariant) { throw new HttpException(404, 'Товар не найден'); } $groups = []; foreach($product->category->getTaxGroups()->all() as $_group) { $groups[$_group->tax_group_id] = $_group; } foreach ($product->options as $option) { $groups[$option->tax_group_id]->_options[] = $option; } foreach($groups as $i => $group) { if (empty($group->_options)) unset($groups[$i]); } $category = $product->category; ProductHelper::addLastProsucts($product->product_id); return $this->render('product', [ 'product' => $product, 'category' => $category, 'properties' => $groups, ]); } public function actionBrands() { $dataProvider = new ActiveDataProvider([ 'query' => Brand::find()->joinWith('brandName')->orderBy('brand_name.value'), 'pagination' => [ 'pageSize' => -1, ] ]); return $this->render('brands', [ 'dataProvider' => $dataProvider, ]); } public function actionBrand($brand) { $filter = Yii::$app->request->get('filter', []); $params = [ 'brand_id' => $brand->brand_id, ]; if ( !empty($filter['prices']) ) { $params['prices'] = $filter['prices']; } if ( !empty($filter['special']) ) { if (!is_array($filter['special'])) { $filter['special'] = [$filter['special']]; } if (in_array('new', $filter['special'])) { $params['special']['is_new'] = true; } if (in_array('top', $filter['special'])) { $params['special']['is_top'] = true; } if (in_array('promo', $filter['special'])) { $params['special']['akciya'] = true; } } $productModel = new ProductFrontendSearch(); $productProvider = $productModel->search(null, $params); $priceLimits = $productModel->priceLimits(null, $params); return $this->render('brand', [ 'productModel' => $productModel, 'productProvider' => $productProvider, 'brand' => $brand, 'priceLimits' => $priceLimits, 'filter' => $filter, ]); } }