Merge pull request #22646 from braders/feature/tasktrait-issingle-docblock
[civicrm-core.git] / CRM / Event / ICalendar.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
6834bea3 13 * Class to generate various "icalendar" type event feeds
6a488035 14 */
74851348 15class CRM_Event_ICalendar {
6a488035
TO
16
17 /**
18 * Heart of the iCalendar data assignment process. The runner gets all the meta
19 * data for the event and calls the method to output the iCalendar
20 * to the user. If gData param is passed on the URL, outputs gData XML format.
21 * Else outputs iCalendar format per IETF RFC2445. Page param true means send
22 * to browser as inline content. Else, we send .ics file as attachment.
6a488035 23 */
74851348 24 public static function run() {
6834bea3
MW
25 $id = CRM_Utils_Request::retrieveValue('id', 'Positive', NULL, FALSE, 'GET');
26 $type = CRM_Utils_Request::retrieveValue('type', 'Positive', 0);
27 $start = CRM_Utils_Request::retrieveValue('start', 'Positive', 0);
28 $end = CRM_Utils_Request::retrieveValue('end', 'Positive', 0);
6a488035 29
6834bea3
MW
30 // We used to handle the event list as a html page at civicrm/event/ical - redirect to the new URL if that was what we requested.
31 if (CRM_Utils_Request::retrieveValue('html', 'Positive', 0)) {
32 $urlParams = [
33 'reset' => 1,
34 ];
35 $id ? $urlParams['id'] = $id : NULL;
36 $type ? $urlParams['type'] = $type : NULL;
37 $start ? $urlParams['start'] = $start : NULL;
38 $end ? $urlParams['end'] = $end : NULL;
39 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/event/list', $urlParams, FALSE, NULL, FALSE, TRUE));
40 }
41
42 $iCalPage = CRM_Utils_Request::retrieveValue('list', 'Positive', 0);
43 $gData = CRM_Utils_Request::retrieveValue('gData', 'Positive', 0);
44 $rss = CRM_Utils_Request::retrieveValue('rss', 'Positive', 0);
6a488035 45
6a488035
TO
46 $template = CRM_Core_Smarty::singleton();
47 $config = CRM_Core_Config::singleton();
6834bea3
MW
48
49 $info = CRM_Event_BAO_Event::getCompleteInfo($start, $type, $id, $end);
96bbfa78
FW
50
51 foreach ($info as &$event) {
52 $event['start_date'] = CRM_Utils_Date::convertTimeZone($event['start_date'], $event['tz']);
53 $event['end_date'] = !empty($event['end_date']) ? CRM_Utils_date::convertTimeZone($event['end_date'], $event['tz']) : NULL;
54 $event['registration_start_date'] = !empty($event['registration_start_date']) ? CRM_Utils_date::convertTimeZone($event['registration_start_date'], $event['tz']) : NULL;
55 $event['registration_end_date'] = !empty($event['registration_end_date']) ? CRM_Utils_date::convertTimeZone($event['registration_end_date'], $event['tz']) : NULL;
56 }
57
6834bea3 58 $template->assign('events', $info);
96bbfa78 59 $template->assign('timezone', CRM_Core_Config::singleton()->userSystem->getTimeZoneString());
6834bea3
MW
60
61 // Send data to the correct template for formatting (iCal vs. gData)
6a488035
TO
62 if ($rss) {
63 // rss 2.0 requires lower case dash delimited locale
6834bea3 64 $template->assign('rssLang', str_replace('_', '-', strtolower($config->lcMessages)));
6a488035
TO
65 $calendar = $template->fetch('CRM/Core/Calendar/Rss.tpl');
66 }
67 elseif ($gData) {
68 $calendar = $template->fetch('CRM/Core/Calendar/GData.tpl');
69 }
6a488035
TO
70 else {
71 $calendar = $template->fetch('CRM/Core/Calendar/ICal.tpl');
72 $calendar = preg_replace('/(?<!\r)\n/', "\r\n", $calendar);
73 }
74
75 // Push output for feed or download
76 if ($iCalPage == 1) {
77 if ($gData || $rss) {
78 CRM_Utils_ICalendar::send($calendar, 'text/xml', 'utf-8');
79 }
80 else {
cb087737 81 CRM_Utils_ICalendar::send($calendar, 'text/calendar', 'utf-8');
6a488035
TO
82 }
83 }
84 else {
85 CRM_Utils_ICalendar::send($calendar, 'text/calendar', 'utf-8', 'civicrm_ical.ics', 'attachment');
86 }
87 CRM_Utils_System::civiExit();
88 }
96025800 89
6a488035 90}