You are here

function webform_expand_date in Webform 7.3

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

Form API #process function for Webform date fields.

1 string reference to 'webform_expand_date'
_webform_render_date in components/date.inc
Implements _webform_render_component().

File

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

Code

function webform_expand_date($element) {

  // Accept a string or array value for #default_value.
  if (!empty($element['#default_value']) && is_string($element['#default_value'])) {
    $timezone = $element['#timezone'] != 'user' ? NULL : 'user';
    $timestring = webform_strtodate('c', $element['#default_value'], $timezone);
    $element['#default_value'] = webform_date_array($timestring, 'date');
  }

  // Prevent an error in PHP 5.4 caused by core's treatment of the #value.
  if (isset($element['#value'])) {
    unset($element['#value']);
  }

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

  // Let Drupal do it's normal expansion.
  $element = form_process_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]['#title'] = $none;
    $element[$type]['#title_display'] = 'invisible';
    $element[$type]['#default_value'] = isset($default_values[$type]) ? $default_values[$type] : NULL;
    $element[$type]['#options'] = array(
      '' => $none,
    ) + $element[$type]['#options'];
  }

  // Convert relative dates to absolute ones.
  foreach (array(
    'start_date',
    'end_date',
  ) as $start_end) {
    $timezone = $element['#timezone'] != 'user' ? NULL : 'user';
    $date = webform_strtodate('Y-m-d', $element['#' . $start_end], $timezone);
    $element['#' . $start_end] = $date ? $date : '';
  }

  // Tweak the year field.
  if ($element['#year_textfield']) {
    $element['year']['#type'] = 'textfield';
    $element['year']['#size'] = 5;
    $element['year']['#maxlength'] = 4;
    unset($element['year']['#options']);
  }
  elseif ($element['#start_date'] || $element['#end_date']) {
    $start_year = $element['#start_date'] ? webform_strtodate('Y', $element['#start_date']) : webform_strtodate('Y', '-2 years');
    $end_year = $element['#end_date'] ? webform_strtodate('Y', $element['#end_date']) : webform_strtodate('Y', '+2 years');
    $element['year']['#options'] = array(
      '' => t('Year'),
    ) + drupal_map_assoc(range($start_year, $end_year));
  }
  return $element;
}