You are here

function date_combo_validate in Date 5.2

Same name and namespace in other branches
  1. 8 date_elements.inc \date_combo_validate()
  2. 6.2 date/date_elements.inc \date_combo_validate()
  3. 6 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.

File

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

Code

function date_combo_validate($element) {
  global $form_values;
  $field_name = $element['#parents'][0];
  $delta = $element['#parents'][1];
  $item = $form_values[$field_name][$delta];

  // If the whole field is empty and that's OK, stop now.
  if (empty($element['#post'][$field_name]) && !$element['#required']) {
    return;
  }
  $field = $element['#field'];
  $delta = $element['#delta'];
  $from_field = 'value';
  $to_field = 'value2';
  $tz_field = 'timezone';
  $offset_field = 'offset';
  $offset_field2 = 'offset2';

  // Check for empty 'From date', which could either be an empty
  // value or an array of empty values, depending on the widget.
  $empty = TRUE;
  if (!empty($item[$from_field])) {
    if (!is_array($item[$from_field])) {
      $empty = FALSE;
    }
    else {
      foreach ($item[$from_field] as $key => $value) {
        if (!empty($value)) {
          $empty = FALSE;
          break;
        }
      }
    }
  }
  if ($empty) {
    $item = date_element_empty($element);
    if (!$element['#required']) {
      return;
    }
  }
  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();
      $to_date_empty = TRUE;
      foreach ($element['#post'][$field_name][$delta][$to_field] as $part => $value) {
        $to_date_empty = $to_date_empty && empty($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;
        }
      }

      // If all date values were empty and a date is required, throw
      // an error on the first element. We don't want to create
      // duplicate messages on every date part, so the error will
      // only go on the first.
      if ($to_date_empty && $field['todate'] == 'required') {
        $error_field = implode('][', $element['#parents']) . '][value2][' . array_shift(array_keys($merged_date));
        form_set_error($error_field, t('Some value must be entered in the To date.'));
      }
      $element[$to_field]['#value'] = $merged_date;

      // Call the right function to turn this altered user input into
      // a new value for the todate.
      $item[$to_field] = $merged_date;
    }
    else {
      $item[$to_field] = $item[$from_field];
    }
    $from_date = date_input_value($field, $element[$from_field]);
    if (!empty($field['todate'])) {
      $to_date = date_input_value($field, $element[$to_field]);
    }
    else {
      $to_date = $from_date;
    }

    // Neither the from date nor the to date should be empty at this point
    // unless they held values that couldn't be evaluated.
    if (!$field['required'] && (empty($from_date) || empty($to_date))) {
      $item = date_element_empty($element);
      form_set_error(implode('][', $element['#parents']), t('The dates are invalid.'));
      return;
    }
    elseif (!empty($field['todate']) && $from_date > $to_date) {
      $error_field = implode('][', $element['#parents']) . '][value2][' . array_shift(array_keys($merged_date));
      $element[$to_field]['#value'] = $merged_date;
      form_set_value($element[$to_field], $to_date, $form_state);
      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.
      $timezone = !empty($item[$tz_field]) ? $item[$tz_field] : $element['#date_timezone'];
      $timezone_db = date_get_timezone_db($field['tz_handling']);
      $item[$tz_field] = $timezone;
      $from_date = date_make_date($from_date, $timezone);
      $item[$offset_field] = date_offset_get($from_date);
      $to_date = date_make_date($to_date, $timezone);
      $item[$offset_field2] = date_offset_get($to_date);
      date_timezone_set($from_date, timezone_open($timezone_db));
      date_timezone_set($to_date, timezone_open($timezone_db));
      $item[$from_field] = date_format($from_date, date_type_format($field['type']));
      $item[$to_field] = date_format($to_date, date_type_format($field['type']));

      // TODO get RRULE working.

      //$item['rrule'] = $rrule;
      form_set_value($element, $item);
    }
  }
}