1 | <?php |
---|
2 | /** |
---|
3 | * Smarty plugin |
---|
4 | * |
---|
5 | * @package Smarty |
---|
6 | * @subpackage PluginsFunction |
---|
7 | */ |
---|
8 | |
---|
9 | /** |
---|
10 | * Smarty {html_checkboxes} function plugin |
---|
11 | * |
---|
12 | * File: function.html_checkboxes.php<br> |
---|
13 | * Type: function<br> |
---|
14 | * Name: html_checkboxes<br> |
---|
15 | * Date: 24.Feb.2003<br> |
---|
16 | * Purpose: Prints out a list of checkbox input types<br> |
---|
17 | * Examples: |
---|
18 | * <pre> |
---|
19 | * {html_checkboxes values=$ids output=$names} |
---|
20 | * {html_checkboxes values=$ids name='box' separator='<br>' output=$names} |
---|
21 | * {html_checkboxes values=$ids checked=$checked separator='<br>' output=$names} |
---|
22 | * </pre> |
---|
23 | * Params: |
---|
24 | * <pre> |
---|
25 | * - name (optional) - string default "checkbox" |
---|
26 | * - values (required) - array |
---|
27 | * - options (optional) - associative array |
---|
28 | * - checked (optional) - array default not set |
---|
29 | * - separator (optional) - ie <br> or |
---|
30 | * - output (optional) - the output next to each checkbox |
---|
31 | * - assign (optional) - assign the output as an array to this variable |
---|
32 | * - escape (optional) - escape the content (not value), defaults to true |
---|
33 | * </pre> |
---|
34 | * |
---|
35 | * @link http://www.smarty.net/manual/en/language.function.html.checkboxes.php {html_checkboxes} |
---|
36 | * (Smarty online manual) |
---|
37 | * @author Christopher Kvarme <christopher.kvarme@flashjab.com> |
---|
38 | * @author credits to Monte Ohrt <monte at ohrt dot com> |
---|
39 | * @version 1.0 |
---|
40 | * @param array $params parameters |
---|
41 | * @param object $template template object |
---|
42 | * @return string |
---|
43 | * @uses smarty_function_escape_special_chars() |
---|
44 | */ |
---|
45 | function smarty_function_html_checkboxes($params, $template) |
---|
46 | { |
---|
47 | require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php'); |
---|
48 | |
---|
49 | $name = 'checkbox'; |
---|
50 | $values = null; |
---|
51 | $options = null; |
---|
52 | $selected = array(); |
---|
53 | $separator = ''; |
---|
54 | $escape = true; |
---|
55 | $labels = true; |
---|
56 | $label_ids = false; |
---|
57 | $output = null; |
---|
58 | |
---|
59 | $extra = ''; |
---|
60 | |
---|
61 | foreach($params as $_key => $_val) { |
---|
62 | switch($_key) { |
---|
63 | case 'name': |
---|
64 | case 'separator': |
---|
65 | $$_key = (string) $_val; |
---|
66 | break; |
---|
67 | |
---|
68 | case 'escape': |
---|
69 | case 'labels': |
---|
70 | case 'label_ids': |
---|
71 | $$_key = (bool) $_val; |
---|
72 | break; |
---|
73 | |
---|
74 | case 'options': |
---|
75 | $$_key = (array) $_val; |
---|
76 | break; |
---|
77 | |
---|
78 | case 'values': |
---|
79 | case 'output': |
---|
80 | $$_key = array_values((array) $_val); |
---|
81 | break; |
---|
82 | |
---|
83 | case 'checked': |
---|
84 | case 'selected': |
---|
85 | if (is_array($_val)) { |
---|
86 | $selected = array(); |
---|
87 | foreach ($_val as $_sel) { |
---|
88 | if (is_object($_sel)) { |
---|
89 | if (method_exists($_sel, "__toString")) { |
---|
90 | $_sel = smarty_function_escape_special_chars((string) $_sel->__toString()); |
---|
91 | } else { |
---|
92 | trigger_error("html_checkboxes: selected attribute contains an object of class '". get_class($_sel) ."' without __toString() method", E_USER_NOTICE); |
---|
93 | continue; |
---|
94 | } |
---|
95 | } else { |
---|
96 | $_sel = smarty_function_escape_special_chars((string) $_sel); |
---|
97 | } |
---|
98 | $selected[$_sel] = true; |
---|
99 | } |
---|
100 | } elseif (is_object($_val)) { |
---|
101 | if (method_exists($_val, "__toString")) { |
---|
102 | $selected = smarty_function_escape_special_chars((string) $_val->__toString()); |
---|
103 | } else { |
---|
104 | trigger_error("html_checkboxes: selected attribute is an object of class '". get_class($_val) ."' without __toString() method", E_USER_NOTICE); |
---|
105 | } |
---|
106 | } else { |
---|
107 | $selected = smarty_function_escape_special_chars((string) $_val); |
---|
108 | } |
---|
109 | break; |
---|
110 | |
---|
111 | case 'checkboxes': |
---|
112 | trigger_error('html_checkboxes: the use of the "checkboxes" attribute is deprecated, use "options" instead', E_USER_WARNING); |
---|
113 | $options = (array) $_val; |
---|
114 | break; |
---|
115 | |
---|
116 | case 'assign': |
---|
117 | break; |
---|
118 | |
---|
119 | default: |
---|
120 | if(!is_array($_val)) { |
---|
121 | $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"'; |
---|
122 | } else { |
---|
123 | trigger_error("html_checkboxes: extra attribute '$_key' cannot be an array", E_USER_NOTICE); |
---|
124 | } |
---|
125 | break; |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | if (!isset($options) && !isset($values)) |
---|
130 | return ''; /* raise error here? */ |
---|
131 | |
---|
132 | $_html_result = array(); |
---|
133 | |
---|
134 | if (isset($options)) { |
---|
135 | foreach ($options as $_key=>$_val) { |
---|
136 | $_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids, $escape); |
---|
137 | } |
---|
138 | } else { |
---|
139 | foreach ($values as $_i=>$_key) { |
---|
140 | $_val = isset($output[$_i]) ? $output[$_i] : ''; |
---|
141 | $_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids, $escape); |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | if(!empty($params['assign'])) { |
---|
146 | $template->assign($params['assign'], $_html_result); |
---|
147 | } else { |
---|
148 | return implode("\n", $_html_result); |
---|
149 | } |
---|
150 | |
---|
151 | } |
---|
152 | |
---|
153 | function smarty_function_html_checkboxes_output($name, $value, $output, $selected, $extra, $separator, $labels, $label_ids, $escape=true) { |
---|
154 | $_output = ''; |
---|
155 | |
---|
156 | if (is_object($value)) { |
---|
157 | if (method_exists($value, "__toString")) { |
---|
158 | $value = (string) $value->__toString(); |
---|
159 | } else { |
---|
160 | trigger_error("html_options: value is an object of class '". get_class($value) ."' without __toString() method", E_USER_NOTICE); |
---|
161 | return ''; |
---|
162 | } |
---|
163 | } else { |
---|
164 | $value = (string) $value; |
---|
165 | } |
---|
166 | |
---|
167 | if (is_object($output)) { |
---|
168 | if (method_exists($output, "__toString")) { |
---|
169 | $output = (string) $output->__toString(); |
---|
170 | } else { |
---|
171 | trigger_error("html_options: output is an object of class '". get_class($output) ."' without __toString() method", E_USER_NOTICE); |
---|
172 | return ''; |
---|
173 | } |
---|
174 | } else { |
---|
175 | $output = (string) $output; |
---|
176 | } |
---|
177 | |
---|
178 | if ($labels) { |
---|
179 | if ($label_ids) { |
---|
180 | $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!' . Smarty::$_UTF8_MODIFIER, '_', $name . '_' . $value)); |
---|
181 | $_output .= '<label for="' . $_id . '">'; |
---|
182 | } else { |
---|
183 | $_output .= '<label>'; |
---|
184 | } |
---|
185 | } |
---|
186 | |
---|
187 | $name = smarty_function_escape_special_chars($name); |
---|
188 | $value = smarty_function_escape_special_chars($value); |
---|
189 | if ($escape) { |
---|
190 | $output = smarty_function_escape_special_chars($output); |
---|
191 | } |
---|
192 | |
---|
193 | $_output .= '<input type="checkbox" name="' . $name . '[]" value="' . $value . '"'; |
---|
194 | |
---|
195 | if ($labels && $label_ids) { |
---|
196 | $_output .= ' id="' . $_id . '"'; |
---|
197 | } |
---|
198 | |
---|
199 | if (is_array($selected)) { |
---|
200 | if (isset($selected[$value])) { |
---|
201 | $_output .= ' checked="checked"'; |
---|
202 | } |
---|
203 | } elseif ($value === $selected) { |
---|
204 | $_output .= ' checked="checked"'; |
---|
205 | } |
---|
206 | |
---|
207 | $_output .= $extra . ' />' . $output; |
---|
208 | if ($labels) { |
---|
209 | $_output .= '</label>'; |
---|
210 | } |
---|
211 | |
---|
212 | $_output .= $separator; |
---|
213 | return $_output; |
---|
214 | } |
---|
215 | |
---|
216 | ?> |
---|