Skip to content

Commit

Permalink
Add reminder for ICS
Browse files Browse the repository at this point in the history
  • Loading branch information
S-Braeutigam committed Sep 12, 2023
1 parent a74d683 commit 071cdc3
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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());
```
Expand Down
18 changes: 18 additions & 0 deletions src/Generators/Ics.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
21 changes: 21 additions & 0 deletions tests/Generators/IcsGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())
);
}
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 071cdc3

Please sign in to comment.