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
|
|
|
protected $uid = null; |
14
|
|
|
|
15
|
|
|
public function __construct($uid = null) |
16
|
|
|
{ |
17
|
|
|
$this->uid = $uid; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function generate(Link $link): string |
21
|
|
|
{ |
22
|
|
|
$url = [ |
23
|
|
|
'BEGIN:VCALENDAR', |
24
|
|
|
'VERSION:2.0', |
25
|
|
|
'BEGIN:VEVENT', |
26
|
|
|
'UID:'.$this->generateEventUid($link), |
27
|
|
|
'SUMMARY:'.$link->title, |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
if ($link->allDay) { |
31
|
|
|
$dateTimeFormat = 'Ymd'; |
32
|
|
|
$url[] = 'DTSTART:'.$link->from->format($dateTimeFormat); |
33
|
|
|
$url[] = 'DURATION:P1D'; |
34
|
|
|
} else { |
35
|
|
|
$dateTimeFormat = "e:Ymd\THis"; |
36
|
|
|
$url[] = 'DTSTART;TZID='.$link->from->format($dateTimeFormat); |
37
|
|
|
$url[] = 'DTEND;TZID='.$link->to->format($dateTimeFormat); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if ($link->description) { |
41
|
|
|
$url[] = 'DESCRIPTION:'.$this->escapeString($link->description); |
42
|
|
|
} |
43
|
|
|
if ($link->address) { |
44
|
|
|
$url[] = 'LOCATION:'.$this->escapeString($link->address); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$url[] = 'END:VEVENT'; |
48
|
|
|
$url[] = 'END:VCALENDAR'; |
49
|
|
|
$redirectLink = implode('%0d%0a', $url); |
50
|
|
|
|
51
|
|
|
return 'data:text/calendar;charset=utf8,'.$redirectLink; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** @see https://tools.ietf.org/html/rfc5545.html#section-3.3.11 */ |
55
|
|
|
protected function escapeString(string $field): string |
56
|
|
|
{ |
57
|
|
|
return addcslashes($field, "\r\n,;"); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** @see https://tools.ietf.org/html/rfc5545#section-3.8.4.7 */ |
61
|
|
|
protected function generateEventUid(Link $link): string |
62
|
|
|
{ |
63
|
|
|
return $this->uid ?? md5($link->from->format(\DateTime::ATOM).$link->to->format(\DateTime::ATOM).$link->title.$link->address); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|