From 071cdc32e8bb2270a85573552676890a046d2710 Mon Sep 17 00:00:00 2001 From: sbsoerf Date: Tue, 12 Sep 2023 10:46:55 +0200 Subject: [PATCH] Add reminder for ICS --- README.md | 6 ++++++ src/Generators/Ics.php | 18 ++++++++++++++++ tests/Generators/IcsGeneratorTest.php | 21 +++++++++++++++++++ ...can_generate_with_a_custom_reminder__1.txt | 18 ++++++++++++++++ ...an_generate_with_a_default_reminder__1.txt | 18 ++++++++++++++++ 5 files changed, 81 insertions(+) create mode 100644 tests/Generators/__snapshots__/IcsGeneratorTest__it_can_generate_with_a_custom_reminder__1.txt create mode 100644 tests/Generators/__snapshots__/IcsGeneratorTest__it_can_generate_with_a_default_reminder__1.txt diff --git a/README.md b/README.md index 37067c7..9cb47e7 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,12 @@ echo $link->webOffice(); // Generate a data uri for an ics file (for iCal & Outlook) echo $link->ics(); +// 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 b738974..63c2a97 100644 --- a/src/Generators/Ics.php +++ b/src/Generators/Ics.php @@ -61,6 +61,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 9c3b481..ad613b6 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; @@ -53,4 +55,23 @@ public function it_has_a_product_dtstamp(): void $this->generator(['DTSTAMP' => '20180201T090000Z'])->generate($this->createShortEventLink()) ); } + + /** @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