Skip to content

Commit 8f6e002

Browse files
committed
wip
1 parent 52723bf commit 8f6e002

2 files changed

Lines changed: 47 additions & 7 deletions

File tree

src/Tags/Events.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Carbon\CarbonInterface;
88
use Carbon\CarbonPeriod;
99
use Carbon\CarbonPeriodImmutable;
10+
use Closure;
1011
use Illuminate\Pagination\Paginator;
1112
use Illuminate\Support\Collection;
1213
use Statamic\Contracts\Query\Builder;
@@ -41,14 +42,14 @@ public function calendar(): Collection
4142
$month = $this->params->get('month', now()->englishMonth);
4243
$year = $this->params->get('year', now()->year);
4344

44-
$from = parse_date($month . ' ' . $year)->startOfMonth()->startOfWeek();
45-
$to = parse_date($month . ' ' . $year)->endOfMonth()->endOfWeek();
45+
$from = parse_date($month.' '.$year)->startOfMonth()->startOfWeek();
46+
$to = parse_date($month.' '.$year)->endOfMonth()->endOfWeek();
4647

4748
$occurrences = $this
4849
->generator()
4950
->between(from: $from, to: $to)
5051
->groupBy($this->spanningDays())
51-
->map(fn(EntryCollection $occurrences, string $date) => $this->day(date: $date, occurrences: $occurrences));
52+
->map(fn (EntryCollection $occurrences, string $date) => $this->day(date: $date, occurrences: $occurrences));
5253

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

@@ -139,8 +140,16 @@ private function day(string $date, EntryCollection $occurrences): array
139140
{
140141
return [
141142
'date' => $date,
142-
'dates' => $occurrences,
143-
'occurrences' => $occurrences,
143+
'occurrences' => $occurrences->map(function (Entry $occurrence) use ($date): Entry {
144+
if ($occurrence->spanning) {
145+
$carbonDate = Carbon::parse($date)->shiftTimezone($occurrence->start->timezone);
146+
$occurrence
147+
->setSupplement('spanning_start', $occurrence->start->isSameDay($carbonDate))
148+
->setSupplement('spanning_end', $occurrence->end->isSameDay($carbonDate));
149+
}
150+
151+
return $occurrence;
152+
})->values(),
144153
];
145154
}
146155

@@ -244,15 +253,15 @@ private function parseTermIds(string $key, array|Builder|string $terms): array
244253
->all();
245254
}
246255

247-
private function spanningDays(): \Closure
256+
private function spanningDays(): Closure
248257
{
249258
return function (Entry $occurrence) {
250259
$spanningDays = CarbonPeriodImmutable::between(
251260
$occurrence->start->startOfDay(),
252261
$occurrence->end->endOfDay()
253262
)->toArray();
254263

255-
return collect($spanningDays)->map(fn(CarbonImmutable $date) => $date->toDateString())->all();
264+
return collect($spanningDays)->map(fn (CarbonImmutable $date) => $date->toDateString())->all();
256265
};
257266
}
258267
}

tests/Tags/EventsTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,3 +456,34 @@
456456
expect($occurrences)->toHaveCount(1)
457457
->first()->spanning->toBeTrue();
458458
});
459+
460+
it('sets "spanning-start" and "spanning-end"', function () {
461+
Carbon::setTestNow(Carbon::parse('April 16 10:00am'));
462+
463+
Entry::make()
464+
->collection('events')
465+
->slug('single-event')
466+
->id('single-event')
467+
->data([
468+
'title' => 'Event',
469+
'start_date' => Carbon::now()->toDateString(),
470+
'start_time' => '11:00',
471+
'end_time' => '23:00',
472+
])->save();
473+
474+
$this->tag
475+
->setContext([])
476+
->setParameters(['timezone' => 'Europe/Kyiv']);
477+
478+
$days = $this->tag->calendar();
479+
$firstSpanningOccurrence = $days[17]['occurrences'][1];
480+
$secondSpanningOccurrence = $days[18]['occurrences'][0];
481+
482+
ray($days, $firstSpanningOccurrence);
483+
expect($firstSpanningOccurrence)
484+
->spanning_start->toBeTrue()
485+
->spanning_end->toBeFalse();
486+
// expect($secondSpanningOccurrence)
487+
// ->spanning_start->toBeFalse()
488+
// ->spanning_end->toBeTrue();
489+
});

0 commit comments

Comments
 (0)