/** * 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]); }