66 * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
77 * SPDX-License-Identifier: AGPL-3.0-or-later
88 */
9+
910namespace OCA \DAV \Search ;
1011
1112use OCA \DAV \CalDAV \CalDavBackend ;
2728 *
2829 * @package OCA\DAV\Search
2930 */
30- class EventsSearchProvider extends ACalendarSearchProvider implements IFilteringProvider {
31+ class EventsSearchProvider extends ACalendarSearchProvider implements IFilteringProvider
32+ {
3133 /**
3234 * @var string[]
3335 */
@@ -56,21 +58,24 @@ class EventsSearchProvider extends ACalendarSearchProvider implements IFiltering
5658 /**
5759 * @inheritDoc
5860 */
59- public function getId (): string {
61+ public function getId (): string
62+ {
6063 return 'calendar ' ;
6164 }
6265
6366 /**
6467 * @inheritDoc
6568 */
66- public function getName (): string {
69+ public function getName (): string
70+ {
6771 return $ this ->l10n ->t ('Events ' );
6872 }
6973
7074 /**
7175 * @inheritDoc
7276 */
73- public function getOrder (string $ route , array $ routeParameters ): ?int {
77+ public function getOrder (string $ route , array $ routeParameters ): ?int
78+ {
7479 if ($ this ->appManager ->isEnabledForUser ('calendar ' )) {
7580 return $ route === 'calendar.View.index ' ? -1 : 30 ;
7681 }
@@ -114,6 +119,7 @@ public function search(
114119 ]
115120 );
116121 }
122+
117123 /** @var IUser|null $person */
118124 $ person = $ query ->getFilter ('person ' )?->get();
119125 $ personDisplayName = $ person ?->getDisplayName();
@@ -146,6 +152,16 @@ public function search(
146152 $ searchResults [] = $ attendeeResult ;
147153 }
148154 }
155+
156+ // Sorting the search results by event start date (DTSTART)
157+ usort ($ searchResults , function ($ a , $ b ) {
158+ $ componentA = $ this ->getPrimaryComponent ($ a ['calendardata ' ], self ::$ componentType );
159+ $ componentB = $ this ->getPrimaryComponent ($ b ['calendardata ' ], self ::$ componentType );
160+ $ dateA = $ componentA ->DTSTART ->getDateTime ();
161+ $ dateB = $ componentB ->DTSTART ->getDateTime ();
162+ return $ dateB <=> $ dateA ;
163+ });
164+
149165 $ formattedResults = \array_map (function (array $ eventRow ) use ($ calendarsById , $ subscriptionsById ): SearchResultEntry {
150166 $ component = $ this ->getPrimaryComponent ($ eventRow ['calendardata ' ], self ::$ componentType );
151167 $ title = (string )($ component ->SUMMARY ?? $ this ->l10n ->t ('Untitled event ' ));
@@ -177,8 +193,8 @@ protected function getDeepLinkToCalendarApp(
177193 // This route will automatically figure out what recurrence-id to open
178194 return $ this ->urlGenerator ->getAbsoluteURL (
179195 $ this ->urlGenerator ->linkToRoute ('calendar.view.index ' )
180- . 'edit/ '
181- . base64_encode ($ davUrl )
196+ . 'edit/ '
197+ . base64_encode ($ davUrl )
182198 );
183199 }
184200
@@ -195,7 +211,8 @@ protected function getDavUrlForCalendarObject(
195211 . $ calendarObjectUri ;
196212 }
197213
198- protected function generateSubline (Component $ eventComponent ): string {
214+ protected function generateSubline (Component $ eventComponent ): string
215+ {
199216 $ dtStart = $ eventComponent ->DTSTART ;
200217 $ dtEnd = $ this ->getDTEndForEvent ($ eventComponent );
201218 $ isAllDayEvent = $ dtStart instanceof Property \ICalendar \Date;
@@ -225,7 +242,8 @@ protected function generateSubline(Component $eventComponent): string {
225242 return "$ formattedStartDate $ formattedStartTime - $ formattedEndDate $ formattedEndTime " ;
226243 }
227244
228- protected function getDTEndForEvent (Component $ eventComponent ):Property {
245+ protected function getDTEndForEvent (Component $ eventComponent ): Property
246+ {
229247 if (isset ($ eventComponent ->DTEND )) {
230248 $ end = $ eventComponent ->DTEND ;
231249 } elseif (isset ($ eventComponent ->DURATION )) {
@@ -254,7 +272,8 @@ protected function isDayEqual(
254272 return $ dtStart ->format ('Y-m-d ' ) === $ dtEnd ->format ('Y-m-d ' );
255273 }
256274
257- public function getSupportedFilters (): array {
275+ public function getSupportedFilters (): array
276+ {
258277 return [
259278 'term ' ,
260279 'person ' ,
@@ -263,11 +282,13 @@ public function getSupportedFilters(): array {
263282 ];
264283 }
265284
266- public function getAlternateIds (): array {
285+ public function getAlternateIds (): array
286+ {
267287 return [];
268288 }
269289
270- public function getCustomFilters (): array {
290+ public function getCustomFilters (): array
291+ {
271292 return [];
272293 }
273294}
0 commit comments