/** * 新建话题 * @return mixed */ public function actionCreate() { $model = new Topic(); if ($model->load(Yii::$app->request->post()) && $model->validate()) { $topService = new TopicService(); if (!$topService->filterContent($model->title) || !$topService->filterContent($model->content)) { $this->flash('请勿发表无意义的内容', 'warning'); return $this->redirect('create'); } $model->user_id = Yii::$app->user->id; $model->type = 'topic'; if ($model->tags) { $model->addTags(explode(',', $model->tags)); } $rawContent = $model->content; $model->content = TopicService::replace($rawContent); if ($model->save()) { (new UserMeta())->saveNewMeta('topic', $model->id, 'follow'); (new NotificationService())->newPostNotify(Yii::$app->user->identity, $model, $rawContent); // 更新个人总统计 UserInfo::updateAllCounters(['post_count' => 1], ['user_id' => $model->user_id]); $this->flash('发表文章成功!', 'success'); return $this->redirect(['view', 'id' => $model->id]); } } else { return $this->render('create', ['model' => $model]); } }
public function beforeSave($insert) { if (parent::beforeSave($insert)) { if ($this->tags) { $this->addTags(explode(',', $this->tags)); } $this->content = TopicService::replace($this->content) . ($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; } }