-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathIcalCalendarController.php
106 lines (87 loc) · 4.21 KB
/
IcalCalendarController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/**
* Class IcalCalendarController
* This controller generates and renders .ics(iCal)-files with the help of the Eluceo plugin.
* These can be generated by school+calendar, or by an individual id.
*/
class IcalCalendarController extends \BaseController
{
/**
* Find correct appointments depending on $school and $calendar
* Process these appointments and render them to an .ics file which will be returned for download
* @param $school_slug int the school slug
* @param $calendar_slug int the calendar slug
* @return mixed cal.ics download file
*
*/
public function index($school_slug, $calendar_slug)
{
$appointments = CalendarController::getAppointmentsBySlugs($school_slug, $calendar_slug);
// Compose iCal with the help of the eluceo plugin
$title = 'kalender' . ' ' . $school_slug . '-' . str_replace('+', '-', $calendar_slug);
$calendar = self::composeIcal($title, $appointments);
return $calendar->render();
}
/**
* This function composes the contents of the iCal file, which are eventually rendered by the index function.
* This function makes use of the Eluceo plugin
* @param $appointments
* @return \Eluceo\iCal\Component\Calendar
*/
public function composeIcal($title, $appointments)
{
// Set default timezone (PHP 5.4)
date_default_timezone_set('Europe/Berlin');
// Create new calendar object
$calendar = new \Eluceo\iCal\Component\Calendar('-//Open Knowledge//EduCal//NL');
$calendar->setName($title . ' (educal)');
// Loop through appointments and add them to the calendar.
foreach ($appointments as $appointment) {
// TODO: Add location
// Create an event
$event = new \Eluceo\iCal\Component\Event();
$event->setSummary($appointment['attributes']['title']);
$event->setDescription(str_replace("\r\n", " ", $appointment['attributes']['description']));
$event->setDtStart(new \DateTime($appointment['attributes']['start']));
$event->setDtEnd(new \DateTime($appointment['attributes']['end']));
$event->setNoTime($appointment['attributes']['allDay']);
// Generate unique ID apIDed
$uid = 'ap' . $appointment['attributes']['id'] . 'ed';
$event->setUniqueId($uid);
//$event->setStatus('TENTATIVE');
// Recurrence option (e.g. New Year happens every year)
// Set recurrence rule
/* if ($appointment['attributes']['repeat_type']) {
$recurrenceRule = new \Eluceo\iCal\Property\Event\RecurrenceRule();
// Check the repeat type (day, week, month, year) and set the corresponding recurrence rule
switch ($appointment['attributes']['repeat_type']) {
case 'd':
$recurrenceRule->setFreq(\Eluceo\iCal\Property\Event\RecurrenceRule::FREQ_DAILY);
break;
case 'w':
$recurrenceRule->setFreq(\Eluceo\iCal\Property\Event\RecurrenceRule::FREQ_WEEKLY);
break;
case 'M':
$recurrenceRule->setFreq(\Eluceo\iCal\Property\Event\RecurrenceRule::FREQ_MONTHLY);
break;
case 'y':
$recurrenceRule->setFreq(\Eluceo\iCal\Property\Event\RecurrenceRule::FREQ_YEARLY);
break;
}
$recurrenceRule->setInterval($appointment['attributes']['repeat_freq']);
$recurrenceRule->setCount($appointment['attributes']['nr_repeat']);
$event->setRecurrenceRule($recurrenceRule);
} */
// Adding Timezone (optional)
$event->setUseTimezone(true);
$event->setTimeTransparency('TRANSPARENT');
// Add event to calendar
$calendar->addEvent($event);
}
// Set headers
header("Content-Type: text/calendar; charset=utf-8");
header('Content-Disposition: attachment; filename="' . str_replace(' ', '_', $title) . '.ics"');
// Output
return $calendar;
}
}