You are here

function _date_repeat_widget_validate in Date 6

Same name and namespace in other branches
  1. 5.2 date/date_repeat.inc \_date_repeat_widget_validate()
  2. 6.2 date/date_repeat.inc \_date_repeat_widget_validate()
  3. 7 date_repeat.inc \_date_repeat_widget_validate()

Validation for date repeat form element.

Create multiple values from the RRULE results. Lots more work needed here.

1 call to _date_repeat_widget_validate()
date_widget_validate in date/date_elements.inc
Handle widget processing.

File

date/date_repeat.inc, line 48
Implementation of Date Repeat API calculations for the CCK Date field.

Code

function _date_repeat_widget_validate($element, &$form_state) {
  $form_values = $form_state['values'];
  $field_name = $element['#field_name'];
  $fields = content_fields();
  $field = $fields[$field_name];
  $item = $form_values[$field_name];
  $values = date_repeat_merge($element['#post'][$field_name]['rrule']);

  // If no repeats are set, clean up the form and return.
  if ($values['FREQ'] == 'NONE') {
    form_set_value($element, $value[$field_name], $form_state);
    form_set_value($element['rrule'], NULL);
    return;
  }

  // Require the UNTIL date for now.
  // The RRULE has already been created by this point, so go back
  // to the posted values to see if this was filled out.
  $error_field = implode('][', $element['#parents']) . '][rrule][UNTIL][datetime][date';
  if (empty($values['UNTIL']['datetime'])) {
    form_set_error($error_field, t('The UNTIL value is required for repeating dates.'));
  }
  if (form_get_errors()) {
    return;
  }

  // If the rule, the start date, or the end date have changed, re-calculate
  // the repeating dates, wipe out the previous values, and populate the
  // field with the new values.
  // TODO
  // Is it right to not do anything unless there are changes? Will that
  // confuse anyone? Commenting that out for now...
  $rrule = $form_values[$field_name]['rrule'];
  if (!empty($rrule)) {
    $item = $form_values[$field_name][0];

    // By the time we get here, the start and end dates have been
    // adjusted back to UTC, but we want localtime dates to do
    // things like '+1 Tuesday', so adjust back to localtime.
    $timezone = date_get_timezone($field['tz_handling'], $item[0]['timezone']);
    if (date_timezone_convert($field, $item)) {
      $start = date_make_date($item['value'], 'UTC', $field['type']);
      date_timezone_set($start, timezone_open($timezone));
      if (!empty($item['value2']) && $new_field['value2'] != $item['value']) {
        $end = date_make_date($item['value2'], 'UTC', $field['type']);
        date_timezone_set($end, timezone_open($timezone));
      }
      else {
        $end = $start;
      }
    }
    else {
      $start = date_make_date($item['value'], $timezone, $field['type']);
      if (!empty($item['value2']) && $new_field['value2'] != $item['value']) {
        $end = date_make_date($item['value2'], $timezone, $field['type']);
      }
      else {
        $end = $start;
      }
    }
    $duration = date_difference($start, $end);
    $start_datetime = date_format($start, DATE_FORMAT_DATETIME);
    $end = date_ical_date($values['UNTIL'], $timezone);
    $end_datetime = date_format($end, DATE_FORMAT_DATETIME);
    $parts = date_repeat_split_rrule($rrule);
    $parsed = $parts[0];
    $parsed_exceptions = (array) $parts[1];
    $exceptions = array();
    foreach ($parsed_exceptions as $exception) {
      $date = date_ical_date($exception);
      $exceptions[] = date_format($date, 'Y-m-d');
    }
    $dates = date_repeat_calc($rrule, $start_datetime, $end_datetime, $exceptions);
    foreach ($dates as $delta => $date) {

      // date_repeat_calc always returns DATE_DATETIME dates, which is
      // not necessarily $field['type'] dates.
      // Convert returned dates back to UTC before storing.
      $date_start = date_make_date($date, $timezone, DATE_DATETIME);
      if (date_timezone_convert($field, $item)) {
        date_timezone_set($date_start, timezone_open('UTC'));
      }
      $date_end = drupal_clone($date_start);
      date_modify($date_end, '+' . $duration . ' seconds');
      $value[$field_name][$delta] = array(
        'value' => date_format($date_start, date_type_format($field['type'])),
        'value2' => date_format($date_end, date_type_format($field['type'])),
        'offset' => date_offset_get($date_start),
        'offset2' => date_offset_get($date_end),
        'timezone' => $timezone,
        'rrule' => $delta == 0 ? $rrule : '',
      );
    }
    form_set_value($element, $value[$field_name], $form_state);
  }
  else {

    // If no changes are needed, move the RRULE back to the zero value
    // item of the field.
    form_set_value(array(
      '#parents' => array(
        $field_name,
        0,
        'rrule',
      ),
    ), $rrule, $form_state);
    form_set_value($element['rrule'], NULL, $form_state);
  }
}