comparison Smarty/plugins/function.html_radios.php @ 1:f5a9f0eb4858

deleted: LICENSE.ja
author Sushi-k <epgrec@park.mda.or.jp>
date Wed, 08 Jul 2009 11:44:50 +0900
parents
children
comparison
equal deleted inserted replaced
0:96312e6ab8d4 1:f5a9f0eb4858
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 &nbsp;
23 * - output (optional) - the output next to each radio button
24 * - assign (optional) - assign the output as an array to this variable
25 * Examples:
26 * <pre>
27 * {html_radios values=$ids output=$names}
28 * {html_radios values=$ids name='box' separator='<br>' output=$names}
29 * {html_radios values=$ids checked=$checked separator='<br>' output=$names}
30 * </pre>
31 * @link http://smarty.php.net/manual/en/language.function.html.radios.php {html_radios}
32 * (Smarty online manual)
33 * @author Christopher Kvarme <christopher.kvarme@flashjab.com>
34 * @author credits to Monte Ohrt <monte at ohrt dot com>
35 * @version 1.0
36 * @param array
37 * @param Smarty
38 * @return string
39 * @uses smarty_function_escape_special_chars()
40 */
41 function smarty_function_html_radios($params, &$smarty)
42 {
43 require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
44
45 $name = 'radio';
46 $values = null;
47 $options = null;
48 $selected = null;
49 $separator = '';
50 $labels = true;
51 $label_ids = false;
52 $output = null;
53 $extra = '';
54
55 foreach($params as $_key => $_val) {
56 switch($_key) {
57 case 'name':
58 case 'separator':
59 $$_key = (string)$_val;
60 break;
61
62 case 'checked':
63 case 'selected':
64 if(is_array($_val)) {
65 $smarty->trigger_error('html_radios: the "' . $_key . '" attribute cannot be an array', E_USER_WARNING);
66 } else {
67 $selected = (string)$_val;
68 }
69 break;
70
71 case 'labels':
72 case 'label_ids':
73 $$_key = (bool)$_val;
74 break;
75
76 case 'options':
77 $$_key = (array)$_val;
78 break;
79
80 case 'values':
81 case 'output':
82 $$_key = array_values((array)$_val);
83 break;
84
85 case 'radios':
86 $smarty->trigger_error('html_radios: the use of the "radios" attribute is deprecated, use "options" instead', E_USER_WARNING);
87 $options = (array)$_val;
88 break;
89
90 case 'assign':
91 break;
92
93 default:
94 if(!is_array($_val)) {
95 $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
96 } else {
97 $smarty->trigger_error("html_radios: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
98 }
99 break;
100 }
101 }
102
103 if (!isset($options) && !isset($values))
104 return ''; /* raise error here? */
105
106 $_html_result = array();
107
108 if (isset($options)) {
109
110 foreach ($options as $_key=>$_val)
111 $_html_result[] = smarty_function_html_radios_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids);
112
113 } else {
114
115 foreach ($values as $_i=>$_key) {
116 $_val = isset($output[$_i]) ? $output[$_i] : '';
117 $_html_result[] = smarty_function_html_radios_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids);
118 }
119
120 }
121
122 if(!empty($params['assign'])) {
123 $smarty->assign($params['assign'], $_html_result);
124 } else {
125 return implode("\n",$_html_result);
126 }
127
128 }
129
130 function smarty_function_html_radios_output($name, $value, $output, $selected, $extra, $separator, $labels, $label_ids) {
131 $_output = '';
132 if ($labels) {
133 if($label_ids) {
134 $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!', '_', $name . '_' . $value));
135 $_output .= '<label for="' . $_id . '">';
136 } else {
137 $_output .= '<label>';
138 }
139 }
140 $_output .= '<input type="radio" name="'
141 . smarty_function_escape_special_chars($name) . '" value="'
142 . smarty_function_escape_special_chars($value) . '"';
143
144 if ($labels && $label_ids) $_output .= ' id="' . $_id . '"';
145
146 if ((string)$value==$selected) {
147 $_output .= ' checked="checked"';
148 }
149 $_output .= $extra . ' />' . $output;
150 if ($labels) $_output .= '</label>';
151 $_output .= $separator;
152
153 return $_output;
154 }
155
156 ?>