6 * @subpackage PluginsFunction
10 * Smarty {html_options} function plugin
13 * Name: html_options<br>
14 * Purpose: Prints the list of <option> tags generated from
15 * the passed parameters<br>
18 * - name (optional) - string default "select"
19 * - values (required) - if no options supplied) - array
20 * - options (required) - if no values supplied) - associative array
21 * - selected (optional) - string default not set
22 * - output (required) - if not options supplied) - array
23 * - id (optional) - string default not set
24 * - class (optional) - string default not set
27 * @link http://www.smarty.net/manual/en/language.function.html.options.php {html_image}
28 * (Smarty online manual)
29 * @author Monte Ohrt <monte at ohrt dot com>
30 * @author Ralf Strehle (minor optimization) <ralf dot strehle at yahoo dot de>
31 * @param array $params parameters
32 * @param Smarty_Internal_Template $template template object
34 * @uses smarty_function_escape_special_chars()
36 function smarty_function_html_options($params, $template)
38 require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
50 foreach ($params as $_key => $_val) {
55 $$_key = (string) $_val;
59 $options = (array) $_val;
64 $$_key = array_values((array) $_val);
68 if (is_array($_val)) {
70 foreach ($_val as $_sel) {
71 if (is_object($_sel)) {
72 if (method_exists($_sel, "__toString")) {
73 $_sel = smarty_function_escape_special_chars((string) $_sel->__toString());
75 trigger_error("html_options: selected attribute contains an object of class '". get_class($_sel) ."' without __toString() method", E_USER_NOTICE);
79 $_sel = smarty_function_escape_special_chars((string) $_sel);
81 $selected[$_sel] = true;
83 } elseif (is_object($_val)) {
84 if (method_exists($_val, "__toString")) {
85 $selected = smarty_function_escape_special_chars((string) $_val->__toString());
87 trigger_error("html_options: selected attribute is an object of class '". get_class($_val) ."' without __toString() method", E_USER_NOTICE);
90 $selected = smarty_function_escape_special_chars((string) $_val);
95 if (!is_array($_val)) {
96 $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
98 trigger_error("html_options: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
104 if (!isset($options) && !isset($values)) {
105 /* raise error here? */
112 if (isset($options)) {
113 foreach ($options as $_key => $_val) {
114 $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected, $id, $class, $_idx);
117 foreach ($values as $_i => $_key) {
118 $_val = isset($output[$_i]) ? $output[$_i] : '';
119 $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected, $id, $class, $_idx);
124 $_html_class = !empty($class) ? ' class="'.$class.'"' : '';
125 $_html_id = !empty($id) ? ' id="'.$id.'"' : '';
126 $_html_result = '<select name="' . $name . '"' . $_html_class . $_html_id . $extra . '>' . "\n" . $_html_result . '</select>' . "\n";
129 return $_html_result;
132 function smarty_function_html_options_optoutput($key, $value, $selected, $id, $class, &$idx)
134 if (!is_array($value)) {
135 $_key = smarty_function_escape_special_chars($key);
136 $_html_result = '<option value="' . $_key . '"';
137 if (is_array($selected)) {
138 if (isset($selected[$_key])) {
139 $_html_result .= ' selected="selected"';
141 } elseif ($_key === $selected) {
142 $_html_result .= ' selected="selected"';
144 $_html_class = !empty($class) ? ' class="'.$class.' option"' : '';
145 $_html_id = !empty($id) ? ' id="'.$id.'-'.$idx.'"' : '';
146 if (is_object($value)) {
147 if (method_exists($value, "__toString")) {
148 $value = smarty_function_escape_special_chars((string) $value->__toString());
150 trigger_error("html_options: value is an object of class '". get_class($value) ."' without __toString() method", E_USER_NOTICE);
154 $value = smarty_function_escape_special_chars((string) $value);
156 $_html_result .= $_html_class . $_html_id . '>' . $value . '</option>' . "\n";
160 $_html_result = smarty_function_html_options_optgroup($key, $value, $selected, !empty($id) ? ($id.'-'.$idx) : null, $class, $_idx);
163 return $_html_result;
166 function smarty_function_html_options_optgroup($key, $values, $selected, $id, $class, &$idx)
168 $optgroup_html = '<optgroup label="' . smarty_function_escape_special_chars($key) . '">' . "\n";
169 foreach ($values as $key => $value) {
170 $optgroup_html .= smarty_function_html_options_optoutput($key, $value, $selected, $id, $class, $idx);
172 $optgroup_html .= "</optgroup>\n";
173 return $optgroup_html;