|
40 | 40 | namespace OCA\DAV\CalDAV; |
41 | 41 |
|
42 | 42 | use DateTime; |
| 43 | +use DateTimeImmutable; |
43 | 44 | use DateTimeInterface; |
44 | 45 | use OCA\DAV\AppInfo\Application; |
45 | 46 | use OCA\DAV\CalDAV\Sharing\Backend; |
@@ -1965,6 +1966,10 @@ public function search( |
1965 | 1966 |
|
1966 | 1967 | $outerQuery->andWhere($outerQuery->expr()->in('c.id', $outerQuery->createFunction($innerQuery->getSQL()))); |
1967 | 1968 |
|
| 1969 | + // Without explicit order by its undefined in which order the SQL server returns the events. |
| 1970 | + // For the pagination with hasLimit and hasTimeRange, a stable ordering is helpful. |
| 1971 | + $outerQuery->addOrderBy('id'); |
| 1972 | + |
1968 | 1973 | $offset = (int)$offset; |
1969 | 1974 | $outerQuery->setFirstResult($offset); |
1970 | 1975 |
|
@@ -2000,7 +2005,7 @@ public function search( |
2000 | 2005 | $calendarObjects = $this->searchCalendarObjects($outerQuery, $start, $end); |
2001 | 2006 | } |
2002 | 2007 |
|
2003 | | - return array_map(function ($o) use ($options) { |
| 2008 | + $calendarObjects = array_map(function ($o) use ($options) { |
2004 | 2009 | $calendarData = Reader::read($o['calendardata']); |
2005 | 2010 |
|
2006 | 2011 | // Expand recurrences if an explicit time range is requested |
@@ -2036,6 +2041,17 @@ public function search( |
2036 | 2041 | }, $timezones), |
2037 | 2042 | ]; |
2038 | 2043 | }, $calendarObjects); |
| 2044 | + |
| 2045 | + usort($calendarObjects, function (array $a, array $b) { |
| 2046 | + /** @var DateTimeImmutable $startA */ |
| 2047 | + $startA = $a['objects'][0]['DTSTART'][0] ?? new DateTimeImmutable(self::MAX_DATE); |
| 2048 | + /** @var DateTimeImmutable $startB */ |
| 2049 | + $startB = $b['objects'][0]['DTSTART'][0] ?? new DateTimeImmutable(self::MAX_DATE); |
| 2050 | + |
| 2051 | + return $startA->getTimestamp() <=> $startB->getTimestamp(); |
| 2052 | + }); |
| 2053 | + |
| 2054 | + return $calendarObjects; |
2039 | 2055 | } |
2040 | 2056 |
|
2041 | 2057 | private function searchCalendarObjects(IQueryBuilder $query, DateTimeInterface|null $start, DateTimeInterface|null $end): array { |
|
0 commit comments