diff --git a/common/models/SearchForm.php b/common/models/SearchForm.php index 4a38f33..9aee388 100755 --- a/common/models/SearchForm.php +++ b/common/models/SearchForm.php @@ -1,7 +1,11 @@ \Yii::t('app', 'Write text to search for.'), + ], + [ + [ 'word' ], 'string', + 'min' => 3, + 'message' => \Yii::t('app', 'Write at least 3 symbols.'), + 'tooShort' => \Yii::t('app', 'Write at least 3 symbols.'), ], ]; } @@ -35,4 +47,43 @@ 'word' => \Yii::t('app', 'Поиск по сайту'), ]; } + + public function search() + { + return Product::find() + ->joinWith('lang') + ->joinWith('variants.lang') + ->andWhere( + [ + 'product.status' => true, + 'variant.status' => true, + ] + ) + ->andWhere( + [ + 'like', + 'product_lang.title', + $this->word, + ] + ) + ->orWhere( + [ + 'like', + 'variant_lang.title', + $this->word, + ] + ) + ->orWhere([ 'variant.sku' => $this->word ]) + ->groupBy('product.id'); + } + + public function searchCategory(Category $category, ActiveQuery $query) + { + return $query->joinWith('productToCategories') + ->andWhere( + [ + 'product_to_category.category_id' => $category->id, + ] + ); + } } \ No newline at end of file diff --git a/frontend/controllers/SearchController.php b/frontend/controllers/SearchController.php new file mode 100644 index 0000000..9efaa67 --- /dev/null +++ b/frontend/controllers/SearchController.php @@ -0,0 +1,101 @@ + $word, + ] + ); + $dataProvider = null; + $categories = []; + if ($searchForm->validate()) { + $dataProvider = new ActiveDataProvider( + [ + 'query' => $searchForm->search(), + ] + ); + /** + * @var \artbox\catalog\models\queries\ProductQuery $categoryQuery + */ + $categoryQuery = clone $dataProvider->query; + $categoryQuery->select([ 'product.id' ]); + $categories = Category::find() + ->active() + ->with('lang') + ->joinWith('productToCategories') + ->andWhere([ 'product_to_category.product_id' => $categoryQuery->column() ]) + ->all(); + } + return $this->render( + 'index', + [ + 'searchForm' => $searchForm, + 'dataProvider' => $dataProvider, + 'categories' => $categories, + ] + ); + } + + public function actionCategory($categoryId, $word = '') + { + /** + * @var Category $category + */ + $category = Category::find() + ->active() + ->andWhere([ 'id' => $categoryId ]) + ->with('lang') + ->one(); + if (!$category) { + throw new NotFoundHttpException(\Yii::t('app', 'Category not found!')); + } + $searchForm = new SearchForm( + [ + 'word' => $word, + ] + ); + $dataProvider = null; + $categories = []; + if ($searchForm->validate()) { + /** + * @var \artbox\catalog\models\queries\ProductQuery $query + */ + $query = $searchForm->search(); + /** + * @var \artbox\catalog\models\queries\ProductQuery $categoryQuery + */ + $categoryQuery = clone $query; + $dataProvider = new ActiveDataProvider( + [ + 'query' => $searchForm->searchCategory($category, $query), + ] + ); + $categoryQuery->select([ 'product.id' ]); + $categories = Category::find() + ->active() + ->joinWith('productToCategories') + ->andWhere([ 'product_to_category.product_id' => $categoryQuery->column() ]) + ->all(); + } + return $this->render( + 'category', + [ + 'searchForm' => $searchForm, + 'dataProvider' => $dataProvider, + 'categories' => $categories, + 'category' => $category, + ] + ); + } + } \ No newline at end of file diff --git a/frontend/controllers/SiteController.php b/frontend/controllers/SiteController.php index 6918873..fecc6f3 100755 --- a/frontend/controllers/SiteController.php +++ b/frontend/controllers/SiteController.php @@ -77,17 +77,17 @@ ->all(); $topItems = Product::find() ->with('lang', 'image', 'variants') - ->where('mask & 1 != 0') + ->is('mask', 1) ->limit(20) ->all(); $newItems = Product::find() ->with('lang', 'image', 'variants') - ->where('mask & 2 != 0') + ->is('mask', 2) ->limit(20) ->all(); $saleItems = Product::find() ->with('lang', 'image', 'variants') - ->where('mask & 4 != 0') + ->is('mask', 4) ->limit(20) ->all(); $productCount = Product::find() diff --git a/frontend/controllers/SpecialController.php b/frontend/controllers/SpecialController.php index 6846a84..ed3f732 100755 --- a/frontend/controllers/SpecialController.php +++ b/frontend/controllers/SpecialController.php @@ -29,13 +29,13 @@ ->innerJoinWith('products', false) ->groupBy('category.id'); if ($type === 'new') { - $query->where('product.mask & 2 != 0'); + $query->is('product.mask', 2); $productQuery = Product::find() - ->where('product.mask & 2 != 0'); + ->is('product.mask', 2); } else { - $query->where('product.mask & 4 != 0'); + $query->is('product.mask', 4); $productQuery = Product::find() - ->where('product.mask & 4 != 0'); + ->is('product.mask', 4); } $categories = $query->all(); $dataProvider = new ActiveDataProvider( @@ -74,9 +74,9 @@ ] ); if ($type === 'new') { - $query->andWhere('product.mask & 2 != 0'); + $query->is('product.mask', 2); } elseif ($type === 'sale') { - $query->andWhere('product.mask & 4 != 0'); + $query->is('product.mask', 4); } $dataProvider = new ActiveDataProvider( [ diff --git a/frontend/views/category/_product_item.php b/frontend/views/category/_product_item.php index 3dab0bc..550674a 100755 --- a/frontend/views/category/_product_item.php +++ b/frontend/views/category/_product_item.php @@ -53,7 +53,7 @@ ?>
Цена:price ? : 0; ?> грн stock && $variant->price) { + if ($variant->canBuy()) { echo Html::a( Html::tag( 'i', @@ -211,22 +211,24 @@