You are here

public static function DateiCalParse::build_rrule in Date 8

The reverse of the parse_rrule() function.

Build a string RRULE from the array structure created by parsing a RRule.

Parameters

array $ical_array: An array constructed like the one created by 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 ) )

2 calls to DateiCalParse::build_rrule()
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/lib/Drupal/date_api/DateiCalParse.php, line 766
Parse iCal data.

Class

DateiCalParse
Return an array of iCalendar information from an iCalendar file.

Namespace

Drupal\date_api

Code

public static function build_rrule($ical_array) {
  $RRULE = '';
  if (empty($ical_array) || !is_array($ical_array)) {
    return $RRULE;
  }

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

  // Unset the empty 'All' values.
  if (array_key_exists('BYDAY', $ical_array) && is_array($ical_array['BYDAY'])) {
    unset($ical_array['BYDAY']['']);
  }
  if (array_key_exists('BYMONTH', $ical_array) && is_array($ical_array['BYMONTH'])) {
    unset($ical_array['BYMONTH']['']);
  }
  if (array_key_exists('BYMONTHDAY', $ical_array) && is_array($ical_array['BYMONTHDAY'])) {
    unset($ical_array['BYMONTHDAY']['']);
  }
  if (array_key_exists('BYDAY', $ical_array) && is_array($ical_array['BYDAY']) && ($BYDAY = implode(",", $ical_array['BYDAY']))) {
    $RRULE .= ';BYDAY=' . $BYDAY;
  }
  if (array_key_exists('BYMONTH', $ical_array) && is_array($ical_array['BYMONTH']) && ($BYMONTH = implode(",", $ical_array['BYMONTH']))) {
    $RRULE .= ';BYMONTH=' . $BYMONTH;
  }
  if (array_key_exists('BYMONTHDAY', $ical_array) && is_array($ical_array['BYMONTHDAY']) && ($BYMONTHDAY = implode(",", $ical_array['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', $ical_array) && array_key_exists('datetime', $ical_array['UNTIL']) && !empty($ical_array['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 (!$ical_array['UNTIL']['datetime'] instanceof DrupalDateTime) {

      // If this is a date without time, give it time.
      if (strlen($ical_array['UNTIL']['datetime']) < 11) {
        $ical_array['UNTIL']['datetime'] .= ' 23:59:59';
        $ical_array['UNTIL']['granularity'] = serialize(drupal_map_assoc(array(
          'year',
          'month',
          'day',
          'hour',
          'minute',
          'second',
        )));
        $ical_array['UNTIL']['all_day'] = FALSE;
      }
      $until = self::ical_date($ical_array['UNTIL'], 'UTC');
    }
    else {
      $until = $ical_array['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', $ical_array)) {
    $RRULE .= ';COUNT=' . $ical_array['COUNT'];
  }

  // iCal rules presume the week starts on Monday unless otherwise
  // specified, so we'll specify it.
  if (array_key_exists('WKST', $ical_array)) {
    $RRULE .= ';WKST=' . $ical_array['WKST'];
  }
  else {
    $RRULE .= ';WKST=' . self::$week_start_day;
  }

  // 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($ical_array['EXDATE']) && is_array($ical_array['EXDATE'])) {
    $ex_dates = array();
    foreach ($ical_array['EXDATE'] as $value) {
      if (!empty($value['datetime'])) {
        $date = !$value['datetime'] instanceof DrupalDateTime ? self::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($ical_array['EXDATE'])) {
    $RRULE .= chr(13) . chr(10) . 'EXDATE:' . $ical_array['EXDATE'];
  }

  // Exceptions dates go last, on their own line.
  if (isset($ical_array['RDATE']) && is_array($ical_array['RDATE'])) {
    $ex_dates = array();
    foreach ($ical_array['RDATE'] as $value) {
      $date = !$value['datetime'] instanceof DrupalDateTime ? self::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($ical_array['RDATE'])) {
    $RRULE .= chr(13) . chr(10) . 'RDATE:' . $ical_array['RDATE'];
  }
  return $RRULE;
}