Skip to content
Merged
Show file tree
Hide file tree
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
30 changes: 23 additions & 7 deletions src/Tags/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Carbon\CarbonInterface;
use Carbon\CarbonPeriod;
use Carbon\CarbonPeriodImmutable;
use Closure;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;
use Statamic\Contracts\Query\Builder;
Expand Down Expand Up @@ -41,14 +42,14 @@ public function calendar(): Collection
$month = $this->params->get('month', now()->englishMonth);
$year = $this->params->get('year', now()->year);

$from = parse_date($month . ' ' . $year)->startOfMonth()->startOfWeek();
$to = parse_date($month . ' ' . $year)->endOfMonth()->endOfWeek();
$from = parse_date($month.' '.$year)->startOfMonth()->startOfWeek();
$to = parse_date($month.' '.$year)->endOfMonth()->endOfWeek();

$occurrences = $this
->generator()
->between(from: $from, to: $to)
->groupBy($this->spanningDays())
->map(fn(EntryCollection $occurrences, string $date) => $this->day(date: $date, occurrences: $occurrences));
->map(fn (EntryCollection $occurrences, string $date) => $this->day(date: $date, occurrences: $occurrences));

$days = $this->output($this->makeEmptyDates(from: $from, to: $to)->merge($occurrences)->values());

Expand Down Expand Up @@ -135,12 +136,27 @@ public function upcoming(): EntryCollection|array
return $this->output($occurrences->take($limit));
}

private function addSpanningStartEnd(string $date, EntryCollection $occurrences): EntryCollection
{
return $occurrences->map(function (Entry $occurrence) use ($date) {
if (! $occurrence->spanning) {
return $occurrence;
}

$carbonDate = Carbon::parse($date)->shiftTimezone($occurrence->start->timezone);
$occurrence
->setSupplement('spanning_start', $occurrence->start->isSameDay($carbonDate))
->setSupplement('spanning_end', $occurrence->end->isSameDay($carbonDate));

return clone $occurrence;
});
}

private function day(string $date, EntryCollection $occurrences): array
{
return [
'date' => $date,
'dates' => $occurrences,
'occurrences' => $occurrences,
'occurrences' => $this->addSpanningStartEnd($date, $occurrences),
];
}

Expand Down Expand Up @@ -244,15 +260,15 @@ private function parseTermIds(string $key, array|Builder|string $terms): array
->all();
}

private function spanningDays(): \Closure
private function spanningDays(): Closure
{
return function (Entry $occurrence) {
$spanningDays = CarbonPeriodImmutable::between(
$occurrence->start->startOfDay(),
$occurrence->end->endOfDay()
)->toArray();

return collect($spanningDays)->map(fn(CarbonImmutable $date) => $date->toDateString())->all();
return collect($spanningDays)->map(fn (CarbonImmutable $date) => $date->toDateString())->all();
};
}
}
33 changes: 32 additions & 1 deletion tests/Tags/EventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
$occurrences = $this->tag->calendar();

expect($occurrences)->toHaveCount(42);
expect(Arr::get($occurrences, '5.dates'))->toHaveCount(2);
expect(Arr::get($occurrences, '5.occurrences'))->toHaveCount(2);
expect(Arr::get($occurrences, '6.no_results'))->toBeTrue();
});

Expand Down Expand Up @@ -456,3 +456,34 @@
expect($occurrences)->toHaveCount(1)
->first()->spanning->toBeTrue();
});

it('sets "spanning-start" and "spanning-end"', function () {
Carbon::setTestNow(Carbon::parse('April 16 10:00am'));

Entry::make()
->collection('events')
->slug('single-event')
->id('single-event')
->data([
'title' => 'Event',
'start_date' => Carbon::now()->toDateString(),
'start_time' => '11:00',
'end_time' => '23:00',
])->save();

$this->tag
->setContext([])
->setParameters(['timezone' => 'Europe/Kyiv']);

$days = $this->tag->calendar();
$firstSpanningOccurrence = $days[17]['occurrences'][0];
$secondSpanningOccurrence = $days[18]['occurrences'][0];

expect($firstSpanningOccurrence)
->spanning_start->toBeTrue()
->spanning_end->toBeFalse();

expect($secondSpanningOccurrence)
->spanning_start->toBeFalse()
->spanning_end->toBeTrue();
});
Loading