Commit | Line | Data |
---|---|---|
e586807d 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 'selected': | |
52 | case 'values': | |
53 | case 'output': | |
54 | $$_key = array_values((array)$_val); | |
55 | break; | |
56 | ||
57 | default: | |
58 | if(!is_array($_val)) { | |
59 | $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"'; | |
60 | } else { | |
61 | $smarty->trigger_error("html_options: extra attribute '$_key' cannot be an array", E_USER_NOTICE); | |
62 | } | |
63 | break; | |
64 | } | |
65 | } | |
66 | ||
67 | if (!isset($options) && !isset($values)) | |
68 | return ''; /* raise error here? */ | |
69 | ||
70 | $_html_result = ''; | |
71 | ||
72 | if (is_array($options)) { | |
73 | ||
74 | foreach ($options as $_key=>$_val) | |
75 | $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected); | |
76 | ||
77 | } else { | |
78 | ||
79 | foreach ((array)$values as $_i=>$_key) { | |
80 | $_val = isset($output[$_i]) ? $output[$_i] : ''; | |
81 | $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected); | |
82 | } | |
83 | ||
84 | } | |
85 | ||
86 | if(!empty($name)) { | |
87 | $_html_result = '<select name="' . $name . '"' . $extra . '>' . "\n" . $_html_result . '</select>' . "\n"; | |
88 | } | |
89 | ||
90 | return $_html_result; | |
91 | ||
92 | } | |
93 | ||
94 | function smarty_function_html_options_optoutput($key, $value, $selected) { | |
95 | if(!is_array($value)) { | |
96 | $_html_result = '<option label="' . smarty_function_escape_special_chars($value) . '" value="' . | |
97 | smarty_function_escape_special_chars($key) . '"'; | |
98 | if (in_array($key, $selected)) | |
99 | $_html_result .= ' selected="selected"'; | |
100 | $_html_result .= '>' . smarty_function_escape_special_chars($value) . '</option>' . "\n"; | |
101 | } else { | |
102 | $_html_result = smarty_function_html_options_optgroup($key, $value, $selected); | |
103 | } | |
104 | return $_html_result; | |
105 | } | |
106 | ||
107 | function smarty_function_html_options_optgroup($key, $values, $selected) { | |
108 | $optgroup_html = '<optgroup label="' . smarty_function_escape_special_chars($key) . '">' . "\n"; | |
109 | foreach ($values as $key => $value) { | |
110 | $optgroup_html .= smarty_function_html_options_optoutput($key, $value, $selected); | |
111 | } | |
112 | $optgroup_html .= "</optgroup>\n"; | |
113 | return $optgroup_html; | |
114 | } | |
115 | ||
116 | /* vim: set expandtab: */ | |
117 | ||
118 | ?> |