You are here

function webform_expand_date in Webform 5.2

Same name and namespace in other branches
  1. 6.3 components/date.inc \webform_expand_date()
  2. 6.2 components/date.inc \webform_expand_date()
  3. 7.4 components/date.inc \webform_expand_date()
  4. 7.3 components/date.inc \webform_expand_date()

File

components/date.inc, line 120
Webform module date component.

Code

function webform_expand_date($element) {
  $component = $element['#webform_component'];

  // Set defaults according to existing #default_value (set by Form API)
  if (isset($element['month']['#default_value']) || isset($element['day']['#default_value']) || isset($element['year']['#default_value'])) {
    $default_values = array(
      'month' => $element['month']['#default_value'],
      'day' => $element['day']['#default_value'],
      'year' => $element['year']['#default_value'],
    );

    // Remove the default values, or the elements will be forced into month/day/year order.
    unset($element['month']);
    unset($element['day']);
    unset($element['year']);
  }
  elseif (drupal_strlen($component['value']) > 0) {

    // Calculate the timestamp in GMT.
    $timestamp = strtotime(_webform_filter_values($component['value']));
    if ($component['extra']['timezone'] == 'user') {

      // Use the users timezone.
      global $user;
      $timestamp += (int) $user->timezone;
    }
    elseif ($component['extra']['timezone'] == 'gmt') {

      // Use GMT.
      $timestamp += 0;
    }
    else {

      // Use the Drupal site time.
      $timestamp += variable_get('date_default_timezone', 0);
    }

    // Check for daylight savings time.
    if ($component['extra']['check_daylight_savings'] && date('I')) {
      $timestamp += 3600;
    }
    $default_values = array(
      'day' => gmdate('j', $timestamp),
      'month' => gmdate('n', $timestamp),
      'year' => gmdate('Y', $timestamp),
    );
  }
  else {
    $default_values = array(
      'day' => NULL,
      'month' => NULL,
      'year' => NULL,
    );
  }

  // Let Drupal do it's normal expansion.
  $element = expand_date($element);

  // Set default values.
  foreach ($default_values as $type => $value) {
    switch ($type) {
      case 'month':
        $none = t('Month');
        break;
      case 'day':
        $none = t('Day');
        break;
      case 'year':
        $none = t('Year');
        break;
    }
    unset($element[$type]['#value']);
    $element[$type]['#default_value'] = isset($default_values[$type]) ? $default_values[$type] : NULL;
    $element[$type]['#options'] = array(
      '' => $none,
    ) + $element[$type]['#options'];
  }

  // Tweak the year field.
  if ($component['extra']['year_textfield']) {
    $element['year']['#type'] = 'textfield';
    $element['year']['#size'] = 5;
    $element['year']['#maxlength'] = 4;
    unset($element['year']['#options']);
  }
  elseif (is_numeric($component['extra']['year_start']) && is_numeric($component['extra']['year_end'])) {
    $element['year']['#options'] = array(
      '' => t('Year'),
    ) + drupal_map_assoc(range($component['extra']['year_start'], $component['extra']['year_end']));
  }
  return $element;
}