]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/Smarty/plugins/function.html_checkboxes.php
Release 6.5.0
[Github/sugarcrm.git] / include / Smarty / plugins / function.html_checkboxes.php
1 <?php
2
3 /*
4
5 Modification information for LGPL compliance
6
7 r56990 - 2010-06-16 13:05:36 -0700 (Wed, 16 Jun 2010) - kjing - snapshot "Mango" svn branch to a new one for GitHub sync
8
9 r56989 - 2010-06-16 13:01:33 -0700 (Wed, 16 Jun 2010) - kjing - defunt "Mango" svn dev branch before github cutover
10
11 r55980 - 2010-04-19 13:31:28 -0700 (Mon, 19 Apr 2010) - kjing - create Mango (6.1) based on windex
12
13 r51719 - 2009-10-22 10:18:00 -0700 (Thu, 22 Oct 2009) - mitani - Converted to Build 3  tags and updated the build system 
14
15 r51634 - 2009-10-19 13:32:22 -0700 (Mon, 19 Oct 2009) - mitani - Windex is the branch for Sugar Sales 1.0 development
16
17 r50375 - 2009-08-24 18:07:43 -0700 (Mon, 24 Aug 2009) - dwong - branch kobe2 from tokyo r50372
18
19 r42807 - 2008-12-29 11:16:59 -0800 (Mon, 29 Dec 2008) - dwong - Branch from trunk/sugarcrm r42806 to branches/tokyo/sugarcrm
20
21 r8230 - 2005-10-03 17:47:19 -0700 (Mon, 03 Oct 2005) - majed - Added Sugar_Smarty to the code tree.
22
23
24 */
25
26
27 /**
28  * Smarty plugin
29  * @package Smarty
30  * @subpackage plugins
31  */
32
33
34 /**
35  * Smarty {html_checkboxes} function plugin
36  *
37  * File:       function.html_checkboxes.php<br>
38  * Type:       function<br>
39  * Name:       html_checkboxes<br>
40  * Date:       24.Feb.2003<br>
41  * Purpose:    Prints out a list of checkbox input types<br>
42  * Input:<br>
43  *           - name       (optional) - string default "checkbox"
44  *           - values     (required) - array
45  *           - options    (optional) - associative array
46  *           - checked    (optional) - array default not set
47  *           - separator  (optional) - ie <br> or &nbsp;
48  *           - output     (optional) - the output next to each checkbox
49  *           - assign     (optional) - assign the output as an array to this variable
50  * Examples:
51  * <pre>
52  * {html_checkboxes values=$ids output=$names}
53  * {html_checkboxes values=$ids name='box' separator='<br>' output=$names}
54  * {html_checkboxes values=$ids checked=$checked separator='<br>' output=$names}
55  * </pre>
56  * @link http://smarty.php.net/manual/en/language.function.html.checkboxes.php {html_checkboxes}
57  *      (Smarty online manual)
58  * @author     Christopher Kvarme <christopher.kvarme@flashjab.com>
59  * @author credits to Monte Ohrt <monte at ohrt dot com>
60  * @version    1.0
61  * @param array
62  * @param Smarty
63  * @return string
64  * @uses smarty_function_escape_special_chars()
65  */
66 function smarty_function_html_checkboxes($params, &$smarty)
67 {
68     require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
69
70     $name = 'checkbox';
71     $values = null;
72     $options = null;
73     $selected = null;
74     $separator = '';
75     $labels = true;
76     $output = null;
77
78     $extra = '';
79
80     foreach($params as $_key => $_val) {
81         switch($_key) {
82             case 'name':
83             case 'separator':
84                 $$_key = $_val;
85                 break;
86
87             case 'labels':
88                 $$_key = (bool)$_val;
89                 break;
90
91             case 'options':
92                 $$_key = (array)$_val;
93                 break;
94
95             case 'values':
96             case 'output':
97                 $$_key = array_values((array)$_val);
98                 break;
99
100             case 'checked':
101             case 'selected':
102                 $selected = array_map('strval', array_values((array)$_val));
103                 break;
104
105             case 'checkboxes':
106                 $smarty->trigger_error('html_checkboxes: the use of the "checkboxes" attribute is deprecated, use "options" instead', E_USER_WARNING);
107                 $options = (array)$_val;
108                 break;
109
110             case 'assign':
111                 break;
112
113             default:
114                 if(!is_array($_val)) {
115                     $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
116                 } else {
117                     $smarty->trigger_error("html_checkboxes: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
118                 }
119                 break;
120         }
121     }
122
123     if (!isset($options) && !isset($values))
124         return ''; /* raise error here? */
125
126     settype($selected, 'array');
127     $_html_result = array();
128
129     if (isset($options)) {
130
131         foreach ($options as $_key=>$_val)
132             $_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels);
133
134
135     } else {
136         foreach ($values as $_i=>$_key) {
137             $_val = isset($output[$_i]) ? $output[$_i] : '';
138             $_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels);
139         }
140
141     }
142
143     if(!empty($params['assign'])) {
144         $smarty->assign($params['assign'], $_html_result);
145     } else {
146         return implode("\n",$_html_result);
147     }
148
149 }
150
151 function smarty_function_html_checkboxes_output($name, $value, $output, $selected, $extra, $separator, $labels) {
152     $_output = '';
153     if ($labels) $_output .= '<label>';
154     $_output .= '<input type="checkbox" name="'
155         . smarty_function_escape_special_chars($name) . '[]" value="'
156         . smarty_function_escape_special_chars($value) . '"';
157
158     if (in_array((string)$value, $selected)) {
159         $_output .= ' checked="checked"';
160     }
161     $_output .= $extra . ' />' . $output;
162     if ($labels) $_output .= '</label>';
163     $_output .=  $separator;
164
165     return $_output;
166 }
167
168 ?>