Редакция 2 | Только различия | Не учитывать пробелы | Содержимое файла | Авторство | Последнее изменение | Открыть журнал | RSS
Редакция 2 | Редакция 145 | ||
---|---|---|---|
1 | <?php
|
1 | <?php
|
2 | /**
|
2 | /**
|
3 | * Smarty plugin
|
3 | * Smarty plugin
|
4 | * @package Smarty
|
4 | * @package Smarty
|
5 | * @subpackage plugins
|
5 | * @subpackage plugins
|
6 | */
|
6 | */
|
7 | 7 | ||
8 | 8 | ||
9 | /**
|
9 | /**
|
10 | * Smarty {html_checkboxes} function plugin
|
10 | * Smarty {html_checkboxes} function plugin
|
11 | *
|
11 | *
|
12 | * File: function.html_checkboxes.php<br>
|
12 | * File: function.html_checkboxes.php<br>
|
13 | * Type: function<br>
|
13 | * Type: function<br>
|
14 | * Name: html_checkboxes<br>
|
14 | * Name: html_checkboxes<br>
|
15 | * Date: 24.Feb.2003<br>
|
15 | * Date: 24.Feb.2003<br>
|
16 | * Purpose: Prints out a list of checkbox input types<br>
|
16 | * Purpose: Prints out a list of checkbox input types<br>
|
17 | * Input:<br>
|
17 | * Input:<br>
|
18 | * - name (optional) - string default "checkbox"
|
18 | * - name (optional) - string default "checkbox"
|
19 | * - values (required) - array
|
19 | * - values (required) - array
|
20 | * - options (optional) - associative array
|
20 | * - options (optional) - associative array
|
21 | * - checked (optional) - array default not set
|
21 | * - checked (optional) - array default not set
|
22 | * - separator (optional) - ie <br> or
|
22 | * - separator (optional) - ie <br> or
|
23 | * - output (optional) - the output next to each checkbox
|
23 | * - output (optional) - the output next to each checkbox
|
24 | * - assign (optional) - assign the output as an array to this variable
|
24 | * - assign (optional) - assign the output as an array to this variable
|
25 | * Examples:
|
25 | * Examples:
|
26 | * <pre>
|
26 | * <pre>
|
27 | * {html_checkboxes values=$ids output=$names}
|
27 | * {html_checkboxes values=$ids output=$names}
|
28 | * {html_checkboxes values=$ids name='box' separator='<br>' output=$names}
|
28 | * {html_checkboxes values=$ids name='box' separator='<br>' output=$names}
|
29 | * {html_checkboxes values=$ids checked=$checked separator='<br>' output=$names}
|
29 | * {html_checkboxes values=$ids checked=$checked separator='<br>' output=$names}
|
30 | * </pre>
|
30 | * </pre>
|
31 | * @link http://smarty.php.net/manual/en/language.function.html.checkboxes.php {html_checkboxes}
|
31 | * @link http://smarty.php.net/manual/en/language.function.html.checkboxes.php {html_checkboxes}
|
32 | * (Smarty online manual)
|
32 | * (Smarty online manual)
|
33 | * @author Christopher Kvarme <christopher.kvarme@flashjab.com>
|
33 | * @author Christopher Kvarme <christopher.kvarme@flashjab.com>
|
34 | * @author credits to Monte Ohrt <monte at ohrt dot com>
|
34 | * @author credits to Monte Ohrt <monte at ohrt dot com>
|
35 | * @version 1.0
|
35 | * @version 1.0
|
36 | * @param array
|
36 | * @param array
|
37 | * @param Smarty
|
37 | * @param Smarty
|
38 | * @return string
|
38 | * @return string
|
39 | * @uses smarty_function_escape_special_chars()
|
39 | * @uses smarty_function_escape_special_chars()
|
40 | */
|
40 | */
|
41 | function smarty_function_html_checkboxes($params, &$smarty) |
41 | function smarty_function_html_checkboxes($params, &$smarty) |
42 | {
|
42 | {
|
43 | require_once $smarty->_get_plugin_filepath('shared','escape_special_chars'); |
43 | require_once $smarty->_get_plugin_filepath('shared','escape_special_chars'); |
44 | 44 | ||
45 | $name = 'checkbox'; |
45 | $name = 'checkbox'; |
46 | $values = null; |
46 | $values = null; |
47 | $options = null; |
47 | $options = null; |
48 | $selected = null; |
48 | $selected = null; |
49 | $separator = ''; |
49 | $separator = ''; |
50 | $labels = true; |
50 | $labels = true; |
51 | $output = null; |
51 | $output = null; |
52 | 52 | ||
53 | $extra = ''; |
53 | $extra = ''; |
54 | 54 | ||
55 | foreach($params as $_key => $_val) { |
55 | foreach($params as $_key => $_val) { |
56 | switch($_key) { |
56 | switch($_key) { |
57 | case 'name': |
57 | case 'name': |
58 | case 'separator': |
58 | case 'separator': |
59 | $$_key = $_val; |
59 | $$_key = $_val; |
60 | break; |
60 | break; |
61 | 61 | ||
62 | case 'labels': |
62 | case 'labels': |
63 | $$_key = (bool)$_val; |
63 | $$_key = (bool)$_val; |
64 | break; |
64 | break; |
65 | 65 | ||
66 | case 'options': |
66 | case 'options': |
67 | $$_key = (array)$_val; |
67 | $$_key = (array)$_val; |
68 | break; |
68 | break; |
69 | 69 | ||
70 | case 'values': |
70 | case 'values': |
71 | case 'output': |
71 | case 'output': |
72 | $$_key = array_values((array)$_val); |
72 | $$_key = array_values((array)$_val); |
73 | break; |
73 | break; |
74 | 74 | ||
75 | case 'checked': |
75 | case 'checked': |
76 | case 'selected': |
76 | case 'selected': |
77 | $selected = array_map('strval', array_values((array)$_val)); |
77 | $selected = array_map('strval', array_values((array)$_val)); |
78 | break; |
78 | break; |
79 | 79 | ||
80 | case 'checkboxes': |
80 | case 'checkboxes': |
81 | $smarty->trigger_error('html_checkboxes: the use of the "checkboxes" attribute is deprecated, use "options" instead', E_USER_WARNING); |
81 | $smarty->trigger_error('html_checkboxes: the use of the "checkboxes" attribute is deprecated, use "options" instead', E_USER_WARNING); |
82 | $options = (array)$_val; |
82 | $options = (array)$_val; |
83 | break; |
83 | break; |
84 | 84 | ||
85 | case 'assign': |
85 | case 'assign': |
86 | break; |
86 | break; |
87 | 87 | ||
88 | default: |
88 | default: |
89 | if(!is_array($_val)) { |
89 | if(!is_array($_val)) { |
90 | $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"'; |
90 | $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"'; |
91 | } else { |
91 | } else { |
92 | $smarty->trigger_error("html_checkboxes: extra attribute '$_key' cannot be an array", E_USER_NOTICE); |
92 | $smarty->trigger_error("html_checkboxes: extra attribute '$_key' cannot be an array", E_USER_NOTICE); |
93 | }
|
93 | }
|
94 | break; |
94 | break; |
95 | }
|
95 | }
|
96 | }
|
96 | }
|
97 | 97 | ||
98 | if (!isset($options) && !isset($values)) |
98 | if (!isset($options) && !isset($values)) |
99 | return ''; /* raise error here? */ |
99 | return ''; /* raise error here? */ |
100 | 100 | ||
101 | settype($selected, 'array'); |
101 | settype($selected, 'array'); |
102 | $_html_result = array(); |
102 | $_html_result = array(); |
103 | 103 | ||
104 | if (isset($options)) { |
104 | if (isset($options)) { |
105 | 105 | ||
106 | foreach ($options as $_key=>$_val) |
106 | foreach ($options as $_key=>$_val) |
107 | $_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels); |
107 | $_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels); |
108 | 108 | ||
109 | 109 | ||
110 | } else { |
110 | } else { |
111 | foreach ($values as $_i=>$_key) { |
111 | foreach ($values as $_i=>$_key) { |
112 | $_val = isset($output[$_i]) ? $output[$_i] : ''; |
112 | $_val = isset($output[$_i]) ? $output[$_i] : ''; |
113 | $_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels); |
113 | $_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels); |
114 | }
|
114 | }
|
115 | 115 | ||
116 | }
|
116 | }
|
117 | 117 | ||
118 | if(!empty($params['assign'])) { |
118 | if(!empty($params['assign'])) { |
119 | $smarty->assign($params['assign'], $_html_result); |
119 | $smarty->assign($params['assign'], $_html_result); |
120 | } else { |
120 | } else { |
121 | return implode("\n",$_html_result); |
121 | return implode("\n",$_html_result); |
122 | }
|
122 | }
|
123 | 123 | ||
124 | }
|
124 | }
|
125 | 125 | ||
126 | function smarty_function_html_checkboxes_output($name, $value, $output, $selected, $extra, $separator, $labels) { |
126 | function smarty_function_html_checkboxes_output($name, $value, $output, $selected, $extra, $separator, $labels) { |
127 | $_output = ''; |
127 | $_output = ''; |
128 | if ($labels) $_output .= '<label>'; |
128 | if ($labels) $_output .= '<label>'; |
129 | $_output .= '<input type="checkbox" name="' |
129 | $_output .= '<input type="checkbox" name="' |
130 | . smarty_function_escape_special_chars($name) . '[]" value="' |
130 | . smarty_function_escape_special_chars($name) . '[]" value="' |
131 | . smarty_function_escape_special_chars($value) . '"'; |
131 | . smarty_function_escape_special_chars($value) . '"'; |
132 | 132 | ||
133 | if (in_array((string)$value, $selected)) { |
133 | if (in_array((string)$value, $selected)) { |
134 | $_output .= ' checked="checked"'; |
134 | $_output .= ' checked="checked"'; |
135 | }
|
135 | }
|
136 | $_output .= $extra . ' />' . $output; |
136 | $_output .= $extra . ' />' . $output; |
137 | if ($labels) $_output .= '</label>'; |
137 | if ($labels) $_output .= '</label>'; |
138 | $_output .= $separator; |
138 | $_output .= $separator; |
139 | 139 | ||
140 | return $_output; |
140 | return $_output; |
141 | }
|
141 | }
|
142 | 142 | ||
143 | ?>
|
143 | ?>
|
144 | 144 |