From 7271f2df1eb46f01f23b0c802cdc48d35275e02e Mon Sep 17 00:00:00 2001 From: Zura Sekhniashvili Date: Thu, 26 Jan 2017 14:23:41 +0400 Subject: [PATCH] Show article categories in grid. Implement search by category --- controllers/ArticleController.php | 3 ++- models/search/ArticleSearch.php | 38 +++++++++++++++++++------------ views/article/index.php | 28 ++++++++++++++++------- 3 files changed, 46 insertions(+), 23 deletions(-) diff --git a/controllers/ArticleController.php b/controllers/ArticleController.php index b657c54..68f2429 100644 --- a/controllers/ArticleController.php +++ b/controllers/ArticleController.php @@ -66,7 +66,8 @@ public function actionIndex() return $this->render('index', [ 'searchModel' => $searchModel, - 'dataProvider' => $dataProvider + 'dataProvider' => $dataProvider, + 'articleCategories' => ArticleCategory::getCategories() ]); } diff --git a/models/search/ArticleSearch.php b/models/search/ArticleSearch.php index a784aaf..cb32e09 100644 --- a/models/search/ArticleSearch.php +++ b/models/search/ArticleSearch.php @@ -20,13 +20,14 @@ class ArticleSearch extends Article * @var array */ public $catIds; + /** * @inheritdoc */ public function rules() { return [ - [['id', 'status', 'published_at', 'created_at', 'position'], 'integer'], + [['id', 'status', 'category_ids', 'published_at', 'created_at', 'position'], 'integer'], [['author', 'slug', 'title', 'catIds'], 'safe'], ]; } @@ -48,9 +49,9 @@ public function search($params) { // \ChromePhp::log($params); $query = Article::find(); - $query->from('{{%article}} a'); - $query->innerJoin(ArticleTranslation::tableName().'at', 'at.article_id = a.id AND at.locale = :locale', ['locale' => Yii::$app->language]); - $query->innerJoin('{{%user}} u', 'u.id = a.author_id'); + $a = Article::tableName(); + $query->innerJoin(ArticleTranslation::tableName() . 'at', "at.article_id = $a.id AND at.locale = :locale", ['locale' => Yii::$app->language]); + $query->innerJoin('{{%user}} u', "u.id = $a.author_id"); $dataProvider = new ActiveDataProvider([ 'query' => $query, @@ -64,7 +65,7 @@ public function search($params) 'asc' => ['u.username' => SORT_ASC], 'desc' => ['u.username' => SORT_DESC] ]; - $this->load($params); + $this->load($params); if (!$this->validate()) { return $dataProvider; } @@ -72,15 +73,22 @@ public function search($params) //$this->slug = $params['ArticleSearch']['slug']; $query->andFilterWhere([ - 'a.id' => $this->id, - 'a.status' => $this->status, - 'a.position' => $this->position, - 'a.published_at' => $this->published_at, - 'a.created_at' => $this->created_at + "$a.id" => $this->id, + "$a.status" => $this->status, + "$a.position" => $this->position, + "$a.published_at" => $this->published_at, + "$a.created_at" => $this->created_at ]); + if (!is_array($this->category_ids) && $this->category_ids) { + $this->category_ids = [$this->category_ids]; + } - $query->andFilterWhere(['like', 'a.slug', $this->slug]) - ->andFilterWhere(['like', 'at.title', $this->title]); + if (!empty($this->category_ids)) { + $query->byCategoryId($this->category_ids); + } + + $query->andFilterWhere(['like', "$a.slug", $this->slug]) + ->andFilterWhere(['like', "at.title", $this->title]); if ($this->author) { $query->andFilterWhere([ @@ -88,7 +96,9 @@ public function search($params) ]); } -// \ChromePhp::log(QueryHelper::getRawSql($query)); + $query->with([ + 'articleAttachments', 'articleCategoryArticles', 'articleCategoryArticles.articleCategory', 'author' + ]); return $dataProvider; } @@ -103,7 +113,7 @@ public function searchByCategory($params) $query = Article::find(); $query->from('{{%article}} a'); // $query->innerJoin('article_category', 'article_category.id = a.category_id'); - $query->innerJoin(ArticleTranslation::tableName().' at', 'at.article_id = a.id AND at.locale = :locale', ['locale' => Yii::$app->language]); + $query->innerJoin(ArticleTranslation::tableName() . ' at', 'at.article_id = a.id AND at.locale = :locale', ['locale' => Yii::$app->language]); $query->innerJoin('{{%user}} u', 'u.id = a.author_id'); $dataProvider = new ActiveDataProvider([ diff --git a/views/article/index.php b/views/article/index.php index 257ead7..02fe001 100644 --- a/views/article/index.php +++ b/views/article/index.php @@ -6,6 +6,7 @@ /* @var $this yii\web\View */ /* @var $searchModel centigen\i18ncontent\models\search\ArticleSearch */ /* @var $dataProvider yii\data\ActiveDataProvider */ +/* @var $articleCategories \centigen\i18ncontent\models\ArticleCategory[] */ $this->title = Yii::t('i18ncontent', 'Articles'); $this->params['breadcrumbs'][] = $this->title; @@ -26,23 +27,34 @@ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [ - [ - 'attribute' => 'id', + 'attribute' => 'slug', 'contentOptions' => [ - 'style' => 'width: 100px' + 'style' => 'width: auto' ] ], [ - 'attribute' => 'slug', + 'attribute' => 'title', + 'value' => function ($model) { + return $model->activeTranslation ? $model->activeTranslation->title : ""; + }, 'contentOptions' => [ 'style' => 'width: auto' ] ], [ - 'attribute' => 'title', + 'label' => Yii::t('i18ncontent', 'Categories'), + 'attribute' => 'category_ids', + 'filter' => $articleCategories, + 'format' => ['html'], 'value' => function ($model) { - return $model->activeTranslation ? $model->activeTranslation->title : ""; + /** @var $model \centigen\i18ncontent\models\Article */ +// \centigen\base\helpers\UtilHelper::vardump($model->toArray()); + $content = ''; + foreach ($model->articleCategoryArticles as $articleCategoryArticle){ + $content .= \yii\bootstrap\Html::tag('span', $articleCategoryArticle->articleCategory->getTitle(), ['class' => 'label label-default']); + } + return $content; }, 'contentOptions' => [ 'style' => 'width: auto' @@ -57,9 +69,9 @@ [ 'label' => Yii::t('i18ncontent', 'Status'), 'attribute' => 'status', - 'content' => function($model){ + 'content' => function ($model) { /** @var \common\models\User $model */ - return \centigen\i18ncontent\helpers\Html::asFab($model); + return \centigen\i18ncontent\helpers\Html::asFab($model); }, 'headerOptions' => [ 'class' => 'text-center',