Skip to content

Commit

Permalink
Merge pull request #178 from S-Braeutigam/ics_reminder
Browse files Browse the repository at this point in the history
Add reminder functionality to ICS
  • Loading branch information
alies-dev authored Jan 26, 2024
2 parents 082527e + 071cdc3 commit c444b7c
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 @@ -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());
```
Expand Down
18 changes: 18 additions & 0 deletions src/Generators/Ics.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {

Check failure on line 74 in src/Generators/Ics.php

View workflow job for this annotation

GitHub Actions / psalm

InvalidArrayOffset

src/Generators/Ics.php:74:23: InvalidArrayOffset: Cannot access value on variable $this->options['REMINDER'] using offset value of 'DESCRIPTION', expecting int (see https://psalm.dev/115)
$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 @@ -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())
);
}
}
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 c444b7c

Please sign in to comment.