IcsGenerator by thausmann

This module can generate ICS files.

ICSGenerator

The module can generate basic ICS calendar strings and files.

The module simply extends WireData. It has these properties and default values:

$this->set('date', 'now');
$this->set('dateEnd', 'now + 60 minutes');
$this->set('summary', 'ICS Calendar File');
$this->set('description', '');
$this->set('location', '');
$this->set('url', '');
$this->set('timezone', new \DateTimeZone(date_default_timezone_get()));
  • date / dateEnd should be a PHP DateTime object or a string.
  • Date strings will be passed to DateTime constructor.
  • Date strings will use the set timezone.
  • Final output will be converted to UTC (Z timestamp)
// get the module
$icsgen = wire()->modules->IcsGenerator;

// set single properties (date strings)
$icsgen->set('date',    '2033-12-24 12:00');
$icsgen->set('dateEnd', '2033-12-24 14:00');

// set single properties (DateTime)
$icsgen->set('date',    new \DateTime('2033-12-24 12:00'));
$icsgen->set('dateEnd', new \DateTime('2033-12-24 14:00'));

// the set timezone will only impact time strings!
$icsgen->set('timezone', new \DateTimeZone('Europe/Berlin'));
$icsgen->set('date',    'now');
$icsgen->set('dateEnd', 'now + 60 minutes');

// set timezone will be ignored when string is a unix timestamp or contains a timezone
$icsgen->set('date',    '@946684800');
$icsgen->set('date',    '2010-01-28T15:00:00+02:00');

// set timezone will not impact DateTime!
// construct your DateTime with DateTimeZone instead
$icsgen->set('date',    new \DateTime('2033-12-24 12:00', new \DateTimeZone('Asia/Dubai')));
$icsgen->set('dateEnd', new \DateTime('2033-12-24 12:00', new \DateTimeZone('Europe/Paris')));

// set multiple properties at once
$icsgen->setArray(array(
    'date' => '2033-12-24 12:00',
    'dateEnd' => '2033-12-24 14:00',
    'summary' => 'Event Title',
    'description' => 'Event Description',
));

// get ICS string
$str = $icsgen->getString();

// get ICS object
$ics = $icsgen->getICS();

// get path to a temporary .ics file
// using wire()->files->tempDir
$path = $icsgen->getFile();

// get path to a temporary .ics file by ID
// this is useful if you need the same ICS multiple times in one request
// (same file id will only be created once)
$path = $icsgen->getFileByID('christmas-2033');

// example: send email with ics file
$mail = wireMail();
$mail->attachment($path, 'calendar.ics');
$mail->to($user->email);
$mail->subject('ICS Demo');
$mail->body('This is a ICS demo.');
$numSent = $mail->send();

Code is heavily based on https://gist.github.com/jakebellacera/635416 with some improvements.

PRs are open.

Install and use modules at your own risk. Always have a site and database backup before installing new modules.

Latest news

  • ProcessWire Weekly #549
    In the 549th issue of ProcessWire Weekly we’re going to check out the latest core updates, highlight one older yet still very relevant third party module, and more. Read on!
    Weekly.pw / 17 November 2024
  • Custom Fields Module
    This week we look at a new ProFields module named Custom Fields. This module provides a way to rapidly build out ProcessWire fields that contain any number of subfields/properties within them.
    Blog / 30 August 2024
  • Subscribe to weekly ProcessWire news

“I am currently managing a ProcessWire site with 2 million+ pages. It’s admirably fast, and much, much faster than any other CMS we tested.” —Nickie, Web developer