You are here

function _date_repeat_rrule_process in Date 6

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. 7.3 date_repeat/date_repeat_form.inc \_date_repeat_rrule_process()
  5. 7 date_repeat/date_repeat_form.inc \_date_repeat_rrule_process()
  6. 7.2 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 36
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_rrule_process($element, $edit, $form_state, $form) {
  include_once './' . drupal_get_path('module', 'date_api') . '/date_api_ical.inc';
  if (is_array($element['#value'])) {
    $element['#value'] = date_repeat_merge($element['#value']);
    $rrule = date_api_ical_build_rrule($element['#value']);
  }
  else {
    $rrule = $element['#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];
  $element['#type'] = 'fieldset';
  $element['#title'] = t('Repeat');
  $element['#description'] = t('Choose a frequency and period to repeat this date. If nothing is selected, the date will not repeat.');
  $element['#collapsible'] = TRUE;
  $element['#collapsed'] = FALSE;
  $element['INTERVAL'] = array(
    '#type' => 'select',
    //'#title' => t('Interval'),
    '#default_value' => !empty($rrule['INTERVAL']) ? $rrule['INTERVAL'] : 0,
    '#options' => INTERVAL_options(),
    '#prefix' => '<div class="date-repeat-input">',
    '#suffix' => '</div>',
  );
  $element['FREQ'] = array(
    '#type' => 'select',
    //'#title' => t('Frequency'),
    '#default_value' => !empty($rrule['FREQ']) ? $rrule['FREQ'] : 'NONE',
    '#options' => FREQ_options(),
    '#prefix' => '<div class="date-repeat-input">',
    '#suffix' => '</div>',
  );
  $UNTIL = '';
  if (!empty($rrule['UNTIL']['datetime'])) {
    if (!is_array(!empty($rrule['UNTIL']['datetime']))) {
      $UNTIL = $rrule['UNTIL']['datetime'];
    }
    else {
      $UNTIL = $rrule['UNTIL']['datetime']['date'];
    }
  }
  $element['UNTIL'] = array(
    '#tree' => TRUE,
    'datetime' => array(
      '#type' => module_exists('date_popup') ? 'date_popup' : 'date_select',
      '#title' => t('Until'),
      '#description' => t('Date to stop repeating this item.'),
      '#default_value' => $UNTIL,
      '#date_timezone' => $element['#date_timezone'],
      '#date_format' => 'Y-m-d',
      '#date_label_position' => 'within',
    ),
    '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',
      )),
    ),
  );
  $collapsed = TRUE;
  $merged_values = date_repeat_merge($rrule);
  if (!empty($merged_values['BYDAY']) || !empty($merged_values['BYMONTH'])) {
    $collapsed = FALSE;
  }

  // start the advanced fieldset
  $element['advanced'] = array(
    '#type' => 'fieldset',
    '#title' => t('Advanced'),
    '#collapsible' => TRUE,
    '#collapsed' => $collapsed,
    '#description' => t('If no advanced options are selected, the date will repeat on the day of week of the start date for weekly repeats, otherwise on the month and day of the start date. Use the options below to override that behavior to select specific months and days to repeat on. Use the \'Except\' box to input dates that should be omitted from the results. '),
  );
  $element['advanced']['BYMONTH'] = array(
    '#type' => 'select',
    '#title' => t('Month'),
    '#default_value' => !empty($rrule['BYMONTH']) ? $rrule['BYMONTH'] : '',
    '#options' => array(
      '' => t('-- Any'),
    ) + date_month_names(TRUE),
    '#multiple' => TRUE,
    '#size' => 10,
    '#prefix' => '<div class="date-repeat-input">',
    '#suffix' => '</div>',
  );
  $element['advanced']['BYMONTHDAY'] = array(
    '#type' => 'select',
    '#title' => t('Day of Month'),
    '#default_value' => !empty($rrule['BYMONTHDAY']) ? $rrule['BYMONTHDAY'] : '',
    '#options' => array(
      '' => t('-- Any'),
    ) + drupal_map_assoc(range(1, 31)) + drupal_map_assoc(range(-1, -31)),
    '#multiple' => TRUE,
    '#size' => 10,
    '#prefix' => '<div class="date-repeat-input">',
    '#suffix' => '</div>',
  );
  $element['advanced']['BYDAY'] = array(
    '#type' => 'select',
    '#title' => t('Day of Week'),
    '#default_value' => !empty($rrule['BYDAY']) ? $rrule['BYDAY'] : '',
    '#options' => array(
      '' => t('-- Any'),
    ) + date_repeat_dow_options(),
    //'#attributes' => array('size' => '5'),
    '#multiple' => TRUE,
    '#size' => 10,
    '#prefix' => '<div class="date-repeat-input">',
    '#suffix' => '</div>',
  );
  $element['exceptions'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapased' => empty($exceptions) ? TRUE : FALSE,
    '#title' => t('Except'),
    '#description' => t('Dates to omit from the list of repeating dates.'),
    '#prefix' => '<div class="date-repeat">',
    '#suffix' => '</div>',
  );
  $max = !empty($exceptions) ? sizeof($exceptions) : 0;
  for ($i = 0; $i <= $max; $i++) {
    $EXCEPT = '';
    if (!empty($exceptions[$i]['datetime'])) {
      if (!is_array(!empty($exceptions[$i]['datetime']))) {
        $EXCEPT = $exceptions[$i]['datetime'];
      }
      else {
        $EXCEPT = $exceptions[$i]['datetime']['date'];
      }
    }
    $element['exceptions']['EXDATE'][$i] = array(
      '#tree' => TRUE,
      'datetime' => array(
        '#type' => module_exists('date_popup') ? 'date_popup' : 'date_select',
        '#default_value' => $EXCEPT,
        '#date_timezone' => $element['#date_timezone'],
        '#date_format' => 'Y-m-d',
        '#date_label_position' => 'within',
      ),
      '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['help'] = array(
    '#type' => 'fieldset',
    '#title' => t('Help'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#description' => t('<p>Dates will match any selection within the same box [January OR June]. When more than one box has criteria selected, all of them are combined to create repeats [[January OR June] AND [Day 1 OR Day 15]]. Positive numbers count from the beginning of the period. Negative numbers count from the end of the period, i.e. -1 is the last, -2 is the next to last, etc.</p><ul><li>If you select \'Every Year\' above, and \'March\' from \'Month\' and \'1\' and \'15\' from \'Day of Month\' in the Advanced options you will be selecting the 1st and 15th days of March of every year.</li><li>If you select \'Every other Month\' above, and \'Second Tuesday\' in the Advanced options you will be selecting the second Tuesday of every other month.</li><li>If you select \'Every Year\' above, and \'Second Tuesday\' in the Advanced options you will be selecting the second Tuesday of every year.</li><li>If you select \'Every Month\' above, and \'January\' and \'June\' and \'First Saturday\' in the Advanced options, you will be selecting the first Saturday in January or June.</li><li>If you select \'Every Month\' above, and \'-1\' from \'Day of Month\' in the Advanced options you will be selecting the last day of every month.</li></ul>'),
  );
  return $element;
}