Skip to content

Commit cb66eee

Browse files
authored
[5.x] Add ability to set description and URL on an event (#111)
* wip * tidy * not gonna do it this way * add to recurring events * tests * more tests and fixes * update docs * these are optional
1 parent 97d7d5b commit cb66eee

6 files changed

Lines changed: 132 additions & 15 deletions

File tree

DOCUMENTATION.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,10 @@ Tag pair that returns the next X event dates.
228228

229229
### Download Links
230230

231-
Single Tag returns a url to the event data and add it to your calendar. If there's a "location" field (see config above), it'll get added to the download.
231+
Single Tag returns a url to the event data and add it to your calendar. The following fields will be added to the ICS if they exist:
232+
* `location` (see config above)
233+
* `description`
234+
* `link`
232235

233236
Parameters:
234237

src/Http/Controllers/IcsController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ public function __invoke(Request $request)
5151

5252
if ($entry) {
5353
return $this->downloadIcs(
54-
EventFactory::createFromEntry($entry)->toICalendarEvents()
54+
EventFactory::createFromEntry($entry)->toICalendarEvents(),
55+
$entry->title
5556
);
5657
}
5758
}

src/Types/Event.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,18 @@ public function toICalendarEvent(string|CarbonInterface $date): ?ICalendarEvent
109109
->startsAt($immutableDate->setTimeFromTimeString($this->startTime()))
110110
->endsAt($immutableDate->setTimeFromTimeString($this->endTime()));
111111

112-
if ($location = $this->location($this->event)) {
112+
if (!is_null($location = $this->location($this->event))) {
113113
$iCalEvent->address($location);
114114
}
115115

116+
if (!is_null($description = $this->event->description)) {
117+
$iCalEvent->description($description);
118+
}
119+
120+
if (!is_null($link = $this->event->link)) {
121+
$iCalEvent->url($link);
122+
}
123+
116124
return $iCalEvent;
117125
}
118126

src/Types/MultiDayEvent.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,24 @@ public function toICalendarEvent(string|CarbonInterface $date): ?ICalendarEvent
5959
$immutableDate = $this->toCarbonImmutable($date);
6060
$day = $this->getDayFromDate($immutableDate);
6161

62-
return ICalendarEvent::create($this->event->title)
62+
$iCalEvent = ICalendarEvent::create($this->event->title)
6363
->uniqueIdentifier($this->event->id())
6464
->startsAt($immutableDate->setTimeFromTimeString($day->start()))
6565
->endsAt($immutableDate->setTimeFromTimeString($day->end()));
66+
67+
if (! is_null($location = $this->location($this->event))) {
68+
$iCalEvent->address($location);
69+
}
70+
71+
if (! is_null($description = $this->event->description)) {
72+
$iCalEvent->description($description);
73+
}
74+
75+
if (! is_null($link = $this->event->link)) {
76+
$iCalEvent->url($link);
77+
}
78+
79+
return $iCalEvent;
6680
}
6781

6882
/**
@@ -87,7 +101,7 @@ protected function rule(bool $collapseDays = false): RRuleInterface
87101
}
88102

89103
return tap(
90-
new RSet(),
104+
new RSet,
91105
fn (RSet $rset) => $this->days->each(fn (Day $day) => $rset->addRRule([
92106
'count' => 1,
93107
'dtstart' => $day->end()->subSecond(),

src/Types/RecurringEvent.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,25 @@ public function interval(): int
2727
*/
2828
public function toICalendarEvents(): array
2929
{
30-
$timezone = $this->timezone['timezone'];
31-
32-
return [
33-
ICalendarEvent::create($this->event->title)
34-
->uniqueIdentifier($this->event->id())
35-
->startsAt($this->start())
36-
->endsAt($this->end())
37-
->rrule($this->spatieRule()),
38-
];
30+
$iCalEvent = ICalendarEvent::create($this->event->title)
31+
->uniqueIdentifier($this->event->id())
32+
->startsAt($this->start())
33+
->endsAt($this->end())
34+
->rrule($this->spatieRule());
35+
36+
if (! is_null($location = $this->location($this->event))) {
37+
$iCalEvent->address($location);
38+
}
39+
40+
if (! is_null($description = $this->event->description)) {
41+
$iCalEvent->description($description);
42+
}
43+
44+
if (! is_null($link = $this->event->link)) {
45+
$iCalEvent->url($link);
46+
}
47+
48+
return [$iCalEvent];
3949
}
4050

4151
protected function rule(): RRuleInterface

tests/Feature/IcsControllerTest.php

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ protected function setUp(): void
2323
'start_time' => '11:00',
2424
'end_time' => '12:00',
2525
'location' => 'The Location',
26+
'description' => 'The description',
27+
'link' => 'https://transformstudios.com'
2628
])->save();
2729
}
2830

@@ -38,6 +40,8 @@ public function can_create_single_day_event_ics_file()
3840

3941
$this->assertStringContainsString('DTSTART:'.now()->setTimeFromTimeString('11:00')->format('Ymd\THis'), $response->streamedContent());
4042
$this->assertStringContainsString('LOCATION:The Location', $response->streamedContent());
43+
$this->assertStringContainsString('DESCRIPTION:The description', $response->streamedContent());
44+
$this->assertStringContainsString('URL:https://transformstudios.com', $response->streamedContent());
4145
}
4246

4347
#[Test]
@@ -55,6 +59,9 @@ public function can_create_single_day_recurring_event_ics_file()
5559
'start_time' => '11:00',
5660
'end_time' => '12:00',
5761
'recurrence' => 'weekly',
62+
'location' => 'The Location',
63+
'description' => 'The description',
64+
'link' => 'https://transformstudios.com'
5865
])->save();
5966

6067
$response = $this->get(route('statamic.events.ics.show', [
@@ -63,25 +70,95 @@ public function can_create_single_day_recurring_event_ics_file()
6370
]))->assertDownload('recurring-event.ics');
6471

6572
$this->assertStringContainsString('DTSTART:'.now()->setTimeFromTimeString('11:00')->format('Ymd\THis'), $response->streamedContent());
73+
$this->assertStringContainsString('LOCATION:The Location', $response->streamedContent());
74+
$this->assertStringContainsString('DESCRIPTION:The description', $response->streamedContent());
75+
$this->assertStringContainsString('URL:https://transformstudios.com', $response->streamedContent());
6676

6777
$this->get(route('statamic.events.ics.show', [
6878
'date' => now()->addDay()->toDateString(),
6979
'event' => 'the-recurring-id',
7080
]))->assertStatus(404);
7181
}
7282

83+
#[Test]
84+
public function can_create_ics_with_single_date_recurrence()
85+
{
86+
Carbon::setTestNow(now()->addDay()->setTimeFromTimeString('10:00'));
87+
88+
Entry::make()
89+
->collection('events')
90+
->slug('recurring-event')
91+
->id('the-recurring-id')
92+
->data([
93+
'title' => 'Recurring Event',
94+
'start_date' => Carbon::now()->toDateString(),
95+
'start_time' => '11:00',
96+
'end_time' => '12:00',
97+
'recurrence' => 'weekly',
98+
'location' => 'The Location',
99+
'description' => 'The description',
100+
'link' => 'https://transformstudios.com'
101+
])->save();
102+
103+
$response = $this->get(route('statamic.events.ics.show', [
104+
'date' => now()->toDateString(),
105+
'event' => 'the-recurring-id',
106+
]))->assertDownload('recurring-event.ics');
107+
108+
109+
$this->assertStringContainsString('DTSTART:'.now()->setTimeFromTimeString('11:00')->format('Ymd\THis'), $response->streamedContent());
110+
$this->assertStringContainsString('LOCATION:The Location', $response->streamedContent());
111+
$this->assertStringContainsString('DESCRIPTION:The description', $response->streamedContent());
112+
$this->assertStringContainsString('URL:https://transformstudios.com', $response->streamedContent());
113+
114+
}
115+
116+
#[Test]
117+
public function can_create_ics_with_recurrence()
118+
{
119+
Carbon::setTestNow(now()->addDay()->setTimeFromTimeString('10:00'));
120+
121+
Entry::make()
122+
->collection('events')
123+
->slug('recurring-event')
124+
->id('the-recurring-id')
125+
->data([
126+
'title' => 'Recurring Event',
127+
'start_date' => Carbon::now()->toDateString(),
128+
'start_time' => '11:00',
129+
'end_time' => '12:00',
130+
'recurrence' => 'weekly',
131+
'location' => 'The Location',
132+
'description' => 'The description',
133+
'link' => 'https://transformstudios.com'
134+
])->save();
135+
136+
$response = $this->get(route('statamic.events.ics.show', [
137+
'event' => 'the-recurring-id',
138+
]))->assertDownload('recurring-event.ics');
139+
140+
$this->assertStringContainsString('DTSTART:'.now()->setTimeFromTimeString('11:00')->format('Ymd\THis'), $response->streamedContent());
141+
$this->assertStringContainsString('LOCATION:The Location', $response->streamedContent());
142+
$this->assertStringContainsString('DESCRIPTION:The description', $response->streamedContent());
143+
$this->assertStringContainsString('URL:https://transformstudios.com', $response->streamedContent());
144+
145+
}
146+
73147
#[Test]
74148
public function can_create_single_day_multiday_event_ics_file()
75149
{
76150
Carbon::setTestNow(now());
77151

78-
$entry = Entry::make()
152+
Entry::make()
79153
->slug('multi-day-event')
80154
->collection('events')
81155
->id('the-multi-day-event')
82156
->data([
83157
'title' => 'Multi-day Event',
84158
'multi_day' => true,
159+
'location' => 'The Location',
160+
'description' => 'The description',
161+
'link' => 'https://transformstudios.com',
85162
'days' => [
86163
[
87164
'date' => now()->toDateString(),
@@ -112,6 +189,10 @@ public function can_create_single_day_multiday_event_ics_file()
112189
]))->assertDownload('multi-day-event.ics');
113190

114191
$this->assertStringContainsString('DTSTART:'.now()->addDay()->setTimeFromTimeString('11:00')->format('Ymd\THis'), $response->streamedContent());
192+
$this->assertStringContainsString('LOCATION:The Location', $response->streamedContent());
193+
$this->assertStringContainsString('DESCRIPTION:The description', $response->streamedContent());
194+
$this->assertStringContainsString('URL:https://transformstudios.com', $response->streamedContent());
195+
115196
}
116197

117198
#[Test]

0 commit comments

Comments
 (0)