diff --git a/components/time.inc b/components/time.inc index 1194048..b03f609 100644 --- a/components/time.inc +++ b/components/time.inc @@ -22,6 +22,7 @@ function _webform_defaults_time() { 'extra' => array( 'timezone' => 'user', 'hourformat' => '12-hour', + 'minuteincrements' => 1, 'title_display' => 0, 'description' => '', 'private' => FALSE, @@ -62,7 +63,7 @@ function _webform_edit_time($component) { $form['extra']['timezone'] = array( '#type' => 'radios', '#title' => t('Default value timezone'), - '#default_value' => empty($component['extra']['timezone']) ? 'user' : $component['extra']['timezone'], + '#default_value' => $component['extra']['timezone'], '#description' => t('If using relative dates for a default value (e.g. "now") base the current time on this timezone.'), '#options' => array('user' => t('User timezone'), 'site' => t('Website timezone')), '#weight' => 2, @@ -70,13 +71,26 @@ function _webform_edit_time($component) { ); $form['display']['hourformat'] = array( '#type' => 'radios', - '#title' => t('Time Format'), - '#default_value' => isset($component['extra']['hourformat']) ? $component['extra']['hourformat'] : '12-hour', - '#description' => t('Format the display of the time in 12 or 24 hours.'), + '#title' => t('Time format'), + '#default_value' => $component['extra']['hourformat'], '#options' => array('12-hour' => t('12-hour (am/pm)'), '24-hour' => t('24-hour')), '#weight' => 2, '#parents' => array('extra', 'hourformat'), ); + $form['display']['minuteincrements'] = array( + '#type' => 'select', + '#title' => t('Minute increments'), + '#default_value' => $component['extra']['minuteincrements'], + '#options' => array( + 1 => t('1 minute'), + 5 => t('5 minute'), + 10 => t('10 minute'), + 15 => t('15 minute'), + 30 => t('30 minute'), + ), + '#weight' => 3, + '#parents' => array('extra', 'minuteincrements'), + ); return $form; } @@ -95,6 +109,7 @@ function _webform_render_time($component, $value = NULL, $filter = TRUE) { '#description' => $filter ? _webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'], '#element_validate' => array('webform_validate_time'), '#hourformat' => $component['extra']['hourformat'], + '#minuteincrements' => $component['extra']['minuteincrements'], '#default_value' => $filter ? _webform_filter_values($component['value'], $node, NULL, NULL, FALSE) : $component['value'], '#timezone' => $component['extra']['timezone'], '#process' => array('webform_expand_time'), @@ -147,10 +162,45 @@ function webform_expand_time($element) { // Generate the choices for drop-down selects. $hours[''] = t('hour'); $minutes[''] = t('minute'); - for ($i = $first_hour; $i <= $last_hour; $i++) $hours[$i] = $i; - for ($i = 0; $i <= 59; $i++) $minutes[$i] = $i < 10 ? "0$i" : $i; + for ($i = $first_hour; $i <= $last_hour; $i++) { + $hours[$i] = $i; + } + for ($i = 0; $i <= 59; $i++) { + if ($i % $element['#minuteincrements'] == 0) { + $minutes[$i] = $i < 10 ? "0$i" : $i; + } + } $ampms = array('am' => t('am'), 'pm' => t('pm')); + // Adjust the default for minutes if needed, rounding up to the closest value. + if (!isset($minutes[$default_values['minute']])) { + foreach ($minutes as $minute => $padded_minute) { + if ($minute > $default_values['minute']) { + $default_values['minute'] = $minute; + break; + } + } + } + + // If the above loop didn't set a value, it's because rounding up would go to + // the next hour. This gets quite a bit more complicated, since we need to + // deal with looping around on hours, as well as flipping am/pm. + if (!isset($minutes[$default_values['minute']])) { + $default_values['minute'] = 0; + $default_values['hour']++; + // If the hour rolls over also, set hour to the first hour in the list. + if (!isset($hours[$default_values['hour']])) { + $default_values['hour'] = $element['#hourformat'] == '12-hour' ? 1 : 0; + } + // If the hour has been incremented to 12:00 in 12-hour format, flip am/pm. + // Note that technically midnight and noon are neither am or pm, but common + // convention (and US standard) is to represent 12:00am as midnight. + // See http://en.wikipedia.org/wiki/Midnight#Start_and_end_of_day. + if ($element['#hourformat'] == '12-hour' && $default_values['hour'] == 12) { + $default_values['ampm'] = $default_values['ampm'] == 'am' ? 'pm' : 'am'; + } + } + $element['hour'] = array( '#prefix' => '', '#type' => 'select',