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
42 changes: 31 additions & 11 deletions src/Types/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,40 @@ public function toICalendarEvent(string|CarbonInterface $date): ?ICalendarEvent
return null;
}

return $this->decorate($this->buildICalendarEvent($date));
}

/**
* @return ICalendarEvent[]
*/
public function toICalendarEvents(): array
{
if (! $event = $this->toICalendarEvent($this->start())) {
return [];
}

return [$event];
}

protected function buildICalendarEvent(string|CarbonInterface $date): ICalendarEvent
{
$immutableDate = $this->toCarbonImmutable($date);

$iCalEvent = ICalendarEvent::create($this->event->title)
return ICalendarEvent::create($this->event->title)
->withoutTimezone()
->uniqueIdentifier($this->event->id())
->startsAt($immutableDate->setTimeFromTimeString($this->startTime()))
->endsAt($immutableDate->setTimeFromTimeString($this->endTime()));
}

protected function decorate(ICalendarEvent $iCalEvent): ICalendarEvent
{
if ($address = $this->icsAddress()) {
$iCalEvent->address($address);
}

if (! is_null($coords = $this->event->coordinates)) {
$iCalEvent->coordinates($coords['latitude'], $coords['longitude']);
if ($this->hasValidCoordinates($coords = $this->event->get('coordinates'))) {
$iCalEvent->coordinates((float) $coords['latitude'], (float) $coords['longitude']);
}

if (! is_null($description = $this->event->description)) {
Expand All @@ -132,14 +152,6 @@ public function toICalendarEvent(string|CarbonInterface $date): ?ICalendarEvent
return $iCalEvent;
}

/**
* @return ICalendarEvent[]
*/
public function toICalendarEvents(): array
{
return Arr::wrap($this->toICalendarEvent($this->start()));
}

protected function eventUrl(): ?string
{
if (! is_null($link = $this->event->link)) {
Expand All @@ -162,6 +174,14 @@ protected function icsAddress(): ?string
return is_string($address) && $address !== '' ? $address : null;
}

// Entry::get() is untyped; keep mixed so a bad value can't TypeError the public ICS route.
protected function hasValidCoordinates(mixed $coords): bool
{
return is_array($coords)
&& is_numeric($coords['latitude'] ?? null)
&& is_numeric($coords['longitude'] ?? null);
}

protected function supplement(CarbonInterface $date): ?Entry
{
return unserialize(serialize($this->event))
Expand Down
51 changes: 18 additions & 33 deletions src/Types/MultiDayEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Spatie\IcalendarGenerator\Components\Event as ICalendarEvent;
use Statamic\Entries\Entry;
use Statamic\Fields\Values;
use Statamic\Support\Str;
use TransformStudios\Events\Day;

class MultiDayEvent extends Event
Expand Down Expand Up @@ -60,47 +61,31 @@ public function start(): CarbonImmutable
return $this->days->first()->start();
}

public function toICalendarEvent(string|CarbonInterface $date): ?ICalendarEvent
/**
* @return ICalendarEvent[]
*/
public function toICalendarEvents(): array
{
if (! $this->occursOnDate($date)) {
return null;
}
return $this->days
->values()
->map(function (Day $day, int $index) {
$event = $this->toICalendarEvent($day->start());

return $event?->uniqueIdentifier(Str::slug($this->event->title).'-'.$index);
})
->filter()
->all();
}

protected function buildICalendarEvent(string|CarbonInterface $date): ICalendarEvent
{
$immutableDate = $this->toCarbonImmutable($date);
$day = $this->getDayFromDate($immutableDate);

$iCalEvent = ICalendarEvent::create($this->event->title)
return ICalendarEvent::create($this->event->title)
->uniqueIdentifier($this->event->id())
->startsAt($immutableDate->setTimeFromTimeString($day->start()))
->endsAt($immutableDate->setTimeFromTimeString($day->end()));

if ($address = $this->icsAddress()) {
$iCalEvent->address($address);
}

if (! is_null($coords = $this->event->coordinates)) {
$iCalEvent->coordinates($coords['latitude'], $coords['longitude']);
}

if (! is_null($description = $this->event->description)) {
$iCalEvent->description($description);
}

if (! is_null($link = $this->eventUrl())) {
$iCalEvent->url($link);
}

return $iCalEvent;
}

/**
* @return ICalendarEvent[]
*/
public function toICalendarEvents(): array
{
return collect($this->days)
->map(fn (Day $day, int $index) => $day->toICalendarEvent($this->event->title, $index))
->all();
}

protected function rule(bool $useEnd = false): RRuleInterface
Expand Down
25 changes: 3 additions & 22 deletions src/Types/RecurringEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Illuminate\Support\Arr;
use RRule\RRule;
use RRule\RRuleInterface;
use Spatie\IcalendarGenerator\Components\Event as ICalendarEvent;
use Spatie\IcalendarGenerator\Enums\RecurrenceFrequency;
use Spatie\IcalendarGenerator\ValueObjects\RRule as ICalendarRule;

Expand All @@ -27,29 +26,11 @@ public function interval(): int
*/
public function toICalendarEvents(): array
{
$iCalEvent = ICalendarEvent::create($this->event->title)
->uniqueIdentifier($this->event->id())
->startsAt($this->start())
->endsAt($this->end())
->rrule($this->spatieRule());

if ($address = $this->icsAddress()) {
$iCalEvent->address($address);
if (! $event = $this->toICalendarEvent($this->start())) {
return [];
}

if (! is_null($coords = $this->event->coordinates)) {
$iCalEvent->coordinates($coords['latitude'], $coords['longitude']);
}

if (! is_null($description = $this->event->description)) {
$iCalEvent->description($description);
}

if (! is_null($link = $this->eventUrl())) {
$iCalEvent->url($link);
}

return [$iCalEvent];
return [$event->rrule($this->spatieRule())];
}

protected function rule(bool $useEnd = false): RRuleInterface
Expand Down
96 changes: 96 additions & 0 deletions tests/Http/Contollers/IcsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,99 @@

$this->assertStringNotContainsString('LOCATION:', $response->streamedContent());
});

test('multi-day whole-event download includes location url description and geo on every day', function () {
Carbon::setTestNow(now());

Entry::make()
->slug('multi-day-whole-event')
->collection('events')
->id('the-multi-day-whole-event')
->data([
'title' => 'Multi-day Whole Event',
'multi_day' => true,
'address' => '123 Main St',
'link' => 'https://example.com/join',
'coordinates' => [
'latitude' => 40,
'longitude' => 50,
],
'description' => 'The description',
'days' => [
[
'date' => now()->toDateString(),
'start_time' => '19:00',
'end_time' => '21:00',
],
[
'date' => now()->addDay()->toDateString(),
'start_time' => '11:00',
'end_time' => '15:00',
],
],
])->save();

$content = $this->get(route('statamic.events.ics.show', [
'event' => 'the-multi-day-whole-event',
]))->assertDownload('multi-day-whole-event.ics')->streamedContent();

expect(substr_count($content, 'LOCATION:123 Main St'))->toBe(2)
->and(substr_count($content, 'URL:https://example.com/join'))->toBe(2)
->and(substr_count($content, 'DESCRIPTION:The description'))->toBe(2)
->and(substr_count($content, 'GEO:40;50'))->toBe(2);
});

test('partial coordinates do not fatal and omit geo', function () {
Carbon::setTestNow(now()->setTimeFromTimeString('10:00'));

Entry::make()
->collection('events')
->slug('partial-coords-event')
->id('partial-coords-id')
->data([
'title' => 'Partial Coords Event',
'start_date' => Carbon::now()->toDateString(),
'start_time' => '11:00',
'end_time' => '12:00',
'address' => '123 Main St',
'coordinates' => [
'latitude' => 40,
],
])->save();

$content = $this->get(route('statamic.events.ics.show', [
'date' => now()->toDateString(),
'event' => 'partial-coords-id',
]))->assertDownload('partial-coords-event.ics')->streamedContent();

$this->assertStringContainsString('LOCATION:123 Main St', $content);
$this->assertStringNotContainsString('GEO:', $content);
});

test('non-numeric coordinates do not fatal and omit geo', function () {
Carbon::setTestNow(now()->setTimeFromTimeString('10:00'));

Entry::make()
->collection('events')
->slug('bad-coords-event')
->id('bad-coords-id')
->data([
'title' => 'Bad Coords Event',
'start_date' => Carbon::now()->toDateString(),
'start_time' => '11:00',
'end_time' => '12:00',
'address' => '123 Main St',
'coordinates' => [
'latitude' => 'north',
'longitude' => 'west',
],
])->save();

$content = $this->get(route('statamic.events.ics.show', [
'date' => now()->toDateString(),
'event' => 'bad-coords-id',
]))->assertDownload('bad-coords-event.ics')->streamedContent();

$this->assertStringContainsString('LOCATION:123 Main St', $content);
$this->assertStringNotContainsString('GEO:', $content);
});