Code

Updated smarty to 1.0.9
[gosa.git] / gosa-core / include / smarty / plugins / function.html_options.php
1 <?php
2 /**
3  * Smarty plugin
4  * 
5  * @package Smarty
6  * @subpackage PluginsFunction
7  */
9 /**
10  * Smarty {html_options} function plugin
11  * 
12  * Type:     function<br>
13  * Name:     html_options<br>
14  * Purpose:  Prints the list of <option> tags generated from
15  *            the passed parameters
16  * 
17  * @link http://smarty.php.net/manual/en/language.function.html.options.php {html_image}
18  *      (Smarty online manual)
19  * @author Monte Ohrt <monte at ohrt dot com> 
20  * @param array $params parameters
21  * Input:<br>
22  *            - name       (optional) - string default "select"
23  *            - values     (required if no options supplied) - array
24  *            - options    (required if no values supplied) - associative array
25  *            - selected   (optional) - string default not set
26  *            - output     (required if not options supplied) - array
27  * @param object $template template object
28  * @return string 
29  * @uses smarty_function_escape_special_chars()
30  */
31 function smarty_function_html_options($params, $template)
32 {
33     require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
35     $name = null;
36     $values = null;
37     $options = null;
38     $selected = array();
39     $output = null;
40     $id = null;
41     $class = null;
43     $extra = '';
44     $options_extra = '';
46     foreach($params as $_key => $_val) {
47         switch ($_key) {
48             case 'name':
49             case 'class':
50             case 'id':
51                 $$_key = (string)$_val;
52                 break;
54             case 'options':
55                 $$_key = (array)$_val;
56                 break;
58             case 'values':
59             case 'output':
60                 $$_key = array_values((array)$_val);
61                 break;
63             case 'selected':
64                 $$_key = array_map('strval', array_values((array)$_val));
65                 break;
67             default:
68                 if (!is_array($_val)) {
69                     $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
70                 } else {
71                     trigger_error("html_options: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
72                 } 
73                 break;
74         } 
75     } 
77     if (!isset($options) && !isset($values))
78         return '';
79     /* raise error here? */
81     $_html_result = '';
82     $_idx = 0;
84     if (isset($options)) {
85         foreach ($options as $_key => $_val) {
86           $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected, $id, $class, $_idx);
87         }
88     } else {
89         foreach ($values as $_i => $_key) {
90             $_val = isset($output[$_i]) ? $output[$_i] : '';
91             $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected, $id, $class, $_idx);
92         } 
93     } 
95     if (!empty($name)) {
96         $_html_class = !empty($class) ? ' class="'.$class.'"' : '';
97         $_html_id = !empty($id) ? ' id="'.$id.'"' : '';
98         $_html_result = '<select name="' . $name . '"' . $_html_class . $_html_id . $extra . '>' . "\n" . $_html_result . '</select>' . "\n";
99     } 
101     return $_html_result;
102
104 function smarty_function_html_options_optoutput($key, $value, $selected, $id, $class, &$idx)
106     if (!is_array($value)) {
107         $_html_result = '<option value="' .
108         smarty_function_escape_special_chars($key) . '"';
109         if (in_array((string)$key, $selected))
110             $_html_result .= ' selected="selected"';
111         $_html_class = !empty($class) ? ' class="'.$class.' option"' : '';
112         $_html_id = !empty($id) ? ' id="'.$id.'-'.$idx.'"' : '';
113         $_html_result .= $_html_class . $_html_id . '>' . smarty_function_escape_special_chars($value) . '</option>' . "\n";
114         $idx++;
115     } else {
116         $_idx = 0;
117         $_html_result = smarty_function_html_options_optgroup($key, $value, $selected, $id.'-'.$idx, $class, $_idx);
118         $idx++;
119     }
120     return $_html_result;
121
123 function smarty_function_html_options_optgroup($key, $values, $selected, $id, $class, &$idx)
125     $optgroup_html = '<optgroup label="' . smarty_function_escape_special_chars($key) . '">' . "\n";
126     foreach ($values as $key => $value) {
127         $optgroup_html .= smarty_function_html_options_optoutput($key, $value, $selected, $id, $class, $idx);
128     } 
129     $optgroup_html .= "</optgroup>\n";
130     return $optgroup_html;
131
133 ?>