public function __construct($name, $options, $selectedValue = '', $type = self::TYPE_RADIO, $validator = null) { static::$instanceCount++; HtmlElement::__construct('div'); $this->validator = $validator; $this->type = $type; $this->setAttribute('name', $name); $selectedValues = array(); if ($type == self::TYPE_RADIO) { $selectedValues = array($selectedValue); } else { $selectedValues = explode(',', $selectedValue); } $i = 0; foreach ($options as $key => $value) { $optionId = 'data-element-group-' . static::$instanceCount . '-' . $i; $option = new HtmlElement('input'); $option->setAttribute('type', $this->type); $option->setAttribute('name', $name); $option->setAttribute('id', $optionId); $option->setAttribute('value', $value); if (in_array($key, $selectedValues)) { $option->setAttribute('checked'); } $label = new HtmlElement('label'); $label->setAttribute('for', $optionId); $label->addChild(new TextElement($value)); $this->addChild($option); $this->addChild($label); $i++; } }
/** * Add children to first fieldset */ public function addChild(HtmlElement $element, HtmlElement $reference = null, $before = false) { if ($element instanceof FieldsetElement) { parent::addChild($element, $reference, $before); return; } if (!count($this->getChildren())) { $this->addChild(new FieldsetElement()); } current($this->getChildren())->addChild($element, $reference, $before); }
/** * Shorten string if mor that max - and outputs span with original text in title * @param int max | max length * @container */ protected function tagShort($attrs) { $max = intval($attrs->max); $string = trim($this->body()); $orig = $string; if (strlen($string) > $max) { $string = substr($string, 0, $max - 3) . '...'; } $span = new HtmlElement('span', array('title' => $orig)); $span->addChild(new HtmlText($string)); return $span; }
/** * Renders a button. All attributes not listed below will be forwarded to the actual html element * @param string elm | tag type - defaults to a * @param string class | CSS class to apply to button * @param string event | event to trigger when clicked * @deprecated * @container */ protected function tagButton($attrs, $view) { if (!$attrs->elm) { $attrs->elm = 'a'; } if (!$attrs->class) { $attrs->class = ''; } $class = $this->evt2css($attrs->event); $attrs->class = trim(trim(self::CSS_BTN . ' ' . $class) . ' ' . $attrs->class); $elmAttr = ArrayUtil::fromObject($attrs); unset($elmAttr['elm']); unset($elmAttr['event']); $elm = new HtmlElement($attrs->elm, $elmAttr); $elm->addChild(new HtmlText(trim($this->body()))); return $elm; }
/** * Render form tags * @param boolean binary | if set - renders a multipart form - else a url encoded form. * @param string controller | if set - sets the controller of the target url for this form * @param string action | if set - sets the action of the target url for this form * @param string parms | if set - sets the parameters of the target url for this form * @param array|object|json data | A map of data for this form - will be propegated to fields within this form. * @param string method | sets the HTTP method to used (post or get) - defaults to post * @param string url | sets the url of the form - only used if controller is not set * @param string id | sets the id on the form element * @param string class | sets the css class for the form element */ protected function tagForm($attrs, $view) { if ($attrs->binary) { $attrs->enctype = 'multipart/form-data'; } else { $attrs->enctype = 'application/x-www-form-urlencoded '; } if (!$attrs->controller && !$attrs->action && !$attrs->parms) { if (!$attrs->controller) { $attrs->controller = Pimple::instance()->getController(); if (!$attrs->action) { $attrs->action = Pimple::instance()->getAction(); } } $attrs->url = Url::makeLink($attrs->controller, $attrs->action, $_SERVER['QUERY_STRING']); } else { if ($attrs->action && $attrs->controller) { $attrs->url = Url::makeLink($attrs->controller, $attrs->action, $attrs->parms); } } unset($attrs->binary); if ($attrs->data) { $this->formData = $this->toObject($attrs->data); } else { $this->formData = $view->data; } $attrs->method = strtolower($attrs->method ? $attrs->method : 'post'); $this->formMethod = $attrs->method; $elm = new HtmlElement('form'); $elm->setAttribute('method', $attrs->method); $elm->setAttribute('enctype', $attrs->enctype); $elm->setAttribute('action', $attrs->url); if ($attrs->id) { $elm->setAttribute('id', $attrs->id); } if ($attrs->class) { $elm->setAttribute('class', $attrs->class); } $elm->addChild(new HtmlText($this->body())); return $elm->toHtml(); }
/** * Renders a anchor with a properly formatted url based on the parameters * @param string controller | the controller name - defaults to 'index' * @param string action | the action - defaults to 'index' * @param object parms | Parameters to append as GET parms to the url - defaults to most attributes on tag */ protected function tagLink($attrs, $view) { $linkAttrs = new stdClass(); $tagAttrs = new stdClass(); if ($attrs->parms) { //If parms argument is found restrict url to parms, controller and action attributes $linkAttrs = $this->toObject($attrs->parms); if ($attrs->controller) { $linkAttrs->controller = $attrs->controller; } if ($attrs->action) { $linkAttrs->action = $attrs->action; } $tagAttrs = $attrs; unset($tagAttrs->controller); unset($tagAttrs->action); unset($tagAttrs->parms); } else { //If parms argument not present - allow only "class","style" and "title" for tag - assume rest is for url $tagAttrs = new stdClass(); if ($attrs->class) { $tagAttrs->class = $attrs->class; } if ($attrs->style) { $tagAttrs->style = $attrs->style; } if ($attrs->title) { $tagAttrs->title = $attrs->title; } if ($attrs->rel) { $tagAttrs->rel = $attrs->rel; } $linkAttrs = $attrs; unset($linkAttrs->class); unset($linkAttrs->style); unset($linkAttrs->title); unset($linkAttrs->rel); } $link = $this->url($linkAttrs, $this->body(), $view); if (!$this->body()) { $this->body($link); } $tagAttrs->href = $link; $a = new HtmlElement('a', $tagAttrs); $a->addChild(new HtmlText(trim($this->body()))); return $a; }