You are here

function date_combo_validate in Date 6

Same name and namespace in other branches
  1. 8 date_elements.inc \date_combo_validate()
  2. 5.2 date/date_elements.inc \date_combo_validate()
  3. 6.2 date/date_elements.inc \date_combo_validate()
  4. 7.3 date_elements.inc \date_combo_validate()
  5. 7 date_elements.inc \date_combo_validate()
  6. 7.2 date_elements.inc \date_combo_validate()

Validate and update a combo element. Don't try this if there were errors before reaching this point.

2 string references to 'date_combo_validate'
_date_elements in date/date_elements.inc
Implementation of hook_elements().
_date_widget in date/date_elements.inc
Private implementation of hook_widget().

File

date/date_elements.inc, line 328
Date forms and form themes and validation.

Code

function date_combo_validate($element, &$form_state) {
  $form_values = $form_state['values'];
  $field_name = $element['#field_name'];
  $delta = $element['#delta'];
  $item = $form_values[$field_name][$delta];
  $field = $form_state['#field_info'][$element['#field_name']];
  $delta = $element['#delta'];
  $from_field = 'value';
  $to_field = 'value2';
  $tz_field = 'timezone';
  $offset_field = 'offset';
  $offset_field2 = 'offset2';

  // Update the timezone, if changed.
  $timezone = $element['#date_timezone'];
  if (!empty($form_values['timezone']) && $form_values[$field_name]['timezone'] != $element['#date_timezone']) {
    $timezone = $form_values[$field_name]['timezone'];
  }
  if (empty($item[$from_field])) {
    $item[$from_field] = NULL;
    $item[$to_field] = NULL;
    $item[$tz_field] = $timezone;
    $item[$offset_field] = NULL;
    $item[$offset_field2] = NULL;
    form_set_value($element, $item, $form_state);
  }
  elseif (!form_get_errors()) {

    // Check todate input for blank values and substitute in fromdate
    // values where needed, then re-compute the todate with those values.
    if ($field['todate']) {
      $merged_date = array();
      foreach ($element['#post'][$field_name][$delta][$to_field] as $part => $value) {
        $merged_date[$part] = empty($value) ? $element['#post'][$field_name][$delta][$from_field][$part] : $value;
        if ($part == 'ampm' && $merged_date['ampm'] == 'pm' && $merged_date['hour'] < 12) {
          $merged_date['hour'] += 12;
        }
        elseif ($part == 'ampm' && $merged_date['ampm'] == 'am' && $merged_date['hour'] == 12) {
          $merged_date['hour'] -= 12;
        }
      }
      $element[$to_field]['#value'] = $merged_date;

      // Call the right function to turn this altered user input into
      // a new value for the todate.
      $function = $field['widget']['type'] . '_input_value';
      $to_date = $function($element[$to_field]);
      $item[$to_field] = $to_date;
    }
    else {
      $item[$to_field] = $item[$from_field];
    }
    if ($item[$from_field] > $item[$to_field]) {
      form_set_error(implode('][', $element['#parents']), t('The To date must be greater than the From date.'));
    }
    else {

      // Convert input dates back to their UTC values and re-format to ISO
      // or UNIX instead of the DATETIME format used in element processing.
      if (date_timezone_convert($field, $item)) {
        $item[$tz_field] = $timezone;
        $from_date = date_make_date($item[$from_field], $timezone);
        $item[$offset_field] = date_offset_get($from_date);
        $to_date = date_make_date($item[$to_field], $timezone);
        $item[$offset_field2] = date_offset_get($to_date);
        date_timezone_set($from_date, timezone_open('UTC'));
        date_timezone_set($to_date, timezone_open('UTC'));
        $item[$from_field] = date_format($from_date, date_type_format($field['type']));
        $item[$to_field] = date_format($to_date, date_type_format($field['type']));
      }
      else {
        $item[$from_field] = date_convert($item[$from_field], DATE_DATETIME, $field['type']);
        $item[$to_field] = date_convert($item[$to_field], DATE_DATETIME, $field['type']);
      }
      $item['rrule'] = $rrule;
      form_set_value($element, $item, $form_state);
    }
  }
}