AppCalendarPlugin.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2023 Ferdinand Thiessen <opensource@fthiessen.de>
  5. *
  6. * @author Ferdinand Thiessen <opensource@fthiessen.de>
  7. *
  8. * @license AGPL-3.0-or-later
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\DAV\CalDAV\AppCalendar;
  25. use OCA\DAV\CalDAV\Integration\ExternalCalendar;
  26. use OCA\DAV\CalDAV\Integration\ICalendarProvider;
  27. use OCP\Calendar\IManager;
  28. use Psr\Log\LoggerInterface;
  29. /* Plugin for wrapping application generated calendars registered in nextcloud core (OCP\Calendar\ICalendarProvider) */
  30. class AppCalendarPlugin implements ICalendarProvider {
  31. protected IManager $manager;
  32. protected LoggerInterface $logger;
  33. public function __construct(IManager $manager, LoggerInterface $logger) {
  34. $this->manager = $manager;
  35. $this->logger = $logger;
  36. }
  37. public function getAppID(): string {
  38. return 'dav-wrapper';
  39. }
  40. public function fetchAllForCalendarHome(string $principalUri): array {
  41. return array_map(function ($calendar) use (&$principalUri) {
  42. return new AppCalendar($this->getAppID(), $calendar, $principalUri);
  43. }, $this->getWrappedCalendars($principalUri));
  44. }
  45. public function hasCalendarInCalendarHome(string $principalUri, string $calendarUri): bool {
  46. return count($this->getWrappedCalendars($principalUri, [ $calendarUri ])) > 0;
  47. }
  48. public function getCalendarInCalendarHome(string $principalUri, string $calendarUri): ?ExternalCalendar {
  49. $calendars = $this->getWrappedCalendars($principalUri, [ $calendarUri ]);
  50. if (count($calendars) > 0) {
  51. return new AppCalendar($this->getAppID(), $calendars[0], $principalUri);
  52. }
  53. return null;
  54. }
  55. protected function getWrappedCalendars(string $principalUri, array $calendarUris = []): array {
  56. return array_values(
  57. array_filter($this->manager->getCalendarsForPrincipal($principalUri, $calendarUris), function ($c) {
  58. // We must not provide a wrapper for DAV calendars
  59. return ! ($c instanceof \OCA\DAV\CalDAV\CalendarImpl);
  60. })
  61. );
  62. }
  63. }