Source for file select.php
Documentation is available at select.php
* @package Joomla.Libraries
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
* Utility class for creating HTML select lists
* @package Joomla.Libraries
* Default values for options. Organized by option group.
'option' =>
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 <select> tag
* @param string $selected The key that is selected
* @param string $yes Language key for Yes
* @param string $no Language key for no
* @param string $id The id for the field
* @return string HTML for the radio list
public static function booleanlist($name, $attribs =
array(), $selected =
null, $yes =
'JYES', $no =
'JNO', $id =
false)
return JHtml::_('select.radiolist', $arr, $name, $attribs, 'value', 'text', (int)
$selected, $id);
* Generates an HTML selection list.
* @param array $data An array of objects, arrays, or scalars.
* @param string $name The value of the HTML name attribute.
* @param mixed $attribs Additional HTML attributes for the <select> 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 JHtml::$formatOptions}.
* Selection options, see {@see JHtmlSelect::options()}.
* list.attr, string|array: Additional attributes for the select
* 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,
$options =
array_merge(JHtml::$formatOptions, array('format.depth' =>
0, 'id' =>
false));
// Assume we have an options array
// Get options from the parameters
$options['list.attr'] =
$attribs;
$options['list.translate'] =
$translate;
$options['option.key'] =
$optKey;
$options['option.text'] =
$optText;
$options['list.select'] =
$selected;
if (isset
($options['list.attr']))
$attribs =
$options['list.attr'];
$attribs =
' ' .
$attribs;
$id =
$options['id'] !==
false ?
$options['id'] :
$name;
$baseIndent =
str_repeat($options['format.indent'], $options['format.depth']++
);
$html =
$baseIndent .
'<select' .
($id !==
'' ?
' id="' .
$id .
'"' :
'') .
' name="' .
$name .
'"' .
$attribs .
'>' .
$options['format.eol']
.
static::options($data, $options) .
$baseIndent .
'</select>' .
$options['format.eol'];
* 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)
$options =
array_merge(JHtml::$formatOptions, array('format.depth' =>
0, 'id' =>
false));
// Get options from the parameters
$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 .
'<datalist' .
$id .
'>' .
$options['format.eol']
.
static::options($data, $options) .
$baseIndent .
'</datalist>' .
$options['format.eol'];
* 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 JHtml::$formatOptions}.
* Selection options. See {@see JHtmlSelect::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
* 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
* id: Value to use as the select element id attribute. Defaults to
* 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
* @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
array('format.depth' =>
0, 'group.items' =>
'items', 'group.label' =>
'text', 'group.label.toHtml' =>
true, 'id' =>
false),
if ($options['group.items'] ===
null)
$options['group.label'] =
null;
if (isset
($options['list.attr']))
$attribs =
$options['list.attr'];
$attribs =
' ' .
$attribs;
$id =
$options['id'] !==
false ?
$options['id'] :
$name;
// Disable groups in the options.
$options['groups'] =
false;
$baseIndent =
str_repeat($options['format.indent'], $options['format.depth']++
);
$html =
$baseIndent .
'<select' .
($id !==
'' ?
' id="' .
$id .
'"' :
'') .
' name="' .
$name .
'"' .
$attribs .
'>' .
$options['format.eol'];
$groupIndent =
str_repeat($options['format.indent'], $options['format.depth']++
);
foreach ($data as $dataKey =>
$group)
if ($options['group.items'] ==
null)
// Sub-list is an associative array
// 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']];
if (isset
($options['group.id']) && isset
($group[$options['group.id']]))
$id =
$group[$options['group.id']];
// 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'];
if (isset
($options['group.id']) && isset
($group->$options['group.id']))
$id =
$group->$options['group.id'];
throw
new RuntimeException('Invalid group contents.', 1);
$html .=
static::options($subList, $options);
$html .=
$groupIndent .
'<optgroup' .
(empty($id) ?
'' :
' id="' .
$id .
'"') .
' label="'
.
($options['group.label.toHtml'] ?
htmlspecialchars($label, ENT_COMPAT, 'UTF-8') :
$label) .
'">' .
$options['format.eol']
.
static::options($subList, $options) .
$groupIndent .
'</optgroup>' .
$options['format.eol'];
$html .=
$baseIndent .
'</select>' .
$options['format.eol'];
* 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 <select> tag, an array of
* attributes, or an array of options. Treated as options if it is the last
* @param mixed $selected The key that is selected
* @param string $format The printf format to be applied to the number
* @return string HTML for the select list
public static function integerlist($start, $end, $inc, $name, $attribs =
null, $selected =
null, $format =
'')
$options =
array_merge(JHtml::$formatOptions, array('format.depth' =>
0, 'option.format' =>
'', 'id' =>
null));
// Assume we have an options array
// Extract the format and remove it from downstream options
$format =
$options['option.format'];
unset
($options['option.format']);
// Get options from the parameters
$options['list.attr'] =
$attribs;
$options['list.select'] =
$selected;
for ($i =
$start; $i <=
$end; $i +=
$inc)
$data[$i] =
$format ?
sprintf($format, $i) :
$i;
// Tell genericlist() to use array keys
$options['option.key'] =
null;
return JHtml::_('select.genericlist', $data, $name, $options);
* Create a placeholder for an option group.
* @param string $text The text for the option
* @param string $optKey The returned object property name for the value
* @param string $optText The returned object property name for the text
* @deprecated 4.0 Use JHtmlSelect::groupedList()
* @see JHtmlSelect::groupedList()
public static function optgroup($text, $optKey =
'value', $optText =
'text')
JLog::add('JHtmlSelect::optgroup() is deprecated, use JHtmlSelect::groupedList() instead.', JLog::WARNING, 'deprecated');
// Toggle between open and close states:
$obj->$optKey =
'<OPTGROUP>';
$obj->$optKey =
'</OPTGROUP>';
* Create an object that represents an option in an option list.
* @param string $value The value of the option
* @param string $text The text for the option
* @param mixed $optKey If a string, the returned object property name for
* the value. If an array, options. Valid options are:
* attr: String|array. Additional attributes for this option.
* disable: Boolean. If set, this option is disabled.
* label: String. The value for the option label.
* option.attr: The property in each option array to use for
* additional selection attributes. Defaults to none.
* option.disable: The property that will hold the disabled state.
* option.key: The property that will hold the selection value.
* option.label: The property in each option array to use as the
* selection label attribute. If a "label" option is provided, defaults to
* "label", if no label is given, defaults to null (none).
* option.text: The property that will hold the the displayed text.
* Defaults to "text". If set to null, the option array is assumed to be a
* list of displayable scalars.
* @param string $optText The property that will hold the the displayed text. This
* parameter is ignored if an options array is passed.
* @param boolean $disable Not used.
public static function option($value, $text =
'', $optKey =
'value', $optText =
'text', $disable =
false)
$options =
array('attr' =>
null, 'disable' =>
false, 'option.attr' =>
null, 'option.disable' =>
'disable', 'option.key' =>
'value',
'option.label' =>
null, 'option.text' =>
'text');
// Merge in caller's options
// Get options from the parameters
$options['option.key'] =
$optKey;
$options['option.text'] =
$optText;
$options['disable'] =
$disable;
$obj->$options['option.key'] =
$value;
$obj->$options['option.text'] =
trim($text) ?
$text :
$value;
* If a label is provided, save it. If no label is provided and there is
* a label name, initialise to an empty string.
$hasProperty =
$options['option.label'] !==
null;
if (isset
($options['label']))
$labelProperty =
$hasProperty ?
$options['option.label'] :
'label';
$obj->$labelProperty =
$options['label'];
$obj->$options['option.label'] =
'';
// Set attributes only if there is a property and a value
if ($options['attr'] !==
null)
$obj->$options['option.attr'] =
$options['attr'];
// Set disable only if it has a property and a value
if ($options['disable'] !==
null)
$obj->$options['option.disable'] =
$options['disable'];
* Generates the option tags for an HTML select list (with no select tag
* surrounding the options).
* @param array $arr An array of objects, arrays, or values.
* @param mixed $optKey If a string, this is the name of the object variable for
* the option value. If null, the index of the array of objects is used. If
* an array, this is a set of options, as key/value pairs. Valid options are:
* -Format options, {@see JHtml::$formatOptions}.
* -groups: Boolean. If set, looks for keys with the value
* "<optgroup>" and synthesizes groups from them. Deprecated. Defaults
* true for backwards compatibility.
* -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
* JText::_(). Default is false.
* -option.id: The property in each option array to use as the
* selection id attribute. Defaults to none.
* -option.key: The property in each option array to use as the
* selection value. Defaults to "value". If set to null, the index of the
* -option.label: The property in each option array to use as the
* selection label attribute. Defaults to null (none).
* -option.text: The property in each option array to use as the
* displayed text. Defaults to "text". If set to null, the option array is
* assumed to be a list of displayable scalars.
* -option.attr: The property in each option array to use for
* additional selection attributes. Defaults to none.
* -option.disable: The property that will hold the disabled state.
* -option.key: The property that will hold the selection value.
* -option.text: The property that will hold the the displayed text.
* Defaults to "text". If set to null, the option array is assumed to be a
* list of displayable scalars.
* @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 boolean $translate Translate the option values.
* @return string HTML for the select list
public static function options($arr, $optKey =
'value', $optText =
'text', $selected =
null, $translate =
false)
static::$optionDefaults['option'],
array('format.depth' =>
0, 'groups' =>
true, 'list.select' =>
null, 'list.translate' =>
false)
// Set default options and overwrite with anything passed in
// Get options from the parameters
$options['option.key'] =
$optKey;
$options['option.text'] =
$optText;
$options['list.select'] =
$selected;
$options['list.translate'] =
$translate;
$baseIndent =
str_repeat($options['format.indent'], $options['format.depth']);
foreach ($arr as $elementKey =>
&$element)
$key =
$options['option.key'] ===
null ?
$elementKey :
$element[$options['option.key']];
$text =
$element[$options['option.text']];
if (isset
($element[$options['option.attr']]))
$attr =
$element[$options['option.attr']];
if (isset
($element[$options['option.id']]))
$id =
$element[$options['option.id']];
if (isset
($element[$options['option.label']]))
$label =
$element[$options['option.label']];
if (isset
($element[$options['option.disable']]) &&
$element[$options['option.disable']])
$extra .=
' disabled="disabled"';
$key =
$options['option.key'] ===
null ?
$elementKey :
$element->$options['option.key'];
$text =
$element->$options['option.text'];
if (isset
($element->$options['option.attr']))
$attr =
$element->$options['option.attr'];
if (isset
($element->$options['option.id']))
$id =
$element->$options['option.id'];
if (isset
($element->$options['option.label']))
$label =
$element->$options['option.label'];
if (isset
($element->$options['option.disable']) &&
$element->$options['option.disable'])
$extra .=
' disabled="disabled"';
// This is a simple associative array
* The use of options that contain optgroup HTML elements was
* somewhat hacked for J1.5. J1.6 introduces the grouplist() method
* to handle this better. The old solution is retained through the
* "groups" option, which defaults true in J1.6, but should be
* deprecated at some point in the future.
if ($options['groups'] &&
$key ==
'<OPTGROUP>')
$html .=
$baseIndent .
'<optgroup label="' .
($options['list.translate'] ?
JText::_($text) :
$text) .
'">' .
$options['format.eol'];
$baseIndent =
str_repeat($options['format.indent'], ++
$options['format.depth']);
elseif ($options['groups'] &&
$key ==
'</OPTGROUP>')
$baseIndent =
str_repeat($options['format.indent'], --
$options['format.depth']);
$html .=
$baseIndent .
'</optgroup>' .
$options['format.eol'];
// If no string after hyphen - take hyphen out
$splitText =
preg_split('/ -[\s]*/', $text, 2, PREG_SPLIT_NO_EMPTY);
$text = isset
($splitText[0]) ?
$splitText[0] :
'';
if (!empty($splitText[1]))
$text .=
' - ' .
$splitText[1];
if ($options['list.translate'] &&
!empty($label))
if ($options['option.label.toHtml'])
$extra =
($id ?
' id="' .
$id .
'"' :
'') .
($label ?
' label="' .
$label .
'"' :
'') .
($attr ?
' ' .
$attr :
'') .
$extra;
foreach ($options['list.select'] as $val)
$key2 =
is_object($val) ?
$val->$options['option.key'] :
$val;
$extra .=
' selected="selected"';
elseif ((string)
$key == (string)
$options['list.select'])
$extra .=
' selected="selected"';
if ($options['list.translate'])
// Generate the option, encoding as required
$html .=
$baseIndent .
'<option value="' .
($options['option.key.toHtml'] ?
htmlspecialchars($key, ENT_COMPAT, 'UTF-8') :
$key) .
'"'
$html .=
'</option>' .
$options['format.eol'];
* Generates an HTML radio list.
* @param array $data An array of objects
* @param string $name The value of the HTML name attribute
* @param string $attribs Additional HTML attributes for the <select> tag
* @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,
$id_text =
$idtag ?
$idtag :
$name;
$html =
'<div class="controls">';
$t =
$translate ?
JText::_($obj->$optText) :
$obj->$optText;
$id =
(isset
($obj->id) ?
$obj->id :
null);
$id =
$id ?
$obj->id :
$id_text .
$k;
foreach ($selected as $val)
$k2 =
is_object($val) ?
$val->$optKey :
$val;
$extra .=
' selected="selected" ';
$extra .=
((string)
$k == (string)
$selected ?
' checked="checked" ' :
'');
$html .=
"\n\t" .
'<label for="' .
$id .
'" id="' .
$id .
'-lbl" class="radio">';
$html .=
"\n\t\n\t" .
'<input type="radio" name="' .
$name .
'" id="' .
$id .
'" value="' .
$k .
'" ' .
$extra
$html .=
"\n\t" .
'</label>';
Documentation generated on Tue, 19 Nov 2013 15:12:50 +0100 by phpDocumentor 1.4.3