Code type module improvements:
[openemr.git] / library / plugins / function.html_options.php
blobcf06d0c14a4a72523a53252ead366800c3af7cb3
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 (is_array($options)) {
77 foreach ($options as $_key=>$_val) {
78 if (!empty($_val)) {
79 $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected);
83 } else {
85 foreach ((array)$values as $_i=>$_key) {
86 $_val = isset($output[$_i]) ? $output[$_i] : '';
87 $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected);
92 if(!empty($name)) {
93 $_html_result = '<select name="' . $name . '"' . $extra . '>' . "\n" . $_html_result . '</select>' . "\n";
96 return $_html_result;
100 function smarty_function_html_options_optoutput($key, $value, $selected) {
101 if(!is_array($value)) {
102 $_html_result = '<option label="' . smarty_function_escape_special_chars($value) . '" value="' .
103 smarty_function_escape_special_chars($key) . '"';
104 if (in_array((string)$key, $selected))
105 $_html_result .= ' selected="selected"';
106 $_html_result .= '>' . smarty_function_escape_special_chars($value) . '</option>' . "\n";
107 } else {
108 $_html_result = smarty_function_html_options_optgroup($key, $value, $selected);
110 return $_html_result;
113 function smarty_function_html_options_optgroup($key, $values, $selected) {
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);
118 $optgroup_html .= "</optgroup>\n";
119 return $optgroup_html;
122 /* vim: set expandtab: */