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 $smarty Smarty object
28 * @param object $template template object
29 * @return string
30 * @uses smarty_function_escape_special_chars()
31 */
33 function smarty_function_html_options($params, $smarty, $template)
34 {
35 require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
36 //$smarty->loadPlugin('Smarty_shared_escape_special_chars');
38 $name = null;
39 $values = null;
40 $options = null;
41 $selected = array();
42 $output = null;
44 $extra = '';
46 foreach($params as $_key => $_val) {
47 switch ($_key) {
48 case 'name':
49 $$_key = (string)$_val;
50 break;
52 case 'options':
53 $$_key = (array)$_val;
54 break;
56 case 'values':
57 case 'output':
58 $$_key = array_values((array)$_val);
59 break;
61 case 'selected':
62 $$_key = array_map('strval', array_values((array)$_val));
63 break;
65 default:
66 if (!is_array($_val)) {
67 $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
68 } else {
69 trigger_error("html_options: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
70 }
71 break;
72 }
73 }
75 if (!isset($options) && !isset($values))
76 return '';
77 /* raise error here? */
79 $_html_result = '';
81 if (isset($options)) {
82 foreach ($options as $_key => $_val)
83 $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected);
84 } else {
85 foreach ($values as $_i => $_key) {
86 $_val = isset($output[$_i]) ? $output[$_i] : '';
87 $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected);
88 }
89 }
91 if (!empty($name)) {
92 $_html_result = '<select name="' . $name . '"' . $extra . '>' . "\n" . $_html_result . '</select>' . "\n";
93 }
95 return $_html_result;
96 }
98 function smarty_function_html_options_optoutput($key, $value, $selected)
99 {
100 if (!is_array($value)) {
101 $_html_result = '<option value="' .
102 smarty_function_escape_special_chars($key) . '"';
103 if (in_array((string)$key, $selected))
104 $_html_result .= ' selected="selected"';
105 $_html_result .= '>' . smarty_function_escape_special_chars($value) . '</option>' . "\n";
106 } else {
107 $_html_result = smarty_function_html_options_optgroup($key, $value, $selected);
108 }
109 return $_html_result;
110 }
112 function smarty_function_html_options_optgroup($key, $values, $selected)
113 {
114 $optgroup_html = '<optgroup label="' . smarty_function_escape_special_chars($key) . '">' . "\n";
115 foreach ($values as $key => $value) {
116 $optgroup_html .= smarty_function_html_options_optoutput($key, $value, $selected);
117 }
118 $optgroup_html .= "</optgroup>\n";
119 return $optgroup_html;
120 }
122 ?>