source: trunk/include/smarty/libs/plugins/function.html_checkboxes.php @ 23384

Last change on this file since 23384 was 23384, checked in by rvelices, 11 years ago

smarty 3 - first pass for tests

  • Property svn:eol-style set to LF
File size: 7.8 KB
Line 
1<?php
2/**
3 * Smarty plugin
4 *
5 * @package Smarty
6 * @subpackage PluginsFunction
7 */
8
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 &nbsp;
30 * - output     (optional) - the output next to each checkbox
31 * - assign     (optional) - assign the output as an array to this variable
32 * - escape     (optional) - escape the content (not value), defaults to true
33 * </pre>
34 *
35 * @link http://www.smarty.net/manual/en/language.function.html.checkboxes.php {html_checkboxes}
36 *      (Smarty online manual)
37 * @author     Christopher Kvarme <christopher.kvarme@flashjab.com>
38 * @author credits to Monte Ohrt <monte at ohrt dot com>
39 * @version    1.0
40 * @param array $params parameters
41 * @param object $template template object
42 * @return string
43 * @uses smarty_function_escape_special_chars()
44 */
45function smarty_function_html_checkboxes($params, $template)
46{
47    require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
48
49    $name = 'checkbox';
50    $values = null;
51    $options = null;
52    $selected = array();
53    $separator = '';
54    $escape = true;
55    $labels = true;
56    $label_ids = false;
57    $output = null;
58
59    $extra = '';
60
61    foreach($params as $_key => $_val) {
62        switch($_key) {
63            case 'name':
64            case 'separator':
65                $$_key = (string) $_val;
66                break;
67
68            case 'escape':
69            case 'labels':
70            case 'label_ids':
71                $$_key = (bool) $_val;
72                break;
73
74            case 'options':
75                $$_key = (array) $_val;
76                break;
77
78            case 'values':
79            case 'output':
80                $$_key = array_values((array) $_val);
81                break;
82
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("html_checkboxes: selected attribute contains an object of class '". get_class($_sel) ."' without __toString() method", E_USER_NOTICE);
93                                continue;
94                            }
95                        } else {
96                            $_sel = smarty_function_escape_special_chars((string) $_sel);
97                        }
98                        $selected[$_sel] = true;
99                    }
100                } elseif (is_object($_val)) {
101                    if (method_exists($_val, "__toString")) {
102                        $selected = smarty_function_escape_special_chars((string) $_val->__toString());
103                    } else {
104                        trigger_error("html_checkboxes: selected attribute is an object of class '". get_class($_val) ."' without __toString() method", E_USER_NOTICE);
105                    }
106                } else {
107                    $selected = smarty_function_escape_special_chars((string) $_val);
108                }
109                break;
110
111            case 'checkboxes':
112                trigger_error('html_checkboxes: the use of the "checkboxes" attribute is deprecated, use "options" instead', E_USER_WARNING);
113                $options = (array) $_val;
114                break;
115
116            case 'assign':
117                break;
118
119            case 'strict': break;
120
121            case 'disabled':
122            case 'readonly':
123                if (!empty($params['strict'])) {
124                    if (!is_scalar($_val)) {
125                        trigger_error("html_options: $_key attribute must be a scalar, only boolean true or string '$_key' will actually add the attribute", E_USER_NOTICE);
126                    }
127
128                    if ($_val === true || $_val === $_key) {
129                        $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_key) . '"';
130                    }
131
132                    break;
133                }
134                // omit break; to fall through!
135
136            default:
137                if(!is_array($_val)) {
138                    $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
139                } else {
140                    trigger_error("html_checkboxes: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
141                }
142                break;
143        }
144    }
145
146    if (!isset($options) && !isset($values))
147        return ''; /* raise error here? */
148
149    $_html_result = array();
150
151    if (isset($options)) {
152        foreach ($options as $_key=>$_val) {
153            $_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids, $escape);
154        }
155    } else {
156        foreach ($values as $_i=>$_key) {
157            $_val = isset($output[$_i]) ? $output[$_i] : '';
158            $_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $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
170function smarty_function_html_checkboxes_output($name, $value, $output, $selected, $extra, $separator, $labels, $label_ids, $escape=true) {
171    $_output = '';
172   
173    if (is_object($value)) {
174        if (method_exists($value, "__toString")) {
175            $value = (string) $value->__toString();
176        } else {
177            trigger_error("html_options: value is an object of class '". get_class($value) ."' without __toString() method", E_USER_NOTICE);
178            return '';
179        }
180    } else {
181        $value = (string) $value;
182    }
183   
184    if (is_object($output)) {
185        if (method_exists($output, "__toString")) {
186            $output = (string) $output->__toString();
187        } else {
188            trigger_error("html_options: output is an object of class '". get_class($output) ."' without __toString() method", E_USER_NOTICE);
189            return '';
190        }
191    } else {
192        $output = (string) $output;
193    }
194   
195    if ($labels) {
196        if ($label_ids) {
197            $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!' . Smarty::$_UTF8_MODIFIER, '_', $name . '_' . $value));
198            $_output .= '<label for="' . $_id . '">';
199        } else {
200            $_output .= '<label>';
201        } 
202    }
203   
204    $name = smarty_function_escape_special_chars($name);
205    $value = smarty_function_escape_special_chars($value);
206    if ($escape) {
207        $output = smarty_function_escape_special_chars($output);
208    }
209   
210    $_output .= '<input type="checkbox" name="' . $name . '[]" value="' . $value . '"';
211   
212    if ($labels && $label_ids) {
213        $_output .= ' id="' . $_id . '"';
214    }
215   
216    if (is_array($selected)) {
217        if (isset($selected[$value])) {
218            $_output .= ' checked="checked"';
219        }
220    } elseif ($value === $selected) {
221        $_output .= ' checked="checked"';
222    }
223   
224    $_output .= $extra . ' />' . $output;
225    if ($labels) {
226        $_output .= '</label>';
227    }
228   
229    $_output .=  $separator;
230    return $_output;
231}
232
233?>
Note: See TracBrowser for help on using the repository browser.