I need to generate links that download an .ics
file for an event. I have an events
page where these download links need to appear, which is a listing page for child event
pages. The child pages are not used on the front end of the site, they are just for organisation.
I have set up a route to try and achieve what I need, this is what it looks like at the moment:
site/config/config.php
'pattern' => 'events/(:all).ics',
'action' => function($all) {
$event = page('events/' . $all);
if ($event != '') {
Header::download(['mime'=>'text/calendar', 'name' => 'test.ics']);
return snippet('ical');
}
}
And then on my events listing page (simplified for ease):
site/templates/events.php
<?php
foreach($page->children() as $event):
?>
<div id="<?= $event->slug() ?>">
<h2><?= $event->title() ?></h2>
<a href="<?= $event->url() ?>.ics">Download ICS file</a>
</div>
<?php
endforeach;
?>
I also have a snippet file for the ics - this is just a placeholder for now for testing. It looks like this:
site/snippets/ical.php
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//ZContent.net//Zap Calendar 1.0//EN
CALSCALE:GREGORIAN
METHOD:PUBLISH
BEGIN:VEVENT
SUMMARY:Abraham Lincoln
UID:c7614cff-3549-4a00-9152-d25cc1fe077d
SEQUENCE:0
STATUS:CONFIRMED
TRANSP:TRANSPARENT
RRULE:FREQ=YEARLY;INTERVAL=1;BYMONTH=2;BYMONTHDAY=12
DTSTART:20080212
DTEND:20080213
DTSTAMP:20150421T141403
CATEGORIES:U.S. Presidents,Civil War People
LOCATION:Hodgenville\, Kentucky
GEO:37.5739497;-85.7399606
DESCRIPTION:Born February 12\, 1809\nSixteenth President (1861-1865)\n\n\n
\nhttp://AmericanHistoryCalendar.com
URL:http://americanhistorycalendar.com/peoplecalendar/1,328-abraham-lincol
n
END:VEVENT
END:VCALENDAR
I also have a little template for an individual event page, to stop people visiting them:
site/templates/event.php
<?php
go('/events#' . $page->slug())
?>
Currently when I click on the link the browser opens a new, empty tab. Nothing is downloaded / visible. I think my approach is right, but I think perhaps something in my route setup is wrong. If anyone could offer any thoughts that would be great.