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