Example #1
0
 /**
  * @param integer|null $item_id
  */
 public function __construct($item_id = null)
 {
     parent::__construct();
     if ($item_id) {
         /** @var $poll PollRecord */
         $poll = PollRecord::findOne($item_id);
         $this->item_id = $poll->id;
         $this->language_id = $poll->language_id;
         $this->question = $poll->question;
         $this->end_date = $poll->end_date ? Yii::$app->formatter->asDate($poll->end_date, 'dd.MM.y') : null;
         if ($poll->active) {
             $this->boxes[] = self::PROPERTY_ACTIVE;
         }
         if ($poll->main) {
             $this->boxes[] = self::PROPERTY_MAIN;
         }
         foreach ($poll->answers as $pollAnswer) {
             $this->answers[] = $pollAnswer->answer;
         }
         $voices = PollAnswerRecord::find()->where(['poll_id' => $poll->id])->andWhere('voices>0')->count();
         $this->isAnswersEditable = !$voices;
     } else {
         $session = Yii::$app->session;
         if (!$session['language_id']) {
             $session['language_id'] = LanguageRecord::getMainLanguageId();
         }
         $this->language_id = $session['language_id'];
         $this->boxes[] = self::PROPERTY_ACTIVE;
         $this->answers[1] = $this->answers[0] = '';
         $this->isAnswersEditable = true;
     }
 }