You are here

function _date_repeat_widget_validate in Date 6.2

Same name and namespace in other branches
  1. 5.2 date/date_repeat.inc \_date_repeat_widget_validate()
  2. 6 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 55
Implementation of Date Repeat API calculations for the CCK Date field.

Code

function _date_repeat_widget_validate($element, &$form_state) {
  require_once './' . drupal_get_path('module', 'date_repeat') . '/date_repeat_form.inc';
  $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'], $element['rrule']);

  // If no start date was set, clean up the form and return.
  // If no repeats are set, clean up the form and return.
  if (empty($form_values[$field_name]['value']) || $values['FREQ'] == 'NONE') {
    $form_values[$field_name]['rrule'] = NULL;
    form_set_value($element, array(
      $form_values[$field_name],
    ), $form_state);
    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];

    // Avoid undefined index problems on dates that don't have all parts.
    $possible_items = array(
      'value',
      'value2',
      'timezone',
      'offset',
      'offset2',
    );
    foreach ($possible_items as $key) {
      if (empty($item[$key])) {
        $item[$key] = '';
      }
    }
    $value = date_repeat_build_dates($rrule, $values, $field, $item);
    form_set_value($element, $value, $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);
  }
}