00001 <?php
00029 function smarty_function_html_options($params, &$smarty)
00030 {
00031 require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
00032
00033 $name = null;
00034 $values = null;
00035 $options = null;
00036 $selected = array();
00037 $output = null;
00038
00039 $extra = '';
00040
00041 foreach($params as $_key => $_val) {
00042 switch($_key) {
00043 case 'name':
00044 $$_key = (string)$_val;
00045 break;
00046
00047 case 'options':
00048 $$_key = (array)$_val;
00049 break;
00050
00051 case 'values':
00052 case 'output':
00053 $$_key = array_values((array)$_val);
00054 break;
00055
00056 case 'selected':
00057 $$_key = array_map('strval', array_values((array)$_val));
00058 break;
00059
00060 default:
00061 if(!is_array($_val)) {
00062 $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
00063 } else {
00064 $smarty->trigger_error("html_options: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
00065 }
00066 break;
00067 }
00068 }
00069
00070 if (!isset($options) && !isset($values))
00071 return '';
00072
00073 $_html_result = '';
00074
00075 if (isset($options)) {
00076
00077 foreach ($options as $_key=>$_val)
00078 $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected);
00079
00080 } else {
00081
00082 foreach ($values as $_i=>$_key) {
00083 $_val = isset($output[$_i]) ? $output[$_i] : '';
00084 $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected);
00085 }
00086
00087 }
00088
00089 if(!empty($name)) {
00090 $_html_result = '<select name="' . $name . '"' . $extra . '>' . "\n" . $_html_result . '</select>' . "\n";
00091 }
00092
00093 return $_html_result;
00094
00095 }
00096
00097 function smarty_function_html_options_optoutput($key, $value, $selected) {
00098 if(!is_array($value)) {
00099 $_html_result = '<option label="' . smarty_function_escape_special_chars($value) . '" value="' .
00100 smarty_function_escape_special_chars($key) . '"';
00101 if (in_array((string)$key, $selected))
00102 $_html_result .= ' selected="selected"';
00103 $_html_result .= '>' . smarty_function_escape_special_chars($value) . '</option>' . "\n";
00104 } else {
00105 $_html_result = smarty_function_html_options_optgroup($key, $value, $selected);
00106 }
00107 return $_html_result;
00108 }
00109
00110 function smarty_function_html_options_optgroup($key, $values, $selected) {
00111 $optgroup_html = '<optgroup label="' . smarty_function_escape_special_chars($key) . '">' . "\n";
00112 foreach ($values as $key => $value) {
00113 $optgroup_html .= smarty_function_html_options_optoutput($key, $value, $selected);
00114 }
00115 $optgroup_html .= "</optgroup>\n";
00116 return $optgroup_html;
00117 }
00118
00119
00120
00121 ?>