]> git.mxchange.org Git - friendica.git/blob - vendor/smarty/smarty/libs/plugins/function.html_checkboxes.php
Add Smarty to Composer
[friendica.git] / vendor / smarty / smarty / libs / plugins / function.html_checkboxes.php
1 <?php
2 /**
3  * Smarty plugin
4  *
5  * @package    Smarty
6  * @subpackage PluginsFunction
7  */
8
9 /**
10  * Smarty {html_checkboxes} function plugin
11  * File:       function.html_checkboxes.php<br>
12  * Type:       function<br>
13  * Name:       html_checkboxes<br>
14  * Date:       24.Feb.2003<br>
15  * Purpose:    Prints out a list of checkbox input types<br>
16  * Examples:
17  * <pre>
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}
21  * </pre>
22  * Params:
23  * <pre>
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 &nbsp;
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
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  *
40  * @param array  $params   parameters
41  * @param object $template template object
42  *
43  * @return string
44  * @uses       smarty_function_escape_special_chars()
45  */
46 function smarty_function_html_checkboxes($params, $template)
47 {
48     if (!is_callable('smarty_function_escape_special_chars')) {
49         require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
50     }
51
52     $name = 'checkbox';
53     $values = null;
54     $options = null;
55     $selected = array();
56     $separator = '';
57     $escape = true;
58     $labels = true;
59     $label_ids = false;
60     $output = null;
61
62     $extra = '';
63
64     foreach ($params as $_key => $_val) {
65         switch ($_key) {
66             case 'name':
67             case 'separator':
68                 $$_key = (string) $_val;
69                 break;
70
71             case 'escape':
72             case 'labels':
73             case 'label_ids':
74                 $$_key = (bool) $_val;
75                 break;
76
77             case 'options':
78                 $$_key = (array) $_val;
79                 break;
80
81             case 'values':
82             case 'output':
83                 $$_key = array_values((array) $_val);
84                 break;
85
86             case 'checked':
87             case 'selected':
88                 if (is_array($_val)) {
89                     $selected = array();
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());
94                             } else {
95                                 trigger_error("html_checkboxes: selected attribute contains an object of class '" .
96                                               get_class($_sel) . "' without __toString() method", E_USER_NOTICE);
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("html_checkboxes: selected attribute is an object of class '" . get_class($_val) .
109                                       "' without __toString() method", E_USER_NOTICE);
110                     }
111                 } else {
112                     $selected = smarty_function_escape_special_chars((string) $_val);
113                 }
114                 break;
115
116             case 'checkboxes':
117                 trigger_error('html_checkboxes: the use of the "checkboxes" attribute is deprecated, use "options" instead',
118                               E_USER_WARNING);
119                 $options = (array) $_val;
120                 break;
121
122             case 'assign':
123                 break;
124
125             case 'strict':
126                 break;
127
128             case 'disabled':
129             case 'readonly':
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",
133                                       E_USER_NOTICE);
134                     }
135
136                     if ($_val === true || $_val === $_key) {
137                         $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_key) . '"';
138                     }
139
140                     break;
141                 }
142             // omit break; to fall through!
143
144             default:
145                 if (!is_array($_val)) {
146                     $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
147                 } else {
148                     trigger_error("html_checkboxes: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
149                 }
150                 break;
151         }
152     }
153
154     if (!isset($options) && !isset($values)) {
155         return '';
156     } /* raise error here? */
157
158     $_html_result = array();
159
160     if (isset($options)) {
161         foreach ($options as $_key => $_val) {
162             $_html_result[] =
163                 smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels,
164                                                        $label_ids, $escape);
165         }
166     } else {
167         foreach ($values as $_i => $_key) {
168             $_val = isset($output[ $_i ]) ? $output[ $_i ] : '';
169             $_html_result[] =
170                 smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels,
171                                                        $label_ids, $escape);
172         }
173     }
174
175     if (!empty($params[ 'assign' ])) {
176         $template->assign($params[ 'assign' ], $_html_result);
177     } else {
178         return implode("\n", $_html_result);
179     }
180 }
181
182 function smarty_function_html_checkboxes_output($name, $value, $output, $selected, $extra, $separator, $labels,
183                                                 $label_ids, $escape = true)
184 {
185     $_output = '';
186
187     if (is_object($value)) {
188         if (method_exists($value, "__toString")) {
189             $value = (string) $value->__toString();
190         } else {
191             trigger_error("html_options: value is an object of class '" . get_class($value) .
192                           "' without __toString() method", E_USER_NOTICE);
193
194             return '';
195         }
196     } else {
197         $value = (string) $value;
198     }
199
200     if (is_object($output)) {
201         if (method_exists($output, "__toString")) {
202             $output = (string) $output->__toString();
203         } else {
204             trigger_error("html_options: output is an object of class '" . get_class($output) .
205                           "' without __toString() method", E_USER_NOTICE);
206
207             return '';
208         }
209     } else {
210         $output = (string) $output;
211     }
212
213     if ($labels) {
214         if ($label_ids) {
215             $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!' . Smarty::$_UTF8_MODIFIER, '_',
216                                                                      $name . '_' . $value));
217             $_output .= '<label for="' . $_id . '">';
218         } else {
219             $_output .= '<label>';
220         }
221     }
222
223     $name = smarty_function_escape_special_chars($name);
224     $value = smarty_function_escape_special_chars($value);
225     if ($escape) {
226         $output = smarty_function_escape_special_chars($output);
227     }
228
229     $_output .= '<input type="checkbox" name="' . $name . '[]" value="' . $value . '"';
230
231     if ($labels && $label_ids) {
232         $_output .= ' id="' . $_id . '"';
233     }
234
235     if (is_array($selected)) {
236         if (isset($selected[ $value ])) {
237             $_output .= ' checked="checked"';
238         }
239     } elseif ($value === $selected) {
240         $_output .= ' checked="checked"';
241     }
242
243     $_output .= $extra . ' />' . $output;
244     if ($labels) {
245         $_output .= '</label>';
246     }
247
248     $_output .= $separator;
249
250     return $_output;
251 }