Skip to content

Commit b0f9f70

Browse files
killianeletellierjoshtrichards
authored andcommitted
Sort events by descending date
Signed-off-by: Killiane Letellier <killiane.letellier@mailo.com>
1 parent 6b6401f commit b0f9f70

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

apps/dav/lib/Search/EventsSearchProvider.php

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
77
* SPDX-License-Identifier: AGPL-3.0-or-later
88
*/
9+
910
namespace OCA\DAV\Search;
1011

1112
use OCA\DAV\CalDAV\CalDavBackend;
@@ -28,7 +29,8 @@
2829
*
2930
* @package OCA\DAV\Search
3031
*/
31-
class EventsSearchProvider extends ACalendarSearchProvider implements IFilteringProvider {
32+
class EventsSearchProvider extends ACalendarSearchProvider implements IFilteringProvider
33+
{
3234
/**
3335
* @var string[]
3436
*/
@@ -57,21 +59,24 @@ class EventsSearchProvider extends ACalendarSearchProvider implements IFiltering
5759
/**
5860
* @inheritDoc
5961
*/
60-
public function getId(): string {
62+
public function getId(): string
63+
{
6164
return 'calendar';
6265
}
6366

6467
/**
6568
* @inheritDoc
6669
*/
67-
public function getName(): string {
70+
public function getName(): string
71+
{
6872
return $this->l10n->t('Events');
6973
}
7074

7175
/**
7276
* @inheritDoc
7377
*/
74-
public function getOrder(string $route, array $routeParameters): ?int {
78+
public function getOrder(string $route, array $routeParameters): ?int
79+
{
7580
if ($this->appManager->isEnabledForUser('calendar')) {
7681
return $route === 'calendar.View.index' ? -1 : 30;
7782
}
@@ -115,6 +120,7 @@ public function search(
115120
]
116121
);
117122
}
123+
118124
/** @var IUser|null $person */
119125
$person = $query->getFilter('person')?->get();
120126
$personDisplayName = $person?->getDisplayName();
@@ -147,6 +153,16 @@ public function search(
147153
$searchResults[] = $attendeeResult;
148154
}
149155
}
156+
157+
// Sorting the search results by event start date (DTSTART)
158+
usort($searchResults, function ($a, $b) {
159+
$componentA = $this->getPrimaryComponent($a['calendardata'], self::$componentType);
160+
$componentB = $this->getPrimaryComponent($b['calendardata'], self::$componentType);
161+
$dateA = $componentA->DTSTART->getDateTime();
162+
$dateB = $componentB->DTSTART->getDateTime();
163+
return $dateB <=> $dateA;
164+
});
165+
150166
$formattedResults = \array_map(function (array $eventRow) use ($calendarsById, $subscriptionsById): SearchResultEntry {
151167
$component = $this->getPrimaryComponent($eventRow['calendardata'], self::$componentType);
152168
$title = (string)($component->SUMMARY ?? $this->l10n->t('Untitled event'));
@@ -186,8 +202,8 @@ protected function getDeepLinkToCalendarApp(
186202
// This route will automatically figure out what recurrence-id to open
187203
return $this->urlGenerator->getAbsoluteURL(
188204
$this->urlGenerator->linkToRoute('calendar.view.index')
189-
. 'edit/'
190-
. base64_encode($davUrl)
205+
. 'edit/'
206+
. base64_encode($davUrl)
191207
);
192208
}
193209

@@ -204,7 +220,8 @@ protected function getDavUrlForCalendarObject(
204220
. $calendarObjectUri;
205221
}
206222

207-
protected function generateSubline(Component $eventComponent): string {
223+
protected function generateSubline(Component $eventComponent): string
224+
{
208225
$dtStart = $eventComponent->DTSTART;
209226
$dtEnd = $this->getDTEndForEvent($eventComponent);
210227
$isAllDayEvent = $dtStart instanceof Property\ICalendar\Date;
@@ -234,7 +251,8 @@ protected function generateSubline(Component $eventComponent): string {
234251
return "$formattedStartDate $formattedStartTime - $formattedEndDate $formattedEndTime";
235252
}
236253

237-
protected function getDTEndForEvent(Component $eventComponent):Property {
254+
protected function getDTEndForEvent(Component $eventComponent): Property
255+
{
238256
if (isset($eventComponent->DTEND)) {
239257
$end = $eventComponent->DTEND;
240258
} elseif (isset($eventComponent->DURATION)) {
@@ -263,7 +281,8 @@ protected function isDayEqual(
263281
return $dtStart->format('Y-m-d') === $dtEnd->format('Y-m-d');
264282
}
265283

266-
public function getSupportedFilters(): array {
284+
public function getSupportedFilters(): array
285+
{
267286
return [
268287
'term',
269288
'person',
@@ -272,11 +291,13 @@ public function getSupportedFilters(): array {
272291
];
273292
}
274293

275-
public function getAlternateIds(): array {
294+
public function getAlternateIds(): array
295+
{
276296
return [];
277297
}
278298

279-
public function getCustomFilters(): array {
299+
public function getCustomFilters(): array
300+
{
280301
return [];
281302
}
282303
}

0 commit comments

Comments
 (0)