Commit 1729300e8c43d13b6ebaf0d9a71df67c10eeace7
1 parent
2309b955
blog categories & tags end
Showing
4 changed files
with
133 additions
and
74 deletions
Show diff stats
frontend/controllers/BlogController.php
@@ -7,6 +7,7 @@ | @@ -7,6 +7,7 @@ | ||
7 | use yii\web\Controller; | 7 | use yii\web\Controller; |
8 | use artbox\weblog\models\Article; | 8 | use artbox\weblog\models\Article; |
9 | use yii\web\NotFoundHttpException; | 9 | use yii\web\NotFoundHttpException; |
10 | + use yii\db\ActiveQuery; | ||
10 | 11 | ||
11 | /** | 12 | /** |
12 | * User: timur | 13 | * User: timur |
@@ -16,17 +17,8 @@ | @@ -16,17 +17,8 @@ | ||
16 | 17 | ||
17 | class BlogController extends Controller | 18 | class BlogController extends Controller |
18 | { | 19 | { |
19 | - public function actionIndex() | ||
20 | - { | ||
21 | 20 | ||
22 | - $query = Article::find() | ||
23 | - ->where( | ||
24 | - [ | ||
25 | - 'status' => true, | ||
26 | - ] | ||
27 | - ) | ||
28 | - ->orderBy("sort"); | ||
29 | - | 21 | + protected function prepareProviderAndRender(ActiveQuery $query): string { |
30 | $dataProvider = new ActiveDataProvider( | 22 | $dataProvider = new ActiveDataProvider( |
31 | [ | 23 | [ |
32 | 'query' => $query, | 24 | 'query' => $query, |
@@ -35,7 +27,7 @@ | @@ -35,7 +27,7 @@ | ||
35 | ], | 27 | ], |
36 | ] | 28 | ] |
37 | ); | 29 | ); |
38 | - | 30 | + |
39 | return $this->render( | 31 | return $this->render( |
40 | 'index', | 32 | 'index', |
41 | [ | 33 | [ |
@@ -44,6 +36,20 @@ | @@ -44,6 +36,20 @@ | ||
44 | ); | 36 | ); |
45 | } | 37 | } |
46 | 38 | ||
39 | + public function actionIndex() | ||
40 | + { | ||
41 | + | ||
42 | + $query = Article::find() | ||
43 | + ->where( | ||
44 | + [ | ||
45 | + 'status' => true, | ||
46 | + ] | ||
47 | + ) | ||
48 | + ->orderBy("sort"); | ||
49 | + | ||
50 | + return $this->prepareProviderAndRender($query); | ||
51 | + } | ||
52 | + | ||
47 | public function actionArticle($id) | 53 | public function actionArticle($id) |
48 | { | 54 | { |
49 | 55 | ||
@@ -60,10 +66,6 @@ | @@ -60,10 +66,6 @@ | ||
60 | 66 | ||
61 | protected function findModel($id) | 67 | protected function findModel($id) |
62 | { | 68 | { |
63 | - /** | ||
64 | - * Some comment | ||
65 | - */ | ||
66 | - | ||
67 | $model = Article::find() | 69 | $model = Article::find() |
68 | ->where( | 70 | ->where( |
69 | [ | 71 | [ |
@@ -81,9 +83,11 @@ | @@ -81,9 +83,11 @@ | ||
81 | } | 83 | } |
82 | } | 84 | } |
83 | 85 | ||
84 | - public function actionSearch(){ | 86 | + public function actionSearch() |
87 | + { | ||
88 | + | ||
85 | if( \Yii::$app->request->isPost ){ | 89 | if( \Yii::$app->request->isPost ){ |
86 | - | 90 | + |
87 | $req = \Yii::$app->request; | 91 | $req = \Yii::$app->request; |
88 | if (!empty($req->post("title"))){ | 92 | if (!empty($req->post("title"))){ |
89 | $title = Html::encode($req->post("title")); | 93 | $title = Html::encode($req->post("title")); |
@@ -101,21 +105,7 @@ | @@ -101,21 +105,7 @@ | ||
101 | ) | 105 | ) |
102 | ->orderBy("sort"); | 106 | ->orderBy("sort"); |
103 | 107 | ||
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 | - ); | 108 | + return $this->prepareProviderAndRender($query); |
119 | } | 109 | } |
120 | } | 110 | } |
121 | 111 | ||
@@ -123,7 +113,7 @@ | @@ -123,7 +113,7 @@ | ||
123 | 113 | ||
124 | } | 114 | } |
125 | 115 | ||
126 | - public function actionCategory($id){ | 116 | + public function actionCategory($id) { |
127 | 117 | ||
128 | $query = Article::find() | 118 | $query = Article::find() |
129 | ->joinWith("categories.lang") | 119 | ->joinWith("categories.lang") |
@@ -140,21 +130,21 @@ | @@ -140,21 +130,21 @@ | ||
140 | ) | 130 | ) |
141 | ->orderBy("sort"); | 131 | ->orderBy("sort"); |
142 | 132 | ||
143 | - $dataProvider = new ActiveDataProvider( | ||
144 | - [ | ||
145 | - 'query' => $query, | ||
146 | - 'pagination' => [ | ||
147 | - 'pageSize' => 5, | ||
148 | - ], | ||
149 | - ] | ||
150 | - ); | 133 | + return $this->prepareProviderAndRender($query); |
134 | + } | ||
151 | 135 | ||
152 | - return $this->render( | ||
153 | - 'index', | ||
154 | - [ | ||
155 | - 'dataProvider' => $dataProvider, | ||
156 | - ] | ||
157 | - ); | 136 | + public function actionTag($id){ |
137 | + $query = Article::find() | ||
138 | + ->joinWith("tags.lang") | ||
139 | + ->where( | ||
140 | + [ | ||
141 | + 'blog_article.status' => true, | ||
142 | + 'blog_tag.id' => $id, | ||
143 | + ] | ||
144 | + ) | ||
145 | + ->orderBy('sort'); | ||
146 | + | ||
147 | + return $this->prepareProviderAndRender($query); | ||
158 | } | 148 | } |
159 | 149 | ||
160 | } | 150 | } |
161 | \ No newline at end of file | 151 | \ No newline at end of file |
frontend/widgets/TagWidget.php
@@ -8,13 +8,37 @@ | @@ -8,13 +8,37 @@ | ||
8 | */ | 8 | */ |
9 | 9 | ||
10 | use yii\base\Widget; | 10 | use yii\base\Widget; |
11 | - | 11 | + use artbox\weblog\models\Tag; |
12 | + | ||
13 | + /** | ||
14 | + * Class TagWidget | ||
15 | + * | ||
16 | + * @property Tag[] $tags | ||
17 | + * | ||
18 | + * @package frontend\widgets | ||
19 | + * | ||
20 | + */ | ||
12 | class TagWidget extends Widget | 21 | class TagWidget extends Widget |
13 | { | 22 | { |
23 | + | ||
24 | + public $tags; | ||
25 | + | ||
26 | + public function init() | ||
27 | + { | ||
28 | + parent::init(); | ||
29 | + | ||
30 | + $this->tags = Tag::find() | ||
31 | + ->with("lang.alias") | ||
32 | + ->all(); | ||
33 | + } | ||
34 | + | ||
14 | public function run() | 35 | public function run() |
15 | { | 36 | { |
16 | return $this->render( | 37 | return $this->render( |
17 | - 'tag_view' | 38 | + 'tag_view', |
39 | + [ | ||
40 | + "tags" => $this->tags | ||
41 | + ] | ||
18 | ); | 42 | ); |
19 | } | 43 | } |
20 | } | 44 | } |
21 | \ No newline at end of file | 45 | \ No newline at end of file |
frontend/widgets/views/category_view.php
@@ -23,23 +23,25 @@ | @@ -23,23 +23,25 @@ | ||
23 | 23 | ||
24 | <?php | 24 | <?php |
25 | $items = []; | 25 | $items = []; |
26 | - $items[] = Html::a( | 26 | + $url = Url::toRoute( |
27 | + [ | ||
28 | + 'blog/index', | ||
29 | + ] | ||
30 | + ); | ||
31 | + $items[$url] = Html::a( | ||
27 | Yii::t("app", "All"), | 32 | Yii::t("app", "All"), |
28 | - Url::toRoute( | ||
29 | - [ | ||
30 | - 'blog/index', | ||
31 | - ] | ||
32 | - ) | 33 | + $url |
33 | ); | 34 | ); |
34 | foreach ($categories as $category){ | 35 | foreach ($categories as $category){ |
35 | - $items[] = Html::a( | 36 | + $url = Url::toRoute( |
37 | + [ | ||
38 | + 'blog/category', | ||
39 | + 'alias' => $category->lang->alias | ||
40 | + ] | ||
41 | + ); | ||
42 | + $items[$url] = Html::a( | ||
36 | $category->lang->title, | 43 | $category->lang->title, |
37 | - Url::toRoute( | ||
38 | - [ | ||
39 | - 'blog/category', | ||
40 | - 'alias' => $category->lang->alias | ||
41 | - ] | ||
42 | - ) | 44 | + $url |
43 | ); | 45 | ); |
44 | } | 46 | } |
45 | 47 | ||
@@ -48,6 +50,14 @@ | @@ -48,6 +50,14 @@ | ||
48 | [ | 50 | [ |
49 | 'encode' => false, | 51 | 'encode' => false, |
50 | 'class' => "nav nav-pills nav-stacked", | 52 | 'class' => "nav nav-pills nav-stacked", |
53 | + 'item' => function($item, $index){ | ||
54 | + $class = ''; | ||
55 | + if ($index === Yii::$app->request->getUrl()){ | ||
56 | + $class = "active"; | ||
57 | + } | ||
58 | + | ||
59 | + return "<li class={$class}>" . $item . "</li>"; | ||
60 | + } | ||
51 | ] | 61 | ] |
52 | ); | 62 | ); |
53 | 63 |
frontend/widgets/views/tag_view.php
@@ -3,25 +3,60 @@ | @@ -3,25 +3,60 @@ | ||
3 | * User: timur | 3 | * User: timur |
4 | * Date: 28.01.18 | 4 | * Date: 28.01.18 |
5 | * Time: 12:46 | 5 | * Time: 12:46 |
6 | + * | ||
7 | + * @var Tag[] $tags | ||
6 | */ | 8 | */ |
7 | - | 9 | + use artbox\weblog\models\Tag; |
10 | + use yii\helpers\Html; | ||
11 | + use yii\helpers\Url; | ||
8 | ?> | 12 | ?> |
9 | 13 | ||
10 | <div class="panel sidebar-menu"> | 14 | <div class="panel sidebar-menu"> |
11 | <div class="panel-heading"> | 15 | <div class="panel-heading"> |
12 | - <h3 class="panel-title">Теги (Рыба)</h3> | 16 | + <h3 class="panel-title">Теги</h3> |
13 | </div> | 17 | </div> |
14 | 18 | ||
15 | <div class="panel-body"> | 19 | <div class="panel-body"> |
16 | - <ul class="tag-cloud"> | ||
17 | - <li><a href="#"><i class="fa fa-tags"></i> traveling</a> | ||
18 | - </li> | ||
19 | - <li><a href="#"><i class="fa fa-tags"></i> travel</a> | ||
20 | - </li> | ||
21 | - <li><a href="#"><i class="fa fa-tags"></i> travelguide</a> | ||
22 | - </li> | ||
23 | - <li><a href="#"><i class="fa fa-tags"></i> advice</a> | ||
24 | - </li> | ||
25 | - </ul> | 20 | + |
21 | + <?php | ||
22 | + $items = []; | ||
23 | + $url = Url::toRoute(["blog/index"]); | ||
24 | + $items[$url] = Html::a( | ||
25 | + "<i class=\"fa fa-tags\"></i>". | ||
26 | + Yii::t('app', "All"), | ||
27 | + $url | ||
28 | + ); | ||
29 | + | ||
30 | + foreach ($tags as $tag){ | ||
31 | + $url = Url::toRoute( | ||
32 | + [ | ||
33 | + 'blog/tag', | ||
34 | + 'alias' => $tag->lang->alias, | ||
35 | + ] | ||
36 | + ); | ||
37 | + $items[$url] = Html::a( | ||
38 | + "<i class=\"fa fa-tags\"></i>".$tag->lang->label, | ||
39 | + $url | ||
40 | + ); | ||
41 | + } | ||
42 | + | ||
43 | + echo Html::ul( | ||
44 | + $items, | ||
45 | + [ | ||
46 | + 'encode' => false, | ||
47 | + 'class' => "tag-cloud", | ||
48 | + 'item' => function($item, $index){ | ||
49 | + $class = ''; | ||
50 | + if ($index === Yii::$app->request->getUrl()){ | ||
51 | + $class = "active"; | ||
52 | + } | ||
53 | + | ||
54 | + return "<li class={$class}>" . $item . "</li>"; | ||
55 | + } | ||
56 | + ] | ||
57 | + ); | ||
58 | + | ||
59 | + ?> | ||
60 | + | ||
26 | </div> | 61 | </div> |
27 | </div> | 62 | </div> |