Passed
Push — master ( 058135...a4d511 )
by John
13:28 queued 12s
created

TasksSearchProvider::getOrder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2020, Georg Ehrke
7
 *
8
 * @author Georg Ehrke <[email protected]>
9
 *
10
 * @license AGPL-3.0
11
 *
12
 * This code is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License, version 3,
14
 * as published by the Free Software Foundation.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License, version 3,
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>
23
 *
24
 */
25
namespace OCA\DAV\Search;
26
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
/**
35
 * Class TasksSearchProvider
36
 *
37
 * @package OCA\DAV\Search
38
 */
39
class TasksSearchProvider extends ACalendarSearchProvider {
40
41
	/**
42
	 * @var string[]
43
	 */
44
	private static $searchProperties = [
45
		'SUMMARY',
46
		'DESCRIPTION',
47
		'CATEGORIES',
48
	];
49
50
	/**
51
	 * @var string[]
52
	 */
53
	private static $searchParameters = [];
54
55
	/**
56
	 * @var string
57
	 */
58
	private static $componentType = 'VTODO';
59
60
	/**
61
	 * @inheritDoc
62
	 */
63
	public function getId(): string {
64
		return 'tasks-dav';
65
	}
66
67
	/**
68
	 * @inheritDoc
69
	 */
70
	public function getName(): string {
71
		return $this->l10n->t('Tasks');
72
	}
73
74
	/**
75
	 * @inheritDoc
76
	 */
77
	public function getOrder(): int {
78
		return 10;
79
	}
80
81
	/**
82
	 * @inheritDoc
83
	 */
84
	public function search(IUser $user,
85
						   ISearchQuery $query): SearchResult {
86
		if (!$this->appManager->isEnabledForUser('tasks', $user)) {
87
			return SearchResult::complete($this->getName(), []);
88
		}
89
90
		$principalUri = 'principals/users/' . $user->getUID();
91
		$calendarsById = $this->getSortedCalendars($principalUri);
92
		$subscriptionsById = $this->getSortedSubscriptions($principalUri);
93
94
		$searchResults = $this->backend->searchPrincipalUri(
95
			$principalUri,
96
			$query->getTerm(),
97
			[self::$componentType],
98
			self::$searchProperties,
99
			self::$searchParameters,
100
			[
101
				'limit' => $query->getLimit(),
102
				'offset' => $query->getCursor(),
103
			]
104
		);
105
		$formattedResults = \array_map(function (array $taskRow) use ($calendarsById, $subscriptionsById):SearchResultEntry {
106
			$component = $this->getPrimaryComponent($taskRow['calendardata'], self::$componentType);
107
			$title = (string)($component->SUMMARY ?? $this->l10n->t('Untitled task'));
108
			$subline = $this->generateSubline($component);
109
110
			if ($taskRow['calendartype'] === CalDavBackend::CALENDAR_TYPE_CALENDAR) {
111
				$calendar = $calendarsById[$taskRow['calendarid']];
112
			} else {
113
				$calendar = $subscriptionsById[$taskRow['calendarid']];
114
			}
115
			$resourceUrl = $this->getDeepLinkToTasksApp($calendar['uri'], $taskRow['uri']);
116
117
			return new SearchResultEntry('', $title, $subline, $resourceUrl, 'icon-checkmark', false);
118
		}, $searchResults);
119
120
		return SearchResult::paginated(
121
			$this->getName(),
122
			$formattedResults,
123
			$query->getCursor() + count($formattedResults)
124
		);
125
	}
126
127
	/**
128
	 * @param string $calendarUri
129
	 * @param string $taskUri
130
	 * @return string
131
	 */
132
	protected function getDeepLinkToTasksApp(string $calendarUri,
133
											 string $taskUri): string {
134
		return $this->urlGenerator->getAbsoluteURL(
135
			$this->urlGenerator->linkToRoute('tasks.page.index')
136
			. '#/calendars/'
137
			. $calendarUri
138
			. '/tasks/'
139
			. $taskUri
140
		);
141
	}
142
143
	/**
144
	 * @param Component $taskComponent
145
	 * @return string
146
	 */
147
	protected function generateSubline(Component $taskComponent): string {
148
		if ($taskComponent->COMPLETED) {
149
			$completedDateTime = new \DateTime($taskComponent->COMPLETED->getDateTime()->format(\DateTime::ATOM));
0 ignored issues
show
Bug introduced by
The method getDateTime() does not exist on Sabre\VObject\Property. It seems like you code against a sub-type of Sabre\VObject\Property such as Sabre\VObject\Property\ICalendar\DateTime or Sabre\VObject\Property\VCard\DateAndOrTime. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

149
			$completedDateTime = new \DateTime($taskComponent->COMPLETED->/** @scrutinizer ignore-call */ getDateTime()->format(\DateTime::ATOM));
Loading history...
150
			$formattedDate = $this->l10n->l('date', $completedDateTime, ['width' => 'medium']);
151
			return $this->l10n->t('Completed on %s', [$formattedDate]);
152
		}
153
154
		if ($taskComponent->DUE) {
155
			$dueDateTime = new \DateTime($taskComponent->DUE->getDateTime()->format(\DateTime::ATOM));
156
			$formattedDate = $this->l10n->l('date', $dueDateTime, ['width' => 'medium']);
157
158
			if ($taskComponent->DUE->hasTime()) {
0 ignored issues
show
Bug introduced by
The method hasTime() does not exist on Sabre\VObject\Property. It seems like you code against a sub-type of Sabre\VObject\Property such as Sabre\VObject\Property\ICalendar\DateTime. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

158
			if ($taskComponent->DUE->/** @scrutinizer ignore-call */ hasTime()) {
Loading history...
159
				$formattedTime =  $this->l10n->l('time', $dueDateTime, ['width' => 'short']);
160
				return $this->l10n->t('Due on %s by %s', [$formattedDate, $formattedTime]);
161
			}
162
163
			return $this->l10n->t('Due on %s', [$formattedDate]);
164
		}
165
166
		return '';
167
	}
168
}
169