diff --git a/README.md b/README.md index b27aec1..212dc66 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,12 @@ echo $link->ics(); echo $link->ics(['URL' => 'https://my-page.com', 'UID' => 'custom-id']); // echo $link->ics([], ['format' => 'file']); // e.g. to attach ics as a file to an email. +// Generate a data uri for an ics file with default reminder (for iCal & Outlook) +echo $link->ics(['REMINDER' => []]); + +// Generate a data uri for an ics file with a custom reminder (for iCal & Outlook) +echo $link->ics(['REMINDER' => ['DESCRIPTION' => 'Remind me', 'TIME' => DateTime::createFromFormat('Y-m-d H:i', '2018-02-01 08:15', new DateTimeZone('UTC'))]]); + // Generate a data URI using arbitrary generator: echo $link->formatWith(new \Your\Generator()); ``` diff --git a/src/Generators/Ics.php b/src/Generators/Ics.php index 35797e7..0103d32 100644 --- a/src/Generators/Ics.php +++ b/src/Generators/Ics.php @@ -69,6 +69,24 @@ public function generate(Link $link): string $url[] = 'URL;VALUE=URI:'.$this->options['URL']; } + if (isset($this->options['REMINDER'])) { + $description = 'Reminder: '.$this->escapeString($link->title); + if (isset($this->options['REMINDER']['DESCRIPTION'])) { + $description = $this->escapeString($this->options['REMINDER']['DESCRIPTION']); + } + + $trigger = '-PT15M'; + if (isset($this->options[ 'REMINDER'][ 'TIME'])) { + $trigger = 'VALUE=DATE-TIME:'.gmdate($dateTimeFormat, $this->options[ 'REMINDER'][ 'TIME']->getTimestamp()); + } + + $url[] = 'BEGIN:VALARM'; + $url[] = 'ACTION:DISPLAY'; + $url[] = 'DESCRIPTION:'.$description; + $url[] = 'TRIGGER:'.$trigger; + $url[] = 'END:VALARM'; + } + $url[] = 'END:VEVENT'; $url[] = 'END:VCALENDAR'; diff --git a/tests/Generators/IcsGeneratorTest.php b/tests/Generators/IcsGeneratorTest.php index 8f4f998..1564879 100644 --- a/tests/Generators/IcsGeneratorTest.php +++ b/tests/Generators/IcsGeneratorTest.php @@ -2,6 +2,8 @@ namespace Spatie\CalendarLinks\Tests\Generators; +use DateTime; +use DateTimeZone; use Spatie\CalendarLinks\Generator; use Spatie\CalendarLinks\Generators\Ics; use Spatie\CalendarLinks\Tests\TestCase; @@ -73,4 +75,23 @@ public function it_correctly_generates_all_day_events_by_dates(): void $this->generator()->generate($this->createEventMultipleDaysViaStartEndWithTimezoneLink()) ); } + + /** @test */ + public function it_can_generate_with_a_default_reminder(): void + { + $this->assertMatchesSnapshot( + $this->generator(['REMINDER' => []])->generate($this->createShortEventLink()) + ); + } + + /** @test */ + public function it_can_generate_with_a_custom_reminder(): void + { + $this->assertMatchesSnapshot( + $this->generator(['REMINDER' => [ + 'DESCRIPTION' => 'Party with balloons and cake!', + 'TIME' => DateTime::createFromFormat('Y-m-d H:i', '2018-02-01 08:15', new DateTimeZone('UTC')) + ]])->generate($this->createShortEventLink()) + ); + } } diff --git a/tests/Generators/__snapshots__/IcsGeneratorTest__it_can_generate_with_a_custom_reminder__1.txt b/tests/Generators/__snapshots__/IcsGeneratorTest__it_can_generate_with_a_custom_reminder__1.txt new file mode 100644 index 0000000..b799d9e --- /dev/null +++ b/tests/Generators/__snapshots__/IcsGeneratorTest__it_can_generate_with_a_custom_reminder__1.txt @@ -0,0 +1,18 @@ +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:Spatie calendar-links +BEGIN:VEVENT +UID:94ab75add84a67c019eae57539658036 +SUMMARY:Birthday +DTSTAMP:20180201T090000Z +DTSTART:20180201T090000Z +DTEND:20180201T180000Z +DESCRIPTION:With balloons\, clowns and stuff\nBring a dog\, bring a frog +LOCATION:Party Lane 1A\, 1337 Funtown +BEGIN:VALARM +ACTION:DISPLAY +DESCRIPTION:Party with balloons and cake! +TRIGGER:VALUE=DATE-TIME:20180201T081500Z +END:VALARM +END:VEVENT +END:VCALENDAR \ No newline at end of file diff --git a/tests/Generators/__snapshots__/IcsGeneratorTest__it_can_generate_with_a_default_reminder__1.txt b/tests/Generators/__snapshots__/IcsGeneratorTest__it_can_generate_with_a_default_reminder__1.txt new file mode 100644 index 0000000..933cd81 --- /dev/null +++ b/tests/Generators/__snapshots__/IcsGeneratorTest__it_can_generate_with_a_default_reminder__1.txt @@ -0,0 +1,18 @@ +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:Spatie calendar-links +BEGIN:VEVENT +UID:94ab75add84a67c019eae57539658036 +SUMMARY:Birthday +DTSTAMP:20180201T090000Z +DTSTART:20180201T090000Z +DTEND:20180201T180000Z +DESCRIPTION:With balloons\, clowns and stuff\nBring a dog\, bring a frog +LOCATION:Party Lane 1A\, 1337 Funtown +BEGIN:VALARM +ACTION:DISPLAY +DESCRIPTION:Reminder: Birthday +TRIGGER:-PT15M +END:VALARM +END:VEVENT +END:VCALENDAR \ No newline at end of file