diff --git a/src/Events.php b/src/Events.php index 86ffed6..f16fe71 100644 --- a/src/Events.php +++ b/src/Events.php @@ -48,7 +48,6 @@ class Events public static function defaultTimezone(): string { - // return static::setting('timezone', config('statamic.system.display_timezone') ?? config('app.timezone', 'UTC')); return static::setting('timezone'); } @@ -175,7 +174,7 @@ private function output(callable $type): EntryCollection|LengthAwarePaginator return $occurrence ->setSupplement('start', $start) ->setSupplement('end', $end) - ->setSupplement('spans_days', ! $start->isSameDay($end)); + ->setSupplement('spanning', ! $start->isSameDay($end)); }); } diff --git a/src/Tags/Events.php b/src/Tags/Events.php index 67b38a7..6af6405 100755 --- a/src/Tags/Events.php +++ b/src/Tags/Events.php @@ -47,16 +47,7 @@ public function calendar(): Collection $occurrences = $this ->generator() ->between(from: $from, to: $to) - ->groupBy(function (Entry $occurrence) { - $periodInTimezone = CarbonPeriodImmutable::between( - $occurrence->start->setTimezone($this->params->get('timezone') ?? Generator::defaultTimezone())->startOfDay(), - $occurrence->end->setTimezone($this->params->get('timezone') ?? Generator::defaultTimezone())->endOfDay() - ); - - return collect($periodInTimezone->toArray()) - ->map(fn (CarbonImmutable $date) => $date->toDateString()) - ->all(); - }) + ->groupBy($this->spanningDays()) ->map(fn(EntryCollection $occurrences, string $date) => $this->day(date: $date, occurrences: $occurrences)); $days = $this->output($this->makeEmptyDates(from: $from, to: $to)->merge($occurrences)->values()); @@ -194,7 +185,7 @@ private function generator(): Generator value: $this->params->bool('collapse_multi_days'), callback: fn (Generator $generator) => $generator->collapseMultiDays() )->when( - value: $this->params->get('timezone'), + value: $this->params->get('timezone', Generator::defaultTimezone()), callback: fn (Generator $generator, string $tz) => $generator->timezone(timezone: $tz) ); } @@ -252,4 +243,16 @@ private function parseTermIds(string $key, array|Builder|string $terms): array ->map(fn (Term|string $term) => $this->getTermId(handle: $handle, term: $term)) ->all(); } + + 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(); + }; + } } diff --git a/tests/Tags/EventsTest.php b/tests/Tags/EventsTest.php index ca42b5f..51a5b80 100755 --- a/tests/Tags/EventsTest.php +++ b/tests/Tags/EventsTest.php @@ -428,7 +428,7 @@ ->first()->start->timezone->getName()->toBe('America/Vancouver'); }); -it('sets "spans_days"', function () { +it('sets "spanning"', function () { Carbon::setTestNow(now()->setTimeFromTimeString('10:00')); Entry::make() @@ -454,5 +454,5 @@ $occurrences = $this->tag->between(); expect($occurrences)->toHaveCount(1) - ->first()->spans_days->toBeTrue(); + ->first()->spanning->toBeTrue(); });