1
0

ACalendarSearchProvider.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020, Georg Ehrke
  5. *
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  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\Search;
  25. use OCA\DAV\CalDAV\CalDavBackend;
  26. use OCP\App\IAppManager;
  27. use OCP\IL10N;
  28. use OCP\IURLGenerator;
  29. use OCP\Search\IProvider;
  30. use Sabre\VObject\Component;
  31. use Sabre\VObject\Reader;
  32. /**
  33. * Class ACalendarSearchProvider
  34. *
  35. * @package OCA\DAV\Search
  36. */
  37. abstract class ACalendarSearchProvider implements IProvider {
  38. /** @var IAppManager */
  39. protected $appManager;
  40. /** @var IL10N */
  41. protected $l10n;
  42. /** @var IURLGenerator */
  43. protected $urlGenerator;
  44. /** @var CalDavBackend */
  45. protected $backend;
  46. /**
  47. * ACalendarSearchProvider constructor.
  48. *
  49. * @param IAppManager $appManager
  50. * @param IL10N $l10n
  51. * @param IURLGenerator $urlGenerator
  52. * @param CalDavBackend $backend
  53. */
  54. public function __construct(IAppManager $appManager,
  55. IL10N $l10n,
  56. IURLGenerator $urlGenerator,
  57. CalDavBackend $backend) {
  58. $this->appManager = $appManager;
  59. $this->l10n = $l10n;
  60. $this->urlGenerator = $urlGenerator;
  61. $this->backend = $backend;
  62. }
  63. /**
  64. * Get an associative array of calendars
  65. * calendarId => calendar
  66. *
  67. * @param string $principalUri
  68. * @return array
  69. */
  70. protected function getSortedCalendars(string $principalUri): array {
  71. $calendars = $this->backend->getCalendarsForUser($principalUri);
  72. $calendarsById = [];
  73. foreach ($calendars as $calendar) {
  74. $calendarsById[(int) $calendar['id']] = $calendar;
  75. }
  76. return $calendarsById;
  77. }
  78. /**
  79. * Get an associative array of subscriptions
  80. * subscriptionId => subscription
  81. *
  82. * @param string $principalUri
  83. * @return array
  84. */
  85. protected function getSortedSubscriptions(string $principalUri): array {
  86. $subscriptions = $this->backend->getSubscriptionsForUser($principalUri);
  87. $subscriptionsById = [];
  88. foreach ($subscriptions as $subscription) {
  89. $subscriptionsById[(int) $subscription['id']] = $subscription;
  90. }
  91. return $subscriptionsById;
  92. }
  93. /**
  94. * Returns the primary VEvent / VJournal / VTodo component
  95. * If it's a component with recurrence-ids, it will return
  96. * the primary component
  97. *
  98. * TODO: It would be a nice enhancement to show recurrence-exceptions
  99. * as individual search-results.
  100. * For now we will just display the primary element of a recurrence-set.
  101. *
  102. * @param string $calendarData
  103. * @param string $componentName
  104. * @return Component
  105. */
  106. protected function getPrimaryComponent(string $calendarData, string $componentName): Component {
  107. $vCalendar = Reader::read($calendarData, Reader::OPTION_FORGIVING);
  108. $components = $vCalendar->select($componentName);
  109. if (count($components) === 1) {
  110. return $components[0];
  111. }
  112. // If it's a recurrence-set, take the primary element
  113. foreach ($components as $component) {
  114. /** @var Component $component */
  115. if (!$component->{'RECURRENCE-ID'}) {
  116. return $component;
  117. }
  118. }
  119. // In case of error, just fallback to the first element in the set
  120. return $components[0];
  121. }
  122. }