コード例 #1
0
 public function actionEdit($id)
 {
     $request = Yii::$app->getRequest();
     $me = Yii::$app->getUser()->getIdentity();
     $model = $this->findTopicModel($id, ['content', 'node']);
     if (!$me->canEdit($model)) {
         throw new ForbiddenHttpException('您没有权限修改或已超过可修改时间。');
     }
     if ($me->isAdmin()) {
         $model->scenario = Topic::SCENARIO_ADMIN_EDIT;
     } else {
         $model->scenario = Topic::SCENARIO_AUTHOR_EDIT;
     }
     if (!($content = $model->content)) {
         $content = new TopicContent(['topic_id' => $model->id]);
     }
     $oldTags = $model->tags;
     if ($model->load($request->post()) && $model->validate() && $content->load($request->post()) && $content->validate()) {
         //			$model->tags = Tag::editTags($model->tags, $oldTags);
         $model->tags = Tag::getTags($model->tags);
         $model->save(false) && $content->save(false);
         Tag::afterTopicEdit($model->id, $model->tags, $oldTags);
         (new History(['user_id' => $me->id, 'action' => History::ACTION_EDIT_TOPIC, 'action_time' => $model->updated_at, 'target' => $model->id]))->save(false);
         return $this->redirect(Topic::getRedirectUrl($id, 0, $request->get('ip', 1), $request->get('np', 1)));
     }
     return $this->render('edit', ['model' => $model, 'content' => $content]);
 }