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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\Calendar;
use DateTimeInterface;
use InvalidArgumentException;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Calendar\CalendarEventStatus;
use OCP\Calendar\ICalendarEventBuilder;
use OCP\Calendar\ICreateFromString;
use Sabre\VObject\Component\VCalendar;
use Sabre\VObject\Component\VEvent;
class CalendarEventBuilder implements ICalendarEventBuilder {
private ?DateTimeInterface $startDate = null;
private ?DateTimeInterface $endDate = null;
private ?string $summary = null;
private ?string $description = null;
private ?string $location = null;
private ?CalendarEventStatus $status = null;
private ?array $organizer = null;
private array $attendees = [];
public function __construct(
private readonly string $uid,
private readonly ITimeFactory $timeFactory,
) {
}
public function setStartDate(DateTimeInterface $start): ICalendarEventBuilder {
$this->startDate = $start;
return $this;
}
public function setEndDate(DateTimeInterface $end): ICalendarEventBuilder {
$this->endDate = $end;
return $this;
}
public function setSummary(string $summary): ICalendarEventBuilder {
$this->summary = $summary;
return $this;
}
public function setDescription(string $description): ICalendarEventBuilder {
$this->description = $description;
return $this;
}
public function setLocation(string $location): ICalendarEventBuilder {
$this->location = $location;
return $this;
}
public function setStatus(CalendarEventStatus $status): static {
$this->status = $status;
return $this;
}
public function setOrganizer(string $email, ?string $commonName = null): ICalendarEventBuilder {
$this->organizer = [$email, $commonName];
return $this;
}
public function addAttendee(string $email, ?string $commonName = null): ICalendarEventBuilder {
$this->attendees[] = [$email, $commonName];
return $this;
}
public function toIcs(): string {
if ($this->startDate === null) {
throw new InvalidArgumentException('Event is missing a start date');
}
if ($this->endDate === null) {
throw new InvalidArgumentException('Event is missing an end date');
}
if ($this->summary === null) {
throw new InvalidArgumentException('Event is missing a summary');
}
if ($this->organizer === null && $this->attendees !== []) {
throw new InvalidArgumentException('Event has attendees but is missing an organizer');
}
$vcalendar = new VCalendar();
$props = [
'UID' => $this->uid,
'DTSTAMP' => $this->timeFactory->now(),
'SUMMARY' => $this->summary,
'DTSTART' => $this->startDate,
'DTEND' => $this->endDate,
'STATUS' => $this->status->value,
];
if ($this->description !== null) {
$props['DESCRIPTION'] = $this->description;
}
if ($this->location !== null) {
$props['LOCATION'] = $this->location;
}
/** @var VEvent $vevent */
$vevent = $vcalendar->add('VEVENT', $props);
if ($this->organizer !== null) {
self::addAttendeeToVEvent($vevent, 'ORGANIZER', $this->organizer);
}
foreach ($this->attendees as $attendee) {
self::addAttendeeToVEvent($vevent, 'ATTENDEE', $attendee);
}
return $vcalendar->serialize();
}
public function createInCalendar(ICreateFromString $calendar): string {
$fileName = $this->uid . '.ics';
$calendar->createFromString($fileName, $this->toIcs());
return $fileName;
}
/**
* @param array{0: string, 1: ?string} $tuple A tuple of [$email, $commonName] where $commonName may be null.
*/
private static function addAttendeeToVEvent(VEvent $vevent, string $name, array $tuple): void {
[$email, $cn] = $tuple;
if (!str_starts_with($email, 'mailto:')) {
$email = "mailto:$email";
}
$params = [];
if ($cn !== null) {
$params['CN'] = $cn;
if ($name === 'ORGANIZER') {
$params['ROLE'] = 'CHAIR';
$params['PARTSTAT'] = 'ACCEPTED';
} else {
$params['ROLE'] = 'REQ-PARTICIPANT';
$params['PARTSTAT'] = 'NEEDS-ACTION';
}
}
$vevent->add($name, $email, $params);
}
}
|