Commit | Line | Data |
---|---|---|
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 | 15 | class 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); | |
a2cadd75 FW |
45 | $gCalendar = CRM_Utils_Request::retrieveValue('gCalendar', 'Positive', 0); |
46 | ||
47 | $info = CRM_Event_BAO_Event::getCompleteInfo($start, $type, $id, $end); | |
48 | ||
49 | if ($gCalendar) { | |
50 | return self::gCalRedirect($info); | |
51 | } | |
6a488035 | 52 | |
6a488035 TO |
53 | $template = CRM_Core_Smarty::singleton(); |
54 | $config = CRM_Core_Config::singleton(); | |
6834bea3 | 55 | |
6834bea3 | 56 | $template->assign('events', $info); |
a2cadd75 FW |
57 | |
58 | $timezones = [@date_default_timezone_get()]; | |
59 | ||
60 | $template->assign('timezone', $timezones[0]); | |
6834bea3 MW |
61 | |
62 | // Send data to the correct template for formatting (iCal vs. gData) | |
6a488035 TO |
63 | if ($rss) { |
64 | // rss 2.0 requires lower case dash delimited locale | |
6834bea3 | 65 | $template->assign('rssLang', str_replace('_', '-', strtolower($config->lcMessages))); |
6a488035 TO |
66 | $calendar = $template->fetch('CRM/Core/Calendar/Rss.tpl'); |
67 | } | |
68 | elseif ($gData) { | |
69 | $calendar = $template->fetch('CRM/Core/Calendar/GData.tpl'); | |
70 | } | |
6a488035 | 71 | else { |
a2cadd75 FW |
72 | $date_min = min( |
73 | array_map(function ($event) { | |
74 | return strtotime($event['start_date']); | |
75 | }, $info) | |
76 | ); | |
77 | $date_max = max( | |
78 | array_map(function ($event) { | |
79 | return strtotime($event['end_date'] ?? $event['start_date']); | |
80 | }, $info) | |
81 | ); | |
82 | $template->assign('timezones', CRM_Utils_ICalendar::generate_timezones($timezones, $date_min, $date_max)); | |
6a488035 TO |
83 | $calendar = $template->fetch('CRM/Core/Calendar/ICal.tpl'); |
84 | $calendar = preg_replace('/(?<!\r)\n/', "\r\n", $calendar); | |
85 | } | |
86 | ||
87 | // Push output for feed or download | |
88 | if ($iCalPage == 1) { | |
89 | if ($gData || $rss) { | |
90 | CRM_Utils_ICalendar::send($calendar, 'text/xml', 'utf-8'); | |
91 | } | |
92 | else { | |
cb087737 | 93 | CRM_Utils_ICalendar::send($calendar, 'text/calendar', 'utf-8'); |
6a488035 TO |
94 | } |
95 | } | |
96 | else { | |
97 | CRM_Utils_ICalendar::send($calendar, 'text/calendar', 'utf-8', 'civicrm_ical.ics', 'attachment'); | |
98 | } | |
99 | CRM_Utils_System::civiExit(); | |
100 | } | |
96025800 | 101 | |
a2cadd75 FW |
102 | protected static function gCalRedirect(array $events) { |
103 | if (count($events) != 1) { | |
104 | throw new CRM_Core_Exception(ts('Expected one %1, found %2', [1 => ts('Event'), 2 => count($events)])); | |
105 | } | |
106 | ||
107 | $event = reset($events); | |
108 | ||
109 | // Fetch the required Date TimeStamps | |
110 | $start_date = date_create($event['start_date']); | |
111 | ||
112 | // Google Requires that a Full Day event end day happens on the next Day | |
113 | $end_date = ($event['end_date'] | |
114 | ? date_create($event['end_date']) | |
115 | : date_create($event['start_date']) | |
116 | ->add(DateInterval::createFromDateString('1 day')) | |
117 | ->setTime(0, 0, 0) | |
118 | ); | |
119 | ||
120 | $dates = $start_date->format('Ymd\THis') . '/' . $end_date->format('Ymd\THis'); | |
121 | ||
122 | $event_details = $event['description']; | |
123 | ||
124 | // Add space after paragraph | |
125 | $event_details = str_replace('</p>', '</p> ', $event_details); | |
126 | $event_details = strip_tags($event_details); | |
127 | ||
128 | // Truncate Event Description and add permalink if greater than 996 characters | |
129 | if (strlen($event_details) > 996) { | |
130 | if (preg_match('/^.{0,996}(?=\s|$_)/', $event_details, $m)) { | |
131 | $event_details = $m[0] . '...'; | |
132 | } | |
133 | } | |
134 | ||
135 | $event_details .= "\n\n<a href=\"{$event['url']}\">" . ts('View %1 Details', [1 => $event['event_type']]) . '</a>'; | |
136 | ||
137 | $params = [ | |
138 | 'action' => 'TEMPLATE', | |
139 | 'text' => strip_tags($event['title']), | |
140 | 'dates' => $dates, | |
141 | 'details' => $event_details, | |
142 | 'location' => str_replace("\n", "\t", $event['location']), | |
143 | 'trp' => 'false', | |
144 | 'sprop' => 'website:' . CRM_Utils_System::baseCMSURL(), | |
145 | 'ctz' => @date_default_timezone_get(), | |
146 | ]; | |
147 | ||
148 | $url = 'https://www.google.com/calendar/event?' . CRM_Utils_System::makeQueryString($params); | |
149 | ||
150 | CRM_Utils_System::redirect($url); | |
151 | } | |
152 | ||
6a488035 | 153 | } |