array( 'option.attr' => null, 'option.disable' => 'disable', 'option.id' => null, 'option.key' => 'value', 'option.key.toHtml' => true, 'option.label' => null, 'option.label.toHtml' => true, 'option.text' => 'text', 'option.text.toHtml' => true ) ); /** * Generates a yes/no radio list. * * @param string $name The value of the HTML name attribute * @param array $attribs Additional HTML attributes for the tag. This * can be an array of attributes, or an array of options. Treated as options * if it is the last argument passed. Valid options are: * Format options, see {@see Html::$formatOptions}. * Selection options, see {@see Select::options()}. * list.attr, string|array: Additional attributes for the select * element. * id, string: Value to use as the select element id attribute. * Defaults to the same as the name. * list.select, string|array: Identifies one or more option elements * to be selected, based on the option key values. * @param string $optKey The name of the object variable for the option value. If * set to null, the index of the value array is used. * @param string $optText The name of the object variable for the option text. * @param mixed $selected The key that is selected (accepts an array or a string). * @param mixed $idTag Value of the field id or null by default * @param boolean $translate True to translate * * @return string HTML for the select list. */ public static function genericList($data, $name, $attribs = null, $optKey = 'value', $optText = 'text', $selected = null, $idTag = false, $translate = false) { // Set default options $options = array_merge(Html::$formatOptions, array('format.depth' => 0, 'id' => false)); if (is_array($attribs) && func_num_args() == 3) { // Assume we have an options array $options = array_merge($options, $attribs); } else { // Get options from the parameters $options['id'] = $idTag; $options['list.attr'] = $attribs; $options['list.translate'] = $translate; $options['option.key'] = $optKey; $options['option.text'] = $optText; $options['list.select'] = $selected; } $attribs = ''; if (isset($options['list.attr'])) { if (is_array($options['list.attr'])) { $attribs = ArrayHelper::toString($options['list.attr']); } else { $attribs = $options['list.attr']; } if ($attribs != '') { $attribs = ' ' . $attribs; } } $id = $options['id'] !== false ? $options['id'] : $name; $id = str_replace(array('[', ']'), '', $id); $baseIndent = str_repeat($options['format.indent'], $options['format.depth']++); $html = $baseIndent . '' . $options['format.eol'] . static::options($data, $options) . $baseIndent . '' . $options['format.eol']; return $html; } /** * Method to build a list with suggestions * * @param array $data An array of objects, arrays, or values. * @param string $optKey The name of the object variable for the option value. If * set to null, the index of the value array is used. * @param string $optText The name of the object variable for the option text. * @param mixed $idTag Value of the field id or null by default * @param boolean $translate True to translate * * @return string HTML for the select list */ public static function suggestionList($data, $optKey = 'value', $optText = 'text', $idTag = '', $translate = false) { // Set default options $options = array_merge(Html::$formatOptions, array('format.depth' => 0, 'id' => false)); // Get options from the parameters $options['id'] = $idTag; $options['list.attr'] = null; $options['list.translate'] = $translate; $options['option.key'] = $optKey; $options['option.text'] = $optText; $options['list.select'] = null; $id = ' id="' . $idTag . '"'; $baseIndent = str_repeat($options['format.indent'], $options['format.depth']++); $html = $baseIndent . '' . $options['format.eol'] . static::options($data, $options) . $baseIndent . '' . $options['format.eol']; return $html; } /** * Generates a grouped HTML selection list from nested arrays. * * @param array $data An array of groups, each of which is an array of options. * @param string $name The value of the HTML name attribute * @param array $options Options, an array of key/value pairs. Valid options are: * Format options, {@see Html::$formatOptions}. * Selection options. See {@see Select::options()}. * group.id: The property in each group to use as the group id * attribute. Defaults to none. * group.label: The property in each group to use as the group * label. Defaults to "text". If set to null, the data array index key is * used. * group.items: The property in each group to use as the array of * items in the group. Defaults to "items". If set to null, group.id and * group. label are forced to null and the data element is assumed to be a * list of selections. * id: Value to use as the select element id attribute. Defaults to * the same as the name. * list.attr: Attributes for the select element. Can be a string or * an array of key/value pairs. Defaults to none. * list.select: either the value of one selected option or an array * of selected options. Default: none. * list.translate: Boolean. If set, text and labels are translated via * Text::_(). * * @return string HTML for the select list * * @throws \RuntimeException If a group has contents that cannot be processed. */ public static function groupedList($data, $name, $options = array()) { // Set default options and overwrite with anything passed in $options = array_merge( Html::$formatOptions, array('format.depth' => 0, 'group.items' => 'items', 'group.label' => 'text', 'group.label.toHtml' => true, 'id' => false), $options ); // Apply option rules if ($options['group.items'] === null) { $options['group.label'] = null; } $attribs = ''; if (isset($options['list.attr'])) { if (is_array($options['list.attr'])) { $attribs = ArrayHelper::toString($options['list.attr']); } else { $attribs = $options['list.attr']; } if ($attribs != '') { $attribs = ' ' . $attribs; } } $id = $options['id'] !== false ? $options['id'] : $name; $id = str_replace(array('[', ']'), '', $id); // Disable groups in the options. $options['groups'] = false; $baseIndent = str_repeat($options['format.indent'], $options['format.depth']++); $html = $baseIndent . '' . $options['format.eol']; $groupIndent = str_repeat($options['format.indent'], $options['format.depth']++); foreach ($data as $dataKey => $group) { $label = $dataKey; $id = ''; $noGroup = is_int($dataKey); if ($options['group.items'] == null) { // Sub-list is an associative array $subList = $group; } elseif (is_array($group)) { // Sub-list is in an element of an array. $subList = $group[$options['group.items']]; if (isset($group[$options['group.label']])) { $label = $group[$options['group.label']]; $noGroup = false; } if (isset($options['group.id']) && isset($group[$options['group.id']])) { $id = $group[$options['group.id']]; $noGroup = false; } } elseif (is_object($group)) { // Sub-list is in a property of an object $subList = $group->{$options['group.items']}; if (isset($group->{$options['group.label']})) { $label = $group->{$options['group.label']}; $noGroup = false; } if (isset($options['group.id']) && isset($group->{$options['group.id']})) { $id = $group->{$options['group.id']}; $noGroup = false; } } else { throw new \RuntimeException('Invalid group contents.', 1); } if ($noGroup) { $html .= static::options($subList, $options); } else { $html .= $groupIndent . '' . $options['format.eol'] . static::options($subList, $options) . $groupIndent . '' . $options['format.eol']; } } $html .= $baseIndent . '' . $options['format.eol']; return $html; } /** * Generates a selection list of integers. * * @param integer $start The start integer * @param integer $end The end integer * @param integer $inc The increment * @param string $name The value of the HTML name attribute * @param mixed $attribs Additional HTML attributes for the tag, or the following * - inline: boolean Create the radio list as inline elements * - radioType: radio|checkbox Use radio buttons (radio) or checkboxes (checkbox) * @param mixed $optKey The key that is selected * @param string $optText The name of the object variable for the option value * @param string $selected The name of the object variable for the option text * @param boolean $idtag Value of the field id or null by default * @param boolean $translate True if options will be translated * * @return string HTML for the select list */ public static function radioList($data, $name, $attribs = null, $optKey = 'value', $optText = 'text', $selected = null, $idtag = false, $translate = false) { reset($data); $inline = false; $button = false; $radioType = 'radio'; if (isset($attribs['inline'])) { $inline = $attribs['inline']; unset($attribs['inline']); } if (isset($attribs['radioType'])) { $radioType = $attribs['radioType']; if (!in_array($radioType, array('radio', 'checkbox'))) { $radioType = 'radio'; } unset($attribs['radioType']); } if (isset($attribs['button'])) { $button = $attribs['button']; unset($attribs['button']); } if (is_array($attribs)) { $attribs = ArrayHelper::toString($attribs); } $id_text = $idtag ? $idtag : $name; $html = ''; if ($button) { $html .= '
'; } foreach ($data as $obj) { $k = $obj->$optKey; $t = $translate ? Text::_($obj->$optText) : $obj->$optText; $id = (isset($obj->id) ? $obj->id : null); $extra = ''; $id = $id ? $obj->id : $id_text . $k; if (is_array($selected)) { foreach ($selected as $val) { $k2 = is_object($val) ? $val->$optKey : $val; if ($k == $k2) { if ($radioType == 'radio') { $extra .= ' selected="selected" '; } else { $extra .= ' checked="checked" '; } break; } } } else { if ($radioType == 'radio') { $extra .= ((string)$k == (string)$selected ? ' checked="checked" ' : ''); } else { $extra .= ((string)$k == (string)$selected ? ' selected="selected" ' : ''); } } if (!$inline && !$button) { $html .= "\n
\n"; } $class = ''; if ($inline) { $class = ' class="' . $radioType . '-inline"'; } elseif ($button) { $class = ' class="btn btn-default"'; } $html .= "\n\t" . ''; $html .= "\n\t\n\t" . '' . $t; $html .= "\n\t" . ''; if (!$inline && !$button) { $html .= "\n
\n"; } } if ($button) { $html .= '
'; } return $html; } }