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