6 * @subpackage PluginsFunction
10 * Smarty {html_radios} function plugin
12 * File: function.html_radios.php<br>
14 * Name: html_radios<br>
15 * Date: 24.Feb.2003<br>
16 * Purpose: Prints out a list of radio input types<br>
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 * - escape (optional) - escape the content (not value), defaults to true
30 * {html_radios values=$ids output=$names}
31 * {html_radios values=$ids name='box' separator='<br>' output=$names}
32 * {html_radios values=$ids checked=$checked separator='<br>' output=$names}
35 * @link http://smarty.php.net/manual/en/language.function.html.radios.php {html_radios}
36 * (Smarty online manual)
37 * @author Christopher Kvarme <christopher.kvarme@flashjab.com>
38 * @author credits to Monte Ohrt <monte at ohrt dot com>
40 * @param array $params parameters
41 * @param Smarty_Internal_Template $template template object
43 * @uses smarty_function_escape_special_chars()
45 function smarty_function_html_radios($params, $template)
47 require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
60 foreach($params as $_key => $_val) {
64 $$_key = (string) $_val;
69 if (is_array($_val)) {
70 trigger_error('html_radios: the "' . $_key . '" attribute cannot be an array', E_USER_WARNING);
71 } elseif (is_object($_val)) {
72 if (method_exists($_val, "__toString")) {
73 $selected = smarty_function_escape_special_chars((string) $_val->__toString());
75 trigger_error("html_radios: selected attribute is an object of class '". get_class($_val) ."' without __toString() method", E_USER_NOTICE);
78 $selected = (string) $_val;
85 $$_key = (bool) $_val;
89 $$_key = (array) $_val;
94 $$_key = array_values((array) $_val);
98 trigger_error('html_radios: the use of the "radios" attribute is deprecated, use "options" instead', E_USER_WARNING);
99 $options = (array) $_val;
106 if (!is_array($_val)) {
107 $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
109 trigger_error("html_radios: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
115 if (!isset($options) && !isset($values)) {
116 /* raise error here? */
120 $_html_result = array();
122 if (isset($options)) {
123 foreach ($options as $_key => $_val) {
124 $_html_result[] = smarty_function_html_radios_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids, $escape);
127 foreach ($values as $_i => $_key) {
128 $_val = isset($output[$_i]) ? $output[$_i] : '';
129 $_html_result[] = smarty_function_html_radios_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids, $escape);
133 if (!empty($params['assign'])) {
134 $template->assign($params['assign'], $_html_result);
136 return implode("\n", $_html_result);
140 function smarty_function_html_radios_output($name, $value, $output, $selected, $extra, $separator, $labels, $label_ids, $escape)
144 if (is_object($value)) {
145 if (method_exists($value, "__toString")) {
146 $value = (string) $value->__toString();
148 trigger_error("html_options: value is an object of class '". get_class($value) ."' without __toString() method", E_USER_NOTICE);
152 $value = (string) $value;
155 if (is_object($output)) {
156 if (method_exists($output, "__toString")) {
157 $output = (string) $output->__toString();
159 trigger_error("html_options: output is an object of class '". get_class($output) ."' without __toString() method", E_USER_NOTICE);
163 $output = (string) $output;
168 $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!' . Smarty::$_UTF8_MODIFIER, '_', $name . '_' . $value));
169 $_output .= '<label for="' . $_id . '">';
171 $_output .= '<label>';
175 $name = smarty_function_escape_special_chars($name);
176 $value = smarty_function_escape_special_chars($value);
178 $output = smarty_function_escape_special_chars($output);
181 $_output .= '<input type="radio" name="' . $name . '" value="' . $value . '"';
183 if ($labels && $label_ids) {
184 $_output .= ' id="' . $_id . '"';
187 if ($value === $selected) {
188 $_output .= ' checked="checked"';
191 $_output .= $extra . ' />' . $output;
193 $_output .= '</label>';
196 $_output .= $separator;