You are here

function _date_repeat_rrule_process in Date 7.2

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

Generate the repeat setting form.

1 call to _date_repeat_rrule_process()
date_repeat_rrule_process in date_repeat/date_repeat.module
Generate the repeat rule setting form.

File

date_repeat/date_repeat_form.inc, line 39
Add a date repeat selection form to a date field.

Code

function _date_repeat_rrule_process($element, &$form_state, $form) {

  // If the RRULE field is not visible to the user, needs no processing or
  // validation. The Date field module is not adding this element to forms if
  // the field is hidden, this test is just in case some other module attempts
  // to do so.
  if (date_hidden_element($element)) {
    return $element;
  }
  module_load_include('inc', 'date_api', 'date_api_ical');
  if (empty($element['#date_repeat_widget'])) {
    $element['#date_repeat_widget'] = module_exists('date_popup') ? 'date_popup' : 'date_select';
  }
  if (is_array($element['#default_value'])) {
    $element['#value'] = date_repeat_merge($element['#value'], $element);
    $rrule = date_api_ical_build_rrule($element['#value']);
  }
  else {
    $rrule = $element['#default_value'];
  }

  // Empty the original string value of the RRULE so we can create an array of
  // values for the form from the RRULE's contents.
  $element['#value'] = '';
  $parts = date_repeat_split_rrule($rrule);
  $rrule = $parts[0];
  $exceptions = $parts[1];
  $additions = $parts[2];
  $timezone = !empty($element['#date_timezone']) ? $element['#date_timezone'] : date_default_timezone();
  $merged_values = date_repeat_merge($rrule, $element);
  $until = '';
  if (!empty($merged_values['UNTIL']['datetime'])) {
    $until_date = new DateObject($merged_values['UNTIL']['datetime'], $merged_values['UNTIL']['tz']);
    date_timezone_set($until_date, timezone_open($timezone));
    $until = date_format($until_date, DATE_FORMAT_DATETIME);
  }
  $count = '';
  if (!empty($merged_values['COUNT'])) {
    $count = $merged_values['COUNT'];
  }
  $element['FREQ'] = array(
    '#type' => 'select',
    '#title' => t('Repeats', array(), array(
      'context' => 'Date repeat',
    )),
    '#default_value' => !empty($rrule['FREQ']) ? $rrule['FREQ'] : 'WEEKLY',
    '#options' => date_repeat_freq_options(),
    '#prefix' => '<div class="date-repeat-input">',
    '#suffix' => '</div>',
  );
  $element['daily'] = array(
    '#type' => 'container',
    '#tree' => TRUE,
    '#prefix' => '<div class="date-clear daily">',
    '#suffix' => '</div>',
    '#states' => array(
      'visible' => array(
        ":input[name=\"{$element['#name']}[FREQ]\"]" => array(
          'value' => 'DAILY',
        ),
      ),
    ),
  );
  $element['weekly'] = array(
    '#type' => 'container',
    '#tree' => TRUE,
    '#prefix' => '<div class="date-clear weekly">',
    '#suffix' => '</div>',
    '#states' => array(
      'visible' => array(
        ":input[name=\"{$element['#name']}[FREQ]\"]" => array(
          'value' => 'WEEKLY',
        ),
      ),
    ),
  );
  $element['monthly'] = array(
    '#type' => 'container',
    '#tree' => TRUE,
    '#prefix' => '<div class="date-clear monthly">',
    '#suffix' => '</div>',
    '#states' => array(
      'visible' => array(
        ":input[name=\"{$element['#name']}[FREQ]\"]" => array(
          'value' => 'MONTHLY',
        ),
      ),
    ),
  );
  $element['yearly'] = array(
    '#type' => 'container',
    '#tree' => TRUE,
    '#prefix' => '<div class="date-clear yearly">',
    '#suffix' => '</div>',
    '#states' => array(
      'visible' => array(
        ":input[name=\"{$element['#name']}[FREQ]\"]" => array(
          'value' => 'YEARLY',
        ),
      ),
    ),
  );
  list($prefix, $suffix) = explode('@interval', t('Every @interval days', array(), array(
    'context' => 'Date repeat',
  )));
  $daily_interval = array(
    '#type' => 'textfield',
    '#title' => t('Repeats', array(), array(
      'context' => 'Date repeat',
    )),
    '#title_display' => 'invisible',
    '#default_value' => !empty($rrule['INTERVAL']) ? $rrule['INTERVAL'] : 1,
    '#element_validate' => array(
      'element_validate_integer_positive',
    ),
    '#attributes' => array(
      'placeholder' => array(
        '#',
      ),
    ),
    '#size' => 3,
    '#maxlength' => 3,
    '#prefix' => '<div class="date-clear">',
    '#suffix' => t('days') . '</div>',
    '#field_prefix' => $prefix,
    '#field_suffix' => $suffix,
  );
  list($prefix, $suffix) = explode('@interval', t('Every @interval weeks', array(), array(
    'context' => 'Date repeat',
  )));
  $element['weekly']['INTERVAL'] = array(
    '#type' => 'textfield',
    '#title' => t('Repeats', array(), array(
      'context' => 'Date repeat',
    )),
    '#default_value' => !empty($rrule['INTERVAL']) ? $rrule['INTERVAL'] : 1,
    '#element_validate' => array(
      'element_validate_integer_positive',
    ),
    '#attributes' => array(
      'placeholder' => array(
        '#',
      ),
    ),
    '#size' => 3,
    '#maxlength' => 3,
    '#prefix' => '<div class="date-clear">',
    '#suffix' => '</div>',
    '#field_prefix' => $prefix,
    '#field_suffix' => $suffix,
  );
  list($prefix, $suffix) = explode('@interval', t('Every @interval months', array(), array(
    'context' => 'Date repeat',
  )));
  $element['monthly']['INTERVAL'] = array(
    '#access' => FALSE,
    '#type' => 'textfield',
    '#title' => t('Repeats', array(), array(
      'context' => 'Date repeat',
    )),
    '#default_value' => !empty($rrule['INTERVAL']) ? $rrule['INTERVAL'] : 1,
    '#element_validate' => array(
      'element_validate_integer_positive',
    ),
    '#attributes' => array(
      'placeholder' => array(
        '#',
      ),
    ),
    '#size' => 3,
    '#maxlength' => 3,
    '#prefix' => '<div class="date-clear">',
    '#suffix' => '</div>',
    '#field_prefix' => $prefix,
    '#field_suffix' => $suffix,
  );
  list($prefix, $suffix) = explode('@interval', t('Every @interval years', array(), array(
    'context' => 'Date repeat',
  )));
  $element['yearly']['INTERVAL'] = array(
    '#type' => 'textfield',
    '#title' => t('Repeats', array(), array(
      'context' => 'Date repeat',
    )),
    '#default_value' => !empty($rrule['INTERVAL']) ? $rrule['INTERVAL'] : 1,
    '#element_validate' => array(
      'element_validate_integer_positive',
    ),
    '#attributes' => array(
      'placeholder' => array(
        '#',
      ),
    ),
    '#size' => 3,
    '#maxlength' => 3,
    '#prefix' => '<div class="date-clear">',
    '#suffix' => '</div>',
    '#field_prefix' => $prefix,
    '#field_suffix' => $suffix,
  );
  $options = date_repeat_dow_day_options_abbr(TRUE);
  $options = date_repeat_dow_day_options_ordered($options);
  $element['weekly']['BYDAY'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Repeat on', array(), array(
      'context' => 'Date repeat',
    )),
    '#default_value' => !empty($rrule['BYDAY']) && $rrule['FREQ'] === 'WEEKLY' ? $rrule['BYDAY'] : array(),
    '#options' => $options,
    '#attributes' => array(
      'class' => array(
        'container-inline byday',
      ),
    ),
    '#multiple' => TRUE,
    '#prefix' => '<div class="date-clear">',
    '#suffix' => '</div>',
  );
  $daily_radios_default = 'INTERVAL';
  if (isset($rrule['FREQ']) && $rrule['FREQ'] === 'DAILY' && !empty($rrule['BYDAY'])) {
    switch (count($rrule['BYDAY'])) {
      case 2:
        $daily_radios_default = 'every_tu_th';
        break;
      case 3:
        $daily_radios_default = 'every_mo_we_fr';
        break;
      case 5:
        $daily_radios_default = 'every_weekday';
        break;
    }
  }
  $daily_every_weekday = array(
    '#type' => 'item',
    '#markup' => '<div>' . t('Every weekday', array(), array(
      'context' => 'Date repeat',
    )) . '</div>',
  );
  $daily_mo_we_fr = array(
    '#type' => 'item',
    '#markup' => '<div>' . t('Every Mon, Wed, Fri', array(), array(
      'context' => 'Date repeat',
    )) . '</div>',
  );
  $daily_tu_th = array(
    '#type' => 'item',
    '#markup' => '<div>' . t('Every Tue, Thu', array(), array(
      'context' => 'Date repeat',
    )) . '</div>',
  );
  $element['daily']['byday_radios'] = array(
    '#type' => 'date_repeat_form_element_radios',
    '#tree' => TRUE,
    '#title' => t('Repeats every', array(), array(
      'context' => 'Date repeat',
    )),
    '#prefix' => '<div class="date-clear">',
    '#suffix' => '</div>',
    '#states' => array(
      'visible' => array(
        ":input[name=\"{$element['#name']}[FREQ]\"]" => array(
          'value' => 'DAILY',
        ),
      ),
    ),
    '#default_value' => $daily_radios_default,
    '#options' => array(
      'INTERVAL' => t('interval'),
      'every_weekday' => t('every weekday'),
      'every_mo_we_fr' => t('monday wednesday friday'),
      'every_tu_th' => t('tuesday thursday'),
    ),
    'INTERVAL_child' => $daily_interval,
    'every_weekday_child' => $daily_every_weekday,
    'mo_we_fr_child' => $daily_mo_we_fr,
    'tu_th_child' => $daily_tu_th,
    '#div_classes' => array(
      'container-inline interval',
      'container-inline weekday',
      'container-inline mo-we-fr',
      'container-inline tu-th',
    ),
  );
  $monthly_day_month_default = 'BYMONTHDAY_BYMONTH';
  if (isset($rrule['FREQ']) && $rrule['FREQ'] === 'MONTHLY' && !empty($rrule['BYDAY'])) {
    $monthly_day_month_default = 'BYDAY_BYMONTH';
  }
  $monthly_on_day_bymonthday_of_bymonth = array(
    '#type' => 'container',
    '#tree' => TRUE,
  );
  list($bymonthday_title, $bymonthday_suffix) = explode('@bymonthday', t('On day @bymonthday of', array(), array(
    'context' => 'Date repeat',
  )));
  $monthly_on_day_bymonthday_of_bymonth['BYMONTHDAY'] = array(
    '#type' => 'select',
    '#title' => $bymonthday_title,
    '#default_value' => !empty($rrule['BYMONTHDAY']) && $rrule['FREQ'] === 'MONTHLY' ? $rrule['BYMONTHDAY'] : '',
    '#options' => drupal_map_assoc(range(1, 31)) + drupal_map_assoc(range(-1, -31)),
    '#multiple' => FALSE,
    '#prefix' => '<div class="date-clear bymonthday">',
    '#suffix' => '</div>',
    '#field_suffix' => $bymonthday_suffix,
  );
  $monthly_on_day_bymonthday_of_bymonth['BYMONTH'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Bymonth', array(), array(
      'context' => 'Date repeat',
    )),
    '#title_display' => 'invisible',
    '#default_value' => !empty($rrule['BYMONTH']) && $rrule['FREQ'] === 'MONTHLY' && $monthly_day_month_default === 'BYMONTHDAY_BYMONTH' ? $rrule['BYMONTH'] : array(),
    '#options' => date_month_names_abbr(TRUE),
    '#attributes' => array(
      'class' => array(
        'container-inline',
      ),
    ),
    '#multiple' => TRUE,
    '#prefix' => '<div class="date-clear bymonth">',
    '#suffix' => '</div>',
  );
  $monthly_on_the_byday_of_bymonth = array(
    '#type' => 'container',
    '#tree' => TRUE,
  );
  $monthly_byday_count = '';
  $monthly_byday_day = '';
  if (isset($rrule['BYDAY']) && !empty($rrule['BYDAY']) && $rrule['FREQ'] === 'MONTHLY') {
    $monthly_byday_count = substr($rrule['BYDAY'][0], 0, -2);
    $monthly_byday_day = substr($rrule['BYDAY'][0], -2);
  }
  list($byday_count_title, $byday_day_title) = explode('@byday', t('On the @byday of', array(), array(
    'context' => 'Date repeat',
  )));
  $monthly_on_the_byday_of_bymonth['BYDAY_COUNT'] = array(
    '#type' => 'select',
    '#title' => $byday_count_title,
    '#default_value' => !empty($monthly_byday_count) ? $monthly_byday_count : '',
    '#options' => date_order_translated(),
    '#multiple' => FALSE,
    '#prefix' => '<div class="date-repeat-input byday-count">',
    '#suffix' => '</div>',
  );
  $monthly_on_the_byday_of_bymonth['BYDAY_DAY'] = array(
    '#type' => 'select',
    '#title' => $byday_day_title,
    '#title_display' => 'after',
    '#default_value' => !empty($monthly_byday_day) ? $monthly_byday_day : '',
    '#options' => date_repeat_dow_day_options(TRUE),
    '#multiple' => FALSE,
    '#prefix' => '<div class="date-repeat-input byday-day">',
    '#suffix' => '</div>',
  );
  $monthly_on_the_byday_of_bymonth['BYMONTH'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Bymonth', array(), array(
      'context' => 'Date repeat',
    )),
    '#title_display' => 'invisible',
    '#default_value' => !empty($rrule['BYMONTH']) && $rrule['FREQ'] === 'MONTHLY' && $monthly_day_month_default === 'BYDAY_BYMONTH' ? $rrule['BYMONTH'] : array(),
    '#options' => date_month_names_abbr(TRUE),
    '#attributes' => array(
      'class' => array(
        'container-inline',
      ),
    ),
    '#multiple' => TRUE,
    '#prefix' => '<div class="date-clear bymonth">',
    '#suffix' => '</div>',
  );
  $element['monthly']['day_month'] = array(
    '#type' => 'date_repeat_form_element_radios',
    '#tree' => TRUE,
    '#prefix' => '<div class="date-clear">',
    '#suffix' => '</div>',
    '#states' => array(
      'visible' => array(
        ":input[name=\"{$element['#name']}[FREQ]\"]" => array(
          'value' => 'MONTHLY',
        ),
      ),
    ),
    '#attributes' => array(
      'class' => array(
        'date-repeat-radios clearfix',
      ),
    ),
    '#default_value' => $monthly_day_month_default,
    '#options' => array(
      'BYMONTHDAY_BYMONTH' => t('On day ... of ...'),
      'BYDAY_BYMONTH' => t('On the ... of ...'),
    ),
    'BYMONTHDAY_BYMONTH_child' => $monthly_on_day_bymonthday_of_bymonth,
    'BYDAY_BYMONTH_child' => $monthly_on_the_byday_of_bymonth,
    '#div_classes' => array(
      'date-repeat-radios-item date-clear clearfix bymonthday-bymonth',
      'date-repeat-radios-item date-clear clearfix byday-bymonth',
    ),
  );
  $yearly_day_month_default = 'BYMONTHDAY_BYMONTH';
  if (isset($rrule['FREQ']) && $rrule['FREQ'] === 'YEARLY' && !empty($rrule['BYDAY'])) {
    $yearly_day_month_default = 'BYDAY_BYMONTH';
  }
  $yearly_on_day_bymonthday_of_bymonth = array(
    '#type' => 'container',
    '#tree' => TRUE,
  );
  list($bymonthday_title, $bymonthday_suffix) = explode('@bymonthday', t('On day @bymonthday of', array(), array(
    'context' => 'Date repeat',
  )));
  $yearly_on_day_bymonthday_of_bymonth['BYMONTHDAY'] = array(
    '#type' => 'select',
    '#title' => $bymonthday_title,
    '#default_value' => !empty($rrule['BYMONTHDAY']) && $rrule['FREQ'] === 'YEARLY' ? $rrule['BYMONTHDAY'] : '',
    '#options' => drupal_map_assoc(range(1, 31)) + drupal_map_assoc(range(-1, -31)),
    '#multiple' => FALSE,
    '#prefix' => '<div class="date-clear bymonthday">',
    '#suffix' => '</div>',
    '#field_suffix' => $bymonthday_suffix,
  );
  $yearly_on_day_bymonthday_of_bymonth['BYMONTH'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Bymonth', array(), array(
      'context' => 'Date repeat',
    )),
    '#title_display' => 'invisible',
    '#default_value' => !empty($rrule['BYMONTH']) && $rrule['FREQ'] === 'YEARLY' && $yearly_day_month_default === 'BYMONTHDAY_BYMONTH' ? $rrule['BYMONTH'] : array(),
    '#options' => date_month_names_abbr(TRUE),
    '#attributes' => array(
      'class' => array(
        'container-inline',
      ),
    ),
    '#multiple' => TRUE,
    '#prefix' => '<div class="date-clear bymonth">',
    '#suffix' => '</div>',
  );
  $yearly_on_the_byday_of_bymonth = array(
    '#type' => 'container',
    '#tree' => TRUE,
  );
  $yearly_byday_count = '';
  $yearly_byday_day = '';
  if (isset($rrule['BYDAY']) && !empty($rrule['BYDAY']) && $rrule['FREQ'] === 'YEARLY') {
    $yearly_byday_count = substr($rrule['BYDAY'][0], 0, -2);
    $yearly_byday_day = substr($rrule['BYDAY'][0], -2);
  }
  list($byday_count_title, $byday_day_title) = explode('@byday', t('On the @byday of', array(), array(
    'context' => 'Date repeat',
  )));
  $yearly_on_the_byday_of_bymonth['BYDAY_COUNT'] = array(
    '#type' => 'select',
    '#title' => $byday_count_title,
    '#default_value' => !empty($yearly_byday_count) ? $yearly_byday_count : '',
    '#options' => date_order_translated(),
    '#multiple' => FALSE,
    '#prefix' => '<div class="date-repeat-input byday-count">',
    '#suffix' => '</div>',
  );
  $yearly_on_the_byday_of_bymonth['BYDAY_DAY'] = array(
    '#type' => 'select',
    '#title' => $byday_day_title,
    '#title_display' => 'after',
    '#default_value' => !empty($yearly_byday_day) ? $yearly_byday_day : '',
    '#options' => date_repeat_dow_day_options(TRUE),
    '#multiple' => FALSE,
    '#prefix' => '<div class="date-repeat-input byday-day">',
    '#suffix' => '</div>',
  );
  $yearly_on_the_byday_of_bymonth['BYMONTH'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Bymonth', array(), array(
      'context' => 'Date repeat',
    )),
    '#title_display' => 'invisible',
    '#default_value' => !empty($rrule['BYMONTH']) && $rrule['FREQ'] === 'YEARLY' && $yearly_day_month_default === 'BYDAY_BYMONTH' ? $rrule['BYMONTH'] : array(),
    '#options' => date_month_names_abbr(TRUE),
    '#attributes' => array(
      'class' => array(
        'container-inline',
      ),
    ),
    '#multiple' => TRUE,
    '#prefix' => '<div class="date-clear bymonth">',
    '#suffix' => '</div>',
  );
  $element['yearly']['day_month'] = array(
    '#type' => 'date_repeat_form_element_radios',
    '#tree' => TRUE,
    '#prefix' => '<div class="date-clear">',
    '#suffix' => '</div>',
    '#states' => array(
      'visible' => array(
        ":input[name=\"{$element['#name']}[FREQ]\"]" => array(
          'value' => 'YEARLY',
        ),
      ),
    ),
    '#attributes' => array(
      'class' => array(
        'date-repeat-radios clearfix',
      ),
    ),
    '#default_value' => $yearly_day_month_default,
    '#options' => array(
      'BYMONTHDAY_BYMONTH' => t('On day ... of ...'),
      'BYDAY_BYMONTH' => t('On the ... of ...'),
    ),
    'BYMONTHDAY_BYMONTH_child' => $yearly_on_day_bymonthday_of_bymonth,
    'BYDAY_BYMONTH_child' => $yearly_on_the_byday_of_bymonth,
    '#div_classes' => array(
      'date-repeat-radios-item date-clear clearfix bymonthday-bymonth',
      'date-repeat-radios-item date-clear clearfix byday-bymonth',
    ),
  );
  list($prefix, $suffix) = explode('@count', t('After @count occurrences', array(), array(
    'context' => 'Date repeat',
  )));
  $count_form_element = array(
    '#type' => 'textfield',
    '#title' => t('Count', array(), array(
      'context' => 'Date repeat',
    )),
    '#default_value' => $count,
    '#element_validate' => array(
      'element_validate_integer_positive',
    ),
    '#attributes' => array(
      'placeholder' => array(
        '#',
      ),
    ),
    '#prefix' => $prefix,
    '#suffix' => $suffix,
    '#size' => 10,
    '#maxlength' => 10,
  );
  $until_form_element = array(
    '#type' => 'container',
    '#tree' => TRUE,
    '#prefix' => '<div class="date-prefix-inline">' . t('On', array(), array(
      'context' => 'Date repeat',
    )) . '</div>',
    'datetime' => array(
      '#type' => $element['#date_repeat_widget'],
      '#title' => t('Until', array(), array(
        'context' => 'Date repeat',
      )),
      '#title_display' => 'invisible',
      '#default_value' => $until,
      '#date_format' => !empty($element['#date_format']) ? date_limit_format($element['#date_format'], array(
        'year',
        'month',
        'day',
      )) : 'Y-m-d',
      '#date_timezone' => $timezone,
      '#date_text_parts' => !empty($element['#date_text_parts']) ? $element['#date_text_parts'] : array(),
      '#date_year_range' => !empty($element['#date_year_range']) ? $element['#date_year_range'] : '-3:+3',
      '#date_label_position' => !empty($element['#date_label_position']) ? $element['#date_label_position'] : 'within',
      '#date_flexible' => 0,
    ),
    'tz' => array(
      '#type' => 'hidden',
      '#value' => $element['#date_timezone'],
    ),
    'all_day' => array(
      '#type' => 'hidden',
      '#value' => 1,
    ),
    'granularity' => array(
      '#type' => 'hidden',
      '#value' => serialize(array(
        'year',
        'month',
        'day',
      )),
    ),
  );
  $range_of_repeat_default = 'COUNT';
  if (!empty($until)) {
    $range_of_repeat_default = 'UNTIL';
  }
  $element['range_of_repeat'] = array(
    '#type' => 'date_repeat_form_element_radios',
    '#tree' => TRUE,
    '#title' => t('Stop repeating', array(), array(
      'context' => 'Date repeat',
    )),
    '#title_display' => 'before',
    '#prefix' => '<div class="date-clear range-of-repeat">',
    '#suffix' => '</div>',
    '#states' => array(
      'invisible' => array(
        ":input[name=\"{$element['#name']}[FREQ]\"]" => array(
          'value' => 'NONE',
        ),
      ),
    ),
    '#default_value' => $range_of_repeat_default,
    '#options' => array(
      'COUNT' => t('Count'),
      'UNTIL' => t('Until'),
    ),
    'count_child' => $count_form_element,
    'until_child' => $until_form_element,
    '#div_classes' => array(
      'container-inline count',
      "until widget-{$element['#date_repeat_widget']} label-{$element['#date_label_position']}",
    ),
  );
  $parents = $element['#array_parents'];
  $instance = implode('-', $parents);

  // Make sure this will work right either in the normal form or in an AJAX
  // callback from the 'Add more' button.
  if (empty($form_state['num_exceptions'][$instance])) {
    $form_state['num_exceptions'][$instance] = count($exceptions);
  }
  if ($form_state['num_exceptions'][$instance] == 0) {
    $collapsed = TRUE;
  }
  else {
    $collapsed = FALSE;
  }
  $element['show_exceptions'] = array(
    '#type' => 'checkbox',
    '#title' => t('Exclude dates', array(), array(
      'context' => 'Date repeat',
    )),
    '#states' => array(
      'invisible' => array(
        ":input[name=\"{$element['#name']}[FREQ]\"]" => array(
          'value' => 'NONE',
        ),
      ),
    ),
    '#default_value' => empty($form_state['num_exceptions'][$instance]) ? 0 : 1,
  );
  $element['exceptions'] = array(
    '#type' => 'container',
    '#prefix' => '<div id="date-repeat-exceptions-' . $instance . '" class="date-repeat">',
    '#suffix' => '</div>',
    '#states' => array(
      'visible' => array(
        ":input[name=\"{$element['#name']}[show_exceptions]\"]" => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  for ($i = 0; $i < max($form_state['num_exceptions'][$instance], 1); $i++) {
    $except = '';
    if (!empty($exceptions[$i]['datetime'])) {
      $ex_date = new DateObject($exceptions[$i]['datetime'], $exceptions[$i]['tz']);
      date_timezone_set($ex_date, timezone_open($timezone));
      $except = date_format($ex_date, DATE_FORMAT_DATETIME);
    }
    $date_format = 'Y-m-d';
    if (!empty($element['#date_format'])) {
      $grans = array(
        'year',
        'month',
        'day',
      );
      $date_format = date_limit_format($element['#date_format'], $grans);
    }
    $element['exceptions']['EXDATE'][$i] = array(
      '#tree' => TRUE,
      'datetime' => array(
        '#name' => 'exceptions|' . $instance,
        '#type' => $element['#date_repeat_widget'],
        '#default_value' => $except,
        '#date_timezone' => !empty($element['#date_timezone']) ? $element['#date_timezone'] : date_default_timezone(),
        '#date_format' => $date_format,
        '#date_text_parts' => !empty($element['#date_text_parts']) ? $element['#date_text_parts'] : array(),
        '#date_year_range' => !empty($element['#date_year_range']) ? $element['#date_year_range'] : '-3:+3',
        '#date_label_position' => !empty($element['#date_label_position']) ? $element['#date_label_position'] : 'within',
        '#date_flexible' => 0,
      ),
      'tz' => array(
        '#type' => 'hidden',
        '#value' => $element['#date_timezone'],
      ),
      'all_day' => array(
        '#type' => 'hidden',
        '#value' => 1,
      ),
      'granularity' => array(
        '#type' => 'hidden',
        '#value' => serialize(array(
          'year',
          'month',
          'day',
        )),
      ),
    );
  }

  // Collect additions in the same way as exceptions - implements RDATE.
  if (empty($form_state['num_additions'][$instance])) {
    $form_state['num_additions'][$instance] = count($additions);
  }
  if ($form_state['num_additions'][$instance] == 0) {
    $collapsed = TRUE;
  }
  else {
    $collapsed = FALSE;
  }
  $element['show_additions'] = array(
    '#type' => 'checkbox',
    '#title' => t('Include dates', array(), array(
      'context' => 'Date repeat',
    )),
    '#states' => array(
      'invisible' => array(
        ":input[name=\"{$element['#name']}[FREQ]\"]" => array(
          'value' => 'NONE',
        ),
      ),
    ),
    '#default_value' => empty($form_state['num_additions'][$instance]) ? 0 : 1,
  );
  $element['additions'] = array(
    '#type' => 'container',
    '#prefix' => '<div id="date-repeat-additions-' . $instance . '" class="date-repeat">',
    '#suffix' => '</div>',
    '#states' => array(
      'visible' => array(
        ":input[name=\"{$element['#name']}[show_additions]\"]" => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  for ($i = 0; $i < max($form_state['num_additions'][$instance], 1); $i++) {
    $r_date = '';
    if (!empty($additions[$i]['datetime'])) {
      $rdate = new DateObject($additions[$i]['datetime'], $additions[$i]['tz']);
      date_timezone_set($rdate, timezone_open($timezone));
      $r_date = date_format($rdate, DATE_FORMAT_DATETIME);
    }
    $date_format = 'Y-m-d';
    if (!empty($element['#date_format'])) {
      $grans = array(
        'year',
        'month',
        'day',
      );
      $date_format = date_limit_format($element['#date_format'], $grans);
    }
    $element['additions']['RDATE'][$i] = array(
      '#tree' => TRUE,
      'datetime' => array(
        '#type' => $element['#date_repeat_widget'],
        '#name' => 'additions|' . $instance,
        '#default_value' => $r_date,
        '#date_timezone' => !empty($element['#date_timezone']) ? $element['#date_timezone'] : date_default_timezone(),
        '#date_format' => $date_format,
        '#date_text_parts' => !empty($element['#date_text_parts']) ? $element['#date_text_parts'] : array(),
        '#date_year_range' => !empty($element['#date_year_range']) ? $element['#date_year_range'] : '-3:+3',
        '#date_label_position' => !empty($element['#date_label_position']) ? $element['#date_label_position'] : 'within',
        '#date_flexible' => 0,
      ),
      'tz' => array(
        '#type' => 'hidden',
        '#value' => $element['#date_timezone'],
      ),
      'all_day' => array(
        '#type' => 'hidden',
        '#value' => 1,
      ),
      'granularity' => array(
        '#type' => 'hidden',
        '#value' => serialize(array(
          'year',
          'month',
          'day',
        )),
      ),
    );
  }
  $element['exceptions']['exceptions_add'] = array(
    '#type' => 'submit',
    '#name' => 'exceptions_add|' . $instance,
    '#value' => t('Add exception'),
    '#submit' => array(
      'date_repeat_add_exception',
    ),
    '#limit_validation_errors' => array(),
    '#ajax' => array(
      'callback' => 'date_repeat_add_exception_callback',
      'wrapper' => 'date-repeat-exceptions-' . $instance,
    ),
  );
  $element['additions']['additions_add'] = array(
    '#type' => 'submit',
    '#name' => 'additions_add|' . $instance,
    '#value' => t('Add addition'),
    '#submit' => array(
      'date_repeat_add_addition',
    ),
    '#limit_validation_errors' => array(),
    '#ajax' => array(
      'callback' => 'date_repeat_add_addition_callback',
      'wrapper' => 'date-repeat-additions-' . $instance,
    ),
  );
  $element['#date_repeat_collapsed'] = !empty($rrule['INTERVAL']) || !empty($rrule['FREQ']) ? 0 : (!empty($element['#date_repeat_collapsed']) ? $element['#date_repeat_collapsed'] : 0);
  return $element;
}