Commit 8e13c18cdd790e1fe099b636b47cd1cc47b88607

Authored by Yarik
1 parent 543f1653

Search

common/models/SearchForm.php
1 <?php 1 <?php
  2 +
2 namespace common\models; 3 namespace common\models;
3 4
  5 + use artbox\catalog\models\Category;
  6 + use artbox\catalog\models\Product;
4 use yii\base\Model; 7 use yii\base\Model;
  8 + use yii\db\ActiveQuery;
5 9
6 /** 10 /**
7 * Class SearchForm for performing search around site. 11 * Class SearchForm for performing search around site.
@@ -21,7 +25,15 @@ @@ -21,7 +25,15 @@
21 return [ 25 return [
22 [ 26 [
23 [ 'word' ], 27 [ 'word' ],
  28 + 'required',
  29 + 'message' => \Yii::t('app', 'Write text to search for.'),
  30 + ],
  31 + [
  32 + [ 'word' ],
24 'string', 33 'string',
  34 + 'min' => 3,
  35 + 'message' => \Yii::t('app', 'Write at least 3 symbols.'),
  36 + 'tooShort' => \Yii::t('app', 'Write at least 3 symbols.'),
25 ], 37 ],
26 ]; 38 ];
27 } 39 }
@@ -35,4 +47,43 @@ @@ -35,4 +47,43 @@
35 'word' => \Yii::t('app', 'Поиск по сайту'), 47 'word' => \Yii::t('app', 'Поиск по сайту'),
36 ]; 48 ];
37 } 49 }
  50 +
  51 + public function search()
  52 + {
  53 + return Product::find()
  54 + ->joinWith('lang')
  55 + ->joinWith('variants.lang')
  56 + ->andWhere(
  57 + [
  58 + 'product.status' => true,
  59 + 'variant.status' => true,
  60 + ]
  61 + )
  62 + ->andWhere(
  63 + [
  64 + 'like',
  65 + 'product_lang.title',
  66 + $this->word,
  67 + ]
  68 + )
  69 + ->orWhere(
  70 + [
  71 + 'like',
  72 + 'variant_lang.title',
  73 + $this->word,
  74 + ]
  75 + )
  76 + ->orWhere([ 'variant.sku' => $this->word ])
  77 + ->groupBy('product.id');
  78 + }
  79 +
  80 + public function searchCategory(Category $category, ActiveQuery $query)
  81 + {
  82 + return $query->joinWith('productToCategories')
  83 + ->andWhere(
  84 + [
  85 + 'product_to_category.category_id' => $category->id,
  86 + ]
  87 + );
  88 + }
38 } 89 }
39 \ No newline at end of file 90 \ No newline at end of file
frontend/controllers/SearchController.php 0 → 100644
  1 +<?php
  2 +
  3 + namespace frontend\controllers;
  4 +
  5 + use artbox\catalog\models\Category;
  6 + use common\models\SearchForm;
  7 + use yii\data\ActiveDataProvider;
  8 + use yii\web\Controller;
  9 + use yii\web\NotFoundHttpException;
  10 +
  11 + class SearchController extends Controller
  12 + {
  13 + public function actionIndex($word = '')
  14 + {
  15 + $searchForm = new SearchForm(
  16 + [
  17 + 'word' => $word,
  18 + ]
  19 + );
  20 + $dataProvider = null;
  21 + $categories = [];
  22 + if ($searchForm->validate()) {
  23 + $dataProvider = new ActiveDataProvider(
  24 + [
  25 + 'query' => $searchForm->search(),
  26 + ]
  27 + );
  28 + /**
  29 + * @var \artbox\catalog\models\queries\ProductQuery $categoryQuery
  30 + */
  31 + $categoryQuery = clone $dataProvider->query;
  32 + $categoryQuery->select([ 'product.id' ]);
  33 + $categories = Category::find()
  34 + ->active()
  35 + ->with('lang')
  36 + ->joinWith('productToCategories')
  37 + ->andWhere([ 'product_to_category.product_id' => $categoryQuery->column() ])
  38 + ->all();
  39 + }
  40 + return $this->render(
  41 + 'index',
  42 + [
  43 + 'searchForm' => $searchForm,
  44 + 'dataProvider' => $dataProvider,
  45 + 'categories' => $categories,
  46 + ]
  47 + );
  48 + }
  49 +
  50 + public function actionCategory($categoryId, $word = '')
  51 + {
  52 + /**
  53 + * @var Category $category
  54 + */
  55 + $category = Category::find()
  56 + ->active()
  57 + ->andWhere([ 'id' => $categoryId ])
  58 + ->with('lang')
  59 + ->one();
  60 + if (!$category) {
  61 + throw new NotFoundHttpException(\Yii::t('app', 'Category not found!'));
  62 + }
  63 + $searchForm = new SearchForm(
  64 + [
  65 + 'word' => $word,
  66 + ]
  67 + );
  68 + $dataProvider = null;
  69 + $categories = [];
  70 + if ($searchForm->validate()) {
  71 + /**
  72 + * @var \artbox\catalog\models\queries\ProductQuery $query
  73 + */
  74 + $query = $searchForm->search();
  75 + /**
  76 + * @var \artbox\catalog\models\queries\ProductQuery $categoryQuery
  77 + */
  78 + $categoryQuery = clone $query;
  79 + $dataProvider = new ActiveDataProvider(
  80 + [
  81 + 'query' => $searchForm->searchCategory($category, $query),
  82 + ]
  83 + );
  84 + $categoryQuery->select([ 'product.id' ]);
  85 + $categories = Category::find()
  86 + ->active()
  87 + ->joinWith('productToCategories')
  88 + ->andWhere([ 'product_to_category.product_id' => $categoryQuery->column() ])
  89 + ->all();
  90 + }
  91 + return $this->render(
  92 + 'category',
  93 + [
  94 + 'searchForm' => $searchForm,
  95 + 'dataProvider' => $dataProvider,
  96 + 'categories' => $categories,
  97 + 'category' => $category,
  98 + ]
  99 + );
  100 + }
  101 + }
0 \ No newline at end of file 102 \ No newline at end of file
frontend/controllers/SiteController.php
@@ -77,17 +77,17 @@ @@ -77,17 +77,17 @@
77 ->all(); 77 ->all();
78 $topItems = Product::find() 78 $topItems = Product::find()
79 ->with('lang', 'image', 'variants') 79 ->with('lang', 'image', 'variants')
80 - ->where('mask & 1 != 0') 80 + ->is('mask', 1)
81 ->limit(20) 81 ->limit(20)
82 ->all(); 82 ->all();
83 $newItems = Product::find() 83 $newItems = Product::find()
84 ->with('lang', 'image', 'variants') 84 ->with('lang', 'image', 'variants')
85 - ->where('mask & 2 != 0') 85 + ->is('mask', 2)
86 ->limit(20) 86 ->limit(20)
87 ->all(); 87 ->all();
88 $saleItems = Product::find() 88 $saleItems = Product::find()
89 ->with('lang', 'image', 'variants') 89 ->with('lang', 'image', 'variants')
90 - ->where('mask & 4 != 0') 90 + ->is('mask', 4)
91 ->limit(20) 91 ->limit(20)
92 ->all(); 92 ->all();
93 $productCount = Product::find() 93 $productCount = Product::find()
frontend/controllers/SpecialController.php
@@ -29,13 +29,13 @@ @@ -29,13 +29,13 @@
29 ->innerJoinWith('products', false) 29 ->innerJoinWith('products', false)
30 ->groupBy('category.id'); 30 ->groupBy('category.id');
31 if ($type === 'new') { 31 if ($type === 'new') {
32 - $query->where('product.mask & 2 != 0'); 32 + $query->is('product.mask', 2);
33 $productQuery = Product::find() 33 $productQuery = Product::find()
34 - ->where('product.mask & 2 != 0'); 34 + ->is('product.mask', 2);
35 } else { 35 } else {
36 - $query->where('product.mask & 4 != 0'); 36 + $query->is('product.mask', 4);
37 $productQuery = Product::find() 37 $productQuery = Product::find()
38 - ->where('product.mask & 4 != 0'); 38 + ->is('product.mask', 4);
39 } 39 }
40 $categories = $query->all(); 40 $categories = $query->all();
41 $dataProvider = new ActiveDataProvider( 41 $dataProvider = new ActiveDataProvider(
@@ -74,9 +74,9 @@ @@ -74,9 +74,9 @@
74 ] 74 ]
75 ); 75 );
76 if ($type === 'new') { 76 if ($type === 'new') {
77 - $query->andWhere('product.mask & 2 != 0'); 77 + $query->is('product.mask', 2);
78 } elseif ($type === 'sale') { 78 } elseif ($type === 'sale') {
79 - $query->andWhere('product.mask & 4 != 0'); 79 + $query->is('product.mask', 4);
80 } 80 }
81 $dataProvider = new ActiveDataProvider( 81 $dataProvider = new ActiveDataProvider(
82 [ 82 [
frontend/views/category/_product_item.php
@@ -53,7 +53,7 @@ @@ -53,7 +53,7 @@
53 ?></p> 53 ?></p>
54 <p class="buttons"> 54 <p class="buttons">
55 <?php 55 <?php
56 - if ($product->variants[ 0 ]->stock && $product->variants[ 0 ]->price) { 56 + if ($product->variants[ 0 ]->canBuy()) {
57 echo Html::a( 57 echo Html::a(
58 Html::tag( 58 Html::tag(
59 'i', 59 'i',
frontend/views/layouts/main.php
@@ -314,6 +314,7 @@ _________________________________________________________ --&gt; @@ -314,6 +314,7 @@ _________________________________________________________ --&gt;
314 ->textInput( 314 ->textInput(
315 [ 315 [
316 'placeholder' => $search->getAttributeLabel('word'), 316 'placeholder' => $search->getAttributeLabel('word'),
  317 + 'name' => 'word',
317 ] 318 ]
318 ); 319 );
319 echo Html::tag( 320 echo Html::tag(
frontend/views/product/view.php
@@ -174,7 +174,7 @@ @@ -174,7 +174,7 @@
174 <p class="price"> 174 <p class="price">
175 <span class="price-title">Цена:</span><?php echo $variant->price ? : 0; ?> грн&ensp; 175 <span class="price-title">Цена:</span><?php echo $variant->price ? : 0; ?> грн&ensp;
176 <?php 176 <?php
177 - if ($variant->stock && $variant->price) { 177 + if ($variant->canBuy()) {
178 echo Html::a( 178 echo Html::a(
179 Html::tag( 179 Html::tag(
180 'i', 180 'i',
@@ -211,22 +211,24 @@ @@ -211,22 +211,24 @@
211 </p> 211 </p>
212 <hr> 212 <hr>
213 <?php 213 <?php
214 - echo Html::a(  
215 - Html::icon(  
216 - 'phone', 214 + if ($variant->canBuy()) {
  215 + echo Html::a(
  216 + Html::icon(
  217 + 'phone',
  218 + [
  219 + 'prefix' => 'fa fa-',
  220 + ]
  221 + ) . \Yii::t('app', 'Купить в один клик'),
  222 + '#',
217 [ 223 [
218 - 'prefix' => 'fa fa-', 224 + 'data' => [
  225 + 'toggle' => 'modal',
  226 + 'target' => '#oneclick-modal',
  227 + ],
  228 + 'class' => 'btn btn-template-main',
219 ] 229 ]
220 - ) . \Yii::t('app', 'Купить в один клик'),  
221 - '#',  
222 - [  
223 - 'data' => [  
224 - 'toggle' => 'modal',  
225 - 'target' => '#oneclick-modal',  
226 - ],  
227 - 'class' => 'btn btn-template-main',  
228 - ]  
229 - ) 230 + );
  231 + }
230 ?> 232 ?>
231 <?php 233 <?php
232 /* 234 /*
frontend/views/search/category.php 0 → 100644
  1 +<?php
  2 + /**
  3 + * @var \yii\web\View $this
  4 + * @var \common\models\SearchForm $searchForm
  5 + * @var \yii\data\ActiveDataProvider $dataProvider
  6 + * @var \artbox\catalog\models\Category[] $categories
  7 + * @var \artbox\catalog\models\Category $category
  8 + */
  9 + use artbox\catalog\models\Product;
  10 + use yii\bootstrap\ActiveForm;
  11 + use yii\bootstrap\Html;
  12 + use yii\widgets\LinkPager;
  13 + use yii\widgets\ListView;
  14 +
  15 + $view = $this;
  16 +
  17 + $this->params[ 'breadcrumbs' ][] = [
  18 + 'label' => \Yii::t('app', 'Поиск'),
  19 + 'url' => [
  20 + 'index',
  21 + 'word' => $searchForm->word,
  22 + ],
  23 + ];
  24 + $this->params[ 'breadcrumbs' ][] = \Yii::t(
  25 + 'app',
  26 + 'Поиск по категории {category_name}',
  27 + [
  28 + 'category_name' => $category->lang->title,
  29 + ]
  30 + );
  31 +?>
  32 +<div id="content">
  33 + <div class="container">
  34 + <div class="row">
  35 + <div class="col-sm-offset-3 col-sm-6">
  36 + <?php
  37 + $form = ActiveForm::begin(
  38 + [
  39 + 'action' => [
  40 + 'category',
  41 + 'categoryId' => $category->id,
  42 + ],
  43 + 'method' => 'get',
  44 + ]
  45 + );
  46 + echo $form->field($searchForm, 'word')
  47 + ->textInput(
  48 + [
  49 + 'placeholder' => $searchForm->getAttributeLabel('word'),
  50 + 'name' => 'word',
  51 + ]
  52 + )
  53 + ->label(false);
  54 + $form::end();
  55 + ?>
  56 + </div>
  57 + </div>
  58 + <div class="row">
  59 + <div class="col-xs-3">
  60 + <div class="panel panel-default sidebar-menu">
  61 + <div class="panel-body">
  62 + <ul class="nav nav-pills nav-stacked category-menu">
  63 + <?php
  64 + foreach ($categories as $item) {
  65 + echo Html::tag(
  66 + 'li',
  67 + Html::a(
  68 + $item->lang->title,
  69 + [
  70 + 'category',
  71 + 'categoryId' => $item->id,
  72 + 'word' => $searchForm->word,
  73 + ],
  74 + [
  75 + 'class' => ( ( $category->id === $item->id ) ? 'active' : '' ),
  76 + ]
  77 + )
  78 + );
  79 + }
  80 + ?>
  81 + </ul>
  82 + </div>
  83 + </div>
  84 + </div>
  85 + <div class="col-xs-9">
  86 + <?php
  87 + if ($dataProvider) {
  88 + echo ListView::widget(
  89 + [
  90 + 'options' => [
  91 + 'class' => 'row products',
  92 + ],
  93 + 'itemOptions' => [
  94 + 'tag' => false,
  95 + ],
  96 + 'layout' => '{items}',
  97 + 'dataProvider' => $dataProvider,
  98 + 'itemView' => function ($model) use ($view) {
  99 + /**
  100 + * @var Product $model
  101 + */
  102 + return $view->render(
  103 + '@frontend/views/category/_product_item',
  104 + [
  105 + 'product' => $model,
  106 + ]
  107 + );
  108 + },
  109 + ]
  110 + );
  111 + echo Html::tag(
  112 + 'div',
  113 + LinkPager::widget(
  114 + [
  115 + 'pagination' => $dataProvider->pagination,
  116 + ]
  117 + ),
  118 + [
  119 + 'class' => 'pages',
  120 + ]
  121 + );
  122 + } else {
  123 + echo \Yii::t('app', 'Введите текст для поиска в строке выше.');
  124 + }
  125 + ?>
  126 + </div>
  127 + </div>
  128 + </div>
  129 +</div>
0 \ No newline at end of file 130 \ No newline at end of file
frontend/views/search/index.php 0 → 100644
  1 +<?php
  2 + /**
  3 + * @var \yii\web\View $this
  4 + * @var \common\models\SearchForm $searchForm
  5 + * @var \yii\data\ActiveDataProvider $dataProvider
  6 + * @var \artbox\catalog\models\Category[] $categories
  7 + */
  8 + use artbox\catalog\models\Product;
  9 + use yii\bootstrap\ActiveForm;
  10 + use yii\bootstrap\Html;
  11 + use yii\widgets\LinkPager;
  12 + use yii\widgets\ListView;
  13 +
  14 + $view = $this;
  15 +
  16 + $this->params[ 'breadcrumbs' ][] = \Yii::t('app', 'Поиск');
  17 +?>
  18 +<div id="content">
  19 + <div class="container">
  20 + <div class="row">
  21 + <div class="col-sm-offset-3 col-sm-6">
  22 + <?php
  23 + $form = ActiveForm::begin(
  24 + [
  25 + 'action' => [ 'index' ],
  26 + 'method' => 'get',
  27 + ]
  28 + );
  29 + echo $form->field($searchForm, 'word')
  30 + ->textInput(
  31 + [
  32 + 'placeholder' => $searchForm->getAttributeLabel('word'),
  33 + 'name' => 'word',
  34 + ]
  35 + )
  36 + ->label(false);
  37 + $form::end();
  38 + ?>
  39 + </div>
  40 + </div>
  41 + <div class="row">
  42 + <div class="col-xs-3">
  43 + <div class="panel panel-default sidebar-menu">
  44 + <div class="panel-body">
  45 + <ul class="nav nav-pills nav-stacked category-menu">
  46 + <?php
  47 + foreach ($categories as $category) {
  48 + echo Html::tag(
  49 + 'li',
  50 + Html::a(
  51 + $category->lang->title,
  52 + [
  53 + 'category',
  54 + 'categoryId' => $category->id,
  55 + 'word' => $searchForm->word,
  56 + ]
  57 + )
  58 + );
  59 + }
  60 + ?>
  61 + </ul>
  62 + </div>
  63 + </div>
  64 + </div>
  65 + <div class="col-xs-9">
  66 + <?php
  67 + if ($dataProvider) {
  68 + echo ListView::widget(
  69 + [
  70 + 'options' => [
  71 + 'class' => 'row products',
  72 + ],
  73 + 'itemOptions' => [
  74 + 'tag' => false,
  75 + ],
  76 + 'layout' => '{items}',
  77 + 'dataProvider' => $dataProvider,
  78 + 'itemView' => function ($model) use ($view) {
  79 + /**
  80 + * @var Product $model
  81 + */
  82 + return $view->render(
  83 + '@frontend/views/category/_product_item',
  84 + [
  85 + 'product' => $model,
  86 + ]
  87 + );
  88 + },
  89 + ]
  90 + );
  91 + echo Html::tag(
  92 + 'div',
  93 + LinkPager::widget(
  94 + [
  95 + 'pagination' => $dataProvider->pagination,
  96 + ]
  97 + ),
  98 + [
  99 + 'class' => 'pages',
  100 + ]
  101 + );
  102 + } else {
  103 + echo \Yii::t('app', 'Введите текст для поиска в строке выше.');
  104 + }
  105 + ?>
  106 + </div>
  107 + </div>
  108 + </div>
  109 +</div>
0 \ No newline at end of file 110 \ No newline at end of file
frontend/views/site/_slider_product.php
@@ -53,7 +53,7 @@ @@ -53,7 +53,7 @@
53 ?></p> 53 ?></p>
54 <p class="buttons"> 54 <p class="buttons">
55 <?php 55 <?php
56 - if ($product->variants[ 0 ]->stock && $product->variants[ 0 ]->price) { 56 + if ($product->variants[ 0 ]->canBuy()) {
57 echo Html::a( 57 echo Html::a(
58 Html::tag( 58 Html::tag(
59 'i', 59 'i',
frontend/web/css/style.css
@@ -4653,4 +4653,14 @@ a.list-group-item.active &gt; .badge, @@ -4653,4 +4653,14 @@ a.list-group-item.active &gt; .badge,
4653 .alert-cart.active{ 4653 .alert-cart.active{
4654 top: 30px; 4654 top: 30px;
4655 box-shadow: 0px 0px 25px rgba(0, 0, 0, 0.3); 4655 box-shadow: 0px 0px 25px rgba(0, 0, 0, 0.3);
  4656 +}
  4657 +
  4658 +.panel.sidebar-menu ul.nav.category-menu li a.active {
  4659 + background-color: #fd6721;
  4660 + color: white;
  4661 +}
  4662 +
  4663 +.panel.sidebar-menu ul.nav.category-menu li a.active:hover {
  4664 + background-color: #fd9131;
  4665 + color: white;
4656 } 4666 }
4657 \ No newline at end of file 4667 \ No newline at end of file