af29b9af220a6dcefeec99b3e6d1b8566a3ddcce
1 <?php
2 /**
3 * Smarty plugin
4 *
5 * @package Smarty
6 * @subpackage PluginsFunction
7 */
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 * Params:
18 * <pre>
19 * - name (optional) - string default "radio"
20 * - values (required) - array
21 * - options (required) - associative array
22 * - checked (optional) - array default not set
23 * - separator (optional) - ie <br> or
24 * - output (optional) - the output next to each radio button
25 * - assign (optional) - assign the output as an array to this variable
26 * </pre>
27 * Examples:
28 * <pre>
29 * {html_radios values=$ids output=$names}
30 * {html_radios values=$ids name='box' separator='<br>' output=$names}
31 * {html_radios values=$ids checked=$checked separator='<br>' output=$names}
32 * </pre>
33 *
34 * @link http://smarty.php.net/manual/en/language.function.html.radios.php {html_radios}
35 * (Smarty online manual)
36 * @author Christopher Kvarme <christopher.kvarme@flashjab.com>
37 * @author credits to Monte Ohrt <monte at ohrt dot com>
38 * @version 1.0
39 * @param array $params parameters
40 * @param Smarty_Internal_Template $template template object
41 * @return string
42 * @uses smarty_function_escape_special_chars()
43 */
44 function smarty_function_html_radios($params, $template)
45 {
46 require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
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 = '';
58 foreach($params as $_key => $_val) {
59 switch ($_key) {
60 case 'name':
61 case 'separator':
62 $$_key = (string) $_val;
63 break;
65 case 'checked':
66 case 'selected':
67 if (is_array($_val)) {
68 trigger_error('html_radios: the "' . $_key . '" attribute cannot be an array', E_USER_WARNING);
69 } elseif (is_object($_val)) {
70 if (method_exists($_val, "__toString")) {
71 $selected = smarty_function_escape_special_chars((string) $_val->__toString());
72 } else {
73 trigger_error("html_radios: selected attribute is an object of class '". get_class($_val) ."' without __toString() method", E_USER_NOTICE);
74 }
75 } else {
76 $selected = (string) $_val;
77 }
78 break;
80 case 'labels':
81 case 'label_ids':
82 $$_key = (bool) $_val;
83 break;
85 case 'options':
86 $$_key = (array) $_val;
87 break;
89 case 'values':
90 case 'output':
91 $$_key = array_values((array) $_val);
92 break;
94 case 'radios':
95 trigger_error('html_radios: the use of the "radios" attribute is deprecated, use "options" instead', E_USER_WARNING);
96 $options = (array) $_val;
97 break;
99 case 'assign':
100 break;
102 default:
103 if (!is_array($_val)) {
104 $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
105 } else {
106 trigger_error("html_radios: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
107 }
108 break;
109 }
110 }
112 if (!isset($options) && !isset($values)) {
113 /* raise error here? */
114 return '';
115 }
117 $_html_result = array();
119 if (isset($options)) {
120 foreach ($options as $_key => $_val) {
121 $_html_result[] = smarty_function_html_radios_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids);
122 }
123 } else {
124 foreach ($values as $_i => $_key) {
125 $_val = isset($output[$_i]) ? $output[$_i] : '';
126 $_html_result[] = smarty_function_html_radios_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids);
127 }
128 }
130 if (!empty($params['assign'])) {
131 $template->assign($params['assign'], $_html_result);
132 } else {
133 return implode("\n", $_html_result);
134 }
135 }
137 function smarty_function_html_radios_output($name, $value, $output, $selected, $extra, $separator, $labels, $label_ids)
138 {
139 $_output = '';
141 if (is_object($value)) {
142 if (method_exists($value, "__toString")) {
143 $value = (string) $value->__toString();
144 } else {
145 trigger_error("html_options: value is an object of class '". get_class($value) ."' without __toString() method", E_USER_NOTICE);
146 return '';
147 }
148 } else {
149 $value = (string) $value;
150 }
152 if (is_object($output)) {
153 if (method_exists($output, "__toString")) {
154 $output = (string) $output->__toString();
155 } else {
156 trigger_error("html_options: output is an object of class '". get_class($output) ."' without __toString() method", E_USER_NOTICE);
157 return '';
158 }
159 } else {
160 $output = (string) $output;
161 }
163 if ($labels) {
164 if ($label_ids) {
165 $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!u', '_', $name . '_' . $value));
166 $_output .= '<label for="' . $_id . '">';
167 } else {
168 $_output .= '<label>';
169 }
170 }
172 $name = smarty_function_escape_special_chars($name);
173 $value = smarty_function_escape_special_chars($value);
174 $output = smarty_function_escape_special_chars($output);
176 $_output .= '<input type="radio" name="' . $name . '" value="' . $value . '"';
178 if ($labels && $label_ids) {
179 $_output .= ' id="' . $_id . '"';
180 }
182 if ($value === $selected) {
183 $_output .= ' checked="checked"';
184 }
186 $_output .= $extra . ' />' . $output;
187 if ($labels) {
188 $_output .= '</label>';
189 }
191 $_output .= $separator;
192 return $_output;
193 }
195 ?>