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