Skip to content

Commit

Permalink
Sort events by descending date
Browse files Browse the repository at this point in the history
Signed-off-by: Killiane Letellier <[email protected]>
  • Loading branch information
killianeletellier authored and joshtrichards committed Jan 26, 2025
1 parent 6b6401f commit b0f9f70
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions apps/dav/lib/Search/EventsSearchProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,7 +29,8 @@
*
* @package OCA\DAV\Search
*/
class EventsSearchProvider extends ACalendarSearchProvider implements IFilteringProvider {
class EventsSearchProvider extends ACalendarSearchProvider implements IFilteringProvider
{
/**
* @var string[]
*/
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -115,6 +120,7 @@ public function search(
]
);
}

/** @var IUser|null $person */
$person = $query->getFilter('person')?->get();
$personDisplayName = $person?->getDisplayName();
Expand Down Expand Up @@ -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'));
Expand Down Expand Up @@ -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)
);
}

Expand All @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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',
Expand All @@ -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 [];
}
}

0 comments on commit b0f9f70

Please sign in to comment.