Adjusted time.php to honor note on separate row option.
[timetracker.git] / WEB-INF / lib / smarty / plugins / function.html_radios.php
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 &nbsp;
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
39  * @return string 
40  * @uses smarty_function_escape_special_chars()
41  */
42 function smarty_function_html_radios($params, $template)
43 {
44     require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
45
46     $name = 'radio';
47     $values = null;
48     $options = null;
49     $selected = null;
50     $separator = '';
51     $labels = true;
52     $label_ids = false;
53     $output = null;
54     $extra = '';
55
56     foreach($params as $_key => $_val) {
57         switch ($_key) {
58             case 'name':
59             case 'separator':
60                 $$_key = (string)$_val;
61                 break;
62
63             case 'checked':
64             case 'selected':
65                 if (is_array($_val)) {
66                     trigger_error('html_radios: the "' . $_key . '" attribute cannot be an array', E_USER_WARNING);
67                 } else {
68                     $selected = (string)$_val;
69                 } 
70                 break;
71
72             case 'labels':
73             case 'label_ids':
74                 $$_key = (bool)$_val;
75                 break;
76
77             case 'options':
78                 $$_key = (array)$_val;
79                 break;
80
81             case 'values':
82             case 'output':
83                 $$_key = array_values((array)$_val);
84                 break;
85
86             case 'radios':
87                 trigger_error('html_radios: the use of the "radios" attribute is deprecated, use "options" instead', E_USER_WARNING);
88                 $options = (array)$_val;
89                 break;
90
91             case 'assign':
92                 break;
93
94             default:
95                 if (!is_array($_val)) {
96                     $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
97                 } else {
98                     trigger_error("html_radios: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
99                 } 
100                 break;
101         } 
102     } 
103
104     if (!isset($options) && !isset($values))
105         return '';
106     /* raise error here? */
107
108     $_html_result = array();
109
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);
113     } else {
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);
117         } 
118     } 
119
120     if (!empty($params['assign'])) {
121         $template->assign($params['assign'], $_html_result);
122     } else {
123         return implode("\n", $_html_result);
124     } 
125
126
127 function smarty_function_html_radios_output($name, $value, $output, $selected, $extra, $separator, $labels, $label_ids)
128 {
129     $_output = '';
130     if ($labels) {
131         if ($label_ids) {
132             $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!', '_', $name . '_' . $value));
133             $_output .= '<label for="' . $_id . '">';
134         } else {
135             $_output .= '<label>';
136         } 
137     } 
138     $_output .= '<input type="radio" name="'
139      . smarty_function_escape_special_chars($name) . '" value="'
140      . smarty_function_escape_special_chars($value) . '"';
141
142     if ($labels && $label_ids) $_output .= ' id="' . $_id . '"';
143
144     if ((string)$value == $selected) {
145         $_output .= ' checked="checked"';
146     } 
147     $_output .= $extra . ' />' . $output;
148     if ($labels) $_output .= '</label>';
149     $_output .= $separator;
150
151     return $_output;
152
153
154 ?>