示例#1
0
 /**
  * 创建评论
  * @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;
 }
示例#2
0
 /**
  * 创建评论
  * @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;
 }
示例#4
0
文件: Topic.php 项目: iiyii/getyii
 public function beforeSave($insert)
 {
     if (parent::beforeSave($insert)) {
         if ($this->tags) {
             $this->addTags(explode(',', $this->tags));
         }
         $this->content = TopicService::contentTopic($this->content, $this) . ($this->cc ? t('app', 'cc {username}', ['username' => Yii::$app->user->identity->username]) : '');
         if ($insert) {
             $this->user_id = $this->user_id ?: Yii::$app->user->id;
             $this->type = self::TYPE;
             $this->last_comment_time = $this->created_at;
         }
         return true;
     } else {
         return false;
     }
 }
示例#5
0
 /**
  * 加精华
  * @param $id
  * @return \yii\web\Response
  * @throws NotFoundHttpException
  */
 public function actionExcellent($id)
 {
     $user = Yii::$app->user->identity;
     $model = Topic::findTopic($id);
     if ($user && ($user->isAdmin($user->username) || $user->isSuperAdmin($user->username))) {
         TopicService::excellent($model);
         $this->flash("操作成功", 'success');
         return $this->redirect(['/topic/default/view', 'id' => $model->id]);
     } else {
         throw new NotFoundHttpException();
     }
 }
示例#6
0
 /**
  * 收藏话题
  * @param $type
  * @param $id
  * @return array|string
  */
 public function actionFavorite($type, $id)
 {
     if ($type == 'topic') {
         $topicService = new TopicService();
         list($result, $data) = $topicService->userDoAction($id, 'favorite');
         if ($result) {
             return $this->message('提交成功!', 'success');
         } else {
             return $this->message($data ? $data->getErrors() : '提交失败!');
         }
     }
 }