Example #1
0
 /**
  * Create a new topic w/i a forum
  * @return [type] [description]
  */
 public function actionCreatetopic()
 {
     $choiceArr = null;
     $poll = new BbiiPoll();
     $post = new BbiiPost();
     if (\Yii::$app->request->post('BbiiForum')) {
         $post->forum_id = \Yii::$app->request->post('BbiiForum')['id'];
         $forum = BbiiForum::find($post->forum_id);
     }
     if (isset(\Yii::$app->request->post()['choice'])) {
         $choiceArr = \Yii::$app->request->post()['choice'];
         while (count($choiceArr) < 3) {
             $choiceArr[] = '';
         }
     } else {
         $choiceArr = array('', '', '');
     }
     if (\Yii::$app->request->post('BbiiPost')) {
         $forum = BbiiForum::findOne(\Yii::$app->request->post('BbiiPost')['forum_id']);
         $post->load(\Yii::$app->request->post());
         $post->approved = $forum->moderated ?: 1;
         $post->create_time = date('Y-m-d H:m:i');
         //$post->forum_id    = \Yii::$app->request->post('BbiiForum')['id'];
         $post->user_id = \Yii::$app->user->identity->id;
         // if the post is valid, create the topic
         if ($post->validate() && $post->save()) {
             // Topic
             $topic = new BbiiTopic();
             $topic->approved = $post->approved;
             $topic->first_post_id = $post->id;
             $topic->forum_id = $forum->id;
             $topic->last_post_id = $post->id;
             $topic->title = $post->subject;
             $topic->user_id = \Yii::$app->user->identity->id;
             if (\Yii::$app->request->post('sticky')) {
                 $topic->sticky = 1;
             }
             if (\Yii::$app->request->post('global')) {
                 $topic->global = 1;
             }
             if (\Yii::$app->request->post('locked')) {
                 $topic->locked = 1;
             }
             // @todo Enable this feature - DJE : 2015-05-26
             /*
             // Poll
             if (isset(\Yii::$app->request->post()['BbiiPoll']) && isset(\Yii::$app->request->post()['addPoll']) && \Yii::$app->request->post()['addPoll'] == 'yes') {
                 $poll->load(\Yii::$app->request->post()['BbiiPoll']);
                 $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 = \Yii::$app->request->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', Yii::t('BbiiModule.bbii','A poll should have at least 2 choices.'));
                     }
                 }
             } else {
             
                 $correct = true;
             }
             */
             //if ($correct && $topic->save()) {
             if ($topic->validate() && $topic->save()) {
                 // update post with topic
                 $post->topic_id = $topic->id;
                 $post->update();
                 if (!$forum->moderated) {
                     $forum->updateCounters(array('num_posts' => 1, 'num_topics' => 1));
                     // method since Yii 2.0
                     //$post->poster->updateCounters(array('posts' => 1));                    // method since Yii 2.0
                     $forum->last_post_id = $post->id;
                     $forum->update();
                     $this->assignMembergroup(\Yii::$app->user->identity->id);
                 } else {
                     \Yii::$app->session->addFlash('moderation', Yii::t('BbiiModule.bbii', '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.'));
                 }
                 // @todo Enable this feature - DJE : 2015-05-26
                 /*
                 if (isset(\Yii::$app->request->post()['BbiiPoll'])) {
                     // @todo Enable this feature - DJE : 2015-05-26
                     // $poll->save(); 
                     $choices = \Yii::$app->request->post()['choice'];
                     $i = 1;
                     foreach($choices as $choice) {
                         if (!empty($choice)) {
                             $ch          = new BbiiChoice;
                             $ch->choice  = $choice;
                             $ch->poll_id = $poll->id;
                             $ch->sort    = $i++;
                 
                             $ch->save();
                         }
                     }
                 }
                 */
                 return \Yii::$app->response->redirect(array('forum/forum/topic', 'id' => $topic->id));
             } else {
                 \Yii::$app->session->addFlash('warning', Yii::t('BbiiModule.bbii', 'Error, unable to save post.'));
                 $post->delete();
             }
         }
     }
     return $this->render('update/forum', array('choices' => $choiceArr, 'forum' => $forum, 'poll' => $poll, 'post' => $post));
 }