Commit | Line | Data |
---|---|---|
e586807d H |
1 | <?php |
2 | /** | |
3 | * Smarty plugin | |
4 | * @package Smarty | |
5 | * @subpackage plugins | |
6 | */ | |
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 | * Input:<br> | |
18 | * - name (optional) - string default "radio" | |
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_radios values=$ids output=$names} | |
27 | * {html_radios values=$ids name='box' separator='<br>' output=$names} | |
28 | * {html_radios values=$ids checked=$checked separator='<br>' output=$names} | |
29 | * </pre> | |
30 | * @link http://smarty.php.net/manual/en/language.function.html.radios.php {html_radios} | |
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_radios($params, &$smarty) | |
41 | { | |
42 | require_once $smarty->_get_plugin_filepath('shared','escape_special_chars'); | |
43 | ||
44 | $name = 'radio'; | |
45 | $values = null; | |
46 | $options = null; | |
47 | $selected = null; | |
48 | $separator = ''; | |
49 | $labels = true; | |
50 | $output = null; | |
51 | $extra = ''; | |
52 | ||
53 | foreach($params as $_key => $_val) { | |
54 | switch($_key) { | |
55 | case 'name': | |
56 | case 'separator': | |
57 | $$_key = (string)$_val; | |
58 | break; | |
59 | ||
60 | case 'checked': | |
61 | case 'selected': | |
62 | if(is_array($_val)) { | |
63 | $smarty->trigger_error('html_radios: the "' . $_key . '" attribute cannot be an array', E_USER_WARNING); | |
64 | } else { | |
65 | $selected = (string)$_val; | |
66 | } | |
67 | break; | |
68 | ||
69 | case 'labels': | |
70 | $$_key = (bool)$_val; | |
71 | break; | |
72 | ||
73 | case 'options': | |
74 | $$_key = (array)$_val; | |
75 | break; | |
76 | ||
77 | case 'values': | |
78 | case 'output': | |
79 | $$_key = array_values((array)$_val); | |
80 | break; | |
81 | ||
82 | case 'radios': | |
83 | $smarty->trigger_error('html_radios: the use of the "radios" attribute is deprecated, use "options" instead', E_USER_WARNING); | |
84 | $options = (array)$_val; | |
85 | break; | |
86 | ||
87 | ||
88 | default: | |
89 | if(!is_array($_val)) { | |
90 | $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"'; | |
91 | } else { | |
92 | $smarty->trigger_error("html_radios: extra attribute '$_key' cannot be an array", E_USER_NOTICE); | |
93 | } | |
94 | break; | |
95 | } | |
96 | } | |
97 | ||
98 | if (!isset($options) && !isset($values)) | |
99 | return ''; /* raise error here? */ | |
100 | ||
101 | $_html_result = ''; | |
102 | ||
103 | if (isset($options) && is_array($options)) { | |
104 | ||
105 | foreach ((array)$options as $_key=>$_val) | |
106 | $_html_result .= smarty_function_html_radios_output($name, $_key, $_val, $selected, $extra, $separator, $labels); | |
107 | ||
108 | } else { | |
109 | ||
110 | foreach ((array)$values as $_i=>$_key) { | |
111 | $_val = isset($output[$_i]) ? $output[$_i] : ''; | |
112 | $_html_result .= smarty_function_html_radios_output($name, $_key, $_val, $selected, $extra, $separator, $labels); | |
113 | } | |
114 | ||
115 | } | |
116 | ||
117 | return $_html_result; | |
118 | ||
119 | } | |
120 | ||
121 | function smarty_function_html_radios_output($name, $value, $output, $selected, $extra, $separator, $labels) { | |
122 | $_output = ''; | |
123 | if ($labels) $_output .= '<label>'; | |
124 | $_output .= '<input type="radio" name="' | |
125 | . smarty_function_escape_special_chars($name) . '" value="' | |
126 | . smarty_function_escape_special_chars($value) . '"'; | |
127 | ||
128 | if ($value==$selected) { | |
129 | $_output .= ' checked="checked"'; | |
130 | } | |
131 | $_output .= $extra . ' />' . $output; | |
132 | if ($labels) $_output .= '</label>'; | |
133 | $_output .= $separator . "\n"; | |
134 | ||
135 | return $_output; | |
136 | } | |
137 | ||
138 | ?> |