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 * {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}
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>
29 * @param array $params parameters
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 $template template object
40 * @uses smarty_function_escape_special_chars()
42 function smarty_function_html_radios($params, $template)
44 require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
56 foreach($params as $_key => $_val) {
60 $$_key = (string)$_val;
65 if (is_array($_val)) {
66 trigger_error('html_radios: the "' . $_key . '" attribute cannot be an array', E_USER_WARNING);
68 $selected = (string)$_val;
78 $$_key = (array)$_val;
83 $$_key = array_values((array)$_val);
87 trigger_error('html_radios: the use of the "radios" attribute is deprecated, use "options" instead', E_USER_WARNING);
88 $options = (array)$_val;
95 if (!is_array($_val)) {
96 $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
98 trigger_error("html_radios: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
104 if (!isset($options) && !isset($values))
106 /* raise error here? */
108 $_html_result = array();
110 if (isset($options)) {
111 foreach ($options as $_key => $_val)
112 $_html_result[] = smarty_function_html_radios_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids);
114 foreach ($values as $_i => $_key) {
115 $_val = isset($output[$_i]) ? $output[$_i] : '';
116 $_html_result[] = smarty_function_html_radios_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids);
120 if (!empty($params['assign'])) {
121 $template->assign($params['assign'], $_html_result);
123 return implode("\n", $_html_result);
127 function smarty_function_html_radios_output($name, $value, $output, $selected, $extra, $separator, $labels, $label_ids)
132 $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!', '_', $name . '_' . $value));
133 $_output .= '<label for="' . $_id . '">';
135 $_output .= '<label>';
138 $_output .= '<input type="radio" name="'
139 . smarty_function_escape_special_chars($name) . '" value="'
140 . smarty_function_escape_special_chars($value) . '"';
142 if ($labels && $label_ids) $_output .= ' id="' . $_id . '"';
144 if ((string)$value == $selected) {
145 $_output .= ' checked="checked"';
147 $_output .= $extra . ' />' . $output;
148 if ($labels) $_output .= '</label>';
149 $_output .= $separator;