I've been doing events sections for a few sites and for each I needed to make an iCal feed. I've wrapped up this functionality into a module now to make it a little easier for myself and anyone else that needs to make these feeds.
The module is basically a simple wrapper around the iCalcreator library (a copy is included). It's modelled on the MarkupRSS module from Ryan, so anyone familiar with that should have a feed up and running in no time.
Usage
The module takes a PageArray and creates the feed from that. The [tt]->render[/tt] method will send the output to the browser with a generated filename and will automatically download. Because it will return HTTP headers, it has to be included before any html and is best in its own template or followed up by [tt]exit;[/tt]
<?php
$today = time();
$items = $pages->find("template=event, sort=start_date, start_date>$today");
$ics = $modules->get("MarkupiCalendar");
$ics->title = "Upcoming Events";
$ics->description = "Some upcoming events";
$ics->itemStartDateField = 'start_date';
$ics->itemEndDateField = 'end_date';
$ics->itemLocationField = 'location';
$ics->render($items);
You can use the [tt]->renderFeed[/tt] method to return the output as a string instead, which is particularly useful if you want to debug or write the output to a file.
<?php
$today = time();
$items = $pages->find("template=event, sort=start_date, start_date>$today");
$ics = $modules->get("MarkupiCalendar");
$ics->title = "Upcoming Events";
$ics->description = "Some upcoming events";
$ics->itemStartDateField = 'start_date';
$ics->itemEndDateField = 'end_date';
$ics->itemLocationField = 'location';
$cal = $ics->renderFeed($items);
echo $cal;
I often put RSS and iCal feeds at the top of my listing pages so as not to clutter up the site tree with extra templates. This way /events/ may point to my events page, and /events/ical will point to it's feed. Here is an example:
<?php
// iCal feed
if ($input->urlSegment1 == "ical") {
$today = time();
$items = $page->children("sort=start_date, start_date>$today");
$ics = $modules->get("MarkupiCalendar");
$ics->title = "Upcoming Events";
$ics->description = "Upcoming events for my company";
$ics->itemStartDateField = 'start_date';
$ics->itemEndDateField = 'end_date';
$ics->itemLocationField = 'location';
$ics->url = $page->httpUrl;
$ics->render($items);
exit;
}
// Render the template as normal.
include("./includes/head.inc");
Download and feedback
You can download or fork the module here: https://github.com/f...MarkupiCalendar
At the moment it only supports all day events (as these are all I have dealt with) but I hope to add time based events soon. Any feedback is warmly appreciated and feel free to report bugs or suggestions for improvement.
Stephen













