MDL-9123:
[moodle-linuxchix.git] / lib / smarty / plugins / function.html_options.php
blobae864f7327ca9c9f80d055ddb96ca2d09ef5cfaa
1 <?php
2 /**
3 * Smarty plugin
4 * @package Smarty
5 * @subpackage plugins
6 */
9 /**
10 * Smarty {html_options} function plugin
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()
29 function smarty_function_html_options($params, &$smarty)
31 require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
33 $name = null;
34 $values = null;
35 $options = null;
36 $selected = array();
37 $output = null;
39 $extra = '';
41 foreach($params as $_key => $_val) {
42 switch($_key) {
43 case 'name':
44 $$_key = (string)$_val;
45 break;
47 case 'options':
48 $$_key = (array)$_val;
49 break;
51 case 'values':
52 case 'output':
53 $$_key = array_values((array)$_val);
54 break;
56 case 'selected':
57 $$_key = array_map('strval', array_values((array)$_val));
58 break;
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);
66 break;
70 if (!isset($options) && !isset($values))
71 return ''; /* raise error here? */
73 $_html_result = '';
75 if (isset($options)) {
77 foreach ($options as $_key=>$_val)
78 $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected);
80 } else {
82 foreach ($values as $_i=>$_key) {
83 $_val = isset($output[$_i]) ? $output[$_i] : '';
84 $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected);
89 if(!empty($name)) {
90 $_html_result = '<select name="' . $name . '"' . $extra . '>' . "\n" . $_html_result . '</select>' . "\n";
93 return $_html_result;
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);
107 return $_html_result;
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);
115 $optgroup_html .= "</optgroup>\n";
116 return $optgroup_html;
119 /* vim: set expandtab: */