You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TasksSearchProvider.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020, Georg Ehrke
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Georg Ehrke <oc.list@georgehrke.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author John Molakvoæ <skjnldsv@protonmail.com>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCA\DAV\Search;
  28. use OCA\DAV\CalDAV\CalDavBackend;
  29. use OCP\IUser;
  30. use OCP\Search\ISearchQuery;
  31. use OCP\Search\SearchResult;
  32. use OCP\Search\SearchResultEntry;
  33. use Sabre\VObject\Component;
  34. /**
  35. * Class TasksSearchProvider
  36. *
  37. * @package OCA\DAV\Search
  38. */
  39. class TasksSearchProvider extends ACalendarSearchProvider {
  40. /**
  41. * @var string[]
  42. */
  43. private static $searchProperties = [
  44. 'SUMMARY',
  45. 'DESCRIPTION',
  46. 'CATEGORIES',
  47. ];
  48. /**
  49. * @var string[]
  50. */
  51. private static $searchParameters = [];
  52. /**
  53. * @var string
  54. */
  55. private static $componentType = 'VTODO';
  56. /**
  57. * @inheritDoc
  58. */
  59. public function getId(): string {
  60. return 'tasks';
  61. }
  62. /**
  63. * @inheritDoc
  64. */
  65. public function getName(): string {
  66. return $this->l10n->t('Tasks');
  67. }
  68. /**
  69. * @inheritDoc
  70. */
  71. public function getOrder(string $route, array $routeParameters): ?int {
  72. if ($this->appManager->isEnabledForUser('tasks')) {
  73. return $route === 'tasks.Page.index' ? -1 : 35;
  74. }
  75. return null;
  76. }
  77. /**
  78. * @inheritDoc
  79. */
  80. public function search(
  81. IUser $user,
  82. ISearchQuery $query,
  83. ): SearchResult {
  84. if (!$this->appManager->isEnabledForUser('tasks', $user)) {
  85. return SearchResult::complete($this->getName(), []);
  86. }
  87. $principalUri = 'principals/users/' . $user->getUID();
  88. $calendarsById = $this->getSortedCalendars($principalUri);
  89. $subscriptionsById = $this->getSortedSubscriptions($principalUri);
  90. $searchResults = $this->backend->searchPrincipalUri(
  91. $principalUri,
  92. $query->getFilter('term')?->get() ?? '',
  93. [self::$componentType],
  94. self::$searchProperties,
  95. self::$searchParameters,
  96. [
  97. 'limit' => $query->getLimit(),
  98. 'offset' => $query->getCursor(),
  99. 'since' => $query->getFilter('since'),
  100. 'until' => $query->getFilter('until'),
  101. ]
  102. );
  103. $formattedResults = \array_map(function (array $taskRow) use ($calendarsById, $subscriptionsById):SearchResultEntry {
  104. $component = $this->getPrimaryComponent($taskRow['calendardata'], self::$componentType);
  105. $title = (string)($component->SUMMARY ?? $this->l10n->t('Untitled task'));
  106. $subline = $this->generateSubline($component);
  107. if ($taskRow['calendartype'] === CalDavBackend::CALENDAR_TYPE_CALENDAR) {
  108. $calendar = $calendarsById[$taskRow['calendarid']];
  109. } else {
  110. $calendar = $subscriptionsById[$taskRow['calendarid']];
  111. }
  112. $resourceUrl = $this->getDeepLinkToTasksApp($calendar['uri'], $taskRow['uri']);
  113. return new SearchResultEntry('', $title, $subline, $resourceUrl, 'icon-checkmark', false);
  114. }, $searchResults);
  115. return SearchResult::paginated(
  116. $this->getName(),
  117. $formattedResults,
  118. $query->getCursor() + count($formattedResults)
  119. );
  120. }
  121. protected function getDeepLinkToTasksApp(
  122. string $calendarUri,
  123. string $taskUri,
  124. ): string {
  125. return $this->urlGenerator->getAbsoluteURL(
  126. $this->urlGenerator->linkToRoute('tasks.page.index')
  127. . '#/calendars/'
  128. . $calendarUri
  129. . '/tasks/'
  130. . $taskUri
  131. );
  132. }
  133. protected function generateSubline(Component $taskComponent): string {
  134. if ($taskComponent->COMPLETED) {
  135. $completedDateTime = new \DateTime($taskComponent->COMPLETED->getDateTime()->format(\DateTimeInterface::ATOM));
  136. $formattedDate = $this->l10n->l('date', $completedDateTime, ['width' => 'medium']);
  137. return $this->l10n->t('Completed on %s', [$formattedDate]);
  138. }
  139. if ($taskComponent->DUE) {
  140. $dueDateTime = new \DateTime($taskComponent->DUE->getDateTime()->format(\DateTimeInterface::ATOM));
  141. $formattedDate = $this->l10n->l('date', $dueDateTime, ['width' => 'medium']);
  142. if ($taskComponent->DUE->hasTime()) {
  143. $formattedTime = $this->l10n->l('time', $dueDateTime, ['width' => 'short']);
  144. return $this->l10n->t('Due on %s by %s', [$formattedDate, $formattedTime]);
  145. }
  146. return $this->l10n->t('Due on %s', [$formattedDate]);
  147. }
  148. return '';
  149. }
  150. }