/**
  * Creating the thread of given category ID and forum ID.
  * @param integer $cid
  * @param integer $fid
  * @return string|\yii\web\Response
  */
 public function actionNewThread($cid = null, $fid = null)
 {
     if (!User::can(Rbac::PERM_CREATE_THREAD)) {
         if (Yii::$app->user->isGuest) {
             $this->warning(Yii::t('podium/flash', 'Please sign in to create a new thread.'));
             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) {
             $this->error(Yii::t('podium/flash', 'Sorry! We can not find the forum 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 forum 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 forum you are looking for.'));
                 return $this->redirect(['default/index']);
             } else {
                 $model = new Thread();
                 $model->setScenario('new');
                 $postData = Yii::$app->request->post();
                 $preview = '';
                 $model->subscribe = 1;
                 if ($model->load($postData)) {
                     $model->posts = 0;
                     $model->views = 0;
                     $model->category_id = $category->id;
                     $model->forum_id = $forum->id;
                     $model->author_id = User::loggedId();
                     if ($model->validate()) {
                         if (isset($postData['preview-button'])) {
                             $preview = $model->post;
                         } else {
                             $transaction = Thread::getDb()->beginTransaction();
                             try {
                                 if ($model->save()) {
                                     $forum->updateCounters(['threads' => 1]);
                                     $post = new Post();
                                     $post->content = $model->post;
                                     $post->thread_id = $model->id;
                                     $post->forum_id = $model->forum_id;
                                     $post->author_id = User::loggedId();
                                     $post->likes = 0;
                                     $post->dislikes = 0;
                                     if ($post->save()) {
                                         $post->markSeen();
                                         $forum->updateCounters(['posts' => 1]);
                                         $model->updateCounters(['posts' => 1]);
                                         $model->touch('new_post_at');
                                         $model->touch('edited_post_at');
                                         if ($model->subscribe) {
                                             $subscription = new Subscription();
                                             $subscription->user_id = User::loggedId();
                                             $subscription->thread_id = $model->id;
                                             $subscription->post_seen = Subscription::POST_SEEN;
                                             $subscription->save();
                                         }
                                     }
                                 }
                                 $transaction->commit();
                                 Cache::getInstance()->delete('forum.threadscount');
                                 Cache::getInstance()->delete('forum.postscount');
                                 Cache::getInstance()->deleteElement('user.threadscount', User::loggedId());
                                 Cache::getInstance()->deleteElement('user.postscount', User::loggedId());
                                 Cache::getInstance()->delete('forum.latestposts');
                                 Log::info('Thread added', $model->id, __METHOD__);
                                 $this->success(Yii::t('podium/flash', 'New thread has been created.'));
                                 return $this->redirect(['thread', 'cid' => $category->id, 'fid' => $forum->id, 'id' => $model->id, 'slug' => $model->slug]);
                             } catch (Exception $e) {
                                 $transaction->rollBack();
                                 Log::error($e->getMessage(), null, __METHOD__);
                                 $this->error(Yii::t('podium/flash', 'Sorry! There was an error while creating the thread. Contact administrator about this problem.'));
                             }
                         }
                     }
                 }
             }
         }
         return $this->render('new-thread', ['preview' => $preview, 'model' => $model, 'category' => $category, 'forum' => $forum]);
     }
 }