/** * Displays a single Post model. * * @param null $postslug * * @param integer $id * * @throws \yii\web\NotFoundHttpException * @return mixed */ public function actionView($id = null, $postslug = null) { $render = 'view'; $comment = new Comment(); if ($id) { $model = $this->findModel($id); } elseif ($postslug) { $model = $this->findModelBySlug($postslug); } else { throw new NotFoundHttpException(Yii::t('writesdown', '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]); } if (is_file($this->view->theme->basePath . '/post/view-' . $model->postType->post_type_slug . '.php')) { $render = 'view-' . $model->postType->post_type_slug . '.php'; } return $this->render($render, ['post' => $model, 'comment' => $comment]); }
/** * 创建评论 * @param $id * @return PostComment|\yii\web\Response */ public function actionCreate($id) { $post = Topic::findTopic($id); $model = new PostComment(); if ($model->load(Yii::$app->request->post())) { $topService = new TopicService(); if (!$topService->filterContent($model->comment)) { $this->flash('回复内容请勿回复无意义的内容,如你想收藏或赞等功能,请直接操作这篇帖子。', 'warning'); return $this->redirect(['/topic/default/view', 'id' => $id]); } $model->user_id = Yii::$app->user->id; $model->post_id = $id; $model->ip = Yii::$app->getRequest()->getUserIP(); $rawComment = $model->comment; $model->comment = $model->replace($rawComment); if ($model->save()) { (new UserMeta())->saveNewMeta('topic', $id, 'follow'); (new NotificationService())->newReplyNotify(Yii::$app->user->identity, $post, $model, $rawComment); // 更新回复时间 $post->lastCommentToUpdate(Yii::$app->user->identity->username); // 评论计数器 Topic::updateAllCounters(['comment_count' => 1], ['id' => $post->id]); // 更新个人总统计 UserInfo::updateAllCounters(['comment_count' => 1], ['user_id' => $model->user_id]); $this->flash("评论成功", 'success'); } else { $this->flash(array_values($model->getFirstErrors())[0], 'warning'); } return $this->redirect(['/topic/default/view', 'id' => $post->id]); } return $model; }
/** * 创建评论 * @param $id * @return PostComment|\yii\web\Response */ public function actionCreate($id) { $model = new PostComment(); if ($model->load(Yii::$app->request->post())) { $topService = new TopicService(); if (!$topService->filterContent($model->comment)) { $this->flash('回复内容请勿回复无意义的内容,如你想收藏或赞等功能,请直接操作这篇帖子。', 'warning'); return $this->redirect(['/topic/default/view', 'id' => $id]); } $model->user_id = Yii::$app->user->id; $model->post_id = $id; $model->ip = Yii::$app->getRequest()->getUserIP(); if ($model->save()) { $this->flash("评论成功", 'success'); } else { $this->flash(array_values($model->getFirstErrors())[0], 'warning'); } return $this->redirect(['/topic/default/view', 'id' => $id]); } return $model; }
/** * 创建评论 * @param Post $post * @return array|PostComment|string */ protected function newComment(Post $post) { $model = new PostComment(); if ($model->load(Yii::$app->request->post())) { $topService = new TopicService(); if (!$topService->filterContent($model->comment)) { $this->flash('回复内容请勿回复无意义的内容,如你想收藏或赞等功能,请直接操作这篇帖子。', 'warning'); return $this->redirect(['view', 'id' => $post->id]); } $model->user_id = Yii::$app->user->id; $model->post_id = $post->id; $model->ip = Yii::$app->getRequest()->getUserIP(); $rawComment = $model->comment; $model->comment = $model->replace($rawComment); if ($model->save()) { (new UserMeta())->saveNewMeta('topic', $post->id, 'follow'); (new NotificationService())->newReplyNotify(Yii::$app->user->identity, $post, $model, $rawComment); // 评论计数器 Topic::updateAllCounters(['comment_count' => 1], ['id' => $post->id]); // 更新个人总统计 UserInfo::updateAllCounters(['comment_count' => 1], ['user_id' => $model->user_id]); $this->flash("评论成功", 'success'); return $this->redirect(['view', 'id' => $post->id]); } } return $model; }
/** * Reply an existing PostComment model. * If reply is successful, the browser will be redirected to 'update' page. * * @param int $id Find PostComment model based on id as parent. * * @return string */ public function actionReply($id) { $commentParent = $this->findModel($id); $model = new PostComment(['scenario' => 'reply']); if ($model->load(Yii::$app->request->post())) { $model->comment_post_id = $commentParent->comment_post_id; $model->comment_parent = $commentParent->id; if ($model->save()) { $this->redirect(['post-comment/update', 'id' => $model->id]); } } return $this->render('reply', ['commentParent' => $commentParent, 'model' => $model]); }