Commit 2309b955618799e9c7c4ef897375d268e1fd3a8b

Authored by Timur Kastemirov
1 parent 58728e08

blog categories & search in blog

backend/config/main.php
... ... @@ -24,6 +24,8 @@
24 24 'seo' => 'artbox\core\controllers\SeoController',
25 25 'feedback' => 'artbox\core\controllers\FeedbackController',
26 26 'blog' => 'artbox\weblog\controllers\ArticleController',
  27 + 'blog-category' => 'artbox\weblog\controllers\CategoryController',
  28 + 'blog-tag' => 'artbox\weblog\controllers\TagController',
27 29 ],
28 30 'components' => [
29 31 'assetManager' => [
... ...
backend/views/layouts/menu_items.php
... ... @@ -74,6 +74,28 @@
74 74 ],
75 75 ],
76 76 [
  77 + 'label' => \Yii::t('core', 'Блог'),
  78 + 'url' => ['#'],
  79 + 'icon' => 'book',
  80 + 'items' => [
  81 + [
  82 + 'label' => \Yii::t('core', 'Статьи'),
  83 + 'url' => ['/blog/index'],
  84 + 'icon' => 'list-alt',
  85 + ],
  86 + [
  87 + 'label' => \Yii::t('core', 'Категории'),
  88 + 'url' => ['/blog-category/index'],
  89 + 'icon' => 'ticket',
  90 + ],
  91 + [
  92 + 'label' => \Yii::t('core', 'Тэги'),
  93 + 'url' => ['/blog-tag/index'],
  94 + 'icon' => 'tags',
  95 + ]
  96 + ],
  97 + ],
  98 + [
77 99 'label' => \Yii::t('core', 'Image manager'),
78 100 'url' => [ '/imagemanager' ],
79 101 'icon' => 'image',
... ... @@ -81,17 +103,12 @@
81 103 [
82 104 'label' => \Yii::t('core', 'Slider'),
83 105 'url' => ['/slider/index'],
84   - 'icon' => 'bolt',
  106 + 'icon' => 'exchange',
85 107 ],
86 108 [
87 109 'label' => \Yii::t('core', 'Наши Объекты'),
88 110 'url' => ['/objectkb/index'],
89   - 'icon' => 'bolt',
90   - ],
91   - [
92   - 'label' => \Yii::t('core', 'Блог'),
93   - 'url' => ['/blog/index'],
94   - 'icon' => 'bolt',
  111 + 'icon' => 'sun-o',
95 112 ],
96 113 ]
97 114 );
98 115 \ No newline at end of file
... ...
common/messages/ru/app.php
... ... @@ -124,4 +124,5 @@ return [
124 124 "Show on home page" => "Показать на главной странице",
125 125 'Go to contact page' => 'На страницу контактов',
126 126 'Share' => 'Поделиться',
  127 + 'All' => "Все",
127 128 ];
128 129 \ No newline at end of file
... ...
frontend/config/main.php
... ... @@ -52,8 +52,8 @@ return [
52 52 'processRoutes' => [
53 53 'object/view',
54 54 'blog/article',
55   -// 'blog/tag',
56   -// 'blog/category',
  55 + 'blog/tag',
  56 + 'blog/category',
57 57 ],
58 58 ],
59 59 ],
... ...
frontend/controllers/BlogController.php
... ... @@ -2,6 +2,8 @@
2 2 namespace frontend\controllers;
3 3  
4 4 use yii\data\ActiveDataProvider;
  5 + use yii\helpers\Html;
  6 + use yii\helpers\Url;
5 7 use yii\web\Controller;
6 8 use artbox\weblog\models\Article;
7 9 use yii\web\NotFoundHttpException;
... ... @@ -16,16 +18,18 @@
16 18 {
17 19 public function actionIndex()
18 20 {
  21 +
  22 + $query = Article::find()
  23 + ->where(
  24 + [
  25 + 'status' => true,
  26 + ]
  27 + )
  28 + ->orderBy("sort");
19 29  
20 30 $dataProvider = new ActiveDataProvider(
21 31 [
22   - 'query' => Article::find()
23   - ->where(
24   - [
25   - 'status' => true,
26   - ]
27   - )
28   - ->orderBy("sort"),
  32 + 'query' => $query,
29 33 'pagination' => [
30 34 'pageSize' => 5,
31 35 ],
... ... @@ -77,4 +81,80 @@
77 81 }
78 82 }
79 83  
  84 + public function actionSearch(){
  85 + if( \Yii::$app->request->isPost ){
  86 +
  87 + $req = \Yii::$app->request;
  88 + if (!empty($req->post("title"))){
  89 + $title = Html::encode($req->post("title"));
  90 + $query = Article::find()
  91 + ->joinWith("lang")
  92 + ->where(
  93 + [
  94 + 'status' => true,
  95 + ]
  96 + )
  97 + ->andWhere(
  98 + [
  99 + "like", "lower(title)", $title
  100 + ]
  101 + )
  102 + ->orderBy("sort");
  103 +
  104 + $dataProvider = new ActiveDataProvider(
  105 + [
  106 + 'query' => $query,
  107 + 'pagination' => [
  108 + 'pageSize' => 5,
  109 + ],
  110 + ]
  111 + );
  112 +
  113 + return $this->render(
  114 + 'index',
  115 + [
  116 + 'dataProvider' => $dataProvider,
  117 + ]
  118 + );
  119 + }
  120 + }
  121 +
  122 + return $this->redirect(Url::toRoute(['blog/index']));
  123 +
  124 + }
  125 +
  126 + public function actionCategory($id){
  127 +
  128 + $query = Article::find()
  129 + ->joinWith("categories.lang")
  130 + ->where(
  131 + [
  132 + 'blog_article.status' => true,
  133 + ]
  134 + )
  135 + ->andWhere(
  136 + [
  137 + "blog_category.id" => $id,
  138 + "blog_category.status" => true,
  139 + ]
  140 + )
  141 + ->orderBy("sort");
  142 +
  143 + $dataProvider = new ActiveDataProvider(
  144 + [
  145 + 'query' => $query,
  146 + 'pagination' => [
  147 + 'pageSize' => 5,
  148 + ],
  149 + ]
  150 + );
  151 +
  152 + return $this->render(
  153 + 'index',
  154 + [
  155 + 'dataProvider' => $dataProvider,
  156 + ]
  157 + );
  158 + }
  159 +
80 160 }
81 161 \ No newline at end of file
... ...
frontend/views/blog/index.php
... ... @@ -45,8 +45,14 @@
45 45  
46 46 <div class="col-md-3">
47 47  
48   - <?=SearchWidget::widget()?>
  48 + <?=SearchWidget::widget(
  49 + [
  50 + 'route' => 'blog/search'
  51 + ]
  52 + )?>
  53 +
49 54 <?=CategoryWidget::widget()?>
  55 +
50 56 <?=TagWidget::widget()?>
51 57  
52 58 </div>
... ...
frontend/widgets/CategoryWidget.php
... ... @@ -7,13 +7,31 @@
7 7 */
8 8  
9 9 use yii\base\Widget;
  10 + use artbox\weblog\models\Category;
10 11  
11 12 class CategoryWidget extends Widget
12 13 {
  14 +
  15 + public $categories;
  16 +
  17 + public function init()
  18 + {
  19 + parent::init();
  20 +
  21 + $this->categories = Category::find()
  22 + ->with("lang.alias")
  23 + ->where(["status"=>true])
  24 + ->orderBy("sort")
  25 + ->all();
  26 + }
  27 +
13 28 public function run()
14 29 {
15 30 return $this->render(
16   - 'category_view'
  31 + 'category_view',
  32 + [
  33 + 'categories' => $this->categories
  34 + ]
17 35 );
18 36 }
19 37 }
... ...
frontend/widgets/SearchWidget.php
1 1 <?php
  2 +
2 3 namespace frontend\widgets;
3 4  
4 5 /**
... ... @@ -8,12 +9,38 @@
8 9 */
9 10 use yii\base\Widget;
10 11  
  12 + /**
  13 + * Class SearchWidget
  14 + *
  15 + * @package frontend\widgets
  16 + * @property string $route
  17 + * @property string $paramForSearch
  18 + */
11 19 class SearchWidget extends Widget
12 20 {
  21 +
  22 + public $route;
  23 + public $paramForSearch = 'title';
  24 +
  25 + public function init()
  26 + {
  27 + parent::init();
  28 +
  29 + if (!isset($this->route)) {
  30 + // by default route is the same as view in which it was used
  31 + $this->route = \Yii::$app->controller->id . "/" . \Yii::$app->controller->action->id;
  32 + }
  33 +
  34 + }
  35 +
13 36 public function run()
14 37 {
15 38 return $this->render(
16   - 'search_view'
  39 + 'search_view',
  40 + [
  41 + 'route' => $this->route,
  42 + 'paramForSearch' => $this->paramForSearch,
  43 + ]
17 44 );
18 45 }
19 46 }
20 47 \ No newline at end of file
... ...
frontend/widgets/views/category_view.php
... ... @@ -3,24 +3,55 @@
3 3 * User: timur
4 4 * Date: 28.01.18
5 5 * Time: 12:43
  6 + *
  7 + * @var Category[] $categories
6 8 */
7 9  
  10 + use artbox\weblog\models\Category;
  11 + use yii\helpers\Html;
  12 + use yii\helpers\Url;
  13 +
8 14 ?>
9 15  
10 16 <div class="panel panel-default sidebar-menu">
11 17  
12 18 <div class="panel-heading">
13   - <h3 class="panel-title">Категории (Рыба)</h3>
  19 + <h3 class="panel-title">Категории</h3>
14 20 </div>
15 21  
16 22 <div class="panel-body">
17   - <ul class="nav nav-pills nav-stacked">
18   - <li><a href="#">Норвегия</a>
19   - </li>
20   - <li><a href="#">Путешествие</a>
21   - </li>
22   - <li><a href="#">Маршрут</a>
23   - </li>
24   - </ul>
  23 +
  24 + <?php
  25 + $items = [];
  26 + $items[] = Html::a(
  27 + Yii::t("app", "All"),
  28 + Url::toRoute(
  29 + [
  30 + 'blog/index',
  31 + ]
  32 + )
  33 + );
  34 + foreach ($categories as $category){
  35 + $items[] = Html::a(
  36 + $category->lang->title,
  37 + Url::toRoute(
  38 + [
  39 + 'blog/category',
  40 + 'alias' => $category->lang->alias
  41 + ]
  42 + )
  43 + );
  44 + }
  45 +
  46 + echo Html::ul(
  47 + $items,
  48 + [
  49 + 'encode' => false,
  50 + 'class' => "nav nav-pills nav-stacked",
  51 + ]
  52 + );
  53 +
  54 + ?>
  55 +
25 56 </div>
26 57 </div>
... ...
frontend/widgets/views/search_view.php
... ... @@ -3,8 +3,14 @@
3 3 * User: timur
4 4 * Date: 28.01.18
5 5 * Time: 12:38
  6 + *
  7 + * @var string $route
  8 + * @var string $paramForSearch
  9 + *
6 10 */
7 11  
  12 + use yii\widgets\ActiveForm;
  13 + use yii\helpers\Html;
8 14 ?>
9 15  
10 16 <div class="panel panel-default sidebar-menu">
... ... @@ -14,13 +20,50 @@
14 20 </div>
15 21  
16 22 <div class="panel-body">
17   - <form role="search">
18   - <div class="input-group">
19   - <input type="text" class="form-control" placeholder="Поиск">
20   - <span class="input-group-btn">
21   - <button type="submit" class="btn btn-template-main"><i class="fa fa-search"></i></button>
22   - </span>
23   - </div>
24   - </form>
  23 +
  24 + <?php
  25 + $form = ActiveForm::begin(
  26 + [
  27 + 'action' => \yii\helpers\Url::toRoute($route),
  28 + 'options' => [
  29 + 'role' => "search",
  30 + ],
  31 + ]
  32 + );
  33 +
  34 + echo Html::tag(
  35 + "div",
  36 + Html::input(
  37 + "text",
  38 + $paramForSearch,
  39 + null,
  40 + [
  41 + 'class' => "form-control",
  42 + 'placeholder' => Yii::t("app", "Search"),
  43 + ]
  44 + ) . Html::tag(
  45 + "span",
  46 + Html::button(
  47 + "<i class=\"fa fa-search\"></i>",
  48 + [
  49 + 'class' => "btn btn-template-main",
  50 + 'type' => "submit",
  51 + ]
  52 + ),
  53 + [
  54 + 'class' => "input-group-btn",
  55 + ]
  56 + )
  57 +
  58 + ,
  59 + [
  60 + 'class' => "input-group",
  61 + ]
  62 + );
  63 +
  64 + $form::end();
  65 +
  66 + ?>
  67 +
25 68 </div>
26 69 </div>
... ...