/** * @param array $html_attr * @return null|htmlElement */ public function getTag($html_attr = array()) { $html = null; $attr = $this->getAttr(); $html_attr = array_merge($this->getHTMLAttr(), $html_attr); $value = $this->getValue(); $label = $this->getAttrValue(FormElement::$ATTR_LABEL, ''); $type = $this->getFormType(); switch ($type) { case FormElement::$TYPE_SELECT: $options = $this->getAttrValue(FormElement::$ATTR_CHOICES, array()); $list = array(); if (is_array($options)) { $list = $options; } else { if (is_object($options) && $options instanceof Model) { $results = $options->findQuery()->getResults(); if ($results) { foreach ($results as $val) { $list[$val['id']] = $val['name']; } } } else { if (is_callable($options)) { $list = $options(); } } } $html = new htmlElement('select', $html_attr, null, false); foreach ($list as $k => $v) { $opt = array(); if ($value instanceof Model) { if ($value->getId() == $k) { $opt['selected'] = 'selected'; } } else { if ($value == $k) { $opt['selected'] = 'selected'; } } $opt['value'] = $k; $html->addChilds(new htmlElement('option', $opt, $v, false)); } break; case FormElement::$TYPE_CHECKBOX: $id = $html_attr['id']; $checkbox_attr = array_merge($html_attr, array('type' => $type, 'value' => isset($attr[FormElement::$ATTR_VALUE]) ? $attr[FormElement::$ATTR_VALUE] : '')); if (isset($attr[FormElement::$ATTR_VALUE]) && $attr[FormElement::$ATTR_VALUE] == $value) { $checkbox_attr['checked'] = 'checked'; } $html = new htmlElement(null); $html->addChilds(new htmlElement('input', $checkbox_attr)); $html->addChilds(new htmlElement('label', array('for' => $id), htmlElement::escape($label), false)); break; case FormElement::$TYPE_RADIO: case FormElement::$TYPE_CHECKLIST: $html = new htmlElement(null); if (isset($attr[FormElement::$ATTR_CHOICES])) { foreach ($attr[FormElement::$ATTR_CHOICES] as $key => $v) { $id = $html_attr['id'] . '-' . $key; $name = $this->getFormName() . '[]'; $ctype = 'radio'; if ($type == FormElement::$TYPE_CHECKLIST) { $ctype = 'checkbox'; } $input_attr = array_merge($html_attr, array('type' => $ctype, 'id' => $id, 'name' => $name, 'value' => $key)); if (is_array($value)) { if (in_array($key, $value)) { $input_attr['checked'] = 'checked'; } } else { if ($key == $value) { $input_attr['checked'] = 'checked'; } } $html->addChilds(new htmlElement('input', $input_attr)); $html->addChilds(new htmlElement('label', array('for' => $id), htmlElement::escape($v), false)); } } else { throw new \Exception('Form Type Radio OR Checklist Must Be "choices" attr.'); } break; case FormElement::$TYPE_TEXTAREA: $html = new htmlElement('textarea', $html_attr, $value, false); break; case FormElement::$TYPE_PASSWORD: $html = new htmlElement('input', array_merge($html_attr, array('type' => $type))); break; case FormElement::$TYPE_TEXT: case FormElement::$TYPE_EMAIL: case FormElement::$TYPE_URL: default: $html = new htmlElement('input', array_merge($html_attr, array('type' => $type, 'value' => $value))); break; } return $html; }
/** * Creates a new image element. * * @param array $attributes The attributes that should be assigned to the element. */ public function __construct(array $attributes) { parent::__construct($attributes); }
/** * Creates a new anchor element. * * @param array $attributes The attributes that should be assigned to the element. */ public function __construct($content, array $attributes) { parent::__construct($attributes); $this->content = $content; }