Subversion Repositories ALCASAR

Rev

Details | Last modification | View Log

Rev Author Line No. Line
2809 rexy 1
<?php
2
/**
3
 * Smarty plugin
4
 *
5
 * @package    Smarty
6
 * @subpackage PluginsFunction
7
 */
8
/**
9
 * Smarty {html_checkboxes} function plugin
10
 * File:       function.html_checkboxes.php
11
 * Type:       function
12
 * Name:       html_checkboxes
13
 * Date:       24.Feb.2003
14
 * Purpose:    Prints out a list of checkbox input types
15
 * Examples:
16
 *
17
 * {html_checkboxes values=$ids output=$names}
18
 * {html_checkboxes values=$ids name='box' separator='<br>' output=$names}
19
 * {html_checkboxes values=$ids checked=$checked separator='<br>' output=$names}
20
 *
21
 * Params:
22
 *
23
 * - name       (optional) - string default "checkbox"
24
 * - values     (required) - array
25
 * - options    (optional) - associative array
26
 * - checked    (optional) - array default not set
27
 * - separator  (optional) - ie <br> or &nbsp;
28
 * - output     (optional) - the output next to each checkbox
29
 * - assign     (optional) - assign the output as an array to this variable
30
 * - escape     (optional) - escape the content (not value), defaults to true
31
 *
32
 * @link    http://www.smarty.net/manual/en/language.function.html.checkboxes.php {html_checkboxes}
33
 *             (Smarty online manual)
34
 * @author  Christopher Kvarme <christopher.kvarme@flashjab.com>
35
 * @author  credits to Monte Ohrt <monte at ohrt dot com>
36
 * @version 1.0
37
 *
38
 * @param array                    $params   parameters
39
 * @param Smarty_Internal_Template $template template object
40
 *
41
 * @return string
42
 * @uses    smarty_function_escape_special_chars()
43
 * @throws \SmartyException
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
            // omit break; to fall through!
144
            // no break
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
    } /* raise error here? */
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
 * @param      $name
199
 * @param      $value
200
 * @param      $output
201
 * @param      $selected
202
 * @param      $extra
203
 * @param      $separator
204
 * @param      $labels
205
 * @param      $label_ids
206
 * @param bool $escape
207
 *
208
 * @return string
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
}