You are here

function date_ical_export in Date 5

Same name in this branch
  1. 5 date_api_ical.inc \date_ical_export()
  2. 5 date_ical.inc \date_ical_export()
Same name and namespace in other branches
  1. 5.2 date_api_ical.inc \date_ical_export()
  2. 6 date_api_ical.inc \date_ical_export()

Turn an array of events into a valid iCalendar file

@todo add folding and more ical elements

Parameters

$events: An array of events where each event is an array with: 'start' => local start date as unix timestamp, omit time for all day event. 'end' => local end date as unix timestamp, optional, omit for all day event. 'timezone' => name of the timezone for the event, blank for stateless events. 'summary' => Title of event (Text) 'description' => Description of event (Text) 'location' => Location of event (Text) 'uid' => ID of the event for use by calendaring program. Recommend the url of the node 'url' => URL of event information

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

Return value

Text of a date_icalendar file.

File

./date_api_ical.inc, line 35
Parse iCal imports and create iCal exports. This file must be included when these functions are needed.

Code

function date_ical_export($events, $calname = NULL) {
  $output .= "BEGIN:VCALENDAR\nVERSION:2.0\n";
  $output .= "METHOD:PUBLISH\n";
  $output .= 'X-WR-CALNAME:' . date_ical_escape_text($calname ? $calname : variable_get('site_name', '')) . "\n";
  $output .= "PRODID:-//Drupal iCal API//EN\n";
  foreach ($events as $uid => $event) {

    // Skip any items with empty dates.
    if (!empty($event['start'])) {
      $output .= "BEGIN:VEVENT\n";
      $output .= "DTSTAMP;VALUE=DATE-TIME:" . gmdate("Ymd\\THis\\Z", time()) . "\n";
      $tz_append = '';
      if ($event['timezone'] == 'GMT' || $event['timezone'] == 'UTC') {
        $tz_append = 'Z';
      }
      if (empty($event['timezone']) || $tz_append) {
        $output .= "DTSTART;VALUE=DATE-TIME:" . gmdate("Ymd\\THis", $event['start']) . $tz_append . "\n";
      }
      else {
        $output .= "DTSTART;TZID=" . $event['timezone'] . ":" . gmdate("Ymd\\THis", $event['start']) . "\n";
      }
      if ($event['start'] && $event['end']) {
        if (empty($event['timezone']) || $tz_append) {
          $output .= "DTEND;VALUE=DATE-TIME:" . gmdate("Ymd\\THis", $event['end']) . $tz_append . "\n";
        }
        else {
          $output .= "DTEND;TZID=" . $event['timezone'] . ":" . gmdate("Ymd\\THis", $event['end']) . "\n";
        }
      }
      $output .= "UID:" . ($event['uid'] ? $event['uid'] : $uid) . "\n";
      if ($event['url']) {
        $output .= "URL;VALUE=URI:" . $event['url'] . "\n";
      }
      if ($event['location']) {
        $output .= "LOCATION:" . date_ical_escape_text($event['location']) . "\n";
      }
      $output .= "SUMMARY:" . date_ical_escape_text($event['summary']) . "\n";
      if ($event['description']) {
        $output .= "DESCRIPTION:" . date_ical_escape_text($event['description']) . "\n";
      }
      $output .= "END:VEVENT\n";
    }
  }
  $output .= "END:VCALENDAR\n";
  return $output;
}