1 <?php
2 /**
3 * Smarty plugin
4 *
5 * @package Smarty
6 * @subpackage PluginsFunction
7 */
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 * </pre>
33 *
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>
38 * @version 1.0
39 * @param array $params parameters
40 * @param object $template template object
41 * @return string
42 * @uses smarty_function_escape_special_chars()
43 */
44 function smarty_function_html_checkboxes($params, $template)
45 {
46 require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
48 $name = 'checkbox';
49 $values = null;
50 $options = null;
51 $selected = array();
52 $separator = '';
53 $labels = true;
54 $label_ids = false;
55 $output = null;
57 $extra = '';
59 foreach($params as $_key => $_val) {
60 switch($_key) {
61 case 'name':
62 case 'separator':
63 $$_key = (string) $_val;
64 break;
66 case 'labels':
67 case 'label_ids':
68 $$_key = (bool) $_val;
69 break;
71 case 'options':
72 $$_key = (array) $_val;
73 break;
75 case 'values':
76 case 'output':
77 $$_key = array_values((array) $_val);
78 break;
80 case 'checked':
81 case 'selected':
82 if (is_array($_val)) {
83 $selected = array();
84 foreach ($_val as $_sel) {
85 if (is_object($_sel)) {
86 if (method_exists($_sel, "__toString")) {
87 $_sel = smarty_function_escape_special_chars((string) $_sel->__toString());
88 } else {
89 trigger_error("html_checkboxes: selected attribute contains an object of class '". get_class($_sel) ."' without __toString() method", E_USER_NOTICE);
90 continue;
91 }
92 } else {
93 $_sel = smarty_function_escape_special_chars((string) $_sel);
94 }
95 $selected[$_sel] = true;
96 }
97 } elseif (is_object($_val)) {
98 if (method_exists($_val, "__toString")) {
99 $selected = smarty_function_escape_special_chars((string) $_val->__toString());
100 } else {
101 trigger_error("html_checkboxes: selected attribute is an object of class '". get_class($_val) ."' without __toString() method", E_USER_NOTICE);
102 }
103 } else {
104 $selected = smarty_function_escape_special_chars((string) $_val);
105 }
106 break;
108 case 'checkboxes':
109 trigger_error('html_checkboxes: the use of the "checkboxes" attribute is deprecated, use "options" instead', E_USER_WARNING);
110 $options = (array) $_val;
111 break;
113 case 'assign':
114 break;
116 default:
117 if(!is_array($_val)) {
118 $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
119 } else {
120 trigger_error("html_checkboxes: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
121 }
122 break;
123 }
124 }
126 if (!isset($options) && !isset($values))
127 return ''; /* raise error here? */
129 $_html_result = array();
131 if (isset($options)) {
132 foreach ($options as $_key=>$_val) {
133 $_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids);
134 }
135 } else {
136 foreach ($values as $_i=>$_key) {
137 $_val = isset($output[$_i]) ? $output[$_i] : '';
138 $_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids);
139 }
140 }
142 if(!empty($params['assign'])) {
143 $template->assign($params['assign'], $_html_result);
144 } else {
145 return implode("\n", $_html_result);
146 }
148 }
150 function smarty_function_html_checkboxes_output($name, $value, $output, $selected, $extra, $separator, $labels, $label_ids) {
151 $_output = '';
153 if (is_object($value)) {
154 if (method_exists($value, "__toString")) {
155 $value = (string) $value->__toString();
156 } else {
157 trigger_error("html_options: value is an object of class '". get_class($value) ."' without __toString() method", E_USER_NOTICE);
158 return '';
159 }
160 } else {
161 $value = (string) $value;
162 }
164 if (is_object($output)) {
165 if (method_exists($output, "__toString")) {
166 $output = (string) $output->__toString();
167 } else {
168 trigger_error("html_options: output is an object of class '". get_class($output) ."' without __toString() method", E_USER_NOTICE);
169 return '';
170 }
171 } else {
172 $output = (string) $output;
173 }
175 if ($labels) {
176 if ($label_ids) {
177 $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!u', '_', $name . '_' . $value));
178 $_output .= '<label for="' . $_id . '">';
179 } else {
180 $_output .= '<label>';
181 }
182 }
184 $name = smarty_function_escape_special_chars($name);
185 $value = smarty_function_escape_special_chars($value);
186 $output = smarty_function_escape_special_chars($output);
188 $_output .= '<input type="checkbox" name="' . $name . '[]" value="' . $value . '"';
190 if ($labels && $label_ids) {
191 $_output .= ' id="' . $_id . '"';
192 }
194 if (is_array($selected)) {
195 if (isset($selected[$value])) {
196 $_output .= ' checked="checked"';
197 }
198 } elseif ($value === $selected) {
199 $_output .= ' checked="checked"';
200 }
202 $_output .= $extra . ' />' . $output;
203 if ($labels) {
204 $_output .= '</label>';
205 }
207 $_output .= $separator;
208 return $_output;
209 }
211 ?>