You are here

function template_preprocess_date_vcalendar in Date 6.2

Same name and namespace in other branches
  1. 7 date_views/theme/theme.inc \template_preprocess_date_vcalendar()

Preprocessor to construct an ical vcalendar

Parameters

$events: An array of events where each event is an array keyed on the uid: 'start' Start date object, 'end' End date object, optional, omit for all day event. 'summary' Title of event (Text) 'description' Description of event (Text) 'location' Location of event (Text or vvenue id) 'uid' ID of the event for use by calendaring program, usually the url of the node 'url' URL of event information

'alarm' sub-array of alarm information for the event, including:

  • 'action' - the action to take, either 'DISPLAY' or 'EMAIL'
  • 'trigger' - the time period for the trigger, like -P2D.
  • 'repeat' - the number of times to repeat the alarm.
  • 'duration' - the time period between repeated alarms, like P1D.
  • 'description' - the description of the alarm.

An email alarm should have two additional parts:

  • 'email' - a comma-separated list of email recipients.
  • 'summary' - the subject of the alarm email.

$calname: Name of the calendar. Use site name if none is specified.

File

theme/theme.inc, line 173
Theme functions for Date.

Code

function template_preprocess_date_vcalendar(&$vars) {
  $vars['current_date'] = date_format(date_now(), DATE_FORMAT_ICAL);
  $vars['current_date_utc'] = date_format(date_now('UTC'), DATE_FORMAT_ICAL);
  $vars['site_timezone'] = date_default_timezone_name();
  $vars['calname'] = date_ical_escape_text(!empty($vars['calname']) ? $vars['calname'] : variable_get('site_name', ''));

  // Format the event results as iCal expects.
  $events_in = $vars['events'];
  $events = array();
  $rows = $vars['rows'];
  foreach ($events_in as $uid => $event) {
    $row = array_shift($rows);

    // Omit any items with empty dates.
    if (!empty($event['start'])) {
      $events[$uid] = $event;
      $timezone = timezone_name_get(date_timezone_get($event['start']));
      if (!empty($timezone)) {
        $events[$uid]['timezone'] = "TZID={$timezone};";
      }
      else {
        $events[$uid]['timezone'] = '';
      }
      $date_format = $row->calendar_all_day == TRUE ? DATE_FORMAT_ICAL_DATE : DATE_FORMAT_ICAL;
      $events[$uid]['start'] = date_format($event['start'], $date_format);

      // According to RFC 2445 (clarified in RFC 5545) the DTEND value is
      // non-inclusive.  When it is a DATE rather than a DATETIME, this means
      // that we should add one day to its value.
      if ($row->calendar_all_day) {
        if (empty($event['end'])) {
          $event['end'] = $event['start'];
        }
        date_modify($event['end'], "+1 day");
      }
      if ($event['start'] && $event['end']) {
        $events[$uid]['end'] = date_format($event['end'], $date_format);
      }
      else {
        $events[$uid]['end'] = $events[$uid]['start'];
      }
      foreach ($event as $key => $value) {
        if (is_string($value)) {
          $event[trim($key)] = trim($value);
        }
      }

      // Escape text values.
      foreach ($event as $key => $value) {
        if ($key == 'alarm') {
          foreach ($value as $alarm_key => $alarm_value) {
            if (in_array($alarm_key, array(
              'summary',
              'description',
            ))) {
              $events[$uid]['alarm'][$alarm_key] = date_ical_escape_text($alarm_value);
            }
          }
        }
        elseif (in_array($key, array(
          'summary',
          'description',
          'location',
        ))) {
          $events[$uid][$key] = date_ical_escape_text(html_entity_decode($value, ENT_QUOTES, 'UTF-8'));
        }
      }
    }
  }
  $vars['events'] = $events;
}