Commit | Line | Data |
---|---|---|
b42b2bf9 H |
1 | <?php |
2 | /** | |
3 | * Smarty plugin | |
4 | * @package Smarty | |
5 | * @subpackage plugins | |
6 | */ | |
7 | ||
8 | ||
9 | /** | |
10 | * Smarty {html_options} function plugin | |
11 | * | |
12 | * Type: function<br> | |
13 | * Name: html_options<br> | |
14 | * Input:<br> | |
15 | * - name (optional) - string default "select" | |
16 | * - values (required if no options supplied) - array | |
17 | * - options (required if no values supplied) - associative array | |
18 | * - selected (optional) - string default not set | |
19 | * - output (required if not options supplied) - array | |
20 | * Purpose: Prints the list of <option> tags generated from | |
21 | * the passed parameters | |
22 | * @link http://smarty.php.net/manual/en/language.function.html.options.php {html_image} | |
23 | * (Smarty online manual) | |
24 | * @param array | |
25 | * @param Smarty | |
26 | * @return string | |
27 | * @uses smarty_function_escape_special_chars() | |
28 | */ | |
29 | function smarty_function_html_options($params, &$smarty) | |
30 | { | |
31 | require_once $smarty->_get_plugin_filepath('shared','escape_special_chars'); | |
32 | ||
33 | $name = null; | |
34 | $values = null; | |
35 | $options = null; | |
36 | $selected = array(); | |
37 | $output = null; | |
38 | ||
39 | $extra = ''; | |
40 | ||
41 | foreach($params as $_key => $_val) { | |
42 | switch($_key) { | |
43 | case 'name': | |
44 | $$_key = (string)$_val; | |
45 | break; | |
46 | ||
47 | case 'options': | |
48 | $$_key = (array)$_val; | |
49 | break; | |
50 | ||
51 | case 'values': | |
52 | case 'output': | |
53 | $$_key = array_values((array)$_val); | |
54 | break; | |
55 | ||
56 | case 'selected': | |
57 | $$_key = array_map('strval', array_values((array)$_val)); | |
58 | break; | |
59 | ||
60 | default: | |
61 | if(!is_array($_val)) { | |
62 | $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"'; | |
63 | } else { | |
64 | $smarty->trigger_error("html_options: extra attribute '$_key' cannot be an array", E_USER_NOTICE); | |
65 | } | |
66 | break; | |
67 | } | |
68 | } | |
69 | ||
70 | if (!isset($options) && !isset($values)) | |
71 | return ''; /* raise error here? */ | |
72 | ||
73 | $_html_result = ''; | |
74 | ||
75 | if (isset($options)) { | |
76 | ||
77 | foreach ($options as $_key=>$_val) | |
78 | $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected); | |
79 | ||
80 | } else { | |
81 | ||
82 | foreach ($values as $_i=>$_key) { | |
83 | $_val = isset($output[$_i]) ? $output[$_i] : ''; | |
84 | $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected); | |
85 | } | |
86 | ||
87 | } | |
88 | ||
89 | if(!empty($name)) { | |
90 | $_html_result = '<select name="' . $name . '"' . $extra . '>' . "\n" . $_html_result . '</select>' . "\n"; | |
91 | } | |
92 | ||
93 | return $_html_result; | |
94 | ||
95 | } | |
96 | ||
97 | function smarty_function_html_options_optoutput($key, $value, $selected) { | |
98 | if(!is_array($value)) { | |
99 | $_html_result = '<option label="' . smarty_function_escape_special_chars($value) . '" value="' . | |
100 | smarty_function_escape_special_chars($key) . '"'; | |
101 | if (in_array((string)$key, $selected)) | |
102 | $_html_result .= ' selected="selected"'; | |
103 | $_html_result .= '>' . smarty_function_escape_special_chars($value) . '</option>' . "\n"; | |
104 | } else { | |
105 | $_html_result = smarty_function_html_options_optgroup($key, $value, $selected); | |
106 | } | |
107 | return $_html_result; | |
108 | } | |
109 | ||
110 | function smarty_function_html_options_optgroup($key, $values, $selected) { | |
111 | $optgroup_html = '<optgroup label="' . smarty_function_escape_special_chars($key) . '">' . "\n"; | |
112 | foreach ($values as $key => $value) { | |
113 | $optgroup_html .= smarty_function_html_options_optoutput($key, $value, $selected); | |
114 | } | |
115 | $optgroup_html .= "</optgroup>\n"; | |
116 | return $optgroup_html; | |
117 | } | |
118 | ||
119 | /* vim: set expandtab: */ | |
120 | ||
121 | ?> |