1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\CalendarLinks\Generators; |
4
|
|
|
|
5
|
|
|
use Spatie\CalendarLinks\Link; |
6
|
|
|
use Spatie\CalendarLinks\Generator; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @see https://icalendar.org/RFC-Specifications/iCalendar-RFC-5545/ |
10
|
|
|
*/ |
11
|
|
|
class Ics implements Generator |
12
|
|
|
{ |
13
|
|
|
public function generate(Link $link): string |
14
|
|
|
{ |
15
|
|
|
$url = [ |
16
|
|
|
'BEGIN:VCALENDAR', |
17
|
|
|
'VERSION:2.0', |
18
|
|
|
'BEGIN:VTIMEZONE', |
19
|
|
|
'TZID:'.$link->from->format('e'), |
20
|
|
|
'END:VTIMEZONE', |
21
|
|
|
'BEGIN:VEVENT', |
22
|
|
|
'UID:'.$this->generateEventUid($link), |
23
|
|
|
'SUMMARY:'.$link->title, |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
if ($link->allDay) { |
27
|
|
|
$dateTimeFormat = 'Ymd'; |
28
|
|
|
$url[] = 'DTSTART:'.$link->from->format($dateTimeFormat); |
29
|
|
|
$url[] = 'DURATION:P1D'; |
30
|
|
|
} else { |
31
|
|
|
$dateTimeFormat = "e:Ymd\THis"; |
32
|
|
|
$url[] = 'DTSTART;TZID='.$link->from->format($dateTimeFormat); |
33
|
|
|
$url[] = 'DTEND;TZID='.$link->to->format($dateTimeFormat); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if ($link->description) { |
37
|
|
|
$url[] = 'DESCRIPTION:'.$this->escapeString($link->description); |
38
|
|
|
} |
39
|
|
|
if ($link->address) { |
40
|
|
|
$url[] = 'LOCATION:'.$this->escapeString($link->address); |
41
|
|
|
} |
42
|
|
|
if (!empty($link->attendees)) { |
43
|
|
|
foreach ($link->attendees as $attendee) { |
44
|
|
|
$url[] = 'ATTENDEE:'.$attendee; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$url[] = 'END:VEVENT'; |
49
|
|
|
$url[] = 'END:VCALENDAR'; |
50
|
|
|
$redirectLink = implode('%0d%0a', $url); |
51
|
|
|
|
52
|
|
|
return 'data:text/calendar;charset=utf8,'.$redirectLink; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** @see https://tools.ietf.org/html/rfc5545.html#section-3.3.11 */ |
56
|
|
|
protected function escapeString(string $field): string |
57
|
|
|
{ |
58
|
|
|
return addcslashes($field, "\n,;"); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** @see https://tools.ietf.org/html/rfc5545#section-3.8.4.7 */ |
62
|
|
|
protected function generateEventUid(Link $link): string |
63
|
|
|
{ |
64
|
|
|
return md5($link->from->format(\DateTime::ATOM).$link->to->format(\DateTime::ATOM).$link->title.$link->address); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|