source = $source; } /** */ private function hash_tag( string $tag ) : string { return \hash('sha256', $tag); } /** */ private function encode_tag( string $tag ) : string { return \davigil\helpers\call\convey( $tag, [ fn($x) => \strtolower($x), fn($x) => \preg_replace('/ä/', 'ae', $x), fn($x) => \preg_replace('/ö/', 'oe', $x), fn($x) => \preg_replace('/ü/', 'ue', $x), fn($x) => \preg_replace('/ß/', 'sz', $x), fn($x) => \preg_replace('/\-/', '', $x), fn($x) => \preg_replace('/ /', '-', $x), fn($x) => \preg_replace('/[^a-zA-Z0-9_\-]/s', '_', $x), ] ); } /** */ public function getCalendarsForUser( $principalUri ) { $data = $this->source->get([]); $tags = []; foreach ($data as $entry) { foreach ($entry['tags'] as $tag) { $tags[$tag] = null; } } $result = \array_map( fn($tag) => [ 'id' => $this->hash_tag($tag), 'uri' => $this->encode_tag($tag), 'principaluri' => $principalUri, '{DAV:}displayname' => $tag, \sprintf('{%s}supported-calendar-component-set', \Sabre\CalDAV\Plugin::NS_CALDAV) => new \Sabre\CalDAV\Xml\Property\SupportedCalendarComponentSet(['VEVENT']), ], \array_keys($tags) ); // \error_log(\json_encode($result, \JSON_PRETTY_PRINT)); return $result; } /** */ public function createCalendar( $principalUri, $calendarUri, array $properties ) { throw (new \Exception('not implemented: createCalendar')); } /** */ public function deleteCalendar( $calendarId ) { throw (new \Exception('not implemented: deleteCalendar')); } /** */ public function getCalendarObjects( $calendarId ) { $tag = $calendarId; $data = $this->source->get([]); $result = \array_map( fn($entry) => [ 'calendarid' => $calendarId, 'id' => $entry['id'], // 'uri' => \sprintf('%s.ics', $entry['id']), 'uri' => $entry['id'], 'lastmodified' => \time(), // 'etag' => null, // 'size' => null, 'component' => 'vevent', '{DAV:}displayname' => $entry['title'], ], \array_values( \array_filter( $data, fn($entry) => \in_array( $tag, \array_map( fn($x) => $this->hash_tag($x), $entry['tags'] ) ) ) ) ); return $result; } /** * @todo outsource */ private function event_to_vevent( array $event ) : \davigil\helpers\ics\struct_vevent { $vevent = new \davigil\helpers\ics\struct_vevent(); { $vevent->uid = $event['id']; $vevent->dtstamp = \davigil\helpers\ics\datetime_from_unix_timestamp($event['begin']); $vevent->dtstart = new \davigil\helpers\ics\struct_dt( '', \davigil\helpers\ics\datetime_from_unix_timestamp($event['begin']) ); $vevent->dtend = ( ($event['end'] === null) ? null : new \davigil\helpers\ics\struct_dt( '', \davigil\helpers\ics\datetime_from_unix_timestamp($event['end']) ) ); $vevent->summary = $event['title']; $vevent->location = $event['location']; $vevent->description = $event['description']; $vevent->class = \davigil\helpers\ics\enum_class::public_; } return $vevent; } /** * @todo outsource */ private function events_to_vcalendar( array $events ) : \davigil\helpers\ics\struct_vcalendar { $vcalendar = new \davigil\helpers\ics\struct_vcalendar(); { $vcalendar->version = '2.0'; /** * @todo conf */ $vcalendar->prodid = 'davigil'; $vcalendar->method = 'PUBLISH'; $vcalendar->events = \array_map( fn($event) => $this->event_to_vevent($event), $events ); } return $vcalendar; } /** */ public function getCalendarObject( $calendarId, $objectUri ) { $id = $objectUri; $data = $this->source->get([]); $entries = \array_values( \array_filter( $data, fn($entry) => ($entry['id'] === $id) ) ); if (\count($entries) !== 1) { throw (new \Exception(\sprintf('not found or ambiguous'))); } else { $vcalendar = $this->events_to_vcalendar($entries); $ics = \davigil\helpers\ics\vcalendar_encode($vcalendar); return [ 'calendardata' => $ics, 'uri' => $objectUri, /** * @todo */ 'lastmodified' => \time(), /** * @todo */ // 'etag' => '""', /** * @todo */ // 'size' => 1, 'component' => 'vcalendar', ]; } } /** */ public function createCalendarObject( $calendarId, $objectUri, $calendarData ) { throw (new \Exception('not implemented: createCalendarObject')); } /** */ public function updateCalendarObject( $calendarId, $objectUri, $calendarData ) { throw (new \Exception('not implemented: updateCalendarObject')); } /** */ public function deleteCalendarObject( $calendarId, $objectUri ) { throw (new \Exception('not implemented: deleteCalendarObject')); } } ?>