/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Category::find()->joinWith('translations'); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => Yii::$app->request->cookies->getValue('_grid_page_size', 20)], 'sort' => ['defaultOrder' => ['id' => SORT_DESC]]]); $this->load($params); if (!$this->validate()) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'visible' => $this->visible, 'created_by' => $this->created_by, 'updated_by' => $this->updated_by, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]); if (isset($this->parent_id) && $this->parent_id > 1) { $parent = Category::findOne((int) $this->parent_id); $query->andWhere(['>', 'left_border', $parent->left_border]); $query->andWhere(['<', 'right_border', $parent->right_border]); } $query->andFilterWhere(['like', 'slug', $this->slug])->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'description', $this->description]); return $dataProvider; }
/** * Displays homepage. * * @return mixed */ public function actionIndex($slug = 'index') { if (empty($slug) || $slug == 'index') { throw new NotFoundHttpException('Page not found.'); } else { $category = Category::find()->where(['slug' => $slug]); $categoryCount = clone $category; if (!$categoryCount->count()) { throw new NotFoundHttpException('Page not found.'); } } $query = Post::find()->joinWith('category')->where(['status' => Post::STATUS_PUBLISHED, Category::tableName() . '.slug' => $slug])->orderBy('published_at DESC'); $countQuery = clone $query; $pagination = new Pagination(['totalCount' => $countQuery->count(), 'defaultPageSize' => Yii::$app->settings->get('reading.page_size', 10)]); $posts = $query->offset($pagination->offset)->limit($pagination->limit)->all(); return $this->render('index', ['posts' => $posts, 'category' => $category->one(), 'pagination' => $pagination]); }
<div class="row"> <div class="col-sm-12 text-right"> <?php echo GridPageSize::widget(['pjaxId' => 'post-category-grid-pjax']); ?> </div> </div> <?php Pjax::begin(['id' => 'post-category-grid-pjax']); ?> <?php echo GridView::widget(['id' => 'post-category-grid', 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'bulkActionOptions' => ['gridId' => 'post-category-grid', 'actions' => [Url::to(['bulk-delete']) => Yii::t('yee', 'Delete')]], 'columns' => [['class' => 'yeesoft\\grid\\CheckboxColumn', 'options' => ['style' => 'width:10px']], ['class' => 'yeesoft\\grid\\columns\\TitleActionColumn', 'controller' => '/post/category', 'title' => function (Category $model) { return Html::a($model->title, ['/post/category/update', 'id' => $model->id], ['data-pjax' => 0]); }, 'buttonsTemplate' => '{update} {delete}'], ['attribute' => 'parent_id', 'value' => function (Category $model) { if ($parent = $model->getParent()->one() and $parent->id > 1) { return Html::a($parent->title, ['/post/category/update', 'id' => $parent->id], ['data-pjax' => 0]); } else { return '<span class="not-set">' . Yii::t('yii', '(not set)') . '</span>'; } }, 'format' => 'raw', 'filter' => Category::getCategories(), 'filterInputOptions' => ['class' => 'form-control', 'encodeSpaces' => true]], 'description:ntext', ['class' => 'yeesoft\\grid\\columns\\StatusColumn', 'attribute' => 'visible']]]); ?> <?php Pjax::end(); ?> </div> </div> </div>
/** * Return all categories. * * @param bool $asArray * * @return static[] */ public static function getCategories($skipCategories = []) { $result = []; $categories = Category::findOne(1)->getDescendants()->joinWith('translations')->all(); foreach ($categories as $category) { if (!in_array($category->id, $skipCategories)) { $result[$category->id] = str_repeat(' ', $category->depth - 1) . $category->title; } } return $result; }
<?php if ($model->isMultilingual()) { ?> <?php echo LanguagePills::widget(); ?> <?php } ?> <?php echo $form->field($model, 'title')->textInput(['maxlength' => true]); ?> <?php echo $form->field($model, 'parent_id')->dropDownList(Category::getCategories(), ['prompt' => '', 'encodeSpaces' => true]); ?> <?php echo $form->field($model, 'slug')->textInput(['maxlength' => true]); ?> <?php echo $form->field($model, 'description')->textarea(['rows' => 6]); ?> </div> </div> </div> <div class="col-md-3">
/** * @return \yii\db\ActiveQuery */ public function getCategory() { return $this->hasOne(Category::className(), ['id' => 'category_id']); }