1: | <?php
|
2: | |
3: | |
4: | |
5: | |
6: | |
7: |
|
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | |
19: | |
20: | |
21: | |
22: | |
23: | |
24: | |
25: | |
26: | |
27: | |
28: | |
29: | |
30: | |
31: | |
32: | |
33: | |
34: | |
35: | |
36: | |
37: | |
38: | |
39: | |
40: | |
41: | |
42: | |
43: | |
44: |
|
45: | function smarty_function_html_checkboxes($params, Smarty_Internal_Template $template)
|
46: | {
|
47: | $template->_checkPlugins(
|
48: | array(
|
49: | array(
|
50: | 'function' => 'smarty_function_escape_special_chars',
|
51: | 'file' => SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php'
|
52: | )
|
53: | )
|
54: | );
|
55: | $name = 'checkbox';
|
56: | $values = null;
|
57: | $options = null;
|
58: | $selected = array();
|
59: | $separator = '';
|
60: | $escape = true;
|
61: | $labels = true;
|
62: | $label_ids = false;
|
63: | $output = null;
|
64: | $extra = '';
|
65: | foreach ($params as $_key => $_val) {
|
66: | switch ($_key) {
|
67: | case 'name':
|
68: | case 'separator':
|
69: | $$_key = (string)$_val;
|
70: | break;
|
71: | case 'escape':
|
72: | case 'labels':
|
73: | case 'label_ids':
|
74: | $$_key = (bool)$_val;
|
75: | break;
|
76: | case 'options':
|
77: | $$_key = (array)$_val;
|
78: | break;
|
79: | case 'values':
|
80: | case 'output':
|
81: | $$_key = array_values((array)$_val);
|
82: | break;
|
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(
|
93: | 'html_checkboxes: selected attribute contains an object of class \'' .
|
94: | get_class($_sel) . '\' without __toString() method',
|
95: | E_USER_NOTICE
|
96: | );
|
97: | continue;
|
98: | }
|
99: | } else {
|
100: | $_sel = smarty_function_escape_special_chars((string)$_sel);
|
101: | }
|
102: | $selected[ $_sel ] = true;
|
103: | }
|
104: | } elseif (is_object($_val)) {
|
105: | if (method_exists($_val, '__toString')) {
|
106: | $selected = smarty_function_escape_special_chars((string)$_val->__toString());
|
107: | } else {
|
108: | trigger_error(
|
109: | 'html_checkboxes: selected attribute is an object of class \'' . get_class($_val) .
|
110: | '\' without __toString() method',
|
111: | E_USER_NOTICE
|
112: | );
|
113: | }
|
114: | } else {
|
115: | $selected = smarty_function_escape_special_chars((string)$_val);
|
116: | }
|
117: | break;
|
118: | case 'checkboxes':
|
119: | trigger_error(
|
120: | 'html_checkboxes: the use of the "checkboxes" attribute is deprecated, use "options" instead',
|
121: | E_USER_WARNING
|
122: | );
|
123: | $options = (array)$_val;
|
124: | break;
|
125: | case 'assign':
|
126: | break;
|
127: | case 'strict':
|
128: | break;
|
129: | case 'disabled':
|
130: | case 'readonly':
|
131: | if (!empty($params[ 'strict' ])) {
|
132: | if (!is_scalar($_val)) {
|
133: | trigger_error(
|
134: | "html_options: {$_key} attribute must be a scalar, only boolean true or string '{$_key}' will actually add the attribute",
|
135: | E_USER_NOTICE
|
136: | );
|
137: | }
|
138: | if ($_val === true || $_val === $_key) {
|
139: | $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_key) . '"';
|
140: | }
|
141: | break;
|
142: | }
|
143: |
|
144: |
|
145: | default:
|
146: | if (!is_array($_val)) {
|
147: | $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
|
148: | } else {
|
149: | trigger_error("html_checkboxes: extra attribute '{$_key}' cannot be an array", E_USER_NOTICE);
|
150: | }
|
151: | break;
|
152: | }
|
153: | }
|
154: | if (!isset($options) && !isset($values)) {
|
155: | return '';
|
156: | }
|
157: | $_html_result = array();
|
158: | if (isset($options)) {
|
159: | foreach ($options as $_key => $_val) {
|
160: | $_html_result[] =
|
161: | smarty_function_html_checkboxes_output(
|
162: | $name,
|
163: | $_key,
|
164: | $_val,
|
165: | $selected,
|
166: | $extra,
|
167: | $separator,
|
168: | $labels,
|
169: | $label_ids,
|
170: | $escape
|
171: | );
|
172: | }
|
173: | } else {
|
174: | foreach ($values as $_i => $_key) {
|
175: | $_val = isset($output[ $_i ]) ? $output[ $_i ] : '';
|
176: | $_html_result[] =
|
177: | smarty_function_html_checkboxes_output(
|
178: | $name,
|
179: | $_key,
|
180: | $_val,
|
181: | $selected,
|
182: | $extra,
|
183: | $separator,
|
184: | $labels,
|
185: | $label_ids,
|
186: | $escape
|
187: | );
|
188: | }
|
189: | }
|
190: | if (!empty($params[ 'assign' ])) {
|
191: | $template->assign($params[ 'assign' ], $_html_result);
|
192: | } else {
|
193: | return implode("\n", $_html_result);
|
194: | }
|
195: | }
|
196: |
|
197: | |
198: | |
199: | |
200: | |
201: | |
202: | |
203: | |
204: | |
205: | |
206: | |
207: | |
208: | |
209: |
|
210: | function smarty_function_html_checkboxes_output(
|
211: | $name,
|
212: | $value,
|
213: | $output,
|
214: | $selected,
|
215: | $extra,
|
216: | $separator,
|
217: | $labels,
|
218: | $label_ids,
|
219: | $escape = true
|
220: | ) {
|
221: | $_output = '';
|
222: | if (is_object($value)) {
|
223: | if (method_exists($value, '__toString')) {
|
224: | $value = (string)$value->__toString();
|
225: | } else {
|
226: | trigger_error(
|
227: | 'html_options: value is an object of class \'' . get_class($value) .
|
228: | '\' without __toString() method',
|
229: | E_USER_NOTICE
|
230: | );
|
231: | return '';
|
232: | }
|
233: | } else {
|
234: | $value = (string)$value;
|
235: | }
|
236: | if (is_object($output)) {
|
237: | if (method_exists($output, '__toString')) {
|
238: | $output = (string)$output->__toString();
|
239: | } else {
|
240: | trigger_error(
|
241: | 'html_options: output is an object of class \'' . get_class($output) .
|
242: | '\' without __toString() method',
|
243: | E_USER_NOTICE
|
244: | );
|
245: | return '';
|
246: | }
|
247: | } else {
|
248: | $output = (string)$output;
|
249: | }
|
250: | if ($labels) {
|
251: | if ($label_ids) {
|
252: | $_id = smarty_function_escape_special_chars(
|
253: | preg_replace(
|
254: | '![^\w\-\.]!' . Smarty::$_UTF8_MODIFIER,
|
255: | '_',
|
256: | $name . '_' . $value
|
257: | )
|
258: | );
|
259: | $_output .= '<label for="' . $_id . '">';
|
260: | } else {
|
261: | $_output .= '<label>';
|
262: | }
|
263: | }
|
264: | $name = smarty_function_escape_special_chars($name);
|
265: | $value = smarty_function_escape_special_chars($value);
|
266: | if ($escape) {
|
267: | $output = smarty_function_escape_special_chars($output);
|
268: | }
|
269: | $_output .= '<input type="checkbox" name="' . $name . '[]" value="' . $value . '"';
|
270: | if ($labels && $label_ids) {
|
271: | $_output .= ' id="' . $_id . '"';
|
272: | }
|
273: | if (is_array($selected)) {
|
274: | if (isset($selected[ $value ])) {
|
275: | $_output .= ' checked="checked"';
|
276: | }
|
277: | } elseif ($value === $selected) {
|
278: | $_output .= ' checked="checked"';
|
279: | }
|
280: | $_output .= $extra . ' />' . $output;
|
281: | if ($labels) {
|
282: | $_output .= '</label>';
|
283: | }
|
284: | $_output .= $separator;
|
285: | return $_output;
|
286: | }
|
287: | |