You are here

function date_repeat_merge in Date 8

Same name and namespace in other branches
  1. 5.2 date_repeat/date_repeat_form.inc \date_repeat_merge()
  2. 6.2 date_repeat/date_repeat_form.inc \date_repeat_merge()
  3. 6 date_repeat/date_repeat_form.inc \date_repeat_merge()
  4. 7.3 date_repeat/date_repeat_form.inc \date_repeat_merge()
  5. 7 date_repeat/date_repeat_form.inc \date_repeat_merge()
  6. 7.2 date_repeat/date_repeat_form.inc \date_repeat_merge()

Regroup values back into a consistant array, no matter what state it is in.

3 calls to date_repeat_merge()
date_repeat_field_widget_validate in date_repeat_field/date_repeat_field.module
Validation for date repeat form element.
date_repeat_rrule_validate in date_repeat/date_repeat_form.inc
Build a RRULE out of the form values.
_date_repeat_rrule_process in date_repeat/date_repeat_form.inc
Generate the repeat setting form.

File

date_repeat/date_repeat_form.inc, line 732
Code to add a date repeat selection form to a date field and create an iCal RRULE from the chosen selections.

Code

function date_repeat_merge($form_values, $element) {
  if (empty($form_values) || !is_array($form_values)) {
    return $form_values;
  }
  if (array_key_exists('exceptions', $form_values) || array_key_exists('additions', $form_values)) {
    if (!array_key_exists('exceptions', $form_values)) {
      $form_values['exceptions'] = array();
    }
    if (!array_key_exists('additions', $form_values)) {
      $form_values['additions'] = array();
    }
    $form_values = array_merge($form_values, (array) $form_values['exceptions'], (array) $form_values['additions']);
    unset($form_values['exceptions']);
    unset($form_values['additions']);
  }
  if (array_key_exists('FREQ', $form_values)) {
    switch ($form_values['FREQ']) {
      case 'DAILY':
        if (array_key_exists('daily', $form_values)) {
          switch ($form_values['daily']['byday_radios']) {
            case 'INTERVAL':
              $form_values['INTERVAL'] = $form_values['daily']['INTERVAL_child'];
              break;
            case 'every_weekday':
              $form_values['BYDAY'] = array(
                'MO',
                'TU',
                'WE',
                'TH',
                'FR',
              );
              break;
            case 'every_mo_we_fr':
              $form_values['BYDAY'] = array(
                'MO',
                'WE',
                'FR',
              );
              break;
            case 'every_tu_th':
              $form_values['BYDAY'] = array(
                'TU',
                'TH',
              );
              break;
          }
        }
        break;
      case 'WEEKLY':
        if (array_key_exists('weekly', $form_values)) {
          $form_values = array_merge($form_values, (array) $form_values['weekly']);
          if (array_key_exists('BYDAY', $form_values)) {
            $form_values['BYDAY'] = date_repeat_transform_checkbox_values_to_select_values($form_values['BYDAY']);
          }
        }
        break;
      case 'MONTHLY':
        if (array_key_exists('monthly', $form_values)) {
          switch ($form_values['monthly']['day_month']) {
            case 'BYMONTHDAY_BYMONTH':
              $form_values['monthly'] = array_merge($form_values['monthly'], (array) $form_values['monthly']['BYMONTHDAY_BYMONTH_child']);
              break;
            case 'BYDAY_BYMONTH':
              $form_values['monthly']['BYDAY_BYMONTH_child']['BYDAY'] = $form_values['monthly']['BYDAY_BYMONTH_child']['BYDAY_COUNT'] . $form_values['monthly']['BYDAY_BYMONTH_child']['BYDAY_DAY'];
              $form_values['monthly'] = array_merge($form_values['monthly'], (array) $form_values['monthly']['BYDAY_BYMONTH_child']);
              break;
          }
          unset($form_values['monthly']['BYDAY_BYMONTH_child']);
          unset($form_values['monthly']['BYMONTHDAY_BYMONTH_child']);
          $form_values = array_merge($form_values, (array) $form_values['monthly']);
          if (array_key_exists('BYMONTH', $form_values)) {
            $form_values['BYMONTH'] = date_repeat_transform_checkbox_values_to_select_values($form_values['BYMONTH']);
          }
          if (array_key_exists('BYMONTHDAY', $form_values) && !is_array($form_values['BYMONTHDAY'])) {
            $form_values['BYMONTHDAY'] = (array) $form_values['BYMONTHDAY'];
          }
          if (array_key_exists('BYDAY', $form_values) && !is_array($form_values['BYDAY'])) {
            $form_values['BYDAY'] = (array) $form_values['BYDAY'];
          }
        }
        break;
      case 'YEARLY':
        if (array_key_exists('yearly', $form_values)) {
          switch ($form_values['yearly']['day_month']) {
            case 'BYMONTHDAY_BYMONTH':
              $form_values['yearly'] = array_merge($form_values['yearly'], (array) $form_values['yearly']['BYMONTHDAY_BYMONTH_child']);
              break;
            case 'BYDAY_BYMONTH':
              $form_values['yearly']['BYDAY_BYMONTH_child']['BYDAY'] = $form_values['yearly']['BYDAY_BYMONTH_child']['BYDAY_COUNT'] . $form_values['yearly']['BYDAY_BYMONTH_child']['BYDAY_DAY'];
              $form_values['yearly'] = array_merge($form_values['yearly'], (array) $form_values['yearly']['BYDAY_BYMONTH_child']);
              break;
          }
          unset($form_values['yearly']['BYDAY_BYMONTH_child']);
          unset($form_values['yearly']['BYMONTHDAY_BYMONTH_child']);
          $form_values = array_merge($form_values, (array) $form_values['yearly']);
          if (array_key_exists('BYMONTH', $form_values)) {
            $form_values['BYMONTH'] = date_repeat_transform_checkbox_values_to_select_values($form_values['BYMONTH']);
          }
          if (array_key_exists('BYMONTHDAY', $form_values) && !is_array($form_values['BYMONTHDAY'])) {
            $form_values['BYMONTHDAY'] = (array) $form_values['BYMONTHDAY'];
          }
          if (array_key_exists('BYDAY', $form_values) && !is_array($form_values['BYDAY'])) {
            $form_values['BYDAY'] = (array) $form_values['BYDAY'];
          }
        }
        break;
      default:
        break;
    }
  }
  unset($form_values['daily']);
  unset($form_values['weekly']);
  unset($form_values['monthly']);
  unset($form_values['yearly']);
  if (array_key_exists('range_of_repeat', $form_values)) {
    switch ($form_values['range_of_repeat']) {
      case 'COUNT':
        $form_values['COUNT'] = $form_values['count_child'];
        break;
      case 'UNTIL':
        $form_values['UNTIL'] = $form_values['until_child'];
        break;
    }
  }
  unset($form_values['count_child']);
  unset($form_values['until_child']);
  if (array_key_exists('BYDAY', $form_values) && is_array($form_values['BYDAY'])) {
    unset($form_values['BYDAY']['']);
  }
  if (array_key_exists('BYMONTH', $form_values) && is_array($form_values['BYMONTH'])) {
    unset($form_values['BYMONTH']['']);
  }
  if (array_key_exists('BYMONTHDAY', $form_values) && is_array($form_values['BYMONTHDAY'])) {
    unset($form_values['BYMONTHDAY']['']);
  }
  if (array_key_exists('UNTIL', $form_values) && is_array($form_values['UNTIL']['datetime'])) {
    $function = $element['#date_repeat_widget'] . '_input_date';
    $until_element = $element;
    $until_element['#date_date_format'] = !empty($element['#date_date_format']) ? DateGranularity::limitFormat($element['#date_date_format'], array(
      'year',
      'month',
      'day',
    )) : 'Y-m-d';
    $date = $function($until_element, $form_values['UNTIL']['datetime']);
    $form_values['UNTIL']['datetime'] = $date instanceof DrupalDateTime ? $date
      ->format(DATE_FORMAT_DATETIME) : '';
  }
  if (array_key_exists('show_exceptions', $form_values) && $form_values['show_exceptions'] === 0) {
    unset($form_values['EXDATE']);
  }
  if (array_key_exists('EXDATE', $form_values) && is_array($form_values['EXDATE'])) {
    $function = $element['#date_repeat_widget'] . '_input_date';
    $exdate_element = $element;
    foreach ($form_values['EXDATE'] as $delta => $value) {
      if (is_array($value['datetime'])) {
        $exdate_element['#date_date_format'] = !empty($element['#date_date_format']) ? DateGranularity::limitFormat($element['#date_date_format'], array(
          'year',
          'month',
          'day',
        )) : 'Y-m-d';
        $date = $function($exdate_element, $form_values['EXDATE'][$delta]['datetime']);
        $form_values['EXDATE'][$delta]['datetime'] = $date instanceof DrupalDateTime ? $date
          ->format(DATE_FORMAT_DATETIME) : '';
      }
    }
  }
  if (array_key_exists('show_additions', $form_values) && $form_values['show_additions'] === 0) {
    unset($form_values['RDATE']);
  }
  if (array_key_exists('RDATE', $form_values) && is_array($form_values['RDATE'])) {
    $function = $element['#date_repeat_widget'] . '_input_date';
    $rdate_element = $element;
    foreach ($form_values['RDATE'] as $delta => $value) {
      if (is_array($value['datetime'])) {
        $rdate_element['#date_date_format'] = !empty($element['#date_date_format']) ? DateGranularity::limitFormat($element['#date_date_format'], array(
          'year',
          'month',
          'day',
        )) : 'Y-m-d';
        $date = $function($rdate_element, $form_values['RDATE'][$delta]['datetime']);
        $form_values['RDATE'][$delta]['datetime'] = $date instanceof DrupalDateTime ? $date
          ->format(DATE_FORMAT_DATETIME) : '';
      }
    }
  }
  return $form_values;
}