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