6 * @subpackage PluginsFunction
10 * Smarty {html_options} function plugin
12 * Name: html_options<br>
13 * Purpose: Prints the list of <option> tags generated from
14 * the passed parameters<br>
17 * - name (optional) - string default "select"
18 * - values (required) - if no options supplied) - array
19 * - options (required) - if no values supplied) - associative array
20 * - selected (optional) - string default not set
21 * - output (required) - if not options supplied) - array
22 * - id (optional) - string default not set
23 * - class (optional) - string default not set
26 * @link http://www.smarty.net/manual/en/language.function.html.options.php {html_image}
27 * (Smarty online manual)
28 * @author Monte Ohrt <monte at ohrt dot com>
29 * @author Ralf Strehle (minor optimization) <ralf dot strehle at yahoo dot de>
31 * @param array $params parameters
34 * @uses smarty_function_escape_special_chars()
36 function smarty_function_html_options($params)
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);
99 if (!empty($params['strict'])) {
100 if (!is_scalar($_val)) {
101 trigger_error("html_options: $_key attribute must be a scalar, only boolean true or string '$_key' will actually add the attribute", E_USER_NOTICE);
104 if ($_val === true || $_val === $_key) {
105 $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_key) . '"';
110 // omit break; to fall through!
113 if (!is_array($_val)) {
114 $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
116 trigger_error("html_options: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
122 if (!isset($options) && !isset($values)) {
123 /* raise error here? */
131 if (isset($options)) {
132 foreach ($options as $_key => $_val) {
133 $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected, $id, $class, $_idx);
136 foreach ($values as $_i => $_key) {
137 $_val = isset($output[$_i]) ? $output[$_i] : '';
138 $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected, $id, $class, $_idx);
143 $_html_class = !empty($class) ? ' class="' . $class . '"' : '';
144 $_html_id = !empty($id) ? ' id="' . $id . '"' : '';
145 $_html_result = '<select name="' . $name . '"' . $_html_class . $_html_id . $extra . '>' . "\n" . $_html_result . '</select>' . "\n";
148 return $_html_result;
151 function smarty_function_html_options_optoutput($key, $value, $selected, $id, $class, &$idx)
153 if (!is_array($value)) {
154 $_key = smarty_function_escape_special_chars($key);
155 $_html_result = '<option value="' . $_key . '"';
156 if (is_array($selected)) {
157 if (isset($selected[$_key])) {
158 $_html_result .= ' selected="selected"';
160 } elseif ($_key === $selected) {
161 $_html_result .= ' selected="selected"';
163 $_html_class = !empty($class) ? ' class="' . $class . ' option"' : '';
164 $_html_id = !empty($id) ? ' id="' . $id . '-' . $idx . '"' : '';
165 if (is_object($value)) {
166 if (method_exists($value, "__toString")) {
167 $value = smarty_function_escape_special_chars((string) $value->__toString());
169 trigger_error("html_options: value is an object of class '" . get_class($value) . "' without __toString() method", E_USER_NOTICE);
174 $value = smarty_function_escape_special_chars((string) $value);
176 $_html_result .= $_html_class . $_html_id . '>' . $value . '</option>' . "\n";
180 $_html_result = smarty_function_html_options_optgroup($key, $value, $selected, !empty($id) ? ($id . '-' . $idx) : null, $class, $_idx);
184 return $_html_result;
187 function smarty_function_html_options_optgroup($key, $values, $selected, $id, $class, &$idx)
189 $optgroup_html = '<optgroup label="' . smarty_function_escape_special_chars($key) . '">' . "\n";
190 foreach ($values as $key => $value) {
191 $optgroup_html .= smarty_function_html_options_optoutput($key, $value, $selected, $id, $class, $idx);
193 $optgroup_html .= "</optgroup>\n";
195 return $optgroup_html;