1 <?php
2 /**
3 * Smarty plugin
4 *
5 * @package Smarty
6 * @subpackage PluginsFunction
7 */
9 /**
10 * Smarty {html_options} function plugin
11 *
12 * Type: function<br>
13 * Name: html_options<br>
14 * Purpose: Prints the list of <option> tags generated from
15 * the passed parameters<br>
16 * Params:
17 * <pre>
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
25 * </pre>
26 *
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
33 * @return string
34 * @uses smarty_function_escape_special_chars()
35 */
36 function smarty_function_html_options($params, $template)
37 {
38 require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
40 $name = null;
41 $values = null;
42 $options = null;
43 $selected = null;
44 $output = null;
45 $id = null;
46 $class = null;
48 $extra = '';
50 foreach ($params as $_key => $_val) {
51 switch ($_key) {
52 case 'name':
53 case 'class':
54 case 'id':
55 $$_key = (string) $_val;
56 break;
58 case 'options':
59 $options = (array) $_val;
60 break;
62 case 'values':
63 case 'output':
64 $$_key = array_values((array) $_val);
65 break;
67 case 'selected':
68 if (is_array($_val)) {
69 $selected = array();
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());
74 } else {
75 trigger_error("html_options: selected attribute contains an object of class '". get_class($_sel) ."' without __toString() method", E_USER_NOTICE);
76 continue;
77 }
78 } else {
79 $_sel = smarty_function_escape_special_chars((string) $_sel);
80 }
81 $selected[$_sel] = true;
82 }
83 } elseif (is_object($_val)) {
84 if (method_exists($_val, "__toString")) {
85 $selected = smarty_function_escape_special_chars((string) $_val->__toString());
86 } else {
87 trigger_error("html_options: selected attribute is an object of class '". get_class($_val) ."' without __toString() method", E_USER_NOTICE);
88 }
89 } else {
90 $selected = smarty_function_escape_special_chars((string) $_val);
91 }
92 break;
94 default:
95 if (!is_array($_val)) {
96 $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
97 } else {
98 trigger_error("html_options: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
99 }
100 break;
101 }
102 }
104 if (!isset($options) && !isset($values)) {
105 /* raise error here? */
106 return '';
107 }
109 $_html_result = '';
110 $_idx = 0;
112 if (isset($options)) {
113 foreach ($options as $_key => $_val) {
114 $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected, $id, $class, $_idx);
115 }
116 } else {
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);
120 }
121 }
123 if (!empty($name)) {
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";
127 }
129 return $_html_result;
130 }
132 function smarty_function_html_options_optoutput($key, $value, $selected, $id, $class, &$idx)
133 {
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"';
140 }
141 } elseif ($_key === $selected) {
142 $_html_result .= ' selected="selected"';
143 }
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());
149 } else {
150 trigger_error("html_options: value is an object of class '". get_class($value) ."' without __toString() method", E_USER_NOTICE);
151 return '';
152 }
153 }
154 $_html_result .= $_html_class . $_html_id . '>' . $value . '</option>' . "\n";
155 $idx++;
156 } else {
157 $_idx = 0;
158 $_html_result = smarty_function_html_options_optgroup($key, $value, $selected, !empty($id) ? ($id.'-'.$idx) : null, $class, $_idx);
159 $idx++;
160 }
161 return $_html_result;
162 }
164 function smarty_function_html_options_optgroup($key, $values, $selected, $id, $class, &$idx)
165 {
166 $optgroup_html = '<optgroup label="' . smarty_function_escape_special_chars($key) . '">' . "\n";
167 foreach ($values as $key => $value) {
168 $optgroup_html .= smarty_function_html_options_optoutput($key, $value, $selected, $id, $class, $idx);
169 }
170 $optgroup_html .= "</optgroup>\n";
171 return $optgroup_html;
172 }
174 ?>