/** * List all Post models. * * @param integer $post_type * @param null|string $user * * @return mixed * @throws ForbiddenHttpException * @throws NotFoundHttpException */ public function actionIndex($post_type, $user = null) { $postType = $this->findPostType($post_type); $searchModel = new PostSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams, $post_type, $user); if (!Yii::$app->user->can($postType->post_type_permission)) { throw new ForbiddenHttpException(Yii::t('content', 'You are not allowed to perform this action.')); } return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'postType' => $postType, 'user' => $user]); }
/** * Render home page of the site. * * @throws \yii\web\NotFoundHttpException * @return string */ public function actionIndex() { /* @var $post Post */ $query = Post::find()->from(['t' => Post::tableName()])->andWhere(['post_status' => 'publish'])->orderBy(['t.id' => SORT_DESC]); if (Option::get('show_on_front') == 'page' && ($front_page = Option::get('front_page'))) { $render = '/site/view'; $comment = new PostComment(); $query = $query->andWhere(['id' => $front_page]); $post = $query->one(); // Get post list $searchModel = new PostSearch(); $categoryId = Yii::$app->request->get('category'); $dataProvider = $searchModel->searchFront(Yii::$app->request->queryParams, $categoryId); if (!empty($post->post_template)) { $render = '/site/' . $post->post_template; } elseif (is_file($this->view->theme->basePath . '/site/view-' . $post->postType->post_type_slug . '.php')) { $render = '/site/view-' . $post->postType->post_type_slug; } if ($post) { return $this->render($render, ['post' => $post, 'comment' => $comment, 'dataProvider' => $dataProvider]); } else { return new NotFoundHttpException(); } } else { if (Option::get('front_post_type') !== 'all') { $query->innerJoinWith(['postType'])->andWhere(['post_type_name' => Option::get('front_post_type')]); } $countQuery = clone $query; $pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => Option::get('posts_per_page')]); $query->offset($pages->offset)->limit($pages->limit); $posts = $query->all(); if ($posts) { return $this->render('index', ['posts' => $posts, 'pages' => isset($pages) ? $pages : null]); } else { throw new NotFoundHttpException(Yii::t('content', 'Page not found.')); } } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = MediaModel::find()->from(['media' => $this->tableName()]); $query->innerJoinWith(['mediaAuthor' => function ($query) { /* @var $query \yii\db\ActiveQuery */ return $query->from(['author' => User::tableName()]); }]); $query->leftJoin(['post' => Post::tableName()], 'media.media_post_id = post.id'); $dataProvider = new ActiveDataProvider(['query' => $query]); $dataProvider->setSort(['attributes' => ArrayHelper::merge($dataProvider->sort->attributes, ['username' => ['asc' => ['username' => SORT_ASC], 'desc' => ['username' => SORT_DESC], 'label' => 'Author', 'value' => 'username']]), 'defaultOrder' => ['id' => SORT_DESC]]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'media_author' => $this->media_author, 'media_post_id' => $this->media_post_id, 'media_comment_count' => $this->media_comment_count]); $query->andFilterWhere(['like', 'media_title', $this->media_title])->andFilterWhere(['like', 'media_excerpt', $this->media_excerpt])->andFilterWhere(['like', 'media_content', $this->media_content])->andFilterWhere(['like', 'media_password', $this->media_password])->andFilterWhere(['like', 'media_slug', $this->media_slug])->andFilterWhere(['like', 'media_mime_type', $this->media_mime_type])->andFilterWhere(['like', 'media_comment_status', $this->media_comment_status])->andFilterWhere(['like', 'media_date', $this->media_date])->andFilterWhere(['like', 'media_modified', $this->media_modified])->andFilterWhere(['like', 'post.post_title', $this->post_title])->andFilterWhere(['like', 'author.username', $this->username]); return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * * @param int $post_type * @param int|null $post_id * * @return ActiveDataProvider */ public function search($params, $post_type, $post_id = null) { $query = PostCommentModel::find(); $query->innerJoinWith(['commentPost' => function ($query) { /* @var $query \yii\db\ActiveQuery */ return $query->from(['post' => Post::tableName()]); }]); $query->andWhere(['post.post_type' => $post_type]); if ($post_id) { $query->andWhere(['post.id' => $post_id]); } $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['id' => SORT_DESC]]]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'comment_post_id' => $this->comment_post_id, 'comment_parent' => $this->comment_parent, 'comment_user_id' => $this->comment_user_id]); $query->andFilterWhere(['like', 'comment_author', $this->comment_author])->andFilterWhere(['like', 'comment_author_email', $this->comment_author_email])->andFilterWhere(['like', 'comment_author_url', $this->comment_author_url])->andFilterWhere(['like', 'comment_author_ip', $this->comment_author_ip])->andFilterWhere(['like', 'comment_content', $this->comment_content])->andFilterWhere(['like', 'comment_approved', $this->comment_approved])->andFilterWhere(['like', 'comment_agent', $this->comment_agent])->andFilterWhere(['like', 'comment_date', $this->comment_date])->andFilterWhere(['like', 'post.post_title', $this->post_title]); return $dataProvider; }