/**
  * 	download_ics_file
  *
  *  @access 	public
  *  @return 	void
  */
 public static function download_ics_file()
 {
     if (EE_Registry::instance()->REQ->is_set('ics_id')) {
         $DTT_ID = EE_Registry::instance()->REQ->get('ics_id');
         $datetime = EE_Registry::instance()->load_model('Datetime')->get_one_by_ID($DTT_ID);
         if ($datetime instanceof EE_Datetime) {
             // get related event, venues, and event categories
             $event = $datetime->event();
             // get related category Term object and it's name
             $category = $event->first_event_category();
             if ($category instanceof EE_Term) {
                 $category = $category->name();
             }
             $location = '';
             // get first related venue and convert to CSV string
             $venue = $event->venues(array('limit' => 1));
             if (is_array($venue) && !empty($venue)) {
                 $venue = array_shift($venue);
                 if ($venue instanceof EE_Venue) {
                     EE_Registry::instance()->load_helper('Venue_View');
                     $location = espresso_venue_raw_address('inline', $venue->ID(), FALSE);
                 }
             }
             // set variables, escape strings, convert timestamps to ics format, etc
             $filename = $event->slug() . '-' . $datetime->start_date('Y-m-d') . '.ics';
             $organizer = EED_Ical::_escape_ICal_data(EE_Registry::instance()->CFG->organization->name);
             $UID = EED_Ical::_escape_ICal_data(md5($event->name() . $event->ID() . $datetime->ID()));
             $org_email = EED_Ical::_escape_ICal_data($datetime->ID());
             $timestamp = date(EED_Ical::iCal_datetime_format);
             $location = EED_Ical::_escape_ICal_data($location);
             $summary = EED_Ical::_escape_ICal_data($event->name());
             $description = EED_Ical::_escape_ICal_data(wp_strip_all_tags($event->description()));
             $status = $datetime->get_active_status();
             $status = $status == EE_Datetime::cancelled ? 'Cancelled' : 'Confirmed';
             $status = EED_Ical::_escape_ICal_data($status);
             $categories = EED_Ical::_escape_ICal_data($category);
             $url = EED_Ical::_escape_ICal_data(get_permalink($event->ID()));
             $dtt_start = EED_Ical::_escape_ICal_data(date(EED_Ical::iCal_datetime_format, $datetime->start()));
             $dtt_end = EED_Ical::_escape_ICal_data(date(EED_Ical::iCal_datetime_format, $datetime->end()));
             // set headers
             header('Content-type: text/calendar; charset=utf-8');
             header('Content-Disposition: attachment; filename="' . $filename . '"');
             header('Cache-Control: private, max-age=0, must-revalidate');
             header('Pragma: public');
             header('Content-Type: application/octet-stream');
             header('Content-Type: application/force-download');
             header('Cache-Control: no-cache, must-revalidate');
             header('Content-Transfer-Encoding: binary');
             header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
             // past date
             ini_set('zlib.output_compression', '0');
             // echo the output
             echo "BEGIN:VCALENDAR" . PHP_EOL;
             echo "VERSION:2.0" . PHP_EOL;
             echo "PRODID:-//{$organizer}//NONSGML PDA Calendar Version 1.0//EN" . PHP_EOL;
             echo "CALSCALE:GREGORIAN" . PHP_EOL;
             echo "BEGIN:VEVENT" . PHP_EOL;
             echo "UID:{$UID}" . PHP_EOL;
             echo "ORGANIZER:MAILTO:{$org_email}" . PHP_EOL;
             echo "DTSTAMP:{$timestamp}" . PHP_EOL;
             echo "LOCATION:{$location}" . PHP_EOL;
             echo "SUMMARY:{$summary}" . PHP_EOL;
             echo "DESCRIPTION:{$description}" . PHP_EOL;
             echo "STATUS:{$status}" . PHP_EOL;
             echo "CATEGORIES:{$categories}" . PHP_EOL;
             echo "URL;VALUE=URI:{$url}" . PHP_EOL;
             echo "DTSTART:{$dtt_start}" . PHP_EOL;
             echo "DTEND:{$dtt_end}" . PHP_EOL;
             echo "END:VEVENT" . PHP_EOL;
             echo "END:VCALENDAR" . PHP_EOL;
         }
     }
     die;
 }
 /**
  * 	_escape_ICal_description
  *
  *	@access 	private
  *  	@param	string $description
  *  	@return	string
  */
 private static function _escape_ICal_description($description = '')
 {
     //Escape spcial chars within the decription
     $description = EED_Ical::_escape_ICal_data($description);
     //Remove line breaks and output in iCal format
     $description = str_replace(array("\r\n", "\n"), '\\n', $description);
     return $description;
 }