diff --git a/frontend/controllers/BlogController.php b/frontend/controllers/BlogController.php index c73ba41..66d89b0 100644 --- a/frontend/controllers/BlogController.php +++ b/frontend/controllers/BlogController.php @@ -7,6 +7,7 @@ use yii\web\Controller; use artbox\weblog\models\Article; use yii\web\NotFoundHttpException; + use yii\db\ActiveQuery; /** * User: timur @@ -16,17 +17,8 @@ class BlogController extends Controller { - public function actionIndex() - { - $query = Article::find() - ->where( - [ - 'status' => true, - ] - ) - ->orderBy("sort"); - + protected function prepareProviderAndRender(ActiveQuery $query): string { $dataProvider = new ActiveDataProvider( [ 'query' => $query, @@ -35,7 +27,7 @@ ], ] ); - + return $this->render( 'index', [ @@ -44,6 +36,20 @@ ); } + public function actionIndex() + { + + $query = Article::find() + ->where( + [ + 'status' => true, + ] + ) + ->orderBy("sort"); + + return $this->prepareProviderAndRender($query); + } + public function actionArticle($id) { @@ -60,10 +66,6 @@ protected function findModel($id) { - /** - * Some comment - */ - $model = Article::find() ->where( [ @@ -81,9 +83,11 @@ } } - public function actionSearch(){ + public function actionSearch() + { + if( \Yii::$app->request->isPost ){ - + $req = \Yii::$app->request; if (!empty($req->post("title"))){ $title = Html::encode($req->post("title")); @@ -101,21 +105,7 @@ ) ->orderBy("sort"); - $dataProvider = new ActiveDataProvider( - [ - 'query' => $query, - 'pagination' => [ - 'pageSize' => 5, - ], - ] - ); - - return $this->render( - 'index', - [ - 'dataProvider' => $dataProvider, - ] - ); + return $this->prepareProviderAndRender($query); } } @@ -123,7 +113,7 @@ } - public function actionCategory($id){ + public function actionCategory($id) { $query = Article::find() ->joinWith("categories.lang") @@ -140,21 +130,21 @@ ) ->orderBy("sort"); - $dataProvider = new ActiveDataProvider( - [ - 'query' => $query, - 'pagination' => [ - 'pageSize' => 5, - ], - ] - ); + return $this->prepareProviderAndRender($query); + } - return $this->render( - 'index', - [ - 'dataProvider' => $dataProvider, - ] - ); + public function actionTag($id){ + $query = Article::find() + ->joinWith("tags.lang") + ->where( + [ + 'blog_article.status' => true, + 'blog_tag.id' => $id, + ] + ) + ->orderBy('sort'); + + return $this->prepareProviderAndRender($query); } } \ No newline at end of file diff --git a/frontend/widgets/TagWidget.php b/frontend/widgets/TagWidget.php index 8e77eb0..c06144f 100644 --- a/frontend/widgets/TagWidget.php +++ b/frontend/widgets/TagWidget.php @@ -8,13 +8,37 @@ */ use yii\base\Widget; - + use artbox\weblog\models\Tag; + + /** + * Class TagWidget + * + * @property Tag[] $tags + * + * @package frontend\widgets + * + */ class TagWidget extends Widget { + + public $tags; + + public function init() + { + parent::init(); + + $this->tags = Tag::find() + ->with("lang.alias") + ->all(); + } + public function run() { return $this->render( - 'tag_view' + 'tag_view', + [ + "tags" => $this->tags + ] ); } } \ No newline at end of file diff --git a/frontend/widgets/views/category_view.php b/frontend/widgets/views/category_view.php index 885c7a6..8394d1b 100644 --- a/frontend/widgets/views/category_view.php +++ b/frontend/widgets/views/category_view.php @@ -23,23 +23,25 @@ $category->lang->alias + ] + ); + $items[$url] = Html::a( $category->lang->title, - Url::toRoute( - [ - 'blog/category', - 'alias' => $category->lang->alias - ] - ) + $url ); } @@ -48,6 +50,14 @@ [ 'encode' => false, 'class' => "nav nav-pills nav-stacked", + 'item' => function($item, $index){ + $class = ''; + if ($index === Yii::$app->request->getUrl()){ + $class = "active"; + } + + return "
  • " . $item . "
  • "; + } ] ); diff --git a/frontend/widgets/views/tag_view.php b/frontend/widgets/views/tag_view.php index 34a96b2..2d728df 100644 --- a/frontend/widgets/views/tag_view.php +++ b/frontend/widgets/views/tag_view.php @@ -3,25 +3,60 @@ * User: timur * Date: 28.01.18 * Time: 12:46 + * + * @var Tag[] $tags */ - + use artbox\weblog\models\Tag; + use yii\helpers\Html; + use yii\helpers\Url; ?> -- libgit2 0.21.4