1: <?php
2: 3: 4: 5: 6:
7:
8:
9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 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 '';
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: ?>
157: