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_checkboxes($params, &$smarty)
42: {
43: require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
44:
45: $name = 'checkbox';
46: $values = null;
47: $options = null;
48: $selected = null;
49: $separator = '';
50: $labels = true;
51: $output = null;
52:
53: $extra = '';
54:
55: foreach($params as $_key => $_val) {
56: switch($_key) {
57: case 'name':
58: case 'separator':
59: $$_key = $_val;
60: break;
61:
62: case 'labels':
63: $$_key = (bool)$_val;
64: break;
65:
66: case 'options':
67: $$_key = (array)$_val;
68: break;
69:
70: case 'values':
71: case 'output':
72: $$_key = array_values((array)$_val);
73: break;
74:
75: case 'checked':
76: case 'selected':
77: $selected = array_map('strval', array_values((array)$_val));
78: break;
79:
80: case 'checkboxes':
81: $smarty->trigger_error('html_checkboxes: the use of the "checkboxes" attribute is deprecated, use "options" instead', E_USER_WARNING);
82: $options = (array)$_val;
83: break;
84:
85: case 'assign':
86: break;
87:
88: default:
89: if(!is_array($_val)) {
90: $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
91: } else {
92: $smarty->trigger_error("html_checkboxes: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
93: }
94: break;
95: }
96: }
97:
98: if (!isset($options) && !isset($values))
99: return '';
100:
101: settype($selected, 'array');
102: $_html_result = array();
103:
104: if (isset($options)) {
105:
106: foreach ($options as $_key=>$_val)
107: $_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels);
108:
109:
110: } else {
111: foreach ($values as $_i=>$_key) {
112: $_val = isset($output[$_i]) ? $output[$_i] : '';
113: $_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels);
114: }
115:
116: }
117:
118: if(!empty($params['assign'])) {
119: $smarty->assign($params['assign'], $_html_result);
120: } else {
121: return implode("\n",$_html_result);
122: }
123:
124: }
125:
126: function smarty_function_html_checkboxes_output($name, $value, $output, $selected, $extra, $separator, $labels) {
127: $_output = '';
128: if ($labels) $_output .= '<label>';
129: $_output .= '<input type="checkbox" name="'
130: . smarty_function_escape_special_chars($name) . '[]" value="'
131: . smarty_function_escape_special_chars($value) . '"';
132:
133: if (in_array((string)$value, $selected)) {
134: $_output .= ' checked="checked"';
135: }
136: $_output .= $extra . ' />' . $output;
137: if ($labels) $_output .= '</label>';
138: $_output .= $separator;
139:
140: return $_output;
141: }
142:
143: ?>
144: