/** * Render an element * @param object $element * @return string */ protected function element($element) { // set id if (!isset($element->attributes['id'])) { $element->id = $this->id(); $attributes = $element->attributes; $attributes['id'] = $element->id; $element->attributes = $attributes; } else { $element->id = $element->attributes['id']; } // set class if (!isset($element->attributes['class'])) { // set bootstap class $attributes['class'] = ''; } // set label if (!$element->label) { if ($element->text) { $element->label = $this->_text->get($element->text); } else { $element->label = $this->_text->get('label.' . $element->key); } } // set error if ($error = $element->error()) { // create errormessage $element->error = $this->_text->get('error.' . $element->key . '.' . $error); } else { $element->error = FALSE; } // set comment if (!isset($element->comment)) { if ($comment = $this->_text->get('comment.' . $element->key)) { $element->comment = $comment; } else { $element->comment = FALSE; } } // set default view $view = 'form/element'; // render the element itself switch ($element->type) { case 'radio': case 'checkbox': case 'select': if (Arr::is_assoc($element->options) === FALSE) { $options = array(); foreach ($element->options as $value => $label) { $value = $label; $label = $this->_text->get('option.' . $element->key . '.' . $value); $options[$value] = $label; } $element->options = $options; } if ($element->type === 'radio') { $view = 'form/radio'; } if ($element->type === 'checkbox') { $view = 'form/checkbox'; } if ($element->type === 'select') { if (isset($element->first_option)) { $option = $element->first_option; $value = is_array($option) && isset($option['value']) ? $option['value'] : $option; $label = is_array($option) && isset($option['label']) ? $option['label'] : $this->_text->get('option.' . $element->key . '.' . $value); // add to the beginning of options $reverse = array_reverse($element->options, TRUE); $reverse[$value] = $label; $element->options = array_reverse($reverse, TRUE); } // set bootstap class $attributes['class'] .= ' form-control'; $element->html = FormHTML::select($element->name, $element->options, $element->value, $attributes); } break; case 'hidden': $view = FALSE; $element->html = FormHTML::hidden($element->name, $element->value, $attributes); break; case 'submit': $view = FALSE; $label = isset($element->label) ? $element->label : $this->_text->get('label.' . $element->key); $element->html = FormHTML::submit($element->name, $label, $attributes); break; case 'btn': $view = FALSE; $label = isset($element->label) ? $element->label : $this->_text->get('label.' . $element->key); $href = isset($element->href) ? $element->href : '#'; $element->html = HTML::anchor($href, $label, $attributes); break; case 'button': $view = FALSE; $label = isset($element->label) ? $element->label : $this->_text->get('label.' . $element->key); $element->html = FormHTML::button($element->name, $label, $attributes); break; case 'file': $element->html = FormHTML::file($element->name, $attributes); break; case 'image': $element->html = FormHTML::image($element->name, $element->value, $attributes, FALSE); break; case 'text': case 'password': if (!isset($attributes['placeholder'])) { $attributes['placeholder'] = $this->_text->get('placeholder.' . $element->key); } // set bootstap class $attributes['class'] .= ' form-control'; if ($element->type === 'text') { $element->html = FormHTML::input($element->name, $element->value, $attributes); } if ($element->type === 'password') { $element->html = FormHTML::password($element->name, $element->value, $attributes); } break; case 'textarea': $attributes['class'] .= ' form-control'; $element->html = FormHTML::textarea($element->name, $element->value, $attributes, FALSE); break; default: $element->html = View::factory('form/' . $element->type, array('viewer' => $this, 'element' => $element))->render(); } // wrap element if ($view) { $html = View::factory($view, array('element' => $element))->render(); } else { $html = $element->html; } return $html; }