[795] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * Smarty plugin |
---|
| 4 | * @package Smarty |
---|
| 5 | * @subpackage plugins |
---|
| 6 | */ |
---|
| 7 | |
---|
| 8 | |
---|
| 9 | /** |
---|
| 10 | * Smarty {html_options} function plugin |
---|
| 11 | * |
---|
| 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 | * @author Monte Ohrt <monte at ohrt dot com> |
---|
| 25 | * @param array |
---|
| 26 | * @param Smarty |
---|
| 27 | * @return string |
---|
| 28 | * @uses smarty_function_escape_special_chars() |
---|
| 29 | */ |
---|
| 30 | function smarty_function_html_options($params, &$smarty) |
---|
| 31 | { |
---|
| 32 | require_once $smarty->_get_plugin_filepath('shared','escape_special_chars'); |
---|
| 33 | |
---|
| 34 | $name = null; |
---|
| 35 | $values = null; |
---|
| 36 | $options = null; |
---|
| 37 | $selected = array(); |
---|
| 38 | $output = null; |
---|
| 39 | |
---|
| 40 | $extra = ''; |
---|
| 41 | |
---|
| 42 | foreach($params as $_key => $_val) { |
---|
| 43 | switch($_key) { |
---|
| 44 | case 'name': |
---|
| 45 | $$_key = (string)$_val; |
---|
| 46 | break; |
---|
| 47 | |
---|
| 48 | case 'options': |
---|
| 49 | $$_key = (array)$_val; |
---|
| 50 | break; |
---|
| 51 | |
---|
| 52 | case 'values': |
---|
| 53 | case 'output': |
---|
| 54 | $$_key = array_values((array)$_val); |
---|
| 55 | break; |
---|
| 56 | |
---|
| 57 | case 'selected': |
---|
| 58 | $$_key = array_map('strval', array_values((array)$_val)); |
---|
| 59 | break; |
---|
| 60 | |
---|
| 61 | default: |
---|
| 62 | if(!is_array($_val)) { |
---|
| 63 | $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"'; |
---|
| 64 | } else { |
---|
| 65 | $smarty->trigger_error("html_options: extra attribute '$_key' cannot be an array", E_USER_NOTICE); |
---|
| 66 | } |
---|
| 67 | break; |
---|
| 68 | } |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | if (!isset($options) && !isset($values)) |
---|
| 72 | return ''; /* raise error here? */ |
---|
| 73 | |
---|
| 74 | $_html_result = ''; |
---|
| 75 | |
---|
| 76 | if (isset($options)) { |
---|
| 77 | |
---|
| 78 | foreach ($options as $_key=>$_val) |
---|
| 79 | $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected); |
---|
| 80 | |
---|
| 81 | } else { |
---|
| 82 | |
---|
| 83 | foreach ($values as $_i=>$_key) { |
---|
| 84 | $_val = isset($output[$_i]) ? $output[$_i] : ''; |
---|
| 85 | $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | if(!empty($name)) { |
---|
| 91 | $_html_result = '<select name="' . $name . '"' . $extra . '>' . "\n" . $_html_result . '</select>' . "\n"; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | return $_html_result; |
---|
| 95 | |
---|
| 96 | } |
---|
| 97 | |
---|
[3167] | 98 | function smarty_function_html_boxoptions($params, &$smarty) |
---|
| 99 | { |
---|
| 100 | require_once $smarty->_get_plugin_filepath('shared','escape_special_chars'); |
---|
| 101 | |
---|
| 102 | $name = null; |
---|
| 103 | $values = null; |
---|
| 104 | $options = null; |
---|
| 105 | $selected = array(); |
---|
| 106 | $output = null; |
---|
| 107 | |
---|
| 108 | $extra = ''; |
---|
| 109 | |
---|
| 110 | foreach($params as $_key => $_val) { |
---|
| 111 | switch($_key) { |
---|
| 112 | case 'name': |
---|
| 113 | $$_key = (string)$_val; |
---|
| 114 | break; |
---|
| 115 | |
---|
| 116 | case 'options': |
---|
| 117 | $$_key = (array)$_val; |
---|
| 118 | break; |
---|
| 119 | |
---|
| 120 | case 'values': |
---|
| 121 | case 'output': |
---|
| 122 | $$_key = array_values((array)$_val); |
---|
| 123 | break; |
---|
| 124 | |
---|
| 125 | case 'selected': |
---|
| 126 | $$_key = array_map('strval', array_values((array)$_val)); |
---|
| 127 | break; |
---|
| 128 | |
---|
| 129 | default: |
---|
| 130 | if(!is_array($_val)) { |
---|
| 131 | $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"'; |
---|
| 132 | } else { |
---|
| 133 | $smarty->trigger_error("html_options: extra attribute '$_key' cannot be an array", E_USER_NOTICE); |
---|
| 134 | } |
---|
| 135 | break; |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | if (!isset($options) && !isset($values)) |
---|
| 140 | return ''; /* raise error here? */ |
---|
| 141 | |
---|
| 142 | $_html_result = ''; |
---|
| 143 | |
---|
| 144 | if (isset($options)) { |
---|
| 145 | |
---|
| 146 | foreach ($options as $_key=>$_val) |
---|
| 147 | $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected); |
---|
| 148 | |
---|
| 149 | } else { |
---|
| 150 | |
---|
| 151 | foreach ($values as $_i=>$_key) { |
---|
| 152 | $_val = isset($output[$_i]) ? $output[$_i] : ''; |
---|
| 153 | $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | if(!empty($name)) { |
---|
| 159 | $_html_result = '<select name="' . $name . '"' . $extra . ' style="height: 100px;" size="5">' . "\n" . $_html_result . '</select>' . "\n"; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | return $_html_result; |
---|
| 163 | |
---|
| 164 | } |
---|
[795] | 165 | function smarty_function_html_options_optoutput($key, $value, $selected) { |
---|
| 166 | if(!is_array($value)) { |
---|
| 167 | $_html_result = '<option label="' . smarty_function_escape_special_chars($value) . '" value="' . |
---|
| 168 | smarty_function_escape_special_chars($key) . '"'; |
---|
| 169 | if (in_array((string)$key, $selected)) |
---|
| 170 | $_html_result .= ' selected="selected"'; |
---|
| 171 | $_html_result .= '>' . smarty_function_escape_special_chars($value) . '</option>' . "\n"; |
---|
| 172 | } else { |
---|
| 173 | $_html_result = smarty_function_html_options_optgroup($key, $value, $selected); |
---|
| 174 | } |
---|
| 175 | return $_html_result; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | function smarty_function_html_options_optgroup($key, $values, $selected) { |
---|
| 179 | $optgroup_html = '<optgroup label="' . smarty_function_escape_special_chars($key) . '">' . "\n"; |
---|
| 180 | foreach ($values as $key => $value) { |
---|
| 181 | $optgroup_html .= smarty_function_html_options_optoutput($key, $value, $selected); |
---|
| 182 | } |
---|
| 183 | $optgroup_html .= "</optgroup>\n"; |
---|
| 184 | return $optgroup_html; |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | /* vim: set expandtab: */ |
---|
| 188 | |
---|
| 189 | ?> |
---|