]> git.mxchange.org Git - friendica.git/blob - vendor/smarty/smarty/libs/plugins/function.html_radios.php
Add Smarty to Composer
[friendica.git] / vendor / smarty / smarty / libs / plugins / function.html_radios.php
1 <?php
2 /**
3  * Smarty plugin
4  *
5  * @package    Smarty
6  * @subpackage PluginsFunction
7  */
8
9 /**
10  * Smarty {html_radios} function plugin
11  * File:       function.html_radios.php<br>
12  * Type:       function<br>
13  * Name:       html_radios<br>
14  * Date:       24.Feb.2003<br>
15  * Purpose:    Prints out a list of radio input types<br>
16  * Params:
17  * <pre>
18  * - name       (optional) - string default "radio"
19  * - values     (required) - array
20  * - options    (required) - associative array
21  * - checked    (optional) - array default not set
22  * - separator  (optional) - ie <br> or &nbsp;
23  * - output     (optional) - the output next to each radio button
24  * - assign     (optional) - assign the output as an array to this variable
25  * - escape     (optional) - escape the content (not value), defaults to true
26  * </pre>
27  * Examples:
28  * <pre>
29  * {html_radios values=$ids output=$names}
30  * {html_radios values=$ids name='box' separator='<br>' output=$names}
31  * {html_radios values=$ids checked=$checked separator='<br>' output=$names}
32  * </pre>
33  *
34  * @link    http://smarty.php.net/manual/en/language.function.html.radios.php {html_radios}
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 Smarty_Internal_Template $template template object
42  *
43  * @return string
44  * @uses    smarty_function_escape_special_chars()
45  */
46 function smarty_function_html_radios($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 = 'radio';
53     $values = null;
54     $options = null;
55     $selected = null;
56     $separator = '';
57     $escape = true;
58     $labels = true;
59     $label_ids = false;
60     $output = null;
61     $extra = '';
62
63     foreach ($params as $_key => $_val) {
64         switch ($_key) {
65             case 'name':
66             case 'separator':
67                 $$_key = (string) $_val;
68                 break;
69
70             case 'checked':
71             case 'selected':
72                 if (is_array($_val)) {
73                     trigger_error('html_radios: the "' . $_key . '" attribute cannot be an array', E_USER_WARNING);
74                 } elseif (is_object($_val)) {
75                     if (method_exists($_val, "__toString")) {
76                         $selected = smarty_function_escape_special_chars((string) $_val->__toString());
77                     } else {
78                         trigger_error("html_radios: selected attribute is an object of class '" . get_class($_val) .
79                                       "' without __toString() method", E_USER_NOTICE);
80                     }
81                 } else {
82                     $selected = (string) $_val;
83                 }
84                 break;
85
86             case 'escape':
87             case 'labels':
88             case 'label_ids':
89                 $$_key = (bool) $_val;
90                 break;
91
92             case 'options':
93                 $$_key = (array) $_val;
94                 break;
95
96             case 'values':
97             case 'output':
98                 $$_key = array_values((array) $_val);
99                 break;
100
101             case 'radios':
102                 trigger_error('html_radios: the use of the "radios" attribute is deprecated, use "options" instead',
103                               E_USER_WARNING);
104                 $options = (array) $_val;
105                 break;
106
107             case 'assign':
108                 break;
109
110             case 'strict':
111                 break;
112
113             case 'disabled':
114             case 'readonly':
115                 if (!empty($params[ 'strict' ])) {
116                     if (!is_scalar($_val)) {
117                         trigger_error("html_options: $_key attribute must be a scalar, only boolean true or string '$_key' will actually add the attribute",
118                                       E_USER_NOTICE);
119                     }
120
121                     if ($_val === true || $_val === $_key) {
122                         $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_key) . '"';
123                     }
124
125                     break;
126                 }
127             // omit break; to fall through!
128
129             default:
130                 if (!is_array($_val)) {
131                     $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
132                 } else {
133                     trigger_error("html_radios: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
134                 }
135                 break;
136         }
137     }
138
139     if (!isset($options) && !isset($values)) {
140         /* raise error here? */
141
142         return '';
143     }
144
145     $_html_result = array();
146
147     if (isset($options)) {
148         foreach ($options as $_key => $_val) {
149             $_html_result[] =
150                 smarty_function_html_radios_output($name, $_key, $_val, $selected, $extra, $separator, $labels,
151                                                    $label_ids, $escape);
152         }
153     } else {
154         foreach ($values as $_i => $_key) {
155             $_val = isset($output[ $_i ]) ? $output[ $_i ] : '';
156             $_html_result[] =
157                 smarty_function_html_radios_output($name, $_key, $_val, $selected, $extra, $separator, $labels,
158                                                    $label_ids, $escape);
159         }
160     }
161
162     if (!empty($params[ 'assign' ])) {
163         $template->assign($params[ 'assign' ], $_html_result);
164     } else {
165         return implode("\n", $_html_result);
166     }
167 }
168
169 function smarty_function_html_radios_output($name, $value, $output, $selected, $extra, $separator, $labels, $label_ids,
170                                             $escape)
171 {
172     $_output = '';
173
174     if (is_object($value)) {
175         if (method_exists($value, "__toString")) {
176             $value = (string) $value->__toString();
177         } else {
178             trigger_error("html_options: value is an object of class '" . get_class($value) .
179                           "' without __toString() method", E_USER_NOTICE);
180
181             return '';
182         }
183     } else {
184         $value = (string) $value;
185     }
186
187     if (is_object($output)) {
188         if (method_exists($output, "__toString")) {
189             $output = (string) $output->__toString();
190         } else {
191             trigger_error("html_options: output is an object of class '" . get_class($output) .
192                           "' without __toString() method", E_USER_NOTICE);
193
194             return '';
195         }
196     } else {
197         $output = (string) $output;
198     }
199
200     if ($labels) {
201         if ($label_ids) {
202             $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!' . Smarty::$_UTF8_MODIFIER, '_',
203                                                                      $name . '_' . $value));
204             $_output .= '<label for="' . $_id . '">';
205         } else {
206             $_output .= '<label>';
207         }
208     }
209
210     $name = smarty_function_escape_special_chars($name);
211     $value = smarty_function_escape_special_chars($value);
212     if ($escape) {
213         $output = smarty_function_escape_special_chars($output);
214     }
215
216     $_output .= '<input type="radio" name="' . $name . '" value="' . $value . '"';
217
218     if ($labels && $label_ids) {
219         $_output .= ' id="' . $_id . '"';
220     }
221
222     if ($value === $selected) {
223         $_output .= ' checked="checked"';
224     }
225
226     $_output .= $extra . ' />' . $output;
227     if ($labels) {
228         $_output .= '</label>';
229     }
230
231     $_output .= $separator;
232
233     return $_output;
234 }