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: 42: 43: 44: 45:
46: function smarty_function_html_radios($params, $template)
47: {
48: require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
49:
50: $name = 'radio';
51: $values = null;
52: $options = null;
53: $selected = null;
54: $separator = '';
55: $escape = true;
56: $labels = true;
57: $label_ids = false;
58: $output = null;
59: $extra = '';
60:
61: foreach ($params as $_key => $_val) {
62: switch ($_key) {
63: case 'name':
64: case 'separator':
65: $$_key = (string) $_val;
66: break;
67:
68: case 'checked':
69: case 'selected':
70: if (is_array($_val)) {
71: trigger_error('html_radios: the "' . $_key . '" attribute cannot be an array', E_USER_WARNING);
72: } elseif (is_object($_val)) {
73: if (method_exists($_val, "__toString")) {
74: $selected = smarty_function_escape_special_chars((string) $_val->__toString());
75: } else {
76: trigger_error("html_radios: selected attribute is an object of class '" . get_class($_val) . "' without __toString() method", E_USER_NOTICE);
77: }
78: } else {
79: $selected = (string) $_val;
80: }
81: break;
82:
83: case 'escape':
84: case 'labels':
85: case 'label_ids':
86: $$_key = (bool) $_val;
87: break;
88:
89: case 'options':
90: $$_key = (array) $_val;
91: break;
92:
93: case 'values':
94: case 'output':
95: $$_key = array_values((array) $_val);
96: break;
97:
98: case 'radios':
99: trigger_error('html_radios: the use of the "radios" attribute is deprecated, use "options" instead', E_USER_WARNING);
100: $options = (array) $_val;
101: break;
102:
103: case 'assign':
104: break;
105:
106: case 'strict':
107: break;
108:
109: case 'disabled':
110: case 'readonly':
111: if (!empty($params['strict'])) {
112: if (!is_scalar($_val)) {
113: trigger_error("html_options: $_key attribute must be a scalar, only boolean true or string '$_key' will actually add the attribute", E_USER_NOTICE);
114: }
115:
116: if ($_val === true || $_val === $_key) {
117: $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_key) . '"';
118: }
119:
120: break;
121: }
122:
123:
124: default:
125: if (!is_array($_val)) {
126: $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
127: } else {
128: trigger_error("html_radios: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
129: }
130: break;
131: }
132: }
133:
134: if (!isset($options) && !isset($values)) {
135:
136:
137: return '';
138: }
139:
140: $_html_result = array();
141:
142: if (isset($options)) {
143: foreach ($options as $_key => $_val) {
144: $_html_result[] = smarty_function_html_radios_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids, $escape);
145: }
146: } else {
147: foreach ($values as $_i => $_key) {
148: $_val = isset($output[$_i]) ? $output[$_i] : '';
149: $_html_result[] = smarty_function_html_radios_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids, $escape);
150: }
151: }
152:
153: if (!empty($params['assign'])) {
154: $template->assign($params['assign'], $_html_result);
155: } else {
156: return implode("\n", $_html_result);
157: }
158: }
159:
160: function smarty_function_html_radios_output($name, $value, $output, $selected, $extra, $separator, $labels, $label_ids, $escape)
161: {
162: $_output = '';
163:
164: if (is_object($value)) {
165: if (method_exists($value, "__toString")) {
166: $value = (string) $value->__toString();
167: } else {
168: trigger_error("html_options: value is an object of class '" . get_class($value) . "' without __toString() method", E_USER_NOTICE);
169:
170: return '';
171: }
172: } else {
173: $value = (string) $value;
174: }
175:
176: if (is_object($output)) {
177: if (method_exists($output, "__toString")) {
178: $output = (string) $output->__toString();
179: } else {
180: trigger_error("html_options: output is an object of class '" . get_class($output) . "' without __toString() method", E_USER_NOTICE);
181:
182: return '';
183: }
184: } else {
185: $output = (string) $output;
186: }
187:
188: if ($labels) {
189: if ($label_ids) {
190: $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!' . Smarty::$_UTF8_MODIFIER, '_', $name . '_' . $value));
191: $_output .= '<label for="' . $_id . '">';
192: } else {
193: $_output .= '<label>';
194: }
195: }
196:
197: $name = smarty_function_escape_special_chars($name);
198: $value = smarty_function_escape_special_chars($value);
199: if ($escape) {
200: $output = smarty_function_escape_special_chars($output);
201: }
202:
203: $_output .= '<input type="radio" name="' . $name . '" value="' . $value . '"';
204:
205: if ($labels && $label_ids) {
206: $_output .= ' id="' . $_id . '"';
207: }
208:
209: if ($value === $selected) {
210: $_output .= ' checked="checked"';
211: }
212:
213: $_output .= $extra . ' />' . $output;
214: if ($labels) {
215: $_output .= '</label>';
216: }
217:
218: $_output .= $separator;
219:
220: return $_output;
221: }
222: