/**
  * Creating the post of given category ID, forum ID and thread ID.
  * This can be reply to selected post of given ID.
  * @param integer $cid
  * @param integer $fid
  * @param integer $tid
  * @param integer $pid
  * @return string|\yii\web\Response
  */
 public function actionPost($cid = null, $fid = null, $tid = null, $pid = null)
 {
     if (!User::can(Rbac::PERM_CREATE_POST)) {
         if (Yii::$app->user->isGuest) {
             $this->warning(Yii::t('podium/flash', 'Please sign in to post a reply.'));
             return $this->redirect(['account/login']);
         } else {
             $this->error(Yii::t('podium/flash', 'Sorry! You do not have the required permission to perform this action.'));
             return $this->redirect(['default/index']);
         }
     } else {
         if (!is_numeric($cid) || $cid < 1 || !is_numeric($fid) || $fid < 1 || !is_numeric($tid) || $tid < 1) {
             $this->error(Yii::t('podium/flash', 'Sorry! We can not find the thread you are looking for.'));
             return $this->redirect(['default/index']);
         }
         $category = Category::findOne((int) $cid);
         if (!$category) {
             $this->error(Yii::t('podium/flash', 'Sorry! We can not find the thread you are looking for.'));
             return $this->redirect(['default/index']);
         } else {
             $forum = Forum::find()->where(['id' => (int) $fid, 'category_id' => $category->id])->limit(1)->one();
             if (!$forum) {
                 $this->error(Yii::t('podium/flash', 'Sorry! We can not find the thread you are looking for.'));
                 return $this->redirect(['default/index']);
             } else {
                 $thread = Thread::find()->where(['id' => (int) $tid, 'category_id' => $category->id, 'forum_id' => $forum->id])->limit(1)->one();
                 if (!$thread) {
                     $this->error(Yii::t('podium/flash', 'Sorry! We can not find the thread you are looking for.'));
                     return $this->redirect(['default/index']);
                 } else {
                     if ($thread->locked == 0 || $thread->locked == 1 && User::can(Rbac::PERM_UPDATE_THREAD, ['item' => $thread])) {
                         $model = new Post();
                         $model->subscribe = 1;
                         $postData = Yii::$app->request->post();
                         $replyFor = null;
                         if (is_numeric($pid) && $pid > 0) {
                             $replyFor = Post::findOne((int) $pid);
                             if ($replyFor) {
                                 if (isset($postData['quote']) && !empty($postData['quote'])) {
                                     $model->content = Helper::prepareQuote($replyFor, $postData['quote']);
                                 } else {
                                     $model->content = Helper::prepareQuote($replyFor);
                                 }
                             }
                         }
                         $preview = '';
                         $previous = Post::find()->where(['thread_id' => $thread->id])->orderBy(['id' => SORT_DESC])->limit(1)->one();
                         if ($model->load($postData)) {
                             $model->thread_id = $thread->id;
                             $model->forum_id = $forum->id;
                             $model->author_id = User::loggedId();
                             if ($model->validate()) {
                                 if (isset($postData['preview-button'])) {
                                     $preview = $model->content;
                                 } else {
                                     $transaction = Post::getDb()->beginTransaction();
                                     try {
                                         $id = null;
                                         if ($previous->author_id == User::loggedId()) {
                                             $previous->content .= '<hr>' . $model->content;
                                             $previous->edited = 1;
                                             $previous->edited_at = time();
                                             if ($previous->save()) {
                                                 $previous->markSeen();
                                                 $thread->touch('edited_post_at');
                                                 $id = $previous->id;
                                             }
                                         } else {
                                             if ($model->save(false)) {
                                                 $model->markSeen();
                                                 $forum->updateCounters(['posts' => 1]);
                                                 $thread->updateCounters(['posts' => 1]);
                                                 $thread->touch('new_post_at');
                                                 $thread->touch('edited_post_at');
                                                 $id = $model->id;
                                             }
                                         }
                                         if ($id !== null) {
                                             Subscription::notify($thread->id);
                                             if ($model->subscribe && !$model->thread->subscription) {
                                                 $subscription = new Subscription();
                                                 $subscription->user_id = User::loggedId();
                                                 $subscription->thread_id = $model->thread->id;
                                                 $subscription->post_seen = Subscription::POST_SEEN;
                                                 $subscription->save();
                                             }
                                             $transaction->commit();
                                             Cache::getInstance()->delete('forum.postscount');
                                             Cache::getInstance()->deleteElement('user.postscount', User::loggedId());
                                             Cache::getInstance()->delete('forum.latestposts');
                                             Log::info('Post added', $model->id, __METHOD__);
                                             $this->success(Yii::t('podium/flash', 'New reply has been added.'));
                                             return $this->redirect(['default/show', 'id' => $id]);
                                         } else {
                                             throw new Exception('Saved Post ID missing.');
                                         }
                                     } catch (Exception $e) {
                                         $transaction->rollBack();
                                         Log::error($e->getMessage(), null, __METHOD__);
                                         $this->error(Yii::t('podium/flash', 'Sorry! There was an error while adding the reply. Contact administrator about this problem.'));
                                     }
                                 }
                             }
                         }
                         return $this->render('post', ['replyFor' => $replyFor, 'preview' => $preview, 'model' => $model, 'category' => $category, 'forum' => $forum, 'thread' => $thread, 'previous' => $previous]);
                     } else {
                         $this->info(Yii::t('podium/flash', 'This thread is locked.'));
                         return $this->redirect(['default/thread', 'cid' => $category->id, 'fid' => $forum->id, 'id' => $thread->id, 'slug' => $thread->slug]);
                     }
                 }
             }
         }
     }
 }
Example #2
0
 /**
  * Creating the post of given category ID, forum ID and thread ID.
  * This can be reply to selected post of given ID.
  * @param integer $cid category's ID
  * @param integer $fid forum's ID
  * @param integer $tid thread's ID
  * @param integer $pid ID of post to reply to
  * @return string|\yii\web\Response
  */
 public function actionPost($cid = null, $fid = null, $tid = null, $pid = null)
 {
     if (Yii::$app->user->isGuest) {
         $this->warning(Yii::t('podium/flash', 'Please sign in to update the thread.'));
         return $this->redirect(['account/login']);
     }
     $thread = Thread::find()->where(['id' => $tid, 'category_id' => $cid, 'forum_id' => $fid])->limit(1)->one();
     if (empty($thread)) {
         $this->error(Yii::t('podium/flash', 'Sorry! We can not find the thread you are looking for.'));
         return $this->redirect(['default/index']);
     }
     if ($thread->locked == 1 && !User::can(Rbac::PERM_UPDATE_THREAD, ['item' => $thread])) {
         $this->info(Yii::t('podium/flash', 'This thread is locked.'));
         return $this->redirect(['default/thread', 'cid' => $thread->forum->category->id, 'fid' => $thread->forum->id, 'id' => $thread->id, 'slug' => $thread->slug]);
     }
     if (!User::can(Rbac::PERM_CREATE_POST)) {
         $this->error(Yii::t('podium/flash', 'Sorry! You do not have the required permission to perform this action.'));
         return $this->redirect(['default/index']);
     }
     $model = new Post();
     $model->subscribe = 1;
     $postData = Yii::$app->request->post();
     $replyFor = null;
     if (is_numeric($pid) && $pid > 0) {
         $replyFor = Post::find()->where(['id' => $pid])->limit(1)->one();
         if ($replyFor) {
             $model->content = Helper::prepareQuote($replyFor, Yii::$app->request->post('quote'));
         }
     }
     $preview = '';
     $previous = Post::find()->where(['thread_id' => $thread->id])->orderBy(['id' => SORT_DESC])->limit(1)->one();
     if ($model->load($postData)) {
         $model->thread_id = $thread->id;
         $model->forum_id = $thread->forum->id;
         $model->author_id = User::loggedId();
         if ($model->validate()) {
             if (isset($postData['preview-button'])) {
                 $preview = $model->content;
             } else {
                 if ($model->podiumNew($previous)) {
                     $this->success(Yii::t('podium/flash', 'New reply has been added.'));
                     if (!empty($previous) && $previous->author_id == User::loggedId()) {
                         return $this->redirect(['default/show', 'id' => $previous->id]);
                     }
                     return $this->redirect(['default/show', 'id' => $model->id]);
                 } else {
                     $this->error(Yii::t('podium/flash', 'Sorry! There was an error while adding the reply. Contact administrator about this problem.'));
                 }
             }
         }
     }
     return $this->render('post', ['replyFor' => $replyFor, 'preview' => $preview, 'model' => $model, 'thread' => $thread, 'previous' => $previous]);
 }