You are here

function date_ical_export in Date 6

Same name and namespace in other branches
  1. 5.2 date_api_ical.inc \date_ical_export()
  2. 5 date_api_ical.inc \date_ical_export()
  3. 5 date_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 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) '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 541
Parse iCal data.

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;TZID=" . date_default_timezone_name() . ";VALUE=DATE-TIME:" . date_format(date_now(), DATE_FORMAT_ICAL) . "\n";
      $timezone = timezone_name_get(date_timezone_get($event['start']));
      if (!empty($timezone)) {
        $timezone = "TZID={$timezone};";
      }
      else {
        $timezone = '';
      }
      if ($event['start'] && $event['end']) {
        $output .= "DTSTART;" . $timezone . "VALUE=DATE-TIME:" . date_format($event['start'], DATE_FORMAT_ICAL) . "\n";
        $output .= "DTEND;" . $timezone . "VALUE=DATE-TIME:" . date_format($event['end'], DATE_FORMAT_ICAL) . "\n";
      }
      else {
        $output .= "DTSTART;" . $timezone . "VALUE=DATE-TIME:" . date_format($event['start'], DATE_FORMAT_ICAL) . "\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;
}