Редакция 2 | Только различия | Учитывать пробелы | Содержимое файла | Авторство | Последнее изменение | Открыть журнал | RSS
Редакция 2 | Редакция 296 | ||
---|---|---|---|
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_radios} function plugin
|
10 | * Smarty {html_radios} function plugin
|
11 | *
|
11 | *
|
12 | * File: function.html_radios.php<br>
|
12 | * File: function.html_radios.php<br>
|
13 | * Type: function<br>
|
13 | * Type: function<br>
|
14 | * Name: html_radios<br>
|
14 | * Name: html_radios<br>
|
15 | * Date: 24.Feb.2003<br>
|
15 | * Date: 24.Feb.2003<br>
|
16 | * Purpose: Prints out a list of radio input types<br>
|
16 | * Purpose: Prints out a list of radio input types<br>
|
17 | * Input:<br>
|
17 | * Input:<br>
|
18 | * - name (optional) - string default "radio"
|
18 | * - name (optional) - string default "radio"
|
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 radio button
|
23 | * - output (optional) - the output next to each radio button
|
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_radios values=$ids output=$names}
|
27 | * {html_radios values=$ids output=$names}
|
28 | * {html_radios values=$ids name='box' separator='<br>' output=$names}
|
28 | * {html_radios values=$ids name='box' separator='<br>' output=$names}
|
29 | * {html_radios values=$ids checked=$checked separator='<br>' output=$names}
|
29 | * {html_radios values=$ids checked=$checked separator='<br>' output=$names}
|
30 | * </pre>
|
30 | * </pre>
|
31 | * @link http://smarty.php.net/manual/en/language.function.html.radios.php {html_radios}
|
31 | * @link http://smarty.php.net/manual/en/language.function.html.radios.php {html_radios}
|
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_radios($params, &$smarty) |
41 | function smarty_function_html_radios($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 = 'radio'; |
45 | $name = 'radio'; |
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 | $label_ids = false; |
51 | $label_ids = false; |
52 | $output = null; |
52 | $output = null; |
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 = (string)$_val; |
59 | $$_key = (string)$_val; |
60 | break; |
60 | break; |
61 | 61 | ||
62 | case 'checked': |
62 | case 'checked': |
63 | case 'selected': |
63 | case 'selected': |
64 | if(is_array($_val)) { |
64 | if(is_array($_val)) { |
65 | $smarty->trigger_error('html_radios: the "' . $_key . '" attribute cannot be an array', E_USER_WARNING); |
65 | $smarty->trigger_error('html_radios: the "' . $_key . '" attribute cannot be an array', E_USER_WARNING); |
66 | } else { |
66 | } else { |
67 | $selected = (string)$_val; |
67 | $selected = (string)$_val; |
68 | }
|
68 | }
|
69 | break; |
69 | break; |
70 | 70 | ||
71 | case 'labels': |
71 | case 'labels': |
72 | case 'label_ids': |
72 | case 'label_ids': |
73 | $$_key = (bool)$_val; |
73 | $$_key = (bool)$_val; |
74 | break; |
74 | break; |
75 | 75 | ||
76 | case 'options': |
76 | case 'options': |
77 | $$_key = (array)$_val; |
77 | $$_key = (array)$_val; |
78 | break; |
78 | break; |
79 | 79 | ||
80 | case 'values': |
80 | case 'values': |
81 | case 'output': |
81 | case 'output': |
82 | $$_key = array_values((array)$_val); |
82 | $$_key = array_values((array)$_val); |
83 | break; |
83 | break; |
84 | 84 | ||
85 | case 'radios': |
85 | case 'radios': |
86 | $smarty->trigger_error('html_radios: the use of the "radios" attribute is deprecated, use "options" instead', E_USER_WARNING); |
86 | $smarty->trigger_error('html_radios: the use of the "radios" attribute is deprecated, use "options" instead', E_USER_WARNING); |
87 | $options = (array)$_val; |
87 | $options = (array)$_val; |
88 | break; |
88 | break; |
89 | 89 | ||
90 | case 'assign': |
90 | case 'assign': |
91 | break; |
91 | break; |
92 | 92 | ||
93 | default: |
93 | default: |
94 | if(!is_array($_val)) { |
94 | if(!is_array($_val)) { |
95 | $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"'; |
95 | $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"'; |
96 | } else { |
96 | } else { |
97 | $smarty->trigger_error("html_radios: extra attribute '$_key' cannot be an array", E_USER_NOTICE); |
97 | $smarty->trigger_error("html_radios: extra attribute '$_key' cannot be an array", E_USER_NOTICE); |
98 | }
|
98 | }
|
99 | break; |
99 | break; |
100 | }
|
100 | }
|
101 | }
|
101 | }
|
102 | 102 | ||
103 | if (!isset($options) && !isset($values)) |
103 | if (!isset($options) && !isset($values)) |
104 | return ''; /* raise error here? */ |
104 | return ''; /* raise error here? */ |
105 | 105 | ||
106 | $_html_result = array(); |
106 | $_html_result = array(); |
107 | 107 | ||
108 | if (isset($options)) { |
108 | if (isset($options)) { |
109 | 109 | ||
110 | foreach ($options as $_key=>$_val) |
110 | foreach ($options as $_key=>$_val) |
111 | $_html_result[] = smarty_function_html_radios_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids); |
111 | $_html_result[] = smarty_function_html_radios_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids); |
112 | 112 | ||
113 | } else { |
113 | } else { |
114 | 114 | ||
115 | foreach ($values as $_i=>$_key) { |
115 | foreach ($values as $_i=>$_key) { |
116 | $_val = isset($output[$_i]) ? $output[$_i] : ''; |
116 | $_val = isset($output[$_i]) ? $output[$_i] : ''; |
117 | $_html_result[] = smarty_function_html_radios_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids); |
117 | $_html_result[] = smarty_function_html_radios_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids); |
118 | }
|
118 | }
|
119 | 119 | ||
120 | }
|
120 | }
|
121 | 121 | ||
122 | if(!empty($params['assign'])) { |
122 | if(!empty($params['assign'])) { |
123 | $smarty->assign($params['assign'], $_html_result); |
123 | $smarty->assign($params['assign'], $_html_result); |
124 | } else { |
124 | } else { |
125 | return implode("\n",$_html_result); |
125 | return implode("\n",$_html_result); |
126 | }
|
126 | }
|
127 | 127 | ||
128 | }
|
128 | }
|
129 | 129 | ||
130 | function smarty_function_html_radios_output($name, $value, $output, $selected, $extra, $separator, $labels, $label_ids) { |
130 | function smarty_function_html_radios_output($name, $value, $output, $selected, $extra, $separator, $labels, $label_ids) { |
131 | $_output = ''; |
131 | $_output = ''; |
132 | if ($labels) { |
132 | if ($labels) { |
133 | if($label_ids) { |
133 | if($label_ids) { |
134 | $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!', '_', $name . '_' . $value)); |
134 | $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!', '_', $name . '_' . $value)); |
135 | $_output .= '<label for="' . $_id . '">'; |
135 | $_output .= '<label for="' . $_id . '">'; |
136 | } else { |
136 | } else { |
137 | $_output .= '<label>'; |
137 | $_output .= '<label>'; |
138 | }
|
138 | }
|
139 | }
|
139 | }
|
140 | $_output .= '<input type="radio" name="' |
140 | $_output .= '<input type="radio" name="' |
141 | . smarty_function_escape_special_chars($name) . '" value="' |
141 | . smarty_function_escape_special_chars($name) . '" value="' |
142 | . smarty_function_escape_special_chars($value) . '"'; |
142 | . smarty_function_escape_special_chars($value) . '"'; |
143 | 143 | ||
144 | if ($labels && $label_ids) $_output .= ' id="' . $_id . '"'; |
144 | if ($labels && $label_ids) $_output .= ' id="' . $_id . '"'; |
145 | 145 | ||
146 | if ((string)$value==$selected) { |
146 | if ((string)$value==$selected) { |
147 | $_output .= ' checked="checked"'; |
147 | $_output .= ' checked="checked"'; |
148 | }
|
148 | }
|
149 | $_output .= $extra . ' />' . $output; |
149 | $_output .= $extra . ' />' . $output; |
150 | if ($labels) $_output .= '</label>'; |
150 | if ($labels) $_output .= '</label>'; |
151 | $_output .= $separator; |
151 | $_output .= $separator; |
152 | 152 | ||
153 | return $_output; |
153 | return $_output; |
154 | }
|
154 | }
|
155 | 155 | ||
156 | ?>
|
156 | ?>
|
157 | 157 |