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