From b0f9f70dd878d0724afee44969049b6a7628fc22 Mon Sep 17 00:00:00 2001 From: Killiane Letellier Date: Mon, 15 Jul 2024 23:26:32 +0200 Subject: [PATCH] Sort events by descending date Signed-off-by: Killiane Letellier --- apps/dav/lib/Search/EventsSearchProvider.php | 43 +++++++++++++++----- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/apps/dav/lib/Search/EventsSearchProvider.php b/apps/dav/lib/Search/EventsSearchProvider.php index 55fba40918a1b..e884c5694394f 100644 --- a/apps/dav/lib/Search/EventsSearchProvider.php +++ b/apps/dav/lib/Search/EventsSearchProvider.php @@ -6,6 +6,7 @@ * 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; @@ -28,7 +29,8 @@ * * @package OCA\DAV\Search */ -class EventsSearchProvider extends ACalendarSearchProvider implements IFilteringProvider { +class EventsSearchProvider extends ACalendarSearchProvider implements IFilteringProvider +{ /** * @var string[] */ @@ -57,21 +59,24 @@ class EventsSearchProvider extends ACalendarSearchProvider implements IFiltering /** * @inheritDoc */ - public function getId(): string { + public function getId(): string + { return 'calendar'; } /** * @inheritDoc */ - public function getName(): string { + public function getName(): string + { return $this->l10n->t('Events'); } /** * @inheritDoc */ - public function getOrder(string $route, array $routeParameters): ?int { + public function getOrder(string $route, array $routeParameters): ?int + { if ($this->appManager->isEnabledForUser('calendar')) { return $route === 'calendar.View.index' ? -1 : 30; } @@ -115,6 +120,7 @@ public function search( ] ); } + /** @var IUser|null $person */ $person = $query->getFilter('person')?->get(); $personDisplayName = $person?->getDisplayName(); @@ -147,6 +153,16 @@ public function search( $searchResults[] = $attendeeResult; } } + + // Sorting the search results by event start date (DTSTART) + usort($searchResults, function ($a, $b) { + $componentA = $this->getPrimaryComponent($a['calendardata'], self::$componentType); + $componentB = $this->getPrimaryComponent($b['calendardata'], self::$componentType); + $dateA = $componentA->DTSTART->getDateTime(); + $dateB = $componentB->DTSTART->getDateTime(); + return $dateB <=> $dateA; + }); + $formattedResults = \array_map(function (array $eventRow) use ($calendarsById, $subscriptionsById): SearchResultEntry { $component = $this->getPrimaryComponent($eventRow['calendardata'], self::$componentType); $title = (string)($component->SUMMARY ?? $this->l10n->t('Untitled event')); @@ -186,8 +202,8 @@ protected function getDeepLinkToCalendarApp( // This route will automatically figure out what recurrence-id to open return $this->urlGenerator->getAbsoluteURL( $this->urlGenerator->linkToRoute('calendar.view.index') - . 'edit/' - . base64_encode($davUrl) + . 'edit/' + . base64_encode($davUrl) ); } @@ -204,7 +220,8 @@ protected function getDavUrlForCalendarObject( . $calendarObjectUri; } - protected function generateSubline(Component $eventComponent): string { + protected function generateSubline(Component $eventComponent): string + { $dtStart = $eventComponent->DTSTART; $dtEnd = $this->getDTEndForEvent($eventComponent); $isAllDayEvent = $dtStart instanceof Property\ICalendar\Date; @@ -234,7 +251,8 @@ protected function generateSubline(Component $eventComponent): string { return "$formattedStartDate $formattedStartTime - $formattedEndDate $formattedEndTime"; } - protected function getDTEndForEvent(Component $eventComponent):Property { + protected function getDTEndForEvent(Component $eventComponent): Property + { if (isset($eventComponent->DTEND)) { $end = $eventComponent->DTEND; } elseif (isset($eventComponent->DURATION)) { @@ -263,7 +281,8 @@ protected function isDayEqual( return $dtStart->format('Y-m-d') === $dtEnd->format('Y-m-d'); } - public function getSupportedFilters(): array { + public function getSupportedFilters(): array + { return [ 'term', 'person', @@ -272,11 +291,13 @@ public function getSupportedFilters(): array { ]; } - public function getAlternateIds(): array { + public function getAlternateIds(): array + { return []; } - public function getCustomFilters(): array { + public function getCustomFilters(): array + { return []; } }