]> git.sur5r.net Git - contagged/blob - smarty/plugins/function.html_select_time.php
969c13e62d46ee5a5cf7b5fd55ece6612ed9da5f
[contagged] / smarty / plugins / function.html_select_time.php
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     extract($params);
48
49     $time = smarty_make_timestamp($time);
50
51     $html_result = '';
52
53     if ($display_hours) {
54         $hours       = $use_24_hours ? range(0, 23) : range(1, 12);
55         $hour_fmt = $use_24_hours ? '%H' : '%I';
56         for ($i = 0, $for_max = count($hours); $i < $for_max; $i++)
57             $hours[$i] = sprintf('%02d', $hours[$i]);
58         $html_result .= '<select name=';
59         if (null !== $field_array) {
60             $html_result .= '"' . $field_array . '[' . $prefix . 'Hour]"';
61         } else {
62             $html_result .= '"' . $prefix . 'Hour"';
63         }
64         if (null !== $hour_extra){
65             $html_result .= ' ' . $hour_extra;
66         }
67         if (null !== $all_extra){
68             $html_result .= ' ' . $all_extra;
69         }
70         $html_result .= '>'."\n";
71         $html_result .= smarty_function_html_options(array('output'          => $hours,
72                                                            'values'          => $hours,
73                                                            'selected'      => strftime($hour_fmt, $time),
74                                                            'print_result' => false),
75                                                      $smarty);
76         $html_result .= "</select>\n";
77     }
78
79     if ($display_minutes) {
80         $all_minutes = range(0, 59);
81         for ($i = 0, $for_max = count($all_minutes); $i < $for_max; $i+= $minute_interval)
82             $minutes[] = sprintf('%02d', $all_minutes[$i]);
83         $selected = intval(floor(strftime('%M', $time) / $minute_interval) * $minute_interval);
84         $html_result .= '<select name=';
85         if (null !== $field_array) {
86             $html_result .= '"' . $field_array . '[' . $prefix . 'Minute]"';
87         } else {
88             $html_result .= '"' . $prefix . 'Minute"';
89         }
90         if (null !== $minute_extra){
91             $html_result .= ' ' . $minute_extra;
92         }
93         if (null !== $all_extra){
94             $html_result .= ' ' . $all_extra;
95         }
96         $html_result .= '>'."\n";
97         
98         $html_result .= smarty_function_html_options(array('output'          => $minutes,
99                                                            'values'          => $minutes,
100                                                            'selected'      => $selected,
101                                                            'print_result' => false),
102                                                      $smarty);
103         $html_result .= "</select>\n";
104     }
105
106     if ($display_seconds) {
107         $all_seconds = range(0, 59);
108         for ($i = 0, $for_max = count($all_seconds); $i < $for_max; $i+= $second_interval)
109             $seconds[] = sprintf('%02d', $all_seconds[$i]);
110         $selected = intval(floor(strftime('%S', $time) / $second_interval) * $second_interval);
111         $html_result .= '<select name=';
112         if (null !== $field_array) {
113             $html_result .= '"' . $field_array . '[' . $prefix . 'Second]"';
114         } else {
115             $html_result .= '"' . $prefix . 'Second"';
116         }
117         
118         if (null !== $second_extra){
119             $html_result .= ' ' . $second_extra;
120         }
121         if (null !== $all_extra){
122             $html_result .= ' ' . $all_extra;
123         }
124         $html_result .= '>'."\n";
125         
126         $html_result .= smarty_function_html_options(array('output'          => $seconds,
127                                                            'values'          => $seconds,
128                                                            'selected'      => $selected,
129                                                            'print_result' => false),
130                                                      $smarty);
131         $html_result .= "</select>\n";
132     }
133
134     if ($display_meridian && !$use_24_hours) {
135         $html_result .= '<select name=';
136         if (null !== $field_array) {
137             $html_result .= '"' . $field_array . '[' . $prefix . 'Meridian]"';
138         } else {
139             $html_result .= '"' . $prefix . 'Meridian"';
140         }
141         
142         if (null !== $meridian_extra){
143             $html_result .= ' ' . $meridian_extra;
144         }
145         if (null !== $all_extra){
146             $html_result .= ' ' . $all_extra;
147         }
148         $html_result .= '>'."\n";
149         
150         $html_result .= smarty_function_html_options(array('output'          => array('AM', 'PM'),
151                                                            'values'          => array('am', 'pm'),
152                                                            'selected'      => strtolower(strftime('%p', $time)),
153                                                            'print_result' => false),
154                                                      $smarty);
155         $html_result .= "</select>\n";
156     }
157
158     return $html_result;
159 }
160
161 /* vim: set expandtab: */
162
163 ?>