Mercurial > hg > rr-repo
comparison sites/all/modules/webform/components/time.inc @ 0:ff03f76ab3fe
initial version
author | danieleb <danielebarchiesi@me.com> |
---|---|
date | Wed, 21 Aug 2013 18:51:11 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:ff03f76ab3fe |
---|---|
1 <?php | |
2 | |
3 /** | |
4 * @file | |
5 * Webform module time component. | |
6 */ | |
7 | |
8 // Time depends on functions provided by date. | |
9 webform_component_include('date'); | |
10 | |
11 /** | |
12 * Implements _webform_defaults_component(). | |
13 */ | |
14 function _webform_defaults_time() { | |
15 return array( | |
16 'name' => '', | |
17 'form_key' => NULL, | |
18 'pid' => 0, | |
19 'weight' => 0, | |
20 'value' => '', | |
21 'mandatory' => 0, | |
22 'extra' => array( | |
23 'timezone' => 'user', | |
24 'hourformat' => '12-hour', | |
25 'minuteincrements' => 1, | |
26 'title_display' => 0, | |
27 'description' => '', | |
28 'private' => FALSE, | |
29 ), | |
30 ); | |
31 } | |
32 | |
33 /** | |
34 * Implements _webform_theme_component(). | |
35 */ | |
36 function _webform_theme_time() { | |
37 return array( | |
38 'webform_time' => array( | |
39 'render element' => 'element', | |
40 'file' => 'components/time.inc', | |
41 ), | |
42 'webform_display_time' => array( | |
43 'render element' => 'element', | |
44 'file' => 'components/time.inc', | |
45 ), | |
46 ); | |
47 } | |
48 | |
49 /** | |
50 * Implements _webform_edit_component(). | |
51 */ | |
52 function _webform_edit_time($component) { | |
53 $form = array(); | |
54 $form['value'] = array( | |
55 '#type' => 'textfield', | |
56 '#title' => t('Default value'), | |
57 '#default_value' => $component['value'], | |
58 '#description' => t('The default value of the field.') . '<br />' . t('Accepts a time in any <a href="http://www.gnu.org/software/tar/manual/html_chapter/Date-input-formats.html">GNU Date Input Format</a>. Strings such as now, +2 hours, and 10:30pm are all valid.'), | |
59 '#size' => 60, | |
60 '#maxlength' => 127, | |
61 '#weight' => 0, | |
62 ); | |
63 $form['extra']['timezone'] = array( | |
64 '#type' => 'radios', | |
65 '#title' => t('Default value timezone'), | |
66 '#default_value' => $component['extra']['timezone'], | |
67 '#description' => t('If using relative dates for a default value (e.g. "now") base the current time on this timezone.'), | |
68 '#options' => array('user' => t('User timezone'), 'site' => t('Website timezone')), | |
69 '#weight' => 2, | |
70 '#access' => variable_get('configurable_timezones', 1), | |
71 ); | |
72 $form['display']['hourformat'] = array( | |
73 '#type' => 'radios', | |
74 '#title' => t('Time format'), | |
75 '#default_value' => $component['extra']['hourformat'], | |
76 '#options' => array('12-hour' => t('12-hour (am/pm)'), '24-hour' => t('24-hour')), | |
77 '#weight' => 2, | |
78 '#parents' => array('extra', 'hourformat'), | |
79 ); | |
80 $form['display']['minuteincrements'] = array( | |
81 '#type' => 'select', | |
82 '#title' => t('Minute increments'), | |
83 '#default_value' => $component['extra']['minuteincrements'], | |
84 '#options' => array( | |
85 1 => t('1 minute'), | |
86 5 => t('5 minute'), | |
87 10 => t('10 minute'), | |
88 15 => t('15 minute'), | |
89 30 => t('30 minute'), | |
90 ), | |
91 '#weight' => 3, | |
92 '#parents' => array('extra', 'minuteincrements'), | |
93 ); | |
94 return $form; | |
95 } | |
96 | |
97 /** | |
98 * Implements _webform_render_component(). | |
99 */ | |
100 function _webform_render_time($component, $value = NULL, $filter = TRUE) { | |
101 $node = isset($component['nid']) ? node_load($component['nid']) : NULL; | |
102 | |
103 $element = array( | |
104 '#type' => 'webform_time', | |
105 '#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'], | |
106 '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before', | |
107 '#required' => $component['mandatory'], | |
108 '#weight' => $component['weight'], | |
109 '#description' => $filter ? _webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'], | |
110 '#element_validate' => array('webform_validate_time'), | |
111 '#hourformat' => $component['extra']['hourformat'], | |
112 '#minuteincrements' => $component['extra']['minuteincrements'], | |
113 '#default_value' => $filter ? _webform_filter_values($component['value'], $node, NULL, NULL, FALSE) : $component['value'], | |
114 '#timezone' => $component['extra']['timezone'], | |
115 '#process' => array('webform_expand_time'), | |
116 '#theme' => 'webform_time', | |
117 '#theme_wrappers' => array('webform_element'), | |
118 '#translatable' => array('title', 'description'), | |
119 ); | |
120 | |
121 // Set the value from Webform if available. | |
122 if (!empty($value[0])) { | |
123 $element['#default_value'] = $value[0]; | |
124 } | |
125 | |
126 return $element; | |
127 } | |
128 | |
129 /** | |
130 * Form API #process function for Webform time fields. | |
131 */ | |
132 function webform_expand_time($element) { | |
133 // Expand the default value from a string into an array. | |
134 if (!empty($element['#default_value'])) { | |
135 // Adjust the time based on the user or site timezone. | |
136 if (variable_get('configurable_timezones', 1) && $element['#timezone'] == 'user') { | |
137 $timezone_name = isset($GLOBALS['user']->timezone) ? $GLOBALS['user']->timezone : 'UTC'; | |
138 } | |
139 else { | |
140 $timezone_name = variable_get('date_default_timezone', 'UTC'); | |
141 } | |
142 | |
143 $default_values = webform_date_array(webform_strtodate('c', $element['#default_value'], $timezone_name), 'time'); | |
144 } | |
145 else { | |
146 $default_values = array( | |
147 'hour' => '', | |
148 'minute' => '', | |
149 'second' => '', | |
150 ); | |
151 } | |
152 | |
153 $first_hour = 0; | |
154 $last_hour = 23; | |
155 if ($element['#hourformat'] == '12-hour') { | |
156 $first_hour = 1; | |
157 $last_hour = 12; | |
158 $default_values = webform_time_convert($default_values, '12-hour'); | |
159 $default_values['ampm'] = $default_values['ampm'] ? $default_values['ampm'] : 'am'; | |
160 } | |
161 | |
162 // Generate the choices for drop-down selects. | |
163 $hours[''] = t('hour'); | |
164 $minutes[''] = t('minute'); | |
165 for ($i = $first_hour; $i <= $last_hour; $i++) { | |
166 $hours[$i] = $i; | |
167 } | |
168 for ($i = 0; $i <= 59; $i += $element['#minuteincrements']) { | |
169 $minutes[$i] = $i < 10 ? "0$i" : $i; | |
170 } | |
171 $ampms = array('am' => t('am'), 'pm' => t('pm')); | |
172 | |
173 // Adjust the default for minutes if needed, rounding up to the closest value. | |
174 if (!isset($minutes[$default_values['minute']])) { | |
175 foreach ($minutes as $minute => $padded_minute) { | |
176 if ($minute > $default_values['minute']) { | |
177 $default_values['minute'] = $minute; | |
178 break; | |
179 } | |
180 } | |
181 } | |
182 | |
183 // If the above loop didn't set a value, it's because rounding up would go to | |
184 // the next hour. This gets quite a bit more complicated, since we need to | |
185 // deal with looping around on hours, as well as flipping am/pm. | |
186 if (!isset($minutes[$default_values['minute']])) { | |
187 $default_values['minute'] = 0; | |
188 $default_values['hour']++; | |
189 // If the hour rolls over also, set hour to the first hour in the list. | |
190 if (!isset($hours[$default_values['hour']])) { | |
191 $default_values['hour'] = $element['#hourformat'] == '12-hour' ? 1 : 0; | |
192 } | |
193 // If the hour has been incremented to 12:00 in 12-hour format, flip am/pm. | |
194 // Note that technically midnight and noon are neither am or pm, but common | |
195 // convention (and US standard) is to represent 12:00am as midnight. | |
196 // See http://en.wikipedia.org/wiki/Midnight#Start_and_end_of_day. | |
197 if ($element['#hourformat'] == '12-hour' && $default_values['hour'] == 12) { | |
198 $default_values['ampm'] = $default_values['ampm'] == 'am' ? 'pm' : 'am'; | |
199 } | |
200 } | |
201 | |
202 $element['hour'] = array( | |
203 '#prefix' => '', | |
204 '#type' => 'select', | |
205 '#default_value' => $default_values['hour'], | |
206 '#options' => $hours, | |
207 ); | |
208 $element['minute'] = array( | |
209 '#prefix' => ':', | |
210 '#type' => 'select', | |
211 '#default_value' => $default_values['minute'], | |
212 '#options' => $minutes, | |
213 ); | |
214 if (strcmp($element['#hourformat'], '12-hour') == 0) { | |
215 $element['ampm'] = array( | |
216 '#type' => 'radios', | |
217 '#default_value' => $default_values['ampm'], | |
218 '#options' => $ampms, | |
219 ); | |
220 } | |
221 | |
222 // Set the overall default value. | |
223 if ($default_values['hour'] !== '') { | |
224 $element['#default_value'] = webform_date_string($default_values); | |
225 } | |
226 | |
227 return $element; | |
228 } | |
229 | |
230 /** | |
231 * Theme a webform time element. | |
232 */ | |
233 function theme_webform_time($variables) { | |
234 $element = $variables['element']; | |
235 | |
236 $element['hour']['#attributes']['class'] = array('hour'); | |
237 $element['minute']['#attributes']['class'] = array('minute'); | |
238 | |
239 // Add error classes to all items within the element. | |
240 if (form_get_error($element)) { | |
241 $element['hour']['#attributes']['class'][] = 'error'; | |
242 $element['minute']['#attributes']['class'][] = 'error'; | |
243 } | |
244 | |
245 $output = '<div class="webform-container-inline">' . drupal_render($element['hour']) . drupal_render($element['minute']) . drupal_render($element['ampm']) . '</div>'; | |
246 | |
247 return $output; | |
248 } | |
249 | |
250 function webform_validate_time($element, $form_state) { | |
251 $form_key = $element['#webform_component']['form_key']; | |
252 $name = $element['#webform_component']['name']; | |
253 | |
254 // Check if the user filled the required fields. | |
255 foreach ($element['#hourformat'] == '12-hour' ? array('hour', 'minute', 'ampm') : array('hour', 'minute') as $field_type) { | |
256 if ($element[$field_type]['#value'] === '' && $element['#required']) { | |
257 form_error($element, t('%field field is required.', array('%field' => $name))); | |
258 return; | |
259 } | |
260 } | |
261 | |
262 // Check for a valid time. | |
263 if ($element['hour']['#value'] !== '' || $element['minute']['#value'] !== '') { | |
264 if (!is_numeric($element['hour']['#value']) || !is_numeric($element['minute']['#value']) || (isset($element['ampm']) && $element['ampm']['#value'] === '')) { | |
265 form_error($element, t('Entered %name is not a valid time.', array('%name' => $name))); | |
266 return; | |
267 } | |
268 } | |
269 } | |
270 | |
271 /** | |
272 * Implements _webform_submit_component(). | |
273 */ | |
274 function _webform_submit_time($component, $value) { | |
275 // Convert to 24-hour time before string conversion. | |
276 if ($component['extra']['hourformat'] == '12-hour') { | |
277 $value = webform_time_convert($value, '24-hour'); | |
278 } | |
279 | |
280 // Convert the value into a ISO 8601 string. | |
281 return $value['hour'] !== '' ? webform_date_string($value, 'time') : ''; | |
282 } | |
283 | |
284 /** | |
285 * Implements _webform_display_component(). | |
286 */ | |
287 function _webform_display_time($component, $value, $format = 'html') { | |
288 $value = webform_date_array(isset($value[0]) ? $value[0] : '', 'time'); | |
289 if ($component['extra']['hourformat'] == '12-hour') { | |
290 $value = webform_time_convert($value, '12-hour'); | |
291 } | |
292 | |
293 return array( | |
294 '#title' => $component['name'], | |
295 '#weight' => $component['weight'], | |
296 '#theme' => 'webform_display_time', | |
297 '#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'), | |
298 '#format' => $format, | |
299 '#hourformat' => $component['extra']['hourformat'], | |
300 '#value' => $value, | |
301 '#translatable' => array('title'), | |
302 ); | |
303 } | |
304 | |
305 /** | |
306 * Format the output of data for this component. | |
307 */ | |
308 function theme_webform_display_time($variables) { | |
309 $element = $variables['element']; | |
310 $output = ' '; | |
311 if (isset($element['#value']['hour']) && $element['#value']['hour'] !== '' && isset($element['#value']['minute']) && $element['#value']['minute'] !== '') { | |
312 if ($element['#hourformat'] == '24-hour') { | |
313 $output = sprintf('%02d', $element['#value']['hour']) . ':' . sprintf('%02d', $element['#value']['minute']); | |
314 } | |
315 else { | |
316 $output = $element['#value']['hour'] . ':' . sprintf('%02d', $element['#value']['minute']) . ' ' . $element['#value']['ampm']; | |
317 } | |
318 } | |
319 return $output; | |
320 } | |
321 | |
322 /** | |
323 * Implements _webform_analysis_component(). | |
324 */ | |
325 function _webform_analysis_time($component, $sids = array()) { | |
326 $query = db_select('webform_submitted_data', 'wsd', array('fetch' => PDO::FETCH_ASSOC)) | |
327 ->fields('wsd', array('no', 'data')) | |
328 ->condition('nid', $component['nid']) | |
329 ->condition('cid', $component['cid']) | |
330 ->orderBy('sid'); | |
331 | |
332 if (count($sids)) { | |
333 $query->condition('sid', $sids, 'IN'); | |
334 } | |
335 | |
336 $result = $query->execute(); | |
337 | |
338 $times = array(); | |
339 $submissions = 0; | |
340 foreach ($result as $row) { | |
341 $submissions++; | |
342 if ($row['data']) { | |
343 $times[] = webform_date_array($row['data']); | |
344 } | |
345 } | |
346 | |
347 // Display stats. | |
348 $nonblanks = count($times); | |
349 $rows[0] = array(t('Left Blank'), ($submissions - $nonblanks)); | |
350 $rows[1] = array(t('User entered value'), $nonblanks); | |
351 return $rows; | |
352 } | |
353 | |
354 /** | |
355 * Implements _webform_table_component(). | |
356 */ | |
357 function _webform_table_time($component, $value) { | |
358 if ($value[0]) { | |
359 $time = webform_date_array($value[0], 'time'); | |
360 if ($component['extra']['hourformat'] == '24-hour') { | |
361 return sprintf('%02d', $time['hour']) . ':' . sprintf('%02d', $time['minute']); | |
362 } | |
363 else { | |
364 $time = webform_time_convert($time, '12-hour'); | |
365 return $time['hour'] . ':' . sprintf('%02d', $time['minute']) . ' ' . $time['ampm']; | |
366 } | |
367 } | |
368 else { | |
369 return ''; | |
370 } | |
371 } | |
372 | |
373 /** | |
374 * Implements _webform_csv_headers_component(). | |
375 */ | |
376 function _webform_csv_headers_time($component, $export_options) { | |
377 $header = array(); | |
378 $header[0] = ''; | |
379 $header[1] = ''; | |
380 $header[2] = $component['name']; | |
381 return $header; | |
382 } | |
383 | |
384 /** | |
385 * Implements _webform_csv_data_component(). | |
386 */ | |
387 function _webform_csv_data_time($component, $export_options, $value) { | |
388 if ($value[0]) { | |
389 $time = webform_date_array($value[0], 'time'); | |
390 if ($component['extra']['hourformat'] == '24-hour') { | |
391 return sprintf('%02d', $time['hour']) . ':' . sprintf('%02d', $time['minute']); | |
392 } | |
393 else { | |
394 $time = webform_time_convert($time, '12-hour'); | |
395 return $time['hour'] . ':' . sprintf('%02d', $time['minute']) . ' ' . $time['ampm']; | |
396 } | |
397 } | |
398 else { | |
399 return ''; | |
400 } | |
401 } | |
402 | |
403 /** | |
404 * Convert a time between a 24-hour and a 12-hour value. | |
405 * | |
406 * @param $array | |
407 * An array of hour, minute, second, and optionally ampm. | |
408 * @param $format | |
409 * Either 12-hour or 24-hour. | |
410 * @return | |
411 * An array with hour, minute, second, and ampm (if using "12-hour"). | |
412 */ | |
413 function webform_time_convert($array, $format) { | |
414 if ($array['hour'] !== '') { | |
415 if ($format == '12-hour') { | |
416 $array['ampm'] = ($array['hour'] >= 12 && $array['hour'] < 24) ? 'pm' : 'am'; | |
417 $array['hour'] = ($array['hour'] > 12 || $array['hour'] == 0) ? abs($array['hour'] - 12) : (int) $array['hour']; | |
418 } | |
419 elseif ($format == '24-hour' && isset($array['ampm'])) { | |
420 $array['hour'] = ($array['hour'] < 12 && $array['ampm'] == 'pm') ? $array['hour'] + 12 : (int) $array['hour']; | |
421 $array['hour'] = ($array['hour'] == 12 && $array['ampm'] == 'am') ? 0 : $array['hour']; | |
422 } | |
423 } | |
424 | |
425 if ($format == '12-hour' && !isset($array['ampm'])) { | |
426 $array['ampm'] = ''; | |
427 } | |
428 elseif ($format == '24-hour' && isset($array['ampm'])) { | |
429 unset($array['ampm']); | |
430 } | |
431 | |
432 return $array; | |
433 } |