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 require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
62 foreach ($params as $_key => $_val) {
66 $$_key = (string) $_val;
72 $$_key = (bool) $_val;
76 $$_key = (array) $_val;
81 $$_key = array_values((array) $_val);
86 if (is_array($_val)) {
88 foreach ($_val as $_sel) {
89 if (is_object($_sel)) {
90 if (method_exists($_sel, "__toString")) {
91 $_sel = smarty_function_escape_special_chars((string) $_sel->__toString());
93 trigger_error("html_checkboxes: selected attribute contains an object of class '" . get_class($_sel) . "' without __toString() method", E_USER_NOTICE);
97 $_sel = smarty_function_escape_special_chars((string) $_sel);
99 $selected[$_sel] = true;
101 } elseif (is_object($_val)) {
102 if (method_exists($_val, "__toString")) {
103 $selected = smarty_function_escape_special_chars((string) $_val->__toString());
105 trigger_error("html_checkboxes: selected attribute is an object of class '" . get_class($_val) . "' without __toString() method", E_USER_NOTICE);
108 $selected = smarty_function_escape_special_chars((string) $_val);
113 trigger_error('html_checkboxes: the use of the "checkboxes" attribute is deprecated, use "options" instead', E_USER_WARNING);
114 $options = (array) $_val;
125 if (!empty($params['strict'])) {
126 if (!is_scalar($_val)) {
127 trigger_error("html_options: $_key attribute must be a scalar, only boolean true or string '$_key' will actually add the attribute", E_USER_NOTICE);
130 if ($_val === true || $_val === $_key) {
131 $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_key) . '"';
136 // omit break; to fall through!
139 if (!is_array($_val)) {
140 $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
142 trigger_error("html_checkboxes: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
148 if (!isset($options) && !isset($values)) {
150 } /* raise error here? */
152 $_html_result = array();
154 if (isset($options)) {
155 foreach ($options as $_key => $_val) {
156 $_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids, $escape);
159 foreach ($values as $_i => $_key) {
160 $_val = isset($output[$_i]) ? $output[$_i] : '';
161 $_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids, $escape);
165 if (!empty($params['assign'])) {
166 $template->assign($params['assign'], $_html_result);
168 return implode("\n", $_html_result);
172 function smarty_function_html_checkboxes_output($name, $value, $output, $selected, $extra, $separator, $labels, $label_ids, $escape = true)
176 if (is_object($value)) {
177 if (method_exists($value, "__toString")) {
178 $value = (string) $value->__toString();
180 trigger_error("html_options: value is an object of class '" . get_class($value) . "' without __toString() method", E_USER_NOTICE);
185 $value = (string) $value;
188 if (is_object($output)) {
189 if (method_exists($output, "__toString")) {
190 $output = (string) $output->__toString();
192 trigger_error("html_options: output is an object of class '" . get_class($output) . "' without __toString() method", E_USER_NOTICE);
197 $output = (string) $output;
202 $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!' . Smarty::$_UTF8_MODIFIER, '_', $name . '_' . $value));
203 $_output .= '<label for="' . $_id . '">';
205 $_output .= '<label>';
209 $name = smarty_function_escape_special_chars($name);
210 $value = smarty_function_escape_special_chars($value);
212 $output = smarty_function_escape_special_chars($output);
215 $_output .= '<input type="checkbox" name="' . $name . '[]" value="' . $value . '"';
217 if ($labels && $label_ids) {
218 $_output .= ' id="' . $_id . '"';
221 if (is_array($selected)) {
222 if (isset($selected[$value])) {
223 $_output .= ' checked="checked"';
225 } elseif ($value === $selected) {
226 $_output .= ' checked="checked"';
229 $_output .= $extra . ' />' . $output;
231 $_output .= '</label>';
234 $_output .= $separator;