1
0

AppCalendarPlugin.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\CalDAV\AppCalendar;
  8. use OCA\DAV\CalDAV\CachedSubscriptionImpl;
  9. use OCA\DAV\CalDAV\CalendarImpl;
  10. use OCA\DAV\CalDAV\Integration\ExternalCalendar;
  11. use OCA\DAV\CalDAV\Integration\ICalendarProvider;
  12. use OCP\Calendar\IManager;
  13. use Psr\Log\LoggerInterface;
  14. /* Plugin for wrapping application generated calendars registered in nextcloud core (OCP\Calendar\ICalendarProvider) */
  15. class AppCalendarPlugin implements ICalendarProvider {
  16. public function __construct(
  17. protected IManager $manager,
  18. protected LoggerInterface $logger,
  19. ) {
  20. }
  21. public function getAppID(): string {
  22. return 'dav-wrapper';
  23. }
  24. public function fetchAllForCalendarHome(string $principalUri): array {
  25. return array_map(function ($calendar) use (&$principalUri) {
  26. return new AppCalendar($this->getAppID(), $calendar, $principalUri);
  27. }, $this->getWrappedCalendars($principalUri));
  28. }
  29. public function hasCalendarInCalendarHome(string $principalUri, string $calendarUri): bool {
  30. return count($this->getWrappedCalendars($principalUri, [ $calendarUri ])) > 0;
  31. }
  32. public function getCalendarInCalendarHome(string $principalUri, string $calendarUri): ?ExternalCalendar {
  33. $calendars = $this->getWrappedCalendars($principalUri, [ $calendarUri ]);
  34. if (count($calendars) > 0) {
  35. return new AppCalendar($this->getAppID(), $calendars[0], $principalUri);
  36. }
  37. return null;
  38. }
  39. protected function getWrappedCalendars(string $principalUri, array $calendarUris = []): array {
  40. return array_values(
  41. array_filter($this->manager->getCalendarsForPrincipal($principalUri, $calendarUris), function ($c) {
  42. // We must not provide a wrapper for DAV calendars
  43. return ! (($c instanceof CalendarImpl) || ($c instanceof CachedSubscriptionImpl));
  44. })
  45. );
  46. }
  47. }