6 * @subpackage PluginsFunction
10 * Smarty {html_checkboxes} function plugin
12 * File: function.html_checkboxes.php<br>
14 * Name: html_checkboxes<br>
15 * Date: 24.Feb.2003<br>
16 * Purpose: Prints out a list of checkbox input types<br>
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}
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
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>
40 * @param array $params parameters
41 * @param object $template template object
43 * @uses smarty_function_escape_special_chars()
45 function smarty_function_html_checkboxes($params, $template)
47 require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
61 foreach($params as $_key => $_val) {
65 $$_key = (string) $_val;
71 $$_key = (bool) $_val;
75 $$_key = (array) $_val;
80 $$_key = array_values((array) $_val);
85 if (is_array($_val)) {
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());
92 trigger_error("html_checkboxes: selected attribute contains an object of class '". get_class($_sel) ."' without __toString() method", E_USER_NOTICE);
96 $_sel = smarty_function_escape_special_chars((string) $_sel);
98 $selected[$_sel] = true;
100 } elseif (is_object($_val)) {
101 if (method_exists($_val, "__toString")) {
102 $selected = smarty_function_escape_special_chars((string) $_val->__toString());
104 trigger_error("html_checkboxes: selected attribute is an object of class '". get_class($_val) ."' without __toString() method", E_USER_NOTICE);
107 $selected = smarty_function_escape_special_chars((string) $_val);
112 trigger_error('html_checkboxes: the use of the "checkboxes" attribute is deprecated, use "options" instead', E_USER_WARNING);
113 $options = (array) $_val;
120 if(!is_array($_val)) {
121 $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
123 trigger_error("html_checkboxes: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
129 if (!isset($options) && !isset($values))
130 return ''; /* raise error here? */
132 $_html_result = array();
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);
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);
145 if(!empty($params['assign'])) {
146 $template->assign($params['assign'], $_html_result);
148 return implode("\n", $_html_result);
153 function smarty_function_html_checkboxes_output($name, $value, $output, $selected, $extra, $separator, $labels, $label_ids, $escape=true) {
156 if (is_object($value)) {
157 if (method_exists($value, "__toString")) {
158 $value = (string) $value->__toString();
160 trigger_error("html_options: value is an object of class '". get_class($value) ."' without __toString() method", E_USER_NOTICE);
164 $value = (string) $value;
167 if (is_object($output)) {
168 if (method_exists($output, "__toString")) {
169 $output = (string) $output->__toString();
171 trigger_error("html_options: output is an object of class '". get_class($output) ."' without __toString() method", E_USER_NOTICE);
175 $output = (string) $output;
180 $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!' . Smarty::$_UTF8_MODIFIER, '_', $name . '_' . $value));
181 $_output .= '<label for="' . $_id . '">';
183 $_output .= '<label>';
187 $name = smarty_function_escape_special_chars($name);
188 $value = smarty_function_escape_special_chars($value);
190 $output = smarty_function_escape_special_chars($output);
193 $_output .= '<input type="checkbox" name="' . $name . '[]" value="' . $value . '"';
195 if ($labels && $label_ids) {
196 $_output .= ' id="' . $_id . '"';
199 if (is_array($selected)) {
200 if (isset($selected[$value])) {
201 $_output .= ' checked="checked"';
203 } elseif ($value === $selected) {
204 $_output .= ' checked="checked"';
207 $_output .= $extra . ' />' . $output;
209 $_output .= '</label>';
212 $_output .= $separator;