Example #1
0
 /**
  * Returns whether value is set for given name
  *
  * @param string $name
  * @return bool
  */
 public function has($name)
 {
     if ($this->container instanceof WP_Session) {
         $result = $this->container->offsetExists($name);
     } else {
         $result = $this->container->has($name);
     }
     return $result;
 }
Example #2
0
 /**
  * Заключительная сборка готовой формы
  *
  * @return string
  */
 public function display()
 {
     // Информационные сообщения об ошибке, или успехе
     $message = '';
     $saved = filter_has_var(INPUT_GET, 'saved');
     if ($this->infoMessages !== false && $this->isSubmitted || $saved) {
         if ($this->isValid || $saved) {
             // Сообщение об удачном сохранении данных
             $message = sprintf($this->infoMessages, 'alert-success', $this->successMessage);
             if ($this->confirmation) {
                 // Если задано отдельное окно подтверждения
                 //TODO: Переделать App
                 $message .= '<a class="btn btn-primary" href="' . ($this->continueLink === null ? \App::getInstance()->request()->getBaseUrl() : $this->continueLink) . '/">' . _s('Continue') . '</a>';
                 return $message;
             }
         } else {
             // Сообщение, что имеются ошибки
             $message = sprintf($this->infoMessages, 'alert-danger', $this->errorMessage);
         }
     }
     $out = [];
     foreach ($this->fields as &$element) {
         // Создаем элемент формы
         switch ($element['type']) {
             case 'html':
                 $out[] = $element['content'];
                 break;
             case 'captcha':
                 $captcha = new Captcha();
                 $code = $captcha->generateCode();
                 $this->session->offsetSet('captcha', $code);
                 $out[] = '<img alt="' . _s('If you do not see the picture with the code, turn the graphics support in your browser and refresh the page') . '" width="' . $captcha->width . '" height="' . $captcha->height . '" src="' . $captcha->generateImage($code) . '"/>';
                 break;
             default:
                 if ($this->isSubmitted && isset($element['reset_value'])) {
                     $element['value'] = $element['reset_value'];
                 }
                 $out[] = (new Fields($element))->display();
         }
     }
     // Добавляем токен валидации
     if (!$this->session->offsetExists('form_token')) {
         $this->session->offsetSet('form_token', md5(rand(1, 1000) . microtime()));
     }
     $out[] = (new Fields(['type' => 'hidden', 'name' => 'form_token', 'value' => $this->session->offsetGet('form_token')]))->display();
     return sprintf(PHP_EOL . $message . PHP_EOL . '<form role="form" name="%s" method="%s"%s%s%s>%s</form>' . PHP_EOL, $this->form['name'], $this->form['method'], isset($this->form['action']) ? ' action="' . $this->form['action'] . '"' : '', isset($this->form['enctype']) ? ' enctype="multipart/form-data"' : '', isset($this->form['class']) ? ' class="' . $this->form['class'] . '"' : '', PHP_EOL . implode(PHP_EOL, $out) . PHP_EOL);
 }