/** * A widget containing a dropdown selector * @param string $name The HTML name of the element * @param string[][] $options An array of options, each of which is an array of value and title * @param string $selected The value of the selected item in the dropdown * @param HtmlAttributes $args Allows custom html tag arguments to be specified (not recommended) **/ public function __construct($name, $options, $selected = null, $args = null) { parent::__construct('select', array('name' => $name, 'id' => $name)); $this->AddAttributes($args); $option_value = RTK_EMPTYSTRING; $option_title = RTK_EMPTYSTRING; foreach ($options as $option) { if (RTK::ArrayIsLongerThan($option, 1)) { $option_value = $option[0]; $option_title = $option[1]; } else { $option_value = $option_title = $option; } $optionargs = array('value' => $option_value); if ($selected == $option_value) { $optionargs['selected'] = true; } $this->AddChild(new HtmlElement('option', $optionargs, $option_title)); } }
/** * A widget containing a line of user inputs for a form element * @param string $name The HTML name (and #id) of the input element(s) and label * @param string $title The text written next to the input element(s) * @param string $inputs The input element(s) for the form line **/ public function __construct($name, $title, $inputs) { parent::__construct('div', array('class' => 'formline')); // Add the label $this->AddContainer(new HtmlElement('label', array('for' => $name), $title), 'LABEL'); // Create the input group $group = new HtmlElement('div', array('class' => 'formgroup')); if (is_a($inputs, 'HtmlElement')) { $group->AddChild($inputs); } elseif (RTK::ArrayIsLongerThan($array, 0)) { foreach ($inputs as $input) { if (is_a($input, 'HtmlElement')) { $group->AddChild($input); } } } $this->AddContainer($group, 'GROUP'); // Add the error section $this->AddContainer(new HtmlElement(), 'ERROR'); }
/** * Converts the element into an HTML string * @param boolean $newline Specifies whether or not to start with a newline * @return string A string containing the entire HTML structure of the element and it's children **/ public function ToString(&$newline) { $return = RTK_EMPTYSTRING; if ($this->_tag != null) { if ($newline) { $return .= RTK_OUTPUTNEWLINE; } else { $newline = true; } if ($this->_oneline <= 1) { $return .= str_repeat(RTK_OUTPUTINDENT, $this->_indent); } $return .= '<' . $this->_tag; if (RTK::SetAndNotNull($this->_attributes) && RTK::ArrayIsLongerThan($this->_attributes, 0)) { ksort($this->_attributes); foreach ($this->_attributes as $key => $val) { if (strstr(RTK_BOOLEANPARAMETERS, '|' . $key . '|') && $val == true) { $return .= RTK_SINGLESPACE . $key; } else { $return .= RTK_SINGLESPACE . $key . '="' . $val . '"'; } } } if ($this->_endtag != RTK_EMPTYSTRING) { $return .= $this->_endtag . ">"; } else { if (sizeof($this->_children) == 0) { if ($this->_content != RTK_EMPTYSTRING) { if (strstr($this->_content, RTK_NEWLINE) && !strstr(RTK_PRESERVECONTENTS, $this->_tag)) { $return .= '>'; foreach (explode(NEWLINE, $this->_content) as $line) { if ($line == RTK_EMPTYSTRING || $line[strlen($line) - 1] != '>') { $line .= '<br />'; } $return .= RTK_OUTPUTNEWLINE . str_repeat(RTK_OUTPUTINDENT, $this->_indent + 1) . $line; } $return .= RTK_OUTPUTNEWLINE . str_repeat(RTK_OUTPUTINDENT, $this->_indent) . '</' . $this->_tag . '>'; } else { $return .= '>' . $this->_content . '</' . $this->_tag . '>'; } } elseif (strstr(RTK_VOIDELEMENTS, '|' . $this->_tag . '|')) { $return .= ' />'; } elseif (strstr(RTK_NONVOIDELEMENTS, '|' . $this->_tag . '|') || $this->_oneline) { $return .= '></' . $this->_tag . '>'; } else { $return .= ' />'; } } else { if ($this->_oneline) { $return .= '>'; foreach ($this->_children as $c) { $return .= $c; } $return .= '</' . $this->_tag . '>'; } else { $return .= '>' . RTK_OUTPUTNEWLINE; if ($this->_content != RTK_EMPTYSTRING) { $return .= str_repeat(RTK_OUTPUTINDENT, $this->_indent + 1) . str_replace("\n", '<br />' . RTK_OUTPUTNEWLINE . str_repeat(RTK_OUTPUTINDENT, $this->_indent), $this->_content) . RTK_OUTPUTNEWLINE; } if (sizeof($this->_children) > 0) { $newline = false; foreach ($this->_children as $c) { $return .= $c->ToString($newline); } } $return .= RTK_OUTPUTNEWLINE . str_repeat(RTK_OUTPUTINDENT, $this->_indent) . '</' . $this->_tag . '>'; } } } } else { $sizeofchildren = sizeof($this->_children); if ($sizeofchildren > 0) { $this->UpdateChildren(); // INFO: commenting this line seems to have fixed a double-linebreak issue, but may now cause a no-linebreak in some cases... stay tuned... //if ($newline) { $return .= RTK_OUTPUTNEWLINE; } for ($i = 0; $i < $sizeofchildren; $i++) { $return .= $this->_children[$i]->ToString($newline); } } } return $return; }
/** * Creates a list of HTML attributes from an array of key/values * @param array $array The array with named key/value pairs **/ public static function ListFromArray($array = null) { $list = array(); if (is_array($array) && RTK::ArrayIsLongerThan($array, 0)) { foreach ($array as $key => $value) { $list[] = new HtmlAttribute($key, $value); } } return $list; }
/** * Add a row of radiobuttons to the form * @param string $name The HTML name (and #id) of the input field * @param string $title The text written next to the input field * @param string[][] $options An array of options, each of which is an array of value and title * @param string $selected The value of the selected radiobutton * @param HtmlElement $container (optional) The "container" to add it to **/ public function AddRadioButtons($name, $title, $options, $selected = null, $container = null) { $group = new HtmlElement('div', array('class' => 'formgroup')); $option_value = RTK_EMPTYSTRING; $option_title = RTK_EMPTYSTRING; foreach ($options as $option) { if (RTK::ArrayIsLongerThan($option, 1)) { $option_value = $option[0]; $option_title = $option[1]; } else { $option_value = $option_title = $option; } $args = array('type' => 'radio', 'class' => 'radiobox', 'name' => $name, 'id' => $name, 'value' => $option_value); if ($selected == $option_value) { $args['checked'] = true; } $group->AddChild(new HtmlElement('input', $args)); $group->AddChild(new HtmlElement('span', null, $option_title)); } $field = new HtmlElement('div', array('class' => 'formline')); $field->AddChild(new HtmlElement('label', array('for' => $name), $title)); $field->AddChild($group); $this->AddToContainer($field, $container); }