5 * @subpackage plugins
\r
10 * Smarty {html_options} function plugin
\r
12 * Type: function<br>
\r
13 * Name: html_options<br>
\r
15 * - name (optional) - string default "select"
\r
16 * - values (required if no options supplied) - array
\r
17 * - options (required if no values supplied) - associative array
\r
18 * - selected (optional) - string default not set
\r
19 * - output (required if not options supplied) - array
\r
20 * Purpose: Prints the list of <option> tags generated from
\r
21 * the passed parameters
\r
22 * @link http://smarty.php.net/manual/en/language.function.html.options.php {html_image}
\r
23 * (Smarty online manual)
\r
27 * @uses smarty_function_escape_special_chars()
\r
29 function smarty_function_html_options($params, &$smarty)
\r
31 require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
\r
36 $selected = array();
\r
41 foreach($params as $_key => $_val) {
\r
44 $$_key = (string)$_val;
\r
48 $$_key = (array)$_val;
\r
54 $$_key = array_values((array)$_val);
\r
58 if(!is_array($_val)) {
\r
59 $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
\r
61 $smarty->trigger_error("html_options: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
\r
67 if (!isset($options) && !isset($values))
\r
68 return ''; /* raise error here? */
\r
72 if (is_array($options)) {
\r
74 foreach ($options as $_key=>$_val)
\r
75 $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected);
\r
79 foreach ((array)$values as $_i=>$_key) {
\r
80 $_val = isset($output[$_i]) ? $output[$_i] : '';
\r
81 $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected);
\r
87 $_html_result = '<select name="' . $name . '"' . $extra . '>' . "\n" . $_html_result . '</select>' . "\n";
\r
90 return $_html_result;
\r
94 function smarty_function_html_options_optoutput($key, $value, $selected) {
\r
95 if(!is_array($value)) {
\r
96 $_html_result = '<option label="' . smarty_function_escape_special_chars($value) . '" value="' .
\r
97 smarty_function_escape_special_chars($key) . '"';
\r
98 if (in_array($key, $selected))
\r
99 $_html_result .= ' selected="selected"';
\r
100 $_html_result .= '>' . smarty_function_escape_special_chars($value) . '</option>' . "\n";
\r
102 $_html_result = smarty_function_html_options_optgroup($key, $value, $selected);
\r
104 return $_html_result;
\r
107 function smarty_function_html_options_optgroup($key, $values, $selected) {
\r
108 $optgroup_html = '<optgroup label="' . smarty_function_escape_special_chars($key) . '">' . "\n";
\r
109 foreach ($values as $key => $value) {
\r
110 $optgroup_html .= smarty_function_html_options_optoutput($key, $value, $selected);
\r
112 $optgroup_html .= "</optgroup>\n";
\r
113 return $optgroup_html;
\r
116 /* vim: set expandtab: */
\r