TasksSearchProvider.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 ($route === 'tasks.Page.index') {
  73. return -1;
  74. }
  75. return 35;
  76. }
  77. /**
  78. * @inheritDoc
  79. */
  80. public function search(IUser $user,
  81. ISearchQuery $query): SearchResult {
  82. if (!$this->appManager->isEnabledForUser('tasks', $user)) {
  83. return SearchResult::complete($this->getName(), []);
  84. }
  85. $principalUri = 'principals/users/' . $user->getUID();
  86. $calendarsById = $this->getSortedCalendars($principalUri);
  87. $subscriptionsById = $this->getSortedSubscriptions($principalUri);
  88. $searchResults = $this->backend->searchPrincipalUri(
  89. $principalUri,
  90. $query->getTerm(),
  91. [self::$componentType],
  92. self::$searchProperties,
  93. self::$searchParameters,
  94. [
  95. 'limit' => $query->getLimit(),
  96. 'offset' => $query->getCursor(),
  97. ]
  98. );
  99. $formattedResults = \array_map(function (array $taskRow) use ($calendarsById, $subscriptionsById):SearchResultEntry {
  100. $component = $this->getPrimaryComponent($taskRow['calendardata'], self::$componentType);
  101. $title = (string)($component->SUMMARY ?? $this->l10n->t('Untitled task'));
  102. $subline = $this->generateSubline($component);
  103. if ($taskRow['calendartype'] === CalDavBackend::CALENDAR_TYPE_CALENDAR) {
  104. $calendar = $calendarsById[$taskRow['calendarid']];
  105. } else {
  106. $calendar = $subscriptionsById[$taskRow['calendarid']];
  107. }
  108. $resourceUrl = $this->getDeepLinkToTasksApp($calendar['uri'], $taskRow['uri']);
  109. return new SearchResultEntry('', $title, $subline, $resourceUrl, 'icon-checkmark', false);
  110. }, $searchResults);
  111. return SearchResult::paginated(
  112. $this->getName(),
  113. $formattedResults,
  114. $query->getCursor() + count($formattedResults)
  115. );
  116. }
  117. /**
  118. * @param string $calendarUri
  119. * @param string $taskUri
  120. * @return string
  121. */
  122. protected function getDeepLinkToTasksApp(string $calendarUri,
  123. string $taskUri): string {
  124. return $this->urlGenerator->getAbsoluteURL(
  125. $this->urlGenerator->linkToRoute('tasks.page.index')
  126. . '#/calendars/'
  127. . $calendarUri
  128. . '/tasks/'
  129. . $taskUri
  130. );
  131. }
  132. /**
  133. * @param Component $taskComponent
  134. * @return string
  135. */
  136. protected function generateSubline(Component $taskComponent): string {
  137. if ($taskComponent->COMPLETED) {
  138. $completedDateTime = new \DateTime($taskComponent->COMPLETED->getDateTime()->format(\DateTimeInterface::ATOM));
  139. $formattedDate = $this->l10n->l('date', $completedDateTime, ['width' => 'medium']);
  140. return $this->l10n->t('Completed on %s', [$formattedDate]);
  141. }
  142. if ($taskComponent->DUE) {
  143. $dueDateTime = new \DateTime($taskComponent->DUE->getDateTime()->format(\DateTimeInterface::ATOM));
  144. $formattedDate = $this->l10n->l('date', $dueDateTime, ['width' => 'medium']);
  145. if ($taskComponent->DUE->hasTime()) {
  146. $formattedTime = $this->l10n->l('time', $dueDateTime, ['width' => 'short']);
  147. return $this->l10n->t('Due on %s by %s', [$formattedDate, $formattedTime]);
  148. }
  149. return $this->l10n->t('Due on %s', [$formattedDate]);
  150. }
  151. return '';
  152. }
  153. }