ACalendarSearchProvider.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. /** @var IAppManager */
  22. protected $appManager;
  23. /** @var IL10N */
  24. protected $l10n;
  25. /** @var IURLGenerator */
  26. protected $urlGenerator;
  27. /** @var CalDavBackend */
  28. protected $backend;
  29. /**
  30. * ACalendarSearchProvider constructor.
  31. *
  32. * @param IAppManager $appManager
  33. * @param IL10N $l10n
  34. * @param IURLGenerator $urlGenerator
  35. * @param CalDavBackend $backend
  36. */
  37. public function __construct(IAppManager $appManager,
  38. IL10N $l10n,
  39. IURLGenerator $urlGenerator,
  40. CalDavBackend $backend) {
  41. $this->appManager = $appManager;
  42. $this->l10n = $l10n;
  43. $this->urlGenerator = $urlGenerator;
  44. $this->backend = $backend;
  45. }
  46. /**
  47. * Get an associative array of calendars
  48. * calendarId => calendar
  49. *
  50. * @param string $principalUri
  51. * @return array
  52. */
  53. protected function getSortedCalendars(string $principalUri): array {
  54. $calendars = $this->backend->getCalendarsForUser($principalUri);
  55. $calendarsById = [];
  56. foreach ($calendars as $calendar) {
  57. $calendarsById[(int)$calendar['id']] = $calendar;
  58. }
  59. return $calendarsById;
  60. }
  61. /**
  62. * Get an associative array of subscriptions
  63. * subscriptionId => subscription
  64. *
  65. * @param string $principalUri
  66. * @return array
  67. */
  68. protected function getSortedSubscriptions(string $principalUri): array {
  69. $subscriptions = $this->backend->getSubscriptionsForUser($principalUri);
  70. $subscriptionsById = [];
  71. foreach ($subscriptions as $subscription) {
  72. $subscriptionsById[(int)$subscription['id']] = $subscription;
  73. }
  74. return $subscriptionsById;
  75. }
  76. /**
  77. * Returns the primary VEvent / VJournal / VTodo component
  78. * If it's a component with recurrence-ids, it will return
  79. * the primary component
  80. *
  81. * TODO: It would be a nice enhancement to show recurrence-exceptions
  82. * as individual search-results.
  83. * For now we will just display the primary element of a recurrence-set.
  84. *
  85. * @param string $calendarData
  86. * @param string $componentName
  87. * @return Component
  88. */
  89. protected function getPrimaryComponent(string $calendarData, string $componentName): Component {
  90. $vCalendar = Reader::read($calendarData, Reader::OPTION_FORGIVING);
  91. $components = $vCalendar->select($componentName);
  92. if (count($components) === 1) {
  93. return $components[0];
  94. }
  95. // If it's a recurrence-set, take the primary element
  96. foreach ($components as $component) {
  97. /** @var Component $component */
  98. if (!$component->{'RECURRENCE-ID'}) {
  99. return $component;
  100. }
  101. }
  102. // In case of error, just fallback to the first element in the set
  103. return $components[0];
  104. }
  105. }