ACalendarSearchProvider.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\Search;
  8. use OCA\DAV\CalDAV\CalDavBackend;
  9. use OCP\App\IAppManager;
  10. use OCP\IL10N;
  11. use OCP\IURLGenerator;
  12. use OCP\Search\IProvider;
  13. use Sabre\VObject\Component;
  14. use Sabre\VObject\Reader;
  15. /**
  16. * Class ACalendarSearchProvider
  17. *
  18. * @package OCA\DAV\Search
  19. */
  20. abstract class ACalendarSearchProvider implements IProvider {
  21. /**
  22. * ACalendarSearchProvider constructor.
  23. *
  24. * @param IAppManager $appManager
  25. * @param IL10N $l10n
  26. * @param IURLGenerator $urlGenerator
  27. * @param CalDavBackend $backend
  28. */
  29. public function __construct(
  30. protected IAppManager $appManager,
  31. protected IL10N $l10n,
  32. protected IURLGenerator $urlGenerator,
  33. protected CalDavBackend $backend,
  34. ) {
  35. }
  36. /**
  37. * Get an associative array of calendars
  38. * calendarId => calendar
  39. *
  40. * @param string $principalUri
  41. * @return array
  42. */
  43. protected function getSortedCalendars(string $principalUri): array {
  44. $calendars = $this->backend->getCalendarsForUser($principalUri);
  45. $calendarsById = [];
  46. foreach ($calendars as $calendar) {
  47. $calendarsById[(int)$calendar['id']] = $calendar;
  48. }
  49. return $calendarsById;
  50. }
  51. /**
  52. * Get an associative array of subscriptions
  53. * subscriptionId => subscription
  54. *
  55. * @param string $principalUri
  56. * @return array
  57. */
  58. protected function getSortedSubscriptions(string $principalUri): array {
  59. $subscriptions = $this->backend->getSubscriptionsForUser($principalUri);
  60. $subscriptionsById = [];
  61. foreach ($subscriptions as $subscription) {
  62. $subscriptionsById[(int)$subscription['id']] = $subscription;
  63. }
  64. return $subscriptionsById;
  65. }
  66. /**
  67. * Returns the primary VEvent / VJournal / VTodo component
  68. * If it's a component with recurrence-ids, it will return
  69. * the primary component
  70. *
  71. * TODO: It would be a nice enhancement to show recurrence-exceptions
  72. * as individual search-results.
  73. * For now we will just display the primary element of a recurrence-set.
  74. *
  75. * @param string $calendarData
  76. * @param string $componentName
  77. * @return Component
  78. */
  79. protected function getPrimaryComponent(string $calendarData, string $componentName): Component {
  80. $vCalendar = Reader::read($calendarData, Reader::OPTION_FORGIVING);
  81. $components = $vCalendar->select($componentName);
  82. if (count($components) === 1) {
  83. return $components[0];
  84. }
  85. // If it's a recurrence-set, take the primary element
  86. foreach ($components as $component) {
  87. /** @var Component $component */
  88. if (!$component->{'RECURRENCE-ID'}) {
  89. return $component;
  90. }
  91. }
  92. // In case of error, just fallback to the first element in the set
  93. return $components[0];
  94. }
  95. }