Example #1
0
 public function add_item($item)
 {
     if ($item instanceof Group) {
         $this->_items[] = clone $item;
     } elseif (isset($item['input'])) {
         $group = new Group(Group::FORM_GROUP);
         // Set Input Label
         if (isset($item['label'])) {
             if (isset($item['label']['show']) and $item['label']['show'] === TRUE) {
                 $attr = isset($item['label']['attr']) ? $item['label']['attr'] : ['for' => $item['input']['name']];
                 $label = new Tag('label', $item['label']['text'], $attr);
                 $label->add_class('control-label');
                 if (isset($item['label']['tag'])) {
                     $label->set_tag($item['label']['tag']);
                 }
                 $group->add_item($label);
             }
         }
         // Set Input
         $input = new Input();
         $input->set_type($item['input']['type']);
         $input->set_attributes($item['input']['attr']);
         if (isset($item['input']['options'])) {
             $input->set_options($item['input']['options']);
         }
         $properties = $item['input'];
         unset($properties['type'], $properties['attr'], $properties['options']);
         $input->set_properties($properties);
         // Set Value
         if (!empty($item['input']['value'])) {
             $input->set_value($item['input']['value'], TRUE);
         }
         if (isset($item['container'])) {
             $container = new Tag('div', $input, $item['container']['attr']);
             if (isset($item['container']['tag'])) {
                 $container->set_tag($item['container']['tag']);
             }
             $group->add_item($container);
         } else {
             $group->add_item($input);
         }
         // Set Help
         if (!empty($item['help'])) {
             $group->add_item(new Tag('span', $item['help'], ['class' => 'help-block help-control']));
         }
         $this->_items[] = $group;
     }
 }
Example #2
0
 public function render()
 {
     switch ($this->_type) {
         case 'text':
             $this->_attributes['type'] = 'text';
             $field = new Tag('input', $this->_attributes);
             break;
         case 'password':
             $this->_attributes['type'] = 'password';
             $field = new Tag('input', $this->_attributes);
             break;
         case 'radio':
             $this->_attributes['type'] = 'radio';
             $field = new Tag('input', $this->_attributes);
             break;
         case 'checkbox':
             $this->_attributes['type'] = 'checkbox';
             $field = new Tag('input', $this->_attributes);
             break;
         case 'number':
             $this->_attributes['type'] = 'number';
             $field = new Tag('input', $this->_attributes);
             break;
         case 'date':
             $this->_attributes['type'] = 'date';
             $field = new Tag('input', $this->_attributes);
             break;
         case 'color':
             $this->_attributes['type'] = 'color';
             $field = new Tag('input', $this->_attributes);
             break;
         case 'month':
             $this->_attributes['type'] = 'month';
             $field = new Tag('input', $this->_attributes);
             break;
         case 'week':
             $this->_attributes['type'] = 'week';
             $field = new Tag('input', $this->_attributes);
             break;
         case 'time':
             $this->_attributes['type'] = 'time';
             $field = new Tag('input', $this->_attributes);
             break;
         case 'datetime':
             $this->_attributes['type'] = 'datetime';
             $field = new Tag('input', $this->_attributes);
             break;
         case 'datetime-local':
             $this->_attributes['type'] = 'datetime-local';
             $field = new Tag('input', $this->_attributes);
             break;
         case 'email':
             $this->_attributes['type'] = 'email';
             $field = new Tag('input', $this->_attributes);
             break;
         case 'search':
             $this->_attributes['type'] = 'search';
             $field = new Tag('input', $this->_attributes);
             break;
         case 'tel':
             $this->_attributes['type'] = 'tel';
             $field = new Tag('input', $this->_attributes);
             break;
         case 'url':
             $this->_attributes['type'] = 'url';
             $field = new Tag('input', $this->_attributes);
             break;
         case 'select':
             $field = new Tag('select', $this->_attributes);
             if (!empty($this->_options)) {
                 foreach ($this->_options as $group => $option) {
                     if (is_array($option)) {
                         $group = new Tag('optgroup', ['label' => $group]);
                         foreach ($option as $value => $label) {
                             $attr = ['value' => $value];
                             if (isset($this->_value)) {
                                 if ($this->_value == $value) {
                                     $attr['selected'] = 'selected';
                                 }
                             }
                             $group->append_content(new Tag('option', $label, $attr));
                         }
                         $field->append_content($group);
                     } elseif (is_string($option) or is_numeric($option)) {
                         $attr = ['value' => $group];
                         if (isset($this->_value)) {
                             if ($this->_value == $group) {
                                 $attr['selected'] = 'selected';
                             }
                         }
                         $field->append_content(new Tag('option', $option, $attr));
                     }
                 }
             }
             break;
         case 'textarea':
             $this->_attributes['type'] = 'textarea';
             $field = new Tag('textarea', $this->_attributes);
             break;
         case 'file':
             $this->_attributes['type'] = 'file';
             $field = new Tag('input', $this->_attributes);
             break;
         case 'featured-image':
             $field = new Tag('div', ['data-role' => 'featured-image']);
             $properties['size'] = isset($this->_properties['size']) ? $this->_properties['size'] : ['width' => 270, 'height' => 270];
             $properties['text'] = isset($this->_properties['text']) ? $this->_properties['text'] : 'Featured Image';
             $image_holder = new Tag('img', array('src' => 'holder.js/' . implode('x', $properties['size']) . '?text=' . $this->_properties['text'], 'data-pattern' => 'image-preview'));
             if (!empty($this->_value)) {
                 $image_holder->add_class('hidden');
                 $image_preview = new Tag('img', array('src' => $this->_value, 'data-role' => 'image-preview'));
                 $field->append_content($image_preview);
             }
             $field->append_content($image_holder);
             $this->_attributes['type'] = 'file';
             $this->_attributes['data-role'] = 'image-input';
             $this->_attributes['accept'] = 'image/*';
             $input_group = new Group(Group::INPUT_GROUP);
             $input_group->add_item(new Tag('span', new Tag('span', '<i class="fa fa-camera"></i>' . new Tag('input', $this->_attributes), array('class' => 'btn btn-file', 'data-role' => 'image-browse', 'title' => 'Browse', 'data-toggle' => 'tooltip')), ['class' => 'input-group-btn']));
             $field->append_content($input_group);
             break;
         case 'textarea':
             $this->_attributes['type'] = 'textarea';
             $field = new Tag('textarea', $this->_attributes);
             break;
         default:
             $field = new Tag($this->_tag, $this->_attributes);
             break;
     }
     $field->add_class('form-control');
     if (isset($this->label)) {
         $group = new Group(Group::FORM_GROUP);
         $group->add_item($this->label);
         $group->add_item($field);
         if (isset($this->help)) {
             $group->add_item($this->help);
         }
         return $group->render();
     }
     return $field->render();
 }