Skip to content

Commit 277a35e

Browse files
feat: add calendar name to search entries
Signed-off-by: SebastianKrupinski <krupinskis05@gmail.com>
1 parent f6c79c0 commit 277a35e

5 files changed

Lines changed: 59 additions & 48 deletions

File tree

apps/dav/lib/Search/EventsSearchProvider.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,13 @@ public function search(
150150
$formattedResults = \array_map(function (array $eventRow) use ($calendarsById, $subscriptionsById): SearchResultEntry {
151151
$component = $this->getPrimaryComponent($eventRow['calendardata'], self::$componentType);
152152
$title = (string)($component->SUMMARY ?? $this->l10n->t('Untitled event'));
153-
$subline = $this->generateSubline($component);
154153

155154
if ($eventRow['calendartype'] === CalDavBackend::CALENDAR_TYPE_CALENDAR) {
156155
$calendar = $calendarsById[$eventRow['calendarid']];
157156
} else {
158157
$calendar = $subscriptionsById[$eventRow['calendarid']];
159158
}
159+
$subline = $this->generateSubline($component, $calendar);
160160
$resourceUrl = $this->getDeepLinkToCalendarApp($calendar['principaluri'], $calendar['uri'], $eventRow['uri']);
161161
$result = new SearchResultEntry('', $title, $subline, $resourceUrl, 'icon-calendar-dark', false);
162162

@@ -204,7 +204,7 @@ protected function getDavUrlForCalendarObject(
204204
. $calendarObjectUri;
205205
}
206206

207-
protected function generateSubline(Component $eventComponent): string {
207+
protected function generateSubline(Component $eventComponent, array $calendarInfo): string {
208208
$dtStart = $eventComponent->DTSTART;
209209
$dtEnd = $this->getDTEndForEvent($eventComponent);
210210
$isAllDayEvent = $dtStart instanceof Property\ICalendar\Date;
@@ -214,24 +214,31 @@ protected function generateSubline(Component $eventComponent): string {
214214
if ($isAllDayEvent) {
215215
$endDateTime->modify('-1 day');
216216
if ($this->isDayEqual($startDateTime, $endDateTime)) {
217-
return $this->l10n->l('date', $startDateTime, ['width' => 'medium']);
217+
$formattedSubline = $this->l10n->l('date', $startDateTime, ['width' => 'medium']);
218+
} else {
219+
$formattedStart = $this->l10n->l('date', $startDateTime, ['width' => 'medium']);
220+
$formattedEnd = $this->l10n->l('date', $endDateTime, ['width' => 'medium']);
221+
$formattedSubline = "$formattedStart - $formattedEnd";
218222
}
223+
} else {
224+
$formattedStartDate = $this->l10n->l('date', $startDateTime, ['width' => 'medium']);
225+
$formattedEndDate = $this->l10n->l('date', $endDateTime, ['width' => 'medium']);
226+
$formattedStartTime = $this->l10n->l('time', $startDateTime, ['width' => 'short']);
227+
$formattedEndTime = $this->l10n->l('time', $endDateTime, ['width' => 'short']);
219228

220-
$formattedStart = $this->l10n->l('date', $startDateTime, ['width' => 'medium']);
221-
$formattedEnd = $this->l10n->l('date', $endDateTime, ['width' => 'medium']);
222-
return "$formattedStart - $formattedEnd";
229+
if ($this->isDayEqual($startDateTime, $endDateTime)) {
230+
$formattedSubline = "$formattedStartDate $formattedStartTime - $formattedEndTime";
231+
} else {
232+
$formattedSubline = "$formattedStartDate $formattedStartTime - $formattedEndDate $formattedEndTime";
233+
}
223234
}
224235

225-
$formattedStartDate = $this->l10n->l('date', $startDateTime, ['width' => 'medium']);
226-
$formattedEndDate = $this->l10n->l('date', $endDateTime, ['width' => 'medium']);
227-
$formattedStartTime = $this->l10n->l('time', $startDateTime, ['width' => 'short']);
228-
$formattedEndTime = $this->l10n->l('time', $endDateTime, ['width' => 'short']);
229-
230-
if ($this->isDayEqual($startDateTime, $endDateTime)) {
231-
return "$formattedStartDate $formattedStartTime - $formattedEndTime";
236+
if (isset($calendarInfo['{DAV:}displayname']) && !empty($calendarInfo['{DAV:}displayname'])) {
237+
$formattedSubline = $formattedSubline . " ({$calendarInfo['{DAV:}displayname']})";
232238
}
233239

234-
return "$formattedStartDate $formattedStartTime - $formattedEndDate $formattedEndTime";
240+
// string cast is just to make psalm happy
241+
return (string)$formattedSubline;
235242
}
236243

237244
protected function getDTEndForEvent(Component $eventComponent):Property {

apps/dav/lib/Search/TasksSearchProvider.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@ public function search(
9696
$formattedResults = \array_map(function (array $taskRow) use ($calendarsById, $subscriptionsById):SearchResultEntry {
9797
$component = $this->getPrimaryComponent($taskRow['calendardata'], self::$componentType);
9898
$title = (string)($component->SUMMARY ?? $this->l10n->t('Untitled task'));
99-
$subline = $this->generateSubline($component);
10099

101100
if ($taskRow['calendartype'] === CalDavBackend::CALENDAR_TYPE_CALENDAR) {
102101
$calendar = $calendarsById[$taskRow['calendarid']];
103102
} else {
104103
$calendar = $subscriptionsById[$taskRow['calendarid']];
105104
}
105+
$subline = $this->generateSubline($component, $calendar);
106106
$resourceUrl = $this->getDeepLinkToTasksApp($calendar['uri'], $taskRow['uri']);
107107

108108
return new SearchResultEntry('', $title, $subline, $resourceUrl, 'icon-checkmark', false);
@@ -128,25 +128,29 @@ protected function getDeepLinkToTasksApp(
128128
);
129129
}
130130

131-
protected function generateSubline(Component $taskComponent): string {
131+
protected function generateSubline(Component $taskComponent, array $calendarInfo): string {
132132
if ($taskComponent->COMPLETED) {
133133
$completedDateTime = new \DateTime($taskComponent->COMPLETED->getDateTime()->format(\DateTimeInterface::ATOM));
134134
$formattedDate = $this->l10n->l('date', $completedDateTime, ['width' => 'medium']);
135-
return $this->l10n->t('Completed on %s', [$formattedDate]);
136-
}
137-
138-
if ($taskComponent->DUE) {
135+
$formattedSubline = $this->l10n->t('Completed on %s', [$formattedDate]);
136+
} elseif ($taskComponent->DUE) {
139137
$dueDateTime = new \DateTime($taskComponent->DUE->getDateTime()->format(\DateTimeInterface::ATOM));
140138
$formattedDate = $this->l10n->l('date', $dueDateTime, ['width' => 'medium']);
141139

142140
if ($taskComponent->DUE->hasTime()) {
143141
$formattedTime = $this->l10n->l('time', $dueDateTime, ['width' => 'short']);
144-
return $this->l10n->t('Due on %s by %s', [$formattedDate, $formattedTime]);
142+
$formattedSubline = $this->l10n->t('Due on %s by %s', [$formattedDate, $formattedTime]);
143+
} else {
144+
$formattedSubline = $this->l10n->t('Due on %s', [$formattedDate]);
145145
}
146+
} else {
147+
$formattedSubline = '';
148+
}
146149

147-
return $this->l10n->t('Due on %s', [$formattedDate]);
150+
if (isset($calendarInfo['{DAV:}displayname']) && !empty($calendarInfo['{DAV:}displayname'])) {
151+
$formattedSubline = $formattedSubline . (!empty($formattedSubline) ? ' ' : '') . "({$calendarInfo['{DAV:}displayname']})";
148152
}
149153

150-
return '';
154+
return $formattedSubline;
151155
}
152156
}

apps/dav/tests/unit/Search/EventsSearchProviderTest.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ public function testGetDeepLinkToCalendarApp(
436436
}
437437

438438
#[\PHPUnit\Framework\Attributes\DataProvider(methodName: 'generateSublineDataProvider')]
439-
public function testGenerateSubline(string $ics, string $expectedSubline): void {
439+
public function testGenerateSubline(string $ics, string $expectedSubline, array $calendarInfo = []): void {
440440
$vCalendar = Reader::read($ics, Reader::OPTION_FORGIVING);
441441
$eventComponent = $vCalendar->VEVENT;
442442

@@ -449,19 +449,23 @@ public function testGenerateSubline(string $ics, string $expectedSubline): void
449449
return $date->format('m-d');
450450
});
451451

452-
$actual = self::invokePrivate($this->provider, 'generateSubline', [$eventComponent]);
452+
$actual = self::invokePrivate($this->provider, 'generateSubline', [$eventComponent, $calendarInfo]);
453453
$this->assertEquals($expectedSubline, $actual);
454454
}
455455

456456
public static function generateSublineDataProvider(): array {
457457
return [
458-
[self::$vEvent1, '08-16 09:00 - 10:00'],
459-
[self::$vEvent2, '08-16 09:00 - 08-17 10:00'],
460-
[self::$vEvent3, '10-05'],
461-
[self::$vEvent4, '10-05 - 10-07'],
462-
[self::$vEvent5, '10-05 - 10-09'],
463-
[self::$vEvent6, '10-05'],
464-
[self::$vEvent7, '08-16 09:00 - 09:00'],
458+
[self::$vEvent1, '08-16 09:00 - 10:00', []],
459+
[self::$vEvent2, '08-16 09:00 - 08-17 10:00', []],
460+
[self::$vEvent3, '10-05', []],
461+
[self::$vEvent4, '10-05 - 10-07', []],
462+
[self::$vEvent5, '10-05 - 10-09', []],
463+
[self::$vEvent6, '10-05', []],
464+
[self::$vEvent7, '08-16 09:00 - 09:00', []],
465+
[self::$vEvent1, '08-16 09:00 - 10:00 (My Calendar)', ['{DAV:}displayname' => 'My Calendar']],
466+
[self::$vEvent3, '10-05 (My Calendar)', ['{DAV:}displayname' => 'My Calendar']],
467+
[self::$vEvent2, '08-16 09:00 - 08-17 10:00 (My Calendar)', ['{DAV:}displayname' => 'My Calendar']],
468+
[self::$vEvent1, '08-16 09:00 - 10:00', ['{DAV:}displayname' => '']],
465469
];
466470
}
467471
}

apps/dav/tests/unit/Search/TasksSearchProviderTest.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -290,24 +290,29 @@ public function testGetDeepLinkToTasksApp(): void {
290290
}
291291

292292
#[\PHPUnit\Framework\Attributes\DataProvider(methodName: 'generateSublineDataProvider')]
293-
public function testGenerateSubline(string $ics, string $expectedSubline): void {
293+
public function testGenerateSubline(string $ics, string $expectedSubline, array $calendarInfo = []): void {
294294
$vCalendar = Reader::read($ics, Reader::OPTION_FORGIVING);
295295
$taskComponent = $vCalendar->VTODO;
296296

297297
$this->l10n->method('t')->willReturnArgument(0);
298298
$this->l10n->method('l')->willReturnArgument(0);
299299

300-
$actual = self::invokePrivate($this->provider, 'generateSubline', [$taskComponent]);
300+
$actual = self::invokePrivate($this->provider, 'generateSubline', [$taskComponent, $calendarInfo]);
301301
$this->assertEquals($expectedSubline, $actual);
302302
}
303303

304304
public static function generateSublineDataProvider(): array {
305305
return [
306-
[self::$vTodo0, ''],
307-
[self::$vTodo1, 'Completed on %s'],
308-
[self::$vTodo2, 'Completed on %s'],
309-
[self::$vTodo3, 'Due on %s'],
310-
[self::$vTodo4, 'Due on %s by %s'],
306+
[self::$vTodo0, '', []],
307+
[self::$vTodo1, 'Completed on %s', []],
308+
[self::$vTodo2, 'Completed on %s', []],
309+
[self::$vTodo3, 'Due on %s', []],
310+
[self::$vTodo4, 'Due on %s by %s', []],
311+
[self::$vTodo0, '(My Tasks)', ['{DAV:}displayname' => 'My Tasks']],
312+
[self::$vTodo1, 'Completed on %s (My Tasks)', ['{DAV:}displayname' => 'My Tasks']],
313+
[self::$vTodo3, 'Due on %s (My Tasks)', ['{DAV:}displayname' => 'My Tasks']],
314+
[self::$vTodo4, 'Due on %s by %s (My Tasks)', ['{DAV:}displayname' => 'My Tasks']],
315+
[self::$vTodo1, 'Completed on %s', ['{DAV:}displayname' => '']],
311316
];
312317
}
313318
}

build/psalm-baseline.xml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -839,9 +839,6 @@
839839
</InvalidOperand>
840840
</file>
841841
<file src="apps/dav/lib/Search/EventsSearchProvider.php">
842-
<FalsableReturnStatement>
843-
<code><![CDATA[$this->l10n->l('date', $startDateTime, ['width' => 'medium'])]]></code>
844-
</FalsableReturnStatement>
845842
<InvalidOperand>
846843
<code><![CDATA[$query->getCursor()]]></code>
847844
</InvalidOperand>
@@ -851,12 +848,6 @@
851848
'ORGANIZER' => ['CN'],
852849
]]]></code>
853850
</InvalidPropertyAssignmentValue>
854-
<InvalidReturnStatement>
855-
<code><![CDATA[$this->l10n->l('date', $startDateTime, ['width' => 'medium'])]]></code>
856-
</InvalidReturnStatement>
857-
<InvalidReturnType>
858-
<code><![CDATA[string]]></code>
859-
</InvalidReturnType>
860851
<UndefinedMethod>
861852
<code><![CDATA[getDateTime]]></code>
862853
<code><![CDATA[getDateTime]]></code>

0 commit comments

Comments
 (0)