/** * Displays a single Post model. * * @param null $post_slug * * @param integer $id * * @throws \yii\web\NotFoundHttpException * @return mixed */ public function actionView($id = null, $post_slug = null) { $render = '/site/view'; $comment = new Comment(); if ($id) { $model = $this->findModel($id); } else { if ($post_slug) { $model = $this->findModelBySlug($post_slug); } else { throw new NotFoundHttpException('The requested page does not exist.'); } } if ($comment->load(Yii::$app->request->post()) && $comment->save()) { if (!$comment->comment_parent) { $model->post_comment_count++; } if ($model->save()) { $this->refresh(); } } if ($model->post_password && $model->post_password !== Yii::$app->request->post('password')) { return $this->render('protected', ['post' => $model]); } //var_dump($model->post_template);exit; if ($model->post_template !== null && $model->post_template !== '') { $render = '/site/' . $model->post_template . '.php'; } elseif (is_file(Yii::$app->getModule('content')->viewPath . '/site/view-' . $model->postType->post_type_slug . '.php')) { $render = '/site/view-' . $model->postType->post_type_slug . '.php'; } return $this->render($render, ['post' => $model, 'comment' => $comment]); }
/** * Get comment children based on comment ID. * * @param int $id * * @return array|null */ protected function getChildren($id) { /* @var $models \common\models\PostComment[] */ $comments = []; $models = Comment::find()->select(['id', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_date', 'comment_content'])->andWhere(['comment_parent' => $id])->andWhere(['comment_post_id' => $this->model->id])->andWhere(['comment_approved' => 'approved'])->orderBy(['id' => $this->commentOrder])->all(); if (empty($models)) { $comments = null; } else { foreach ($models as $model) { $comments[$model->id] = $model; $comments[$model->id]['child'] = $this->getChildren($model->id); } } return $comments; }
/** * Show user count, post count, post-comment count on index (dashboard). * * @return string */ public function actionIndex() { $userQuery = User::find()->andWhere(['blocked_at' => null, 'confirmed_at' => 'is not null']); $userCloneQuery = clone $userQuery; $userCount = $userCloneQuery->count(); $users = $userQuery->limit(12)->orderBy(['id' => SORT_DESC])->all(); $postQuery = Post::find()->andWhere(['post_status' => 'publish']); $postCloneQuery = clone $postQuery; $postCount = $postCloneQuery->count(); $posts = $postQuery->limit(10)->orderBy(['id' => SORT_DESC])->all(); $commentQuery = PostComment::find()->andWhere(['comment_approved' => 'approved']); $commentCloneQuery = clone $commentQuery; $commentCount = $commentCloneQuery->count(); $comments = $commentQuery->limit(5)->orderBy(['id' => SORT_DESC])->all(); return $this->render('index', ['users' => $users, 'posts' => $posts, 'comments' => $comments, 'userCount' => $userCount, 'postCount' => $postCount, 'commentCount' => $commentCount]); }
/** * 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; }
/** * @return \yii\db\ActiveQuery */ public function getPostComments() { return $this->hasMany(PostComment::className(), ['comment_post_id' => 'id']); }
/** * Finds the PostComment model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * * @param integer $id * * @return PostComment the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = PostComment::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }