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