Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clone event in grouped finder #8

Merged
merged 5 commits into from
Apr 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 12 additions & 31 deletions src/Controller/Component/CalendarComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace Chialab\Calendar\Controller\Component;

use BEdita\Core\Model\Entity\DateRange;
use BEdita\Core\Model\Entity\ObjectEntity;
use Cake\Controller\Component;
use Cake\Database\Expression\FunctionExpression;
Expand Down Expand Up @@ -381,8 +380,8 @@ public function findGroupedByDay(Query $query, FrozenTime $from, FrozenTime|null
->find('closingDays')
->formatResults(function (iterable $results) use ($from, $to): iterable {
$grouped = collection($results)
->unfold(function (ObjectEntity $event) use ($from, $to): Generator {
foreach ($event->filtered_date_ranges as $dr) {
->unfold(function (ObjectEntity $object) use ($from, $to): Generator {
foreach ($object->filtered_date_ranges as $dr) {
$start = (new FrozenTime($dr->start_date))->startOfDay();
$end = (new FrozenTime($dr->end_date ?: $dr->start_date))->endOfDay();
if ($start->gte($to) || $end->lt($from)) {
Expand All @@ -393,40 +392,22 @@ public function findGroupedByDay(Query $query, FrozenTime $from, FrozenTime|null
while ($start->lte($end) && $start->lte($to)) {
$day = $start->format('Y-m-d');
$start = $start->addDay();
$event = clone $object;
$event->set('primary_date_range', $dr);

yield compact('event', 'day');
}
}
})
->groupBy('day')
->map(function (array $items, string $day): array {
$today = new FrozenTime($day);

return collection($items)
->extract('event')
->sortBy(
function (ObjectEntity $event) use ($today): string {
$closestDR = collection($event->filtered_date_ranges)
->filter(function (DateRange $dr) use ($today): bool {
$start = (new FrozenTime($dr->start_date))->startOfDay();
$end = (new FrozenTime($dr->end_date ?: $dr->start_date))->endOfDay();

return $today->gte($start) && $today->lt($end);
})
->sortBy(
fn (DateRange $dr): string => $dr->start_date->format('c'),
SORT_ASC,
SORT_NATURAL,
)
->first();

return ($closestDR->end_date ?? $closestDR->start_date)->format('c');
},
SORT_ASC,
SORT_NATURAL
)
->toList();
})
->map(fn (array $items): array => collection($items)
->extract('event')
->sortBy(
fn (ObjectEntity $event): string => $event->get('primary_date_range')->start_date->format('c'),
SORT_ASC,
SORT_NATURAL
)
->toList())
->toArray();

ksort($grouped, SORT_STRING);
Expand Down