/**
  * Creates a new YBoardForum model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreateTopic()
 {
     if (!Yii::$app->user->can('app.forum.forum.create-topic')) {
         throw new ForbiddenHttpException(YBoard::t('yboard', 'You have no enough permission to access this page! If you think its a mistake, please consider reporting to us.'));
     }
     $post = new YBoardPost();
     //top post aka OP
     $post->original_post = 1;
     $poll = new YBoardPoll();
     if (isset($_POST['YBoardForum'])) {
         $post->forum_id = $_POST['YBoardForum']['id'];
         $forum = YBoardForum::findOne($post->forum_id);
     }
     if (isset($_POST['choice'])) {
         $choiceArr = $_POST['choice'];
         while (count($choiceArr) < 3) {
             $choiceArr[] = '';
         }
     } else {
         $choiceArr = array('', '', '');
     }
     //print_r(Yii::$app->request->post());die();
     if ($post->load(Yii::$app->request->post())) {
         //$post->attributes = $_POST['YBoardPost'];
         $forum = YBoardForum::findOne($post->forum_id);
         $post->setAttributes(['approved' => $forum->moderated ? 0 : 1, 'user_id' => Yii::$app->user->identity->id]);
         if ($post->save()) {
             // Topic
             $topic = new YBoardTopic();
             $topic->forum_id = $forum->id;
             $topic->title = $post->subject;
             $topic->first_post_id = $post->id;
             $topic->last_post_id = $post->id;
             $topic->approved = $post->approved;
             if (isset($_POST['sticky'])) {
                 $topic->sticky = 1;
             }
             if (isset($_POST['global'])) {
                 $topic->global = 1;
             }
             if (isset($_POST['locked'])) {
                 $topic->locked = 1;
             }
             // Poll
             if (isset($_POST['YBoardPoll']) && isset($_POST['addPoll']) && $_POST['addPoll'] == 'yes') {
                 $poll->attributes = $_POST['YBoardPoll'];
                 $poll->post_id = $post->id;
                 $poll->user_id = Yii::$app->user->identity->id;
                 if (empty($poll->expire_date)) {
                     unset($poll->expire_date);
                 }
                 $count = 0;
                 $choices = $_POST['choice'];
                 foreach ($choices as $choice) {
                     if (!empty($choice)) {
                         $count++;
                     }
                 }
                 if ($poll->validate() && $count > 1) {
                     $correct = true;
                 } else {
                     $correct = false;
                     if ($correct < 2) {
                         $poll->addError('question', YBoard::t('yboard', 'A poll should have at least 2 choices.'));
                     }
                 }
             } else {
                 $correct = true;
             }
             if ($correct && $topic->save()) {
                 $post->topic_id = $topic->id;
                 $post->save();
                 if (!$forum->moderated) {
                     $forum->updateCounters(['num_posts' => 1, 'num_topics' => 1]);
                     $forum->last_post_id = $post->id;
                     $forum->save();
                 } else {
                     Yii::$app->session->setFlash('moderation', YBoard::t('yboard', 'Your post has been saved. It has been placed in a queue and is now waiting for approval by a moderator before it will appear on the forum. Thank you for your contribution to the forum.'));
                 }
                 if (isset($_POST['YBoardPoll'])) {
                     $poll->save();
                     $choices = $_POST['choice'];
                     $i = 1;
                     foreach ($choices as $choice) {
                         if (!empty($choice)) {
                             $ch = new YBoardChoice();
                             $ch->choice = $choice;
                             $ch->poll_id = $poll->id;
                             $ch->sort = $i++;
                             $ch->save();
                         }
                     }
                 }
                 return $this->redirect(['topic', 'id' => $topic->id]);
             } else {
                 $post->delete();
             }
         }
     }
     return $this->render('create', ['forum' => $forum, 'post' => $post, 'poll' => $poll, 'choices' => $choiceArr]);
 }