コード例 #1
0
 public function renderField($field, array $options = null)
 {
     if (in_array($field, $this->renderedFields)) {
         return;
     }
     if (!is_array($options)) {
         $options = $this->formatOptions[$field];
     } else {
         $options = array_merge($this->formatOptions[$field], $options);
     }
     if (isset($options['value'])) {
         $value = $options['value'];
     } else {
         if ($options['type'] == 'extra') {
             $value = $options['title'];
         } else {
             $value = isset($this->item->{$field}) ? $this->item->{$field} : $options['default_value'];
         }
     }
     $type = $options['type'];
     $escapedValue = htmlspecialchars($value);
     $fieldId = 'field_' . $field;
     $commonAttrs = ' name="' . $field . '" id="' . $fieldId . '" ';
     if ($options['required']) {
         $commonAttrs .= ' required ';
     }
     $label = '<label for="' . $fieldId . '">' . $options['label'] . '</label>';
     echo '<div class="form-group"> ';
     $method = 'render' . strtoupper($options['type'] . 'Field');
     if (method_exists($this, $method)) {
         $this->{$method}();
     } else {
         if ($options['readonly'] && in_array($options['type'], ['text', 'textarea', 'password', 'select'])) {
             echo $label . ': ' . $escapedValue;
         } else {
             if ($options['type'] == 'hidden') {
                 echo '<input type="hidden" value="' . $escapedValue . '" ' . $commonAttrs . ' />';
             } else {
                 if ($options['type'] == 'textarea') {
                     echo $label . '<textarea cols="40" rows="4" ' . $commonAttrs . ' ' . 'class="form-control ' . $options['class_names'] . '">' . $escapedValue . '</textarea>';
                 } else {
                     if ($type == 'select') {
                         $optionList = $options['option_list'];
                         if (is_callable($optionList)) {
                             $optionList = call_user_func_array($optionList, [$this->pixie, $options]);
                         } else {
                             if (!is_array($optionList)) {
                                 $optionList = ArraysHelper::arrayFillEqualPairs([$optionList]);
                             }
                         }
                         echo $label . '<br>' . $this->renderSelect($value, $optionList, array_merge(['name' => $field, 'id' => $fieldId, 'class' => 'form-control ' . $options['class_names']], $options['required'] ? ['required' => 'required'] : []));
                     } else {
                         if ($options['type'] == 'image') {
                             echo $label;
                             if ($value) {
                                 if ($options['use_external_dir']) {
                                     $src = "/upload/download.php?image=" . $escapedValue;
                                 } else {
                                     $src = htmlspecialchars($options['dir_path'] . $value);
                                 }
                                 echo '<br><img src="' . $src . '" alt="" ' . 'class="model-image model-' . htmlspecialchars($this->item->model_name) . '-image" /> <br>' . '<label><input type="checkbox" name="remove_image_' . htmlspecialchars($field) . '" /> Remove image</label>';
                             }
                             echo '<br><input type="file" ' . $commonAttrs . ' class="file-input btn btn-default btn-primary btn-lg" ' . 'title="Select image" value="' . $escapedValue . '">';
                         } else {
                             if ($options['type'] == 'boolean') {
                                 $checked = $value ? ' checked ' : (!$this->item->loaded() && $options['default_value'] ? ' checked ' : '');
                                 echo $label . ' <input type="checkbox" ' . $commonAttrs . $checked . ' class="form-horizontal ' . $options['class_names'] . '" value="1" />';
                             } else {
                                 $dataType = $options['data_type'] == 'email' ? 'email' : 'text';
                                 echo $label . '<input type="' . $dataType . '" value="' . $escapedValue . '" ' . $commonAttrs . ' class="form-control ' . $options['class_names'] . '"/>';
                             }
                         }
                     }
                 }
             }
         }
     }
     echo '</div>';
     $this->renderedFields[] = $field;
 }