Create Downloadable iCal Events via Cake

iCal just plain rules. Though it’s not a super-detailed, completely all-knowing calendar application, it’s the most user friendly offline event tracker to date. (The best online calendar is Google Calendar, hands down.)
Allowing your loyal, web-savvy fans to download iCal files (enabling them to instantly add events to their iCal program), or subscribing to iCal feeds (allowing their iCal program to update the users calendar whenever they chose – much like RSS) can bring a smile to even the pickiest of web surfers.

Here’s the guts of a typical iCal file we’ll call theevent.vcs:
BEGIN:VCALENDAR
VERSION:2.0
BEGIN:VEVENT
DURATION:PT1H
SEQUENCE:4
URL;VALUE=URI:http://www.thehotfreaks.com/
DTSTART;TZID=US/Pacific:20070914T210000
DTEND;TZID=US/Pacific:20070914T210000
SUMMARY: Art
DESCRIPTION: Crazy Sexy Rainbow
END:VEVENT
END:VCALENDAR

I’ll let you figure out what every variable means. For now, let’s cut the crap and dive into the good stuff.
CREATE DOWNLOADABLE ICAL EVENTS VIA CAKEPHP WITH THREE FILES:
To create a file based on a particular event, open the CakePHP controller associated with your needs. Since I’m creating an iCal file based off an event, my controller is called events_controller.php – your controller might vary.
In the controller, create a function called ical, like so:
function ical($id = null) {
// use ical layout
$this->layout = "ical";

if (!$id || !$this->Event->read(null,$id)) {
$this->Session->setFlash('Invalid id for Event.');
$this->redirect('/events/index');
exit();
}

$this->set('theEvent', $this->Event->read(null,$id));
}
Then create a new layout (in your views/layouts/ folder) called ical.thtml. Place the following junk inside it:
<?php
$Filename = "TheEvent.vcs";
header("Content-Type: text/x-vCalendar");
header("Content-Disposition: inline; filename=$Filename");
echo $content_for_layout;
?>

The $Filename variable above can be changed to whatever you like as long as it maintains a .vcs ending.
Then in your views/events/ folder, add the ical.thtml view:
BEGIN:VCALENDAR
VERSION:1.0
BEGIN:VEVENT
DURATION:PT1H
SEQUENCE:4
URL;VALUE=URI:< ? php echo $theEvent['Event']['website']. "\n"; ?>
DTSTART;TZID=US/Pacific:< ? php echo date("Ymd\THi00", strtotime($theEvent['Event']['event_date']))."\n"; ?>
DTEND;TZID=US/Pacific:< ? php echo date("Ymd\THi00", strtotime($theEvent['Event']['event_date']))."\n"; ?>
SUMMARY:< ? php echo $theEvent['Event']['summary']."\n"; ?>
DESCRIPTION:< ? php echo $theEvent['Event']['description']."\n" ?>
END:VEVENT
END:VCALENDAR

DONE. Now surf to /events/ical/[id-of-event]/ and you should have a magical iCal file begin downloading. Life is good.