You are here

function date_api_ical_build_rrule in Date 7

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.2 date_api/date_api_ical.inc \date_api_ical_build_rrule()

Build an iCal RULE from $form_values.

Parameters

$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_devel_generate in ./date.devel_generate.inc
Implementation of Devel module's hook_content_generate().
date_repeat_build_dates in ./date_repeat.inc
Helper function to build repeating dates from a $node_field.
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 663
Parse iCal data.

Code

function date_api_ical_build_rrule($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)) {
    unset($form_values['BYDAY']['']);
  }
  if (array_key_exists('BYMONTH', $form_values)) {
    unset($form_values['BYMONTH']['']);
  }
  if (array_key_exists('BYMONTHDAY', $form_values)) {
    unset($form_values['BYMONTHDAY']['']);
  }
  if (array_key_exists('BYDAY', $form_values) && ($BYDAY = implode(",", $form_values['BYDAY']))) {
    $RRULE .= ';BYDAY=' . $BYDAY;
  }
  if (array_key_exists('BYMONTH', $form_values) && ($BYMONTH = implode(",", $form_values['BYMONTH']))) {
    $RRULE .= ';BYMONTH=' . $BYMONTH;
  }
  if (array_key_exists('BYMONTHDAY', $form_values) && ($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'])) {
    $until = !is_object($form_values['UNTIL']['datetime']) ? date_ical_date($form_values['UNTIL'], 'UTC') : $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', 1));
  }

  // 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_FORMAT_ICAL) : '';
        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_FORMAT_ICAL) : '';
      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;
}