]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/Smarty/plugins/function.html_options.php
Release 6.5.0
[Github/sugarcrm.git] / include / Smarty / plugins / function.html_options.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 r54701 - 2010-02-22 09:42:10 -0800 (Mon, 22 Feb 2010) - jmertic - Bug 35849 - Add optional option 'sortoptions' to html_options Smarty function to sort the passed options array. Then I updated that call in the Parent field template so we specify to use this newly added option.
14
15 r51719 - 2009-10-22 10:18:00 -0700 (Thu, 22 Oct 2009) - mitani - Converted to Build 3  tags and updated the build system 
16
17 r51634 - 2009-10-19 13:32:22 -0700 (Mon, 19 Oct 2009) - mitani - Windex is the branch for Sugar Sales 1.0 development
18
19 r50375 - 2009-08-24 18:07:43 -0700 (Mon, 24 Aug 2009) - dwong - branch kobe2 from tokyo r50372
20
21 r42807 - 2008-12-29 11:16:59 -0800 (Mon, 29 Dec 2008) - dwong - Branch from trunk/sugarcrm r42806 to branches/tokyo/sugarcrm
22
23 r35784 - 2008-05-20 14:31:40 -0700 (Tue, 20 May 2008) - dwheeler - 21475: Converted multiselect DynamicField default value to be a multiselect. 
24
25 r10971 - 2006-01-12 14:58:30 -0800 (Thu, 12 Jan 2006) - chris - Bug 4128: updating Smarty templates to 2.6.11, a version supposedly that plays better with PHP 5.1
26
27 r8230 - 2005-10-03 17:47:19 -0700 (Mon, 03 Oct 2005) - majed - Added Sugar_Smarty to the code tree.
28
29
30 */
31
32
33 /**
34  * Smarty plugin
35  * @package Smarty
36  * @subpackage plugins
37  */
38
39
40 /**
41  * Smarty {html_options} function plugin
42  *
43  * Type:     function<br>
44  * Name:     html_options<br>
45  * Input:<br>
46  *           - name       (optional) - string default "select"
47  *           - values     (required if no options supplied) - array
48  *           - options    (required if no values supplied) - associative array
49  *           - selected   (optional) - string default not set
50  *           - output     (required if not options supplied) - array
51  * Purpose:  Prints the list of <option> tags generated from
52  *           the passed parameters
53  * @link http://smarty.php.net/manual/en/language.function.html.options.php {html_image}
54  *      (Smarty online manual)
55  * @author Monte Ohrt <monte at ohrt dot com>
56  * @param array
57  * @param Smarty
58  * @return string
59  * @uses smarty_function_escape_special_chars()
60  */
61 function smarty_function_html_options($params, &$smarty)
62 {
63     require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
64     
65     $name = null;
66     $values = null;
67     $options = null;
68     $selected = array();
69     $output = null;
70     
71     $extra = '';
72     
73     foreach($params as $_key => $_val) {
74         switch($_key) {
75             case 'name':
76                 $$_key = (string)$_val;
77                 break;
78             
79             case 'options':
80                 $$_key = (array)$_val;
81                 break;
82                 
83             case 'values':
84             case 'output':
85                 $$_key = array_values((array)$_val);
86                 break;
87
88             case 'selected':
89                 $$_key = array_map('strval', array_values((array)$_val));
90                 break;  
91             case 'multiple':
92                 if ($_val)
93                         $extra .= ' multiple="true" size="6"';
94                 break;
95                 
96             case 'sortoptions':
97                 if ($_val)
98                     asort($options);
99                 break;
100                 
101             default:
102                 if(!is_array($_val)) {
103                     $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
104                 } else {
105                     $smarty->trigger_error("html_options: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
106                 }
107                 break;
108         }
109     }
110
111     if (!isset($options) && !isset($values))
112         return ''; /* raise error here? */
113
114     $_html_result = '';
115
116     if (isset($options)) {
117         
118         foreach ($options as $_key=>$_val)
119             $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected);
120
121     } else {
122         
123         foreach ($values as $_i=>$_key) {
124             $_val = isset($output[$_i]) ? $output[$_i] : '';
125             $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected);
126         }
127
128     }
129
130     if(!empty($name)) {
131         $_html_result = '<select name="' . $name . '"' . $extra . '>' . "\n" . $_html_result . '</select>' . "\n";
132     }
133
134     return $_html_result;
135
136 }
137
138 function smarty_function_html_options_optoutput($key, $value, $selected) {
139     if(!is_array($value)) {
140         $_html_result = '<option label="' . smarty_function_escape_special_chars($value) . '" value="' .
141             smarty_function_escape_special_chars($key) . '"';
142         if (in_array((string)$key, $selected))
143             $_html_result .= ' selected="selected"';
144         $_html_result .= '>' . smarty_function_escape_special_chars($value) . '</option>' . "\n";
145     } else {
146         $_html_result = smarty_function_html_options_optgroup($key, $value, $selected);
147     }
148     return $_html_result;
149 }
150
151 function smarty_function_html_options_optgroup($key, $values, $selected) {
152     $optgroup_html = '<optgroup label="' . smarty_function_escape_special_chars($key) . '">' . "\n";
153     foreach ($values as $key => $value) {
154         $optgroup_html .= smarty_function_html_options_optoutput($key, $value, $selected);
155     }
156     $optgroup_html .= "</optgroup>\n";
157     return $optgroup_html;
158 }
159
160 /* vim: set expandtab: */
161
162 ?>