3 * @version $Id: select.php 1795 2009-04-24 09:29:31Z fishbone $
4 * @package Joomla.Framework
6 * @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
7 * @license GNU/GPL, see LICENSE.php
8 * Joomla! is free software. This version may have been modified pursuant
9 * to the GNU General Public License, and as distributed it includes or
10 * is derivative of works licensed under the GNU General Public License or
11 * other free or open source software licenses.
12 * See COPYRIGHT.php for copyright notices and details.
16 * Utility class for creating HTML select lists
19 * @package Joomla.Framework
26 * @param string The value of the option
27 * @param string The text for the option
28 * @param string The returned object property name for the value
29 * @param string The returned object property name for the text
32 function option( $value, $text='', $value_name='value', $text_name='text', $disable=false )
35 $obj->$value_name = $value;
36 $obj->$text_name = trim( $text ) ? $text : $value;
37 $obj->disable = $disable;
42 * @param string The text for the option
43 * @param string The returned object property name for the value
44 * @param string The returned object property name for the text
47 function optgroup( $text, $value_name = 'value', $text_name = 'text' )
50 $obj->$value_name = '<OPTGROUP>';
51 $obj->$text_name = $text;
56 * Generates just the option tags for an HTML select list
58 * @param array An array of objects
59 * @param string The name of the object variable for the option value
60 * @param string The name of the object variable for the option text
61 * @param mixed The key that is selected (accepts an array or a string)
62 * @returns string HTML for the select list
64 function options( $arr, $key = 'value', $text = 'text', $selected = null, $translate = false )
68 foreach ($arr as $i => $option)
70 $element =& $arr[$i]; // since current doesn't return a reference, need to do this
72 $isArray = is_array( $element );
78 $id = ( isset( $element['id'] ) ? $element['id'] : null );
79 if(isset($element['disable']) && $element['disable']) {
80 $extra .= ' disabled="disabled"';
87 $id = ( isset( $element->id ) ? $element->id : null );
88 if(isset( $element->disable ) && $element->disable) {
89 $extra .= ' disabled="disabled"';
93 // This is real dirty, open to suggestions,
94 // barring doing a propper object to handle it
95 if ($k === '<OPTGROUP>') {
96 $html .= '<optgroup label="' . $t . '">';
97 } else if ($k === '</OPTGROUP>') {
98 $html .= '</optgroup>';
102 //if no string after hypen - take hypen out
103 $splitText = explode( ' - ', $t, 2 );
105 if(isset($splitText[1])){ $t .= ' - '. $splitText[1]; }
108 //$extra .= $id ? ' id="' . $arr[$i]->id . '"' : '';
109 if (is_array( $selected ))
111 foreach ($selected as $val)
113 $k2 = is_object( $val ) ? $val->$key : $val;
116 $extra .= ' selected="selected"';
121 $extra .= ( (string)$k == (string)$selected ? ' selected="selected"' : '' );
124 //if flag translate text
129 // ensure ampersands are encoded
130 $k = JFilterOutput::ampReplace($k);
131 $t = JFilterOutput::ampReplace($t);
133 $html .= '<option value="'. $k .'" '. $extra .'>' . $t . '</option>';
141 * Generates an HTML select list
143 * @param array An array of objects
144 * @param string The value of the HTML name attribute
145 * @param string Additional HTML attributes for the <select> tag
146 * @param string The name of the object variable for the option value
147 * @param string The name of the object variable for the option text
148 * @param mixed The key that is selected (accepts an array or a string)
149 * @returns string HTML for the select list
151 function genericlist( $arr, $name, $attribs = null, $key = 'value', $text = 'text', $selected = NULL, $idtag = false, $translate = false )
153 if ( is_array( $arr ) ) {
157 if (is_array($attribs)) {
158 $attribs = JArrayHelper::toString($attribs);
167 $id = str_replace('[','',$id);
168 $id = str_replace(']','',$id);
170 $html = '<select name="'. $name .'" id="'. $id .'" '. $attribs .'>';
171 $html .= JHTMLSelect::Options( $arr, $key, $text, $selected, $translate );
172 $html .= '</select>';
178 * Generates a select list of integers
180 * @param int The start integer
181 * @param int The end integer
182 * @param int The increment
183 * @param string The value of the HTML name attribute
184 * @param string Additional HTML attributes for the <select> tag
185 * @param mixed The key that is selected
186 * @param string The printf format to be applied to the number
187 * @returns string HTML for the select list
189 function integerlist( $start, $end, $inc, $name, $attribs = null, $selected = null, $format = "" )
191 $start = intval( $start );
192 $end = intval( $end );
193 $inc = intval( $inc );
196 for ($i=$start; $i <= $end; $i+=$inc)
198 $fi = $format ? sprintf( "$format", $i ) : "$i";
199 $arr[] = JHTML::_('select.option', $fi, $fi );
202 return JHTML::_('select.genericlist', $arr, $name, $attribs, 'value', 'text', $selected );
206 * Generates an HTML radio list
208 * @param array An array of objects
209 * @param string The value of the HTML name attribute
210 * @param string Additional HTML attributes for the <select> tag
211 * @param mixed The key that is selected
212 * @param string The name of the object variable for the option value
213 * @param string The name of the object variable for the option text
214 * @returns string HTML for the select list
216 function radiolist( $arr, $name, $attribs = null, $key = 'value', $text = 'text', $selected = null, $idtag = false, $translate = false )
221 if (is_array($attribs)) {
222 $attribs = JArrayHelper::toString($attribs);
230 for ($i=0, $n=count( $arr ); $i < $n; $i++ )
233 $t = $translate ? JText::_( $arr[$i]->$text ) : $arr[$i]->$text;
234 $id = ( isset($arr[$i]->id) ? @$arr[$i]->id : null);
237 $extra .= $id ? " id=\"" . $arr[$i]->id . "\"" : '';
238 if (is_array( $selected ))
240 foreach ($selected as $val)
242 $k2 = is_object( $val ) ? $val->$key : $val;
245 $extra .= " selected=\"selected\"";
250 $extra .= ((string)$k == (string)$selected ? " checked=\"checked\"" : '');
252 $html .= "\n\t<input type=\"radio\" name=\"$name\" id=\"$id_text$k\" value=\"".$k."\"$extra $attribs />";
253 $html .= "\n\t<label for=\"$id_text$k\">$t</label>";
260 * Generates a yes/no radio list
262 * @param string The value of the HTML name attribute
263 * @param string Additional HTML attributes for the <select> tag
264 * @param mixed The key that is selected
265 * @returns string HTML for the radio list
267 function booleanlist( $name, $attribs = null, $selected = null, $yes='yes', $no='no', $id=false )
270 JHTML::_('select.option', '0', JText::_( $no ) ),
271 JHTML::_('select.option', '1', JText::_( $yes ) )
273 return JHTML::_('select.radiolist', $arr, $name, $attribs, 'value', 'text', (int) $selected, $id );