1 <?php
2 /**
3 * Smarty plugin
4 * @package Smarty
5 * @subpackage PluginsFunction
6 */
9 /**
10 * Smarty {html_checkboxes} function plugin
11 *
12 * File: function.html_checkboxes.php<br>
13 * Type: function<br>
14 * Name: html_checkboxes<br>
15 * Date: 24.Feb.2003<br>
16 * Purpose: Prints out a list of checkbox input types<br>
17 * Examples:
18 * <pre>
19 * {html_checkboxes values=$ids output=$names}
20 * {html_checkboxes values=$ids name='box' separator='<br>' output=$names}
21 * {html_checkboxes values=$ids checked=$checked separator='<br>' output=$names}
22 * </pre>
23 * @link http://smarty.php.net/manual/en/language.function.html.checkboxes.php {html_checkboxes}
24 * (Smarty online manual)
25 * @author Christopher Kvarme <christopher.kvarme@flashjab.com>
26 * @author credits to Monte Ohrt <monte at ohrt dot com>
27 * @version 1.0
28 * @param array $params parameters
29 * Input:<br>
30 * - name (optional) - string default "checkbox"
31 * - values (required) - array
32 * - options (optional) - associative array
33 * - checked (optional) - array default not set
34 * - separator (optional) - ie <br> or
35 * - output (optional) - the output next to each checkbox
36 * - assign (optional) - assign the output as an array to this variable
37 * @param object $smarty Smarty object
38 * @param object $template template object
39 * @return string
40 * @uses smarty_function_escape_special_chars()
41 */
42 function smarty_function_html_checkboxes($params, $smarty, $template)
43 {
44 require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
45 //$smarty->loadPlugin('Smarty_shared_escape_special_chars');
47 $name = 'checkbox';
48 $values = null;
49 $options = null;
50 $selected = null;
51 $separator = '';
52 $labels = true;
53 $output = null;
55 $extra = '';
57 foreach($params as $_key => $_val) {
58 switch($_key) {
59 case 'name':
60 case 'separator':
61 $$_key = $_val;
62 break;
64 case 'labels':
65 $$_key = (bool)$_val;
66 break;
68 case 'options':
69 $$_key = (array)$_val;
70 break;
72 case 'values':
73 case 'output':
74 $$_key = array_values((array)$_val);
75 break;
77 case 'checked':
78 case 'selected':
79 $selected = array_map('strval', array_values((array)$_val));
80 break;
82 case 'checkboxes':
83 trigger_error('html_checkboxes: the use of the "checkboxes" attribute is deprecated, use "options" instead', E_USER_WARNING);
84 $options = (array)$_val;
85 break;
87 case 'assign':
88 break;
90 default:
91 if(!is_array($_val)) {
92 $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
93 } else {
94 trigger_error("html_checkboxes: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
95 }
96 break;
97 }
98 }
100 if (!isset($options) && !isset($values))
101 return ''; /* raise error here? */
103 settype($selected, 'array');
104 $_html_result = array();
106 if (isset($options)) {
108 foreach ($options as $_key=>$_val)
109 $_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels);
112 } else {
113 foreach ($values as $_i=>$_key) {
114 $_val = isset($output[$_i]) ? $output[$_i] : '';
115 $_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels);
116 }
118 }
120 if(!empty($params['assign'])) {
121 $template->assign($params['assign'], $_html_result);
122 } else {
123 return implode("\n",$_html_result);
124 }
126 }
128 function smarty_function_html_checkboxes_output($name, $value, $output, $selected, $extra, $separator, $labels) {
129 $_output = '';
130 if ($labels) $_output .= '<label>';
131 $_output .= '<input type="checkbox" name="'
132 . smarty_function_escape_special_chars($name) . '[]" value="'
133 . smarty_function_escape_special_chars($value) . '"';
135 if (in_array((string)$value, $selected)) {
136 $_output .= ' checked="checked"';
137 }
138 $_output .= $extra . ' />' . $output;
139 if ($labels) $_output .= '</label>';
140 $_output .= $separator;
142 return $_output;
143 }
145 ?>