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