-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathTasksSearchProvider.php
More file actions
156 lines (135 loc) · 4.25 KB
/
TasksSearchProvider.php
File metadata and controls
156 lines (135 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\DAV\Search;
use OCA\DAV\CalDAV\CalDavBackend;
use OCP\IUser;
use OCP\Search\ISearchQuery;
use OCP\Search\SearchResult;
use OCP\Search\SearchResultEntry;
use Sabre\VObject\Component;
/**
* Class TasksSearchProvider
*
* @package OCA\DAV\Search
*/
class TasksSearchProvider extends ACalendarSearchProvider {
/**
* @var string[]
*/
private const SEARCH_PROPERTIES = [
'SUMMARY',
'DESCRIPTION',
'CATEGORIES',
];
/**
* @var string[]
*/
private const SEARCH_PARAMETERS = [];
/**
* @var string
*/
private const COMPONENT_TYPE = 'VTODO';
/**
* @inheritDoc
*/
public function getId(): string {
return 'tasks';
}
/**
* @inheritDoc
*/
public function getName(): string {
return $this->l10n->t('Tasks');
}
/**
* @inheritDoc
*/
public function getOrder(string $route, array $routeParameters): ?int {
if ($this->appManager->isEnabledForUser('tasks')) {
return $route === 'tasks.Page.index' ? -1 : 35;
}
return null;
}
/**
* @inheritDoc
*/
public function search(
IUser $user,
ISearchQuery $query,
): SearchResult {
if (!$this->appManager->isEnabledForUser('tasks', $user)) {
return SearchResult::complete($this->getName(), []);
}
$principalUri = 'principals/users/' . $user->getUID();
$calendarsById = $this->getSortedCalendars($principalUri);
$subscriptionsById = $this->getSortedSubscriptions($principalUri);
$searchResults = $this->backend->searchPrincipalUri(
$principalUri,
$query->getFilter('term')?->get() ?? '',
[self::COMPONENT_TYPE],
self::SEARCH_PROPERTIES,
self::SEARCH_PARAMETERS,
[
'limit' => $query->getLimit(),
'offset' => $query->getCursor(),
'since' => $query->getFilter('since'),
'until' => $query->getFilter('until'),
]
);
$formattedResults = \array_map(function (array $taskRow) use ($calendarsById, $subscriptionsById):SearchResultEntry {
$component = $this->getPrimaryComponent($taskRow['calendardata'], self::COMPONENT_TYPE);
$title = (string)($component->SUMMARY ?? $this->l10n->t('Untitled task'));
if ($taskRow['calendartype'] === CalDavBackend::CALENDAR_TYPE_CALENDAR) {
$calendar = $calendarsById[$taskRow['calendarid']];
} else {
$calendar = $subscriptionsById[$taskRow['calendarid']];
}
$subline = $this->generateSubline($component, $calendar);
$resourceUrl = $this->getDeepLinkToTasksApp($calendar['uri'], $taskRow['uri']);
return new SearchResultEntry('', $title, $subline, $resourceUrl, 'icon-checkmark', false);
}, $searchResults);
return SearchResult::paginated(
$this->getName(),
$formattedResults,
$query->getCursor() + count($formattedResults)
);
}
protected function getDeepLinkToTasksApp(
string $calendarUri,
string $taskUri,
): string {
return $this->urlGenerator->getAbsoluteURL(
$this->urlGenerator->linkToRoute('tasks.page.index')
. 'calendars/'
. $calendarUri
. '/tasks/'
. $taskUri
);
}
protected function generateSubline(Component $taskComponent, array $calendarInfo): string {
if ($taskComponent->COMPLETED) {
$completedDateTime = new \DateTime($taskComponent->COMPLETED->getDateTime()->format(\DateTimeInterface::ATOM));
$formattedDate = $this->l10n->l('date', $completedDateTime, ['width' => 'medium']);
$formattedSubline = $this->l10n->t('Completed on %s', [$formattedDate]);
} elseif ($taskComponent->DUE) {
$dueDateTime = new \DateTime($taskComponent->DUE->getDateTime()->format(\DateTimeInterface::ATOM));
$formattedDate = $this->l10n->l('date', $dueDateTime, ['width' => 'medium']);
if ($taskComponent->DUE->hasTime()) {
$formattedTime = $this->l10n->l('time', $dueDateTime, ['width' => 'short']);
$formattedSubline = $this->l10n->t('Due on %s by %s', [$formattedDate, $formattedTime]);
} else {
$formattedSubline = $this->l10n->t('Due on %s', [$formattedDate]);
}
} else {
$formattedSubline = '';
}
if (isset($calendarInfo['{DAV:}displayname']) && !empty($calendarInfo['{DAV:}displayname'])) {
$formattedSubline = $formattedSubline . (!empty($formattedSubline) ? ' ' : '') . "({$calendarInfo['{DAV:}displayname']})";
}
return $formattedSubline;
}
}