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