You are here

function date_api_ical_build_rrule in Date 7.2

Same name and namespace in other branches
  1. 5.2 date_api_ical.inc \date_api_ical_build_rrule()
  2. 6.2 date_api_ical.inc \date_api_ical_build_rrule()
  3. 6 date_api_ical.inc \date_api_ical_build_rrule()
  4. 7.3 date_api/date_api_ical.inc \date_api_ical_build_rrule()
  5. 7 date_api/date_api_ical.inc \date_api_ical_build_rrule()

Build an iCal RULE from $form_values.

Parameters

array $form_values: An array constructed like the one created by date_ical_parse_rrule(). [RRULE] => Array ( [FREQ] => Array ( [0] => MONTHLY ) [BYDAY] => Array ( [0] => 1SU [1] => -1SU ) [UNTIL] => Array ( [datetime] => 1997-21-31 09:00:00 [all_day] => 0 [tz] => US/Eastern ) ) [EXDATE] => Array ( [0] = Array ( [datetime] => 1997-09-21 09:00:00 [all_day] => 0 [tz] => US/Eastern ) [1] = Array ( [datetime] => 1997-10-05 09:00:00 [all_day] => 0 [tz] => US/Eastern ) ) [RDATE] => Array ( [0] = Array ( [datetime] => 1997-09-21 09:00:00 [all_day] => 0 [tz] => US/Eastern ) [1] = Array ( [datetime] => 1997-10-05 09:00:00 [all_day] => 0 [tz] => US/Eastern ) )

4 calls to date_api_ical_build_rrule()
date_repeat_build_dates in date_repeat_field/date_repeat_field.module
Helper function to build repeating dates from a $node_field.
date_repeat_field_date_field_insert in date_repeat_field/date_repeat_field.devel_generate.inc
Implements hook_date_field_insert().
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_api/date_api_ical.inc, line 698
Parse iCal data.

Code

function date_api_ical_build_rrule(array $form_values) {
  $rrule = '';
  if (empty($form_values) || !is_array($form_values)) {
    return $rrule;
  }

  // Grab the RRULE data and put them into iCal RRULE format.
  $rrule .= 'RRULE:FREQ=' . (!array_key_exists('FREQ', $form_values) ? 'DAILY' : $form_values['FREQ']);
  $rrule .= ';INTERVAL=' . (!array_key_exists('INTERVAL', $form_values) ? 1 : $form_values['INTERVAL']);

  // Unset the empty 'All' values.
  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('BYDAY', $form_values) && is_array($form_values['BYDAY']) && ($byday = implode(",", $form_values['BYDAY']))) {
    $rrule .= ';BYDAY=' . $byday;
  }
  if (array_key_exists('BYMONTH', $form_values) && is_array($form_values['BYMONTH']) && ($bymonth = implode(",", $form_values['BYMONTH']))) {
    $rrule .= ';BYMONTH=' . $bymonth;
  }
  if (array_key_exists('BYMONTHDAY', $form_values) && is_array($form_values['BYMONTHDAY']) && ($bymonthday = implode(",", $form_values['BYMONTHDAY']))) {
    $rrule .= ';BYMONTHDAY=' . $bymonthday;
  }

  // The UNTIL date is supposed to always be expressed in UTC. The input date
  // values may already have been converted to a date object on a previous
  // pass, so check for that.
  if (array_key_exists('UNTIL', $form_values) && array_key_exists('datetime', $form_values['UNTIL']) && !empty($form_values['UNTIL']['datetime'])) {

    // We only collect a date for UNTIL, but we need it to be inclusive, so
    // force it to a full datetime element at the last second of the day.
    if (!is_object($form_values['UNTIL']['datetime'])) {

      // If this is a date without time, give it time.
      if (strlen($form_values['UNTIL']['datetime']) < 11) {
        $granularity_options = drupal_map_assoc(array(
          'year',
          'month',
          'day',
          'hour',
          'minute',
          'second',
        ));
        $form_values['UNTIL']['datetime'] .= ' 23:59:59';
        $form_values['UNTIL']['granularity'] = serialize($granularity_options);
        $form_values['UNTIL']['all_day'] = FALSE;
      }
      $until = date_ical_date($form_values['UNTIL'], 'UTC');
    }
    else {
      $until = $form_values['UNTIL']['datetime'];
    }
    $rrule .= ';UNTIL=' . date_format($until, DATE_FORMAT_ICAL) . 'Z';
  }

  // Our form doesn't allow a value for COUNT, but it may be needed by modules
  // using the API, so add it to the rule.
  if (array_key_exists('COUNT', $form_values)) {
    $rrule .= ';COUNT=' . $form_values['COUNT'];
  }

  // iCal rules presume the week starts on Monday unless otherwise specified,
  // so we'll specify it.
  if (array_key_exists('WKST', $form_values)) {
    $rrule .= ';WKST=' . $form_values['WKST'];
  }
  else {
    $rrule .= ';WKST=' . date_repeat_dow2day(variable_get('date_first_day', 0));
  }

  // Exceptions dates go last, on their own line. The input date values may
  // already have been converted to a date object on a previous pass, so check
  // for that.
  if (isset($form_values['EXDATE']) && is_array($form_values['EXDATE'])) {
    $ex_dates = array();
    foreach ($form_values['EXDATE'] as $value) {
      if (!empty($value['datetime'])) {
        $date = !is_object($value['datetime']) ? date_ical_date($value, 'UTC') : $value['datetime'];
        $ex_date = !empty($date) ? date_format($date, DATE_FORMAT_ICAL) . 'Z' : '';
        if (!empty($ex_date)) {
          $ex_dates[] = $ex_date;
        }
      }
    }
    if (!empty($ex_dates)) {
      sort($ex_dates);
      $rrule .= chr(13) . chr(10) . 'EXDATE:' . implode(',', $ex_dates);
    }
  }
  elseif (!empty($form_values['EXDATE'])) {
    $rrule .= chr(13) . chr(10) . 'EXDATE:' . $form_values['EXDATE'];
  }

  // Exceptions dates go last, on their own line.
  if (isset($form_values['RDATE']) && is_array($form_values['RDATE'])) {
    $ex_dates = array();
    foreach ($form_values['RDATE'] as $value) {
      $date = !is_object($value['datetime']) ? date_ical_date($value, 'UTC') : $value['datetime'];
      $ex_date = !empty($date) ? date_format($date, DATE_FORMAT_ICAL) . 'Z' : '';
      if (!empty($ex_date)) {
        $ex_dates[] = $ex_date;
      }
    }
    if (!empty($ex_dates)) {
      sort($ex_dates);
      $rrule .= chr(13) . chr(10) . 'RDATE:' . implode(',', $ex_dates);
    }
  }
  elseif (!empty($form_values['RDATE'])) {
    $rrule .= chr(13) . chr(10) . 'RDATE:' . $form_values['RDATE'];
  }
  return $rrule;
}