Skip to content

Commit

Permalink
Show article categories in grid. Implement search by category
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodeholic committed Jan 26, 2017
1 parent 902ffdb commit 7271f2d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 23 deletions.
3 changes: 2 additions & 1 deletion controllers/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public function actionIndex()

return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider
'dataProvider' => $dataProvider,
'articleCategories' => ArticleCategory::getCategories()
]);
}

Expand Down
38 changes: 24 additions & 14 deletions models/search/ArticleSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
];
}
Expand All @@ -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,
Expand All @@ -64,31 +65,40 @@ 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;
}

//$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([
'like', 'u.username', $this->author
]);
}

// \ChromePhp::log(QueryHelper::getRawSql($query));
$query->with([
'articleAttachments', 'articleCategoryArticles', 'articleCategoryArticles.articleCategory', 'author'
]);

return $dataProvider;
}
Expand All @@ -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([
Expand Down
28 changes: 20 additions & 8 deletions views/article/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'
Expand All @@ -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',
Expand Down

0 comments on commit 7271f2d

Please sign in to comment.