Skip to content

Commit 3cc7415

Browse files
authored
Merge pull request #12813 from nextcloud/feat/developer/calendar-event-builder
feat(developer): document calendar event builder interface
2 parents fd99370 + f787d18 commit 3cc7415

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

developer_manual/digging_deeper/groupware/calendar.rst

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,74 @@ The returned objects implement ``\OCP\Calendar\ICalendar``. Study the interface
104104

105105
.. note:: All calendars are by default only readable, therefore ``ICalendar`` does not offer methods for mutation. Some of the calendars are mutable, however, and they may further extend the interface ``\OCP\Calendar\ICreateFromString``.
106106

107+
Create calendar events
108+
----------------------
109+
110+
Calendar events can either be imported from raw ICS strings or built programmatically using the ``ICalendarEventBuilder`` interface.
111+
Please consider the example below to see both methods in action.
112+
113+
.. code-block:: php
114+
115+
<?php
116+
117+
use OCP\Calendar\ICalendarEventBuilder;
118+
use OCP\Calendar\ICreateFromString;
119+
use OCP\Calendar\IManager;
120+
121+
class MyService {
122+
123+
/** @var IManager */
124+
private $calendarManager;
125+
126+
public function __construct(IManager $calendarManager) {
127+
$this->calendarManager = $calendarManager;
128+
}
129+
130+
public function createEvent(string $uid): void {
131+
$principal = 'principals/users/' . $uid;
132+
133+
// This will find all calendars of the principal
134+
$calendars = $this->calendarManager->getCalendarsForPrincipal($principal);
135+
136+
$writableCalendar = null;
137+
foreach ($calendars as $calendar) {
138+
if ($calendar instanceof ICreateFromString) {
139+
$writableCalendar = $calendar;
140+
break;
141+
}
142+
}
143+
144+
if ($writableCalendar === null) {
145+
return;
146+
}
147+
148+
// Build an event
149+
$startDate = (new \DateTimeImmutable('now'))
150+
->setTimezone(new \DateTimeZone('Europe/Berlin'));
151+
$endDate = $startDate->add(new \DateInterval('PT1H'));
152+
$builder = $this->calendarManager->createEventBuilder()
153+
->setStartDate($startDate)
154+
->setEndDate($endDate)
155+
->setSummary('An Event')
156+
->setDescription('With a description')
157+
->setLocation('Some address') // A URL (of a meeting) would also work
158+
->setOrganizer('[email protected]')
159+
->addAttendee('[email protected]')
160+
->addAttendee('[email protected]', 'User Two');
161+
162+
// Write the calendar to an event
163+
$builder->createInCalendar($writableCalendar);
164+
165+
// Or, serialize it to a string to do something else with it
166+
$ics = $builder->toIcs();
167+
168+
// For example, make use of ICreateFromString
169+
// Make sure to generate a unique filename/UUID for each event
170+
$writableCalendar->createFromString('edb29d1d-817c-4e43-9d52-cf26dff4be60.ics', $ics);
171+
}
172+
173+
}
174+
107175
Calendar providers
108176
-------------------
109177

0 commit comments

Comments
 (0)