| 1 | <?php |
| 2 | /** |
| 3 | * Smarty plugin |
| 4 | * @package Smarty |
| 5 | * @subpackage plugins |
| 6 | */ |
| 7 | |
| 8 | |
| 9 | /** |
| 10 | * Smarty {html_select_time} function plugin |
| 11 | * |
| 12 | * Type: function<br> |
| 13 | * Name: html_select_time<br> |
| 14 | * Purpose: Prints the dropdowns for time selection |
| 15 | * @link http://smarty.php.net/manual/en/language.function.html.select.time.php {html_select_time} |
| 16 | * (Smarty online manual) |
| 17 | * @author Roberto Berto <roberto@berto.net> |
| 18 | * @credits Monte Ohrt <monte AT ohrt DOT com> |
| 19 | * @param array |
| 20 | * @param Smarty |
| 21 | * @return string |
| 22 | * @uses smarty_make_timestamp() |
| 23 | */ |
| 24 | function smarty_function_html_select_time($params, &$smarty) |
| 25 | { |
| 26 | require_once $smarty->_get_plugin_filepath('shared','make_timestamp'); |
| 27 | require_once $smarty->_get_plugin_filepath('function','html_options'); |
| 28 | /* Default values. */ |
| 29 | $prefix = "Time_"; |
| 30 | $time = time(); |
| 31 | $display_hours = true; |
| 32 | $display_minutes = true; |
| 33 | $display_seconds = true; |
| 34 | $display_meridian = true; |
| 35 | $use_24_hours = true; |
| 36 | $minute_interval = 1; |
| 37 | $second_interval = 1; |
| 38 | /* Should the select boxes be part of an array when returned from PHP? |
| 39 | e.g. setting it to "birthday", would create "birthday[Hour]", |
| 40 | "birthday[Minute]", "birthday[Seconds]" & "birthday[Meridian]". |
| 41 | Can be combined with prefix. */ |
| 42 | $field_array = null; |
| 43 | $all_extra = null; |
| 44 | $hour_extra = null; |
| 45 | $minute_extra = null; |
| 46 | $second_extra = null; |
| 47 | $meridian_extra = null; |
| 48 | |
| 49 | foreach ($params as $_key=>$_value) { |
| 50 | switch ($_key) { |
| 51 | case 'prefix': |
| 52 | case 'time': |
| 53 | case 'field_array': |
| 54 | case 'all_extra': |
| 55 | case 'hour_extra': |
| 56 | case 'minute_extra': |
| 57 | case 'second_extra': |
| 58 | case 'meridian_extra': |
| 59 | $$_key = (string)$_value; |
| 60 | break; |
| 61 | |
| 62 | case 'display_hours': |
| 63 | case 'display_minutes': |
| 64 | case 'display_seconds': |
| 65 | case 'display_meridian': |
| 66 | case 'use_24_hours': |
| 67 | $$_key = (bool)$_value; |
| 68 | break; |
| 69 | |
| 70 | case 'minute_interval': |
| 71 | case 'second_interval': |
| 72 | $$_key = (int)$_value; |
| 73 | break; |
| 74 | |
| 75 | default: |
| 76 | $smarty->trigger_error("[html_select_time] unknown parameter $_key", E_USER_WARNING); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | $time = smarty_make_timestamp($time); |
| 81 | |
| 82 | $html_result = ''; |
| 83 | |
| 84 | if ($display_hours) { |
| 85 | $hours = $use_24_hours ? range(0, 23) : range(1, 12); |
| 86 | $hour_fmt = $use_24_hours ? '%H' : '%I'; |
| 87 | for ($i = 0, $for_max = count($hours); $i < $for_max; $i++) |
| 88 | $hours[$i] = sprintf('%02d', $hours[$i]); |
| 89 | $html_result .= '<select name='; |
| 90 | if (null !== $field_array) { |
| 91 | $html_result .= '"' . $field_array . '[' . $prefix . 'Hour]"'; |
| 92 | } else { |
| 93 | $html_result .= '"' . $prefix . 'Hour"'; |
| 94 | } |
| 95 | if (null !== $hour_extra){ |
| 96 | $html_result .= ' ' . $hour_extra; |
| 97 | } |
| 98 | if (null !== $all_extra){ |
| 99 | $html_result .= ' ' . $all_extra; |
| 100 | } |
| 101 | $html_result .= '>'."\n"; |
| 102 | $html_result .= smarty_function_html_options(array('output' => $hours, |
| 103 | 'values' => $hours, |
| 104 | 'selected' => strftime($hour_fmt, $time), |
| 105 | 'print_result' => false), |
| 106 | $smarty); |
| 107 | $html_result .= "</select>\n"; |
| 108 | } |
| 109 | |
| 110 | if ($display_minutes) { |
| 111 | $all_minutes = range(0, 59); |
| 112 | for ($i = 0, $for_max = count($all_minutes); $i < $for_max; $i+= $minute_interval) |
| 113 | $minutes[] = sprintf('%02d', $all_minutes[$i]); |
| 114 | $selected = intval(floor(strftime('%M', $time) / $minute_interval) * $minute_interval); |
| 115 | $html_result .= '<select name='; |
| 116 | if (null !== $field_array) { |
| 117 | $html_result .= '"' . $field_array . '[' . $prefix . 'Minute]"'; |
| 118 | } else { |
| 119 | $html_result .= '"' . $prefix . 'Minute"'; |
| 120 | } |
| 121 | if (null !== $minute_extra){ |
| 122 | $html_result .= ' ' . $minute_extra; |
| 123 | } |
| 124 | if (null !== $all_extra){ |
| 125 | $html_result .= ' ' . $all_extra; |
| 126 | } |
| 127 | $html_result .= '>'."\n"; |
| 128 | |
| 129 | $html_result .= smarty_function_html_options(array('output' => $minutes, |
| 130 | 'values' => $minutes, |
| 131 | 'selected' => $selected, |
| 132 | 'print_result' => false), |
| 133 | $smarty); |
| 134 | $html_result .= "</select>\n"; |
| 135 | } |
| 136 | |
| 137 | if ($display_seconds) { |
| 138 | $all_seconds = range(0, 59); |
| 139 | for ($i = 0, $for_max = count($all_seconds); $i < $for_max; $i+= $second_interval) |
| 140 | $seconds[] = sprintf('%02d', $all_seconds[$i]); |
| 141 | $selected = intval(floor(strftime('%S', $time) / $second_interval) * $second_interval); |
| 142 | $html_result .= '<select name='; |
| 143 | if (null !== $field_array) { |
| 144 | $html_result .= '"' . $field_array . '[' . $prefix . 'Second]"'; |
| 145 | } else { |
| 146 | $html_result .= '"' . $prefix . 'Second"'; |
| 147 | } |
| 148 | |
| 149 | if (null !== $second_extra){ |
| 150 | $html_result .= ' ' . $second_extra; |
| 151 | } |
| 152 | if (null !== $all_extra){ |
| 153 | $html_result .= ' ' . $all_extra; |
| 154 | } |
| 155 | $html_result .= '>'."\n"; |
| 156 | |
| 157 | $html_result .= smarty_function_html_options(array('output' => $seconds, |
| 158 | 'values' => $seconds, |
| 159 | 'selected' => $selected, |
| 160 | 'print_result' => false), |
| 161 | $smarty); |
| 162 | $html_result .= "</select>\n"; |
| 163 | } |
| 164 | |
| 165 | if ($display_meridian && !$use_24_hours) { |
| 166 | $html_result .= '<select name='; |
| 167 | if (null !== $field_array) { |
| 168 | $html_result .= '"' . $field_array . '[' . $prefix . 'Meridian]"'; |
| 169 | } else { |
| 170 | $html_result .= '"' . $prefix . 'Meridian"'; |
| 171 | } |
| 172 | |
| 173 | if (null !== $meridian_extra){ |
| 174 | $html_result .= ' ' . $meridian_extra; |
| 175 | } |
| 176 | if (null !== $all_extra){ |
| 177 | $html_result .= ' ' . $all_extra; |
| 178 | } |
| 179 | $html_result .= '>'."\n"; |
| 180 | |
| 181 | $html_result .= smarty_function_html_options(array('output' => array('AM', 'PM'), |
| 182 | 'values' => array('am', 'pm'), |
| 183 | 'selected' => strtolower(strftime('%p', $time)), |
| 184 | 'print_result' => false), |
| 185 | $smarty); |
| 186 | $html_result .= "</select>\n"; |
| 187 | } |
| 188 | |
| 189 | return $html_result; |
| 190 | } |
| 191 | |
| 192 | /* vim: set expandtab: */ |
| 193 | |
| 194 | ?> |