diff --git a/src/Tags/Events.php b/src/Tags/Events.php index 537b64f..99e5e20 100755 --- a/src/Tags/Events.php +++ b/src/Tags/Events.php @@ -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; @@ -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()); @@ -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), ]; } @@ -244,7 +260,7 @@ 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( @@ -252,7 +268,7 @@ private function spanningDays(): \Closure $occurrence->end->endOfDay() )->toArray(); - return collect($spanningDays)->map(fn(CarbonImmutable $date) => $date->toDateString())->all(); + return collect($spanningDays)->map(fn (CarbonImmutable $date) => $date->toDateString())->all(); }; } } diff --git a/tests/Tags/EventsTest.php b/tests/Tags/EventsTest.php index 51a5b80..0dc46dd 100755 --- a/tests/Tags/EventsTest.php +++ b/tests/Tags/EventsTest.php @@ -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(); }); @@ -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(); +});