You are here

function webform_expand_time in Webform 6.3

Same name and namespace in other branches
  1. 7.4 components/time.inc \webform_expand_time()
  2. 7.3 components/time.inc \webform_expand_time()

Form API #process function for Webform time fields.

1 string reference to 'webform_expand_time'
_webform_render_time in components/time.inc
Implements _webform_render_component().

File

components/time.inc, line 134
Webform module time component.

Code

function webform_expand_time($element) {

  // Expand the default value from a string into an array.
  if (!empty($element['#default_value'])) {

    // Adjust the time based on the user or site timezone.
    // The "timezone_name" variable is provided by DateAPI in Drupal 6.
    if (variable_get('configurable_timezones', 1) && $element['#timezone'] == 'user') {
      $timezone_name = isset($GLOBALS['user']->timezone_name) ? $GLOBALS['user']->timezone_name : NULL;
    }
    else {
      $timezone_name = variable_get('date_default_timezone_name', NULL);
    }
    if (!empty($timezone_name) && class_exists('DateTimeZone')) {
      $default_values = webform_date_array(webform_strtodate('c', $element['#default_value'], $timezone_name), 'time');
    }
    else {
      $default_values = webform_date_array(date('c', strtotime($element['#default_value'])), 'time');
    }
  }
  else {
    $default_values = array(
      'hour' => '',
      'minute' => '',
      'second' => '',
    );
  }
  $first_hour = 0;
  $last_hour = 23;
  if ($element['#hourformat'] == '12-hour') {
    $first_hour = 1;
    $last_hour = 12;
    $default_values = webform_time_convert($default_values, '12-hour');
    $default_values['ampm'] = $default_values['ampm'] ? $default_values['ampm'] : 'am';
  }

  // 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 += $element['#minuteincrements']) {
    $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',
    '#default_value' => $default_values['hour'],
    '#options' => $hours,
  );
  $element['minute'] = array(
    '#prefix' => ':',
    '#type' => 'select',
    '#default_value' => $default_values['minute'],
    '#options' => $minutes,
  );
  if (strcmp($element['#hourformat'], '12-hour') == 0) {
    $element['ampm'] = array(
      '#type' => 'radios',
      '#default_value' => $default_values['ampm'],
      '#options' => $ampms,
    );
  }

  // Set the overall default value.
  if ($default_values['hour'] !== '') {
    $element['#default_value'] = webform_date_string($default_values);
  }
  return $element;
}