TasksSearchProvider.php 4.7 KB

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