51ff3226 |
1 | <?php |
2 | /** |
3 | * Smarty plugin |
4 | * @package Smarty |
5 | * @subpackage plugins |
6 | */ |
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 | * Input:<br> |
18 | * - name (optional) - string default "checkbox" |
19 | * - values (required) - array |
20 | * - options (optional) - associative array |
21 | * - checked (optional) - array default not set |
22 | * - separator (optional) - ie <br> or |
23 | * - output (optional) - without this one the buttons don't have names |
24 | * Examples: |
25 | * <pre> |
26 | * {html_checkboxes values=$ids output=$names} |
27 | * {html_checkboxes values=$ids name='box' separator='<br>' output=$names} |
28 | * {html_checkboxes values=$ids checked=$checked separator='<br>' output=$names} |
29 | * </pre> |
30 | * @link http://smarty.php.net/manual/en/language.function.html.checkboxes.php {html_checkboxes} |
31 | * (Smarty online manual) |
32 | * @author Christopher Kvarme <christopher.kvarme@flashjab.com> |
33 | * @author credits to Monte Ohrt <monte@ispi.net> |
34 | * @version 1.0 |
35 | * @param array |
36 | * @param Smarty |
37 | * @return string |
38 | * @uses smarty_function_escape_special_chars() |
39 | */ |
40 | function smarty_function_html_checkboxes($params, &$smarty) |
41 | { |
42 | require_once $smarty->_get_plugin_filepath('shared','escape_special_chars'); |
43 | |
44 | $name = 'checkbox'; |
45 | $values = null; |
46 | $options = null; |
47 | $selected = null; |
48 | $separator = ''; |
49 | $labels = true; |
50 | $output = null; |
51 | |
52 | $extra = ''; |
53 | |
54 | foreach($params as $_key => $_val) { |
55 | switch($_key) { |
56 | case 'name': |
57 | case 'separator': |
58 | $$_key = $_val; |
59 | break; |
60 | |
61 | case 'labels': |
62 | $$_key = (bool)$_val; |
63 | break; |
64 | |
65 | case 'options': |
66 | $$_key = (array)$_val; |
67 | break; |
68 | |
69 | case 'values': |
70 | case 'output': |
71 | $$_key = array_values((array)$_val); |
72 | break; |
73 | |
74 | case 'checked': |
75 | case 'selected': |
76 | $selected = array_values((array)$_val); |
77 | break; |
78 | |
79 | case 'checkboxes': |
80 | $smarty->trigger_error('html_checkboxes: the use of the "checkboxes" attribute is deprecated, use "options" instead', E_USER_WARNING); |
81 | $options = (array)$_val; |
82 | break; |
83 | |
84 | default: |
85 | if(!is_array($_val)) { |
86 | $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"'; |
87 | } else { |
88 | $smarty->trigger_error("html_checkboxes: extra attribute '$_key' cannot be an array", E_USER_NOTICE); |
89 | } |
90 | break; |
91 | } |
92 | } |
93 | |
94 | if (!isset($options) && !isset($values)) |
95 | return ''; /* raise error here? */ |
96 | |
97 | settype($selected, 'array'); |
98 | $_html_result = ''; |
99 | |
100 | if (is_array($options)) { |
101 | |
102 | foreach ($options as $_key=>$_val) |
103 | $_html_result .= smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels); |
104 | |
105 | |
106 | } else { |
107 | foreach ($values as $_i=>$_key) { |
108 | $_val = isset($output[$_i]) ? $output[$_i] : ''; |
109 | $_html_result .= smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels); |
110 | } |
111 | |
112 | } |
113 | |
114 | return $_html_result; |
115 | |
116 | } |
117 | |
118 | function smarty_function_html_checkboxes_output($name, $value, $output, $selected, $extra, $separator, $labels) { |
119 | $_output = ''; |
120 | if ($labels) $_output .= '<label>'; |
121 | $_output .= '<input type="checkbox" name="' |
122 | . smarty_function_escape_special_chars($name) . '[]" value="' |
123 | . smarty_function_escape_special_chars($value) . '"'; |
124 | |
125 | if (in_array($value, $selected)) { |
126 | $_output .= ' checked="checked"'; |
127 | } |
128 | $_output .= $extra . ' />' . $output; |
129 | if ($labels) $_output .= '</label>'; |
130 | $_output .= $separator . "\n"; |
131 | |
132 | return $_output; |
133 | } |
134 | |
135 | ?> |