You are here

function ical_export in Event 5.2

Same name and namespace in other branches
  1. 5 ical.inc \ical_export()

Turn an array of events into a valid iCalendar file

Parameters

$events: An array of associative arrays where 'start' => Unix timestamp (GMT) of start time (Required, if no allday_start) 'end' => Unix timestamp (GMT) of end time (Optional) 'allday_start' => Start date of all-day event in YYYYMMDD format (Required, if no start) 'allday_end' => End date of all-day event in YYYYMMDD format (Optional) '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 iCalendar file

2 calls to ical_export()
event_calendar_ical in ./event.module
Creates an ical feed of events.
event_node_ical in ./event.module
Return an ical for a specific event

File

./ical.inc, line 32
API for event import/export in iCalendar format as outlined in Internet Calendaring and Scheduling Core Object Specification http://www.ietf.org/rfc/rfc2445.txt

Code

function ical_export($events, $calname = NULL) {
  $output = "BEGIN:VCALENDAR\nVERSION:2.0\n";
  $output .= "METHOD:PUBLISH\n";
  $output .= 'X-WR-CALNAME:' . variable_get('site_name', '') . ' | ' . ical_escape_text($calname) . "\n";
  $output .= "PRODID:-//strange bird labs//Drupal iCal API//EN\n";
  foreach ($events as $uid => $event) {
    $output .= "BEGIN:VEVENT\n";
    $output .= "DTSTAMP;VALUE=DATE-TIME:" . gmdate("Ymd\\THis\\Z", time()) . "\n";
    if (!$event['has_time']) {

      // all day event
      $output .= "DTSTART;VALUE=DATE-TIME:" . event_format_date($event['start_utc'], 'custom', "Ymd\\THis\\Z") . "\n";

      //If allday event, set to day after allday start
      $end_date = event_date_later($event['start'], 1);
      $output .= "DTEND;VALUE=DATE-TIME:" . event_format_date($end_date, 'custom', 'Ymd') . "\n";
    }
    else {
      if (!empty($event['start_utc']) && !empty($event['end_utc'])) {
        $output .= "DTSTART;VALUE=DATE-TIME:" . event_format_date($event['start_utc'], 'custom', "Ymd\\THis\\Z") . "\n";
        $output .= "DTEND;VALUE=DATE-TIME:" . event_format_date($event['end_utc'], 'custom', "Ymd\\THis\\Z") . "\n";
      }
      else {
        if (!empty($event['start_utc'])) {
          $output .= "DTSTART;VALUE=DATE-TIME:" . event_format_date($event['start_utc'], 'custom', "Ymd\\THis\\Z") . "\n";
        }
      }
    }
    $output .= "UID:" . ($event['uid'] ? $event['uid'] : $uid) . "\n";
    if (!empty($event['url'])) {
      $output .= "URL;VALUE=URI:" . $event['url'] . "\n";
    }
    if (!empty($event['location'])) {
      $output .= "LOCATION:" . ical_escape_text($event['location']) . "\n";
    }
    $output .= "SUMMARY:" . ical_escape_text($event['summary']) . "\n";
    if (!empty($event['description'])) {
      $output .= "DESCRIPTION:" . ical_escape_text($event['description']) . "\n";
    }
    $output .= "END:VEVENT\n";
  }
  $output .= "END:VCALENDAR\n";
  return $output;
}