6 * @subpackage PluginsFunction
10 * Smarty {html_checkboxes} function plugin
11 * File: function.html_checkboxes.php<br>
13 * Name: html_checkboxes<br>
14 * Date: 24.Feb.2003<br>
15 * Purpose: Prints out a list of checkbox input types<br>
18 * {html_checkboxes values=$ids output=$names}
19 * {html_checkboxes values=$ids name='box' separator='<br>' output=$names}
20 * {html_checkboxes values=$ids checked=$checked separator='<br>' output=$names}
24 * - name (optional) - string default "checkbox"
25 * - values (required) - array
26 * - options (optional) - associative array
27 * - checked (optional) - array default not set
28 * - separator (optional) - ie <br> or
29 * - output (optional) - the output next to each checkbox
30 * - assign (optional) - assign the output as an array to this variable
31 * - escape (optional) - escape the content (not value), defaults to true
34 * @link http://www.smarty.net/manual/en/language.function.html.checkboxes.php {html_checkboxes}
35 * (Smarty online manual)
36 * @author Christopher Kvarme <christopher.kvarme@flashjab.com>
37 * @author credits to Monte Ohrt <monte at ohrt dot com>
40 * @param array $params parameters
41 * @param object $template template object
44 * @uses smarty_function_escape_special_chars()
46 function smarty_function_html_checkboxes($params, $template)
48 if (!is_callable('smarty_function_escape_special_chars')) {
49 require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
64 foreach ($params as $_key => $_val) {
68 $$_key = (string) $_val;
74 $$_key = (bool) $_val;
78 $$_key = (array) $_val;
83 $$_key = array_values((array) $_val);
88 if (is_array($_val)) {
90 foreach ($_val as $_sel) {
91 if (is_object($_sel)) {
92 if (method_exists($_sel, "__toString")) {
93 $_sel = smarty_function_escape_special_chars((string) $_sel->__toString());
95 trigger_error("html_checkboxes: selected attribute contains an object of class '" .
96 get_class($_sel) . "' without __toString() method", E_USER_NOTICE);
100 $_sel = smarty_function_escape_special_chars((string) $_sel);
102 $selected[ $_sel ] = true;
104 } elseif (is_object($_val)) {
105 if (method_exists($_val, "__toString")) {
106 $selected = smarty_function_escape_special_chars((string) $_val->__toString());
108 trigger_error("html_checkboxes: selected attribute is an object of class '" . get_class($_val) .
109 "' without __toString() method", E_USER_NOTICE);
112 $selected = smarty_function_escape_special_chars((string) $_val);
117 trigger_error('html_checkboxes: the use of the "checkboxes" attribute is deprecated, use "options" instead',
119 $options = (array) $_val;
130 if (!empty($params[ 'strict' ])) {
131 if (!is_scalar($_val)) {
132 trigger_error("html_options: $_key attribute must be a scalar, only boolean true or string '$_key' will actually add the attribute",
136 if ($_val === true || $_val === $_key) {
137 $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_key) . '"';
142 // omit break; to fall through!
145 if (!is_array($_val)) {
146 $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
148 trigger_error("html_checkboxes: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
154 if (!isset($options) && !isset($values)) {
156 } /* raise error here? */
158 $_html_result = array();
160 if (isset($options)) {
161 foreach ($options as $_key => $_val) {
163 smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels,
164 $label_ids, $escape);
167 foreach ($values as $_i => $_key) {
168 $_val = isset($output[ $_i ]) ? $output[ $_i ] : '';
170 smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels,
171 $label_ids, $escape);
175 if (!empty($params[ 'assign' ])) {
176 $template->assign($params[ 'assign' ], $_html_result);
178 return implode("\n", $_html_result);
182 function smarty_function_html_checkboxes_output($name, $value, $output, $selected, $extra, $separator, $labels,
183 $label_ids, $escape = true)
187 if (is_object($value)) {
188 if (method_exists($value, "__toString")) {
189 $value = (string) $value->__toString();
191 trigger_error("html_options: value is an object of class '" . get_class($value) .
192 "' without __toString() method", E_USER_NOTICE);
197 $value = (string) $value;
200 if (is_object($output)) {
201 if (method_exists($output, "__toString")) {
202 $output = (string) $output->__toString();
204 trigger_error("html_options: output is an object of class '" . get_class($output) .
205 "' without __toString() method", E_USER_NOTICE);
210 $output = (string) $output;
215 $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!' . Smarty::$_UTF8_MODIFIER, '_',
216 $name . '_' . $value));
217 $_output .= '<label for="' . $_id . '">';
219 $_output .= '<label>';
223 $name = smarty_function_escape_special_chars($name);
224 $value = smarty_function_escape_special_chars($value);
226 $output = smarty_function_escape_special_chars($output);
229 $_output .= '<input type="checkbox" name="' . $name . '[]" value="' . $value . '"';
231 if ($labels && $label_ids) {
232 $_output .= ' id="' . $_id . '"';
235 if (is_array($selected)) {
236 if (isset($selected[ $value ])) {
237 $_output .= ' checked="checked"';
239 } elseif ($value === $selected) {
240 $_output .= ' checked="checked"';
243 $_output .= $extra . ' />' . $output;
245 $_output .= '</label>';
248 $_output .= $separator;