<?php
$id            = $item['Event']['id'];
$start_date    = $item['Event']['start_date'];
$start_time    = $item['Event']['start_time'];
$end_date      = $item['Event']['end_date'];
$end_time      = $item['Event']['end_time'];
$name          = $item['Event']['title'];
$location      = $item['Event']['location'];
$description   = $item['Event']['content'];

/*
if ($calendar->whole_day == 1) {
    $end_date = strftime("%Y-%m-%d", strtotime($end_date ."+1 day"));
}
*/

# Remove '-' in $start_date and $end_date
$estart_date   = str_replace("-", "", $start_date);
$eend_date     = str_replace("-", "", $end_date);

# Remove ':' in $start_time and $end_time
$estart_time   = str_replace(":", "", $start_time);
$eend_time     = str_replace(":", "", $end_time);

$name          = str_replace("<br>", "\\n",   $name);
$name          = str_replace("<br />", "\\n", $name);
$name          = str_replace("&amp;", "&",    $name);
$name          = str_replace("&rarr;", "-->", $name);
$name          = str_replace("&larr;", "<--", $name);
$name          = str_replace(",", "\\,",      $name);
$name          = str_replace(";", "\\;",      $name);
$name          = str_replace("&nbsp;", " ",   $name);

$location      = str_replace("<br>", "\\n",   $location);
$location      = str_replace("<br />", "\\n", $location);
$location      = str_replace("&amp;", "&",    $location);
$location      = str_replace("&rarr;", "-->", $location);
$location      = str_replace("&larr;", "<--", $location);
$location      = str_replace(",", "\\,",      $location);
$location      = str_replace(";", "\\;",      $location);
$location      = str_replace("&nbsp;", " ",   $location);

$description   = str_replace("<br>", "\\n",   $description);
$description   = str_replace("<br />", "\\n", $description);
$description   = str_replace("&amp;", "&",    $description);
$description   = str_replace("&rarr;", "-->", $description);
$description   = str_replace("&larr;", "<--", $description);
$description   = str_replace("<em>", "",      $description);
$description   = str_replace("</em>", "",     $description);
$description   = str_replace("&nbsp;", " ",   $description);

$description = trim(strip_tags($description));

header("Content-Type: text/Calendar");
header("Content-Disposition: inline; filename=".$name.".ics");

echo "BEGIN:VCALENDAR\n";
echo "VERSION:2.0\n";
echo "PRODID://JOB//CALENDAR//NL\n";
echo "METHOD:PUBLISH\n"; // requied by Outlook
echo "BEGIN:VEVENT\n";
echo "UID:".$item['Event']['start_date'].'T'.$item['Event']['start_time']."-".$item['Event']['id']."\n"; // required by Outlok
echo "DTSTAMP:".$item['Event']['start_date'].'T'.$item['Event']['start_time']."\n"; // required by Outlook

//if ($calendar->whole_day == 1) {
//echo "DTSTART:".$estart_date."\n"; 
//echo "DTEND:".$eend_date."\n"; 
//} else {
echo "DTSTART:".$estart_date."T".$estart_time."\n"; 
echo "DTEND:".$eend_date."T".$eend_time."\n"; 
//}

echo "SUMMARY:".$name."\n";
echo "LOCATION:".$location."\n";
//if ($calendar->whole_day == 1) {
//echo "X-FUNAMBOL-ALLDAY:1\n";
//}
//echo "DESCRIPTION:".$description."\n";
echo "URL:" . Configure::read('CFG.site_url') . "/events/view/" . $item['Event']['id'] . "\n";
echo "END:VEVENT\n";
echo "END:VCALENDAR\n";
?>