Example #1
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getChoice()
 {
     return $this->hasOne(YBoardChoice::className(), ['id' => 'choice_id']);
 }
 public function actionUpdatePoll($id)
 {
     $poll = YBoardPoll::findOne($id);
     if ($poll === null) {
         throw new NotFoundHttpException(YBoard::t('yboard', 'The requested poll does not exist.'));
     }
     if (!Yii::$app->user->can('app.forum.forum.update-poll', ['poll' => $poll, 'isModerator' => Yii::$app->user->can('moderator')])) {
         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 = YBoardPost::findOne($poll->post_id);
     if ($poll->user_id != Yii::$app->user->id && !Yii::$app->user->can('moderator')) {
         throw new ForbiddenHttpException(Yii::t('yii', 'You are not authorized to perform this action.'));
     }
     if (isset($_POST['YBoardPoll'])) {
         $poll->attributes = $_POST['YBoardPoll'];
         if (empty($poll->expire_date)) {
             unset($poll->expire_date);
         }
         if ($poll->save()) {
             $choices = $_POST['choice'];
             foreach ($choices as $key => $choice) {
                 $ch = YBoardChoice::findOne($key);
                 if ($ch !== null) {
                     $ch->choice = $choice;
                     $ch->save();
                 }
             }
         }
     }
     $this->redirect(array('topic', 'id' => $post->topic_id));
 }