コード例 #1
0
ファイル: ForumController.php プロジェクト: sourcetoad/bbii2
 public function actionUpdate($id = null)
 {
     $id = is_numeric($id) ?: \Yii::$app->request->get('id');
     $post = BbiiPost::findOne($id);
     if ($post === null) {
         throw new HttpException(404, Yii::t('BbiiModule.bbii', 'The requested post does not exist.'));
     }
     if (($post->user_id != \Yii::$app->user->identity->id || $post->topic->locked) && !$this->isModerator()) {
         throw new HttpException(403, Yii::t('yii', 'You are not authorized to perform this action.'));
     }
     $forum = BbiiForum::findOne($post->forum_id);
     $topic = BbiiTopic::findOne($post->topic_id);
     if (\Yii::$app->request->post('BbiiPost')) {
         $post->attributes = \Yii::$app->request->post('BbiiPost');
         $post->change_id = \Yii::$app->user->identity->id;
         $post->change_time = date('Y-m-d H:m:i');
         if ($forum->moderated) {
             $post->approved = 0;
         } else {
             $post->approved = 1;
         }
         if ($post->validate() && $post->save()) {
             if (!$post->approved) {
                 $forum->updateCounters(array('num_posts' => -1));
                 // method since Yii 1.1.8
                 if ($topic->num_replies > 0) {
                     $topic->updateCounters(array('num_replies' => -1));
                     // method since Yii 1.1.8
                 } else {
                     $topic->approved = 0;
                     $topic->update();
                     $forum->updateCounters(array('num_topics' => -1));
                     // method since Yii 1.1.8
                 }
                 $post->poster->updateCounters(array('posts' => -1));
                 // method since Yii 1.1.8
             }
             return \Yii::$app->response->redirect(array('forum/forum/topic', 'id' => $post->topic_id));
         } else {
             \Yii::$app->session->addFlash('moderation', Yii::t('BbiiModule.bbii', 'Error saving data.'));
         }
     }
     return $this->render('update', array('forum' => $forum, 'topic' => $topic, 'post' => $post));
 }