/** * * Cria uma tag HTML * * @param string $tag * @param array $options * @param boolean $close * @param string $label * @return string */ public function tags($tag, $options = [], $close = true, $label = null) { $tag = strtolower($tag); $return = '<' . $tag . ' '; if (count($options) > 0) { foreach ($options as $key => $value) { $key = (string) $key; $return .= \Core\Inflector::parameterize($key) . '="' . trim($value) . '" '; } } if ($close) { return $return . '>' . $label . '</' . $tag . '>'; } return $return . '/>'; }
/** * * Cria um input * * @param string $field * @param array $options * @return string */ public function input($field, $options = []) { $default = ['type' => 'text', 'name' => $this->getNameChave($field), 'id' => $this->getId($field), 'value' => $this->request->data($field), 'label' => '', 'autocomplete' => 'off', 'div' => []]; $options = \Core\Hash::merge($default, $options); if (isset($options['required'])) { if ((bool) $options['required'] === false) { unset($options['required']); } else { $options['required'] = 'required'; } } if (!in_array($options['type'], $this->types)) { $options['type'] = 'text'; } $label = ''; if ($options['label'] !== false) { if ($options['type'] != 'checkbox' and $options['type'] != 'radio') { $label = $this->label($options['label'], ['for' => $options['id']]); } } $classField = 'field'; if ($options['type'] == 'radio' or $options['type'] == 'checkbox') { $classField = ''; } $div = ['class' => $options['type'] . ' ' . $classField . ' ' . (isset($options['required']) ? 'required' : '')]; if (!empty($options['div'])) { if (isset($options['div']['class'])) { $options['div']['class'] .= ' ' . $div['class']; } $div = \Core\Hash::merge($div, $options['div']); } unset($options['div']); if ($options['type'] == 'hidden') { return $this->{\Core\Inflector::parameterize($options['type'], '_')}($options); } $error = ''; if (!empty($this->error[$field])) { $div['class'] .= ' has-error'; $error = '<div style="padding: 5px; color: red;">' . implode('<br />', $this->error[$field]) . '</div>'; unset($this->error[$field]); } if ($this->_label == 'left') { return $this->html->tags('div', $div, true, $label . $this->{\Core\Inflector::parameterize($options['type'], '_')}($options) . $error); } else { return $this->html->tags('div', $div, true, $this->{\Core\Inflector::parameterize($options['type'], '_')}($options) . $label . $error); } }
/** * * Padroniza o id * * @param string $name * @param string|null $prefix * @return string */ public function getId($name, $prefix = null) { if (!is_null($prefix)) { $name = Inflector::parameterize($prefix . '-' . $name); } $name = Inflector::parameterize($name); return str_replace('_', '-', $name); }