From 8e13c18cdd790e1fe099b636b47cd1cc47b88607 Mon Sep 17 00:00:00 2001 From: Yarik Date: Thu, 11 May 2017 18:33:12 +0300 Subject: [PATCH] Search --- common/models/SearchForm.php | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ frontend/controllers/SearchController.php | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ frontend/controllers/SiteController.php | 6 +++--- frontend/controllers/SpecialController.php | 12 ++++++------ frontend/views/category/_product_item.php | 2 +- frontend/views/layouts/main.php | 1 + frontend/views/product/view.php | 32 +++++++++++++++++--------------- frontend/views/search/category.php | 129 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ frontend/views/search/index.php | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ frontend/views/site/_slider_product.php | 2 +- frontend/web/css/style.css | 10 ++++++++++ 11 files changed, 429 insertions(+), 26 deletions(-) create mode 100644 frontend/controllers/SearchController.php create mode 100644 frontend/views/search/category.php create mode 100644 frontend/views/search/index.php 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 @@ ?>

variants[ 0 ]->stock && $product->variants[ 0 ]->price) { + if ($product->variants[ 0 ]->canBuy()) { echo Html::a( Html::tag( 'i', diff --git a/frontend/views/layouts/main.php b/frontend/views/layouts/main.php index d23f2ed..f39e541 100755 --- a/frontend/views/layouts/main.php +++ b/frontend/views/layouts/main.php @@ -314,6 +314,7 @@ _________________________________________________________ --> ->textInput( [ 'placeholder' => $search->getAttributeLabel('word'), + 'name' => 'word', ] ); echo Html::tag( diff --git a/frontend/views/product/view.php b/frontend/views/product/view.php index 71eeb06..c7dc777 100755 --- a/frontend/views/product/view.php +++ b/frontend/views/product/view.php @@ -174,7 +174,7 @@

Цена:price ? : 0; ?> грн  stock && $variant->price) { + if ($variant->canBuy()) { echo Html::a( Html::tag( 'i', @@ -211,22 +211,24 @@


canBuy()) { + echo Html::a( + Html::icon( + 'phone', + [ + 'prefix' => 'fa fa-', + ] + ) . \Yii::t('app', 'Купить в один клик'), + '#', [ - 'prefix' => 'fa fa-', + 'data' => [ + 'toggle' => 'modal', + 'target' => '#oneclick-modal', + ], + 'class' => 'btn btn-template-main', ] - ) . \Yii::t('app', 'Купить в один клик'), - '#', - [ - 'data' => [ - 'toggle' => 'modal', - 'target' => '#oneclick-modal', - ], - 'class' => 'btn btn-template-main', - ] - ) + ); + } ?> params[ 'breadcrumbs' ][] = [ + 'label' => \Yii::t('app', 'Поиск'), + 'url' => [ + 'index', + 'word' => $searchForm->word, + ], + ]; + $this->params[ 'breadcrumbs' ][] = \Yii::t( + 'app', + 'Поиск по категории {category_name}', + [ + 'category_name' => $category->lang->title, + ] + ); +?> +
+
+
+
+ [ + 'category', + 'categoryId' => $category->id, + ], + 'method' => 'get', + ] + ); + echo $form->field($searchForm, 'word') + ->textInput( + [ + 'placeholder' => $searchForm->getAttributeLabel('word'), + 'name' => 'word', + ] + ) + ->label(false); + $form::end(); + ?> +
+
+
+
+ +
+
+ [ + 'class' => 'row products', + ], + 'itemOptions' => [ + 'tag' => false, + ], + 'layout' => '{items}', + 'dataProvider' => $dataProvider, + 'itemView' => function ($model) use ($view) { + /** + * @var Product $model + */ + return $view->render( + '@frontend/views/category/_product_item', + [ + 'product' => $model, + ] + ); + }, + ] + ); + echo Html::tag( + 'div', + LinkPager::widget( + [ + 'pagination' => $dataProvider->pagination, + ] + ), + [ + 'class' => 'pages', + ] + ); + } else { + echo \Yii::t('app', 'Введите текст для поиска в строке выше.'); + } + ?> +
+
+
+
\ No newline at end of file diff --git a/frontend/views/search/index.php b/frontend/views/search/index.php new file mode 100644 index 0000000..260a883 --- /dev/null +++ b/frontend/views/search/index.php @@ -0,0 +1,109 @@ +params[ 'breadcrumbs' ][] = \Yii::t('app', 'Поиск'); +?> +
+
+
+
+ [ 'index' ], + 'method' => 'get', + ] + ); + echo $form->field($searchForm, 'word') + ->textInput( + [ + 'placeholder' => $searchForm->getAttributeLabel('word'), + 'name' => 'word', + ] + ) + ->label(false); + $form::end(); + ?> +
+
+
+
+ +
+
+ [ + 'class' => 'row products', + ], + 'itemOptions' => [ + 'tag' => false, + ], + 'layout' => '{items}', + 'dataProvider' => $dataProvider, + 'itemView' => function ($model) use ($view) { + /** + * @var Product $model + */ + return $view->render( + '@frontend/views/category/_product_item', + [ + 'product' => $model, + ] + ); + }, + ] + ); + echo Html::tag( + 'div', + LinkPager::widget( + [ + 'pagination' => $dataProvider->pagination, + ] + ), + [ + 'class' => 'pages', + ] + ); + } else { + echo \Yii::t('app', 'Введите текст для поиска в строке выше.'); + } + ?> +
+
+
+
\ No newline at end of file diff --git a/frontend/views/site/_slider_product.php b/frontend/views/site/_slider_product.php index 0080230..a7a87c5 100755 --- a/frontend/views/site/_slider_product.php +++ b/frontend/views/site/_slider_product.php @@ -53,7 +53,7 @@ ?>

variants[ 0 ]->stock && $product->variants[ 0 ]->price) { + if ($product->variants[ 0 ]->canBuy()) { echo Html::a( Html::tag( 'i', diff --git a/frontend/web/css/style.css b/frontend/web/css/style.css index f4c1cbd..de27a9c 100755 --- a/frontend/web/css/style.css +++ b/frontend/web/css/style.css @@ -4653,4 +4653,14 @@ a.list-group-item.active > .badge, .alert-cart.active{ top: 30px; box-shadow: 0px 0px 25px rgba(0, 0, 0, 0.3); +} + +.panel.sidebar-menu ul.nav.category-menu li a.active { + background-color: #fd6721; + color: white; +} + +.panel.sidebar-menu ul.nav.category-menu li a.active:hover { + background-color: #fd9131; + color: white; } \ No newline at end of file -- libgit2 0.21.4