Commit 721bf714 authored by christopher-evans's avatar christopher-evans

Add option for arbitrary time zone string

parent 772dd147
<?php
// use composer autoloader
require_once __DIR__ . '/../vendor/autoload.php';
// set default timezone (PHP 5.4)
date_default_timezone_set('Europe/Berlin');
// 1. Create new calendar
$vCalendar = new \Eluceo\iCal\Component\Calendar('www.example.com');
// 2. Create an event
$vEvent = new \Eluceo\iCal\Component\Event('123456');
$vEvent->setDtStart(new \DateTime('2012-11-11 13:00:00'));
$vEvent->setDtEnd(new \DateTime('2012-11-11 14:30:00'));
$vEvent->setSummary('Weekly lunch with Markus');
// Set recurrence rule
$recurrenceRule = new \Eluceo\iCal\Property\Event\RecurrenceRule();
$recurrenceRule->setFreq(\Eluceo\iCal\Property\Event\RecurrenceRule::FREQ_WEEKLY);
$recurrenceRule->setInterval(1);
$vEvent->setRecurrenceRule($recurrenceRule);
// Adding Timezone (optional)
$vEvent->setUseTimezone(true);
$vEvent->setTimezoneString('/example.com/1.0.0-0/Europe/Berlin');
// 3. Add event to calendar
$vCalendar->addComponent($vEvent);
// 4. Set headers
header('Content-Type: text/calendar; charset=utf-8');
header('Content-Disposition: attachment; filename="cal.ics"');
// 5. Output
echo $vCalendar->render();
......@@ -114,6 +114,13 @@ class Event extends Component
*/
protected $useTimezone = false;
/**
* If set will we used as the timezone identifier
*
* @var string
*/
protected $timezoneString = '';
/**
* @var int
*/
......@@ -238,7 +245,7 @@ class Event extends Component
// mandatory information
$propertyBag->set('UID', $this->uniqueId);
$propertyBag->add(new DateTimeProperty('DTSTART', $this->dtStart, $this->noTime, $this->useTimezone, $this->useUtc));
$propertyBag->add(new DateTimeProperty('DTSTART', $this->dtStart, $this->noTime, $this->useTimezone, $this->useUtc, $this->timezoneString));
$propertyBag->set('SEQUENCE', $this->sequence);
$propertyBag->set('TRANSP', $this->transparency);
......@@ -251,7 +258,7 @@ class Event extends Component
if ($this->noTime === true) {
$this->dtEnd->add(new \DateInterval('P1D'));
}
$propertyBag->add(new DateTimeProperty('DTEND', $this->dtEnd, $this->noTime, $this->useTimezone, $this->useUtc));
$propertyBag->add(new DateTimeProperty('DTEND', $this->dtEnd, $this->noTime, $this->useTimezone, $this->useUtc, $this->timezoneString));
} elseif (null != $this->duration) {
$propertyBag->set('DURATION', $this->duration->format('P%dDT%hH%iM%sS'));
}
......@@ -316,12 +323,12 @@ class Event extends Component
}
if (null != $this->recurrenceId) {
$this->recurrenceId->applyTimeSettings($this->noTime, $this->useTimezone, $this->useUtc);
$this->recurrenceId->applyTimeSettings($this->noTime, $this->useTimezone, $this->useUtc, $this->timezoneString);
$propertyBag->add($this->recurrenceId);
}
if (!empty($this->exDates)) {
$propertyBag->add(new DateTimesProperty('EXDATE', $this->exDates, $this->noTime, $this->useTimezone, $this->useUtc));
$propertyBag->add(new DateTimesProperty('EXDATE', $this->exDates, $this->noTime, $this->useTimezone, $this->useUtc, $this->timezoneString));
}
if ($this->cancelled) {
......@@ -532,6 +539,26 @@ class Event extends Component
return $this->useTimezone;
}
/**
* @param $timezoneString
*
* @return $this
*/
public function setTimezoneString($timezoneString)
{
$this->timezoneString = $timezoneString;
return $this;
}
/**
* @return bool
*/
public function getTimezoneString()
{
return $this->timezoneString;
}
/**
* @param Attendees $attendees
*
......
......@@ -22,16 +22,18 @@ class DateTimeProperty extends Property
* @param bool $noTime
* @param bool $useTimezone
* @param bool $useUtc
* @param string $timezoneString
*/
public function __construct(
$name,
\DateTime $dateTime = null,
$noTime = false,
$useTimezone = false,
$useUtc = false
$useUtc = false,
$timezoneString = ''
) {
$dateString = DateUtil::getDateString($dateTime, $noTime, $useTimezone, $useUtc);
$params = DateUtil::getDefaultParams($dateTime, $noTime, $useTimezone);
$params = DateUtil::getDefaultParams($dateTime, $noTime, $useTimezone, $timezoneString);
parent::__construct($name, $dateString, $params);
}
......
......@@ -22,19 +22,23 @@ class DateTimesProperty extends Property
* @param bool $noTime
* @param bool $useTimezone
* @param bool $useUtc
* @param string $timezoneString
*/
public function __construct(
$name,
$dateTimes = [],
$noTime = false,
$useTimezone = false,
$useUtc = false
$useUtc = false,
$timezoneString = ''
) {
$dates = [];
foreach ($dateTimes as $dateTime) {
$dates[] = DateUtil::getDateString($dateTime, $noTime, $useTimezone, $useUtc);
}
$params = DateUtil::getDefaultParams($dateTime, $noTime, $useTimezone);
//@todo stop this triggering an E_NOTICE when $dateTimes is empty
$params = DateUtil::getDefaultParams($dateTime, $noTime, $useTimezone, $timezoneString);
parent::__construct($name, $dates, $params);
}
......
......@@ -54,9 +54,9 @@ class RecurrenceId extends Property
}
}
public function applyTimeSettings($noTime = false, $useTimezone = false, $useUtc = false)
public function applyTimeSettings($noTime = false, $useTimezone = false, $useUtc = false, $timezoneString = '')
{
$params = DateUtil::getDefaultParams($this->dateTime, $noTime, $useTimezone, $useUtc);
$params = DateUtil::getDefaultParams($this->dateTime, $noTime, $useTimezone, $timezoneString);
foreach ($params as $name => $value) {
$this->parameterBag->setParam($name, $value);
}
......@@ -69,7 +69,7 @@ class RecurrenceId extends Property
}
/**
* @return DateTime
* @return \DateTime
*/
public function getDatetime()
{
......@@ -104,6 +104,8 @@ class RecurrenceId extends Property
public function setRange($range)
{
$this->range = $range;
return $this;
}
/**
......
......@@ -13,12 +13,12 @@ namespace Eluceo\iCal\Util;
class DateUtil
{
public static function getDefaultParams(\DateTime $dateTime = null, $noTime = false, $useTimezone = false)
public static function getDefaultParams(\DateTime $dateTime = null, $noTime = false, $useTimezone = false, $timezoneString = '')
{
$params = [];
if ($useTimezone && $noTime === false) {
$timeZone = $dateTime->getTimezone()->getName();
$timeZone = $timezoneString === '' ? $dateTime->getTimezone()->getName() : $timezoneString;
$params['TZID'] = $timeZone;
}
......
......@@ -63,4 +63,60 @@ class CalendarIntegrationTest extends TestCase
$this->assertRegExp($lines[$key], $line);
}
}
/**
* @coversNothing
*/
public function testExample4b()
{
$timeZoneString = '/example.com/1.0.0-0/Europe/Berlin';
// 1. Create new calendar
$vCalendar = new \Eluceo\iCal\Component\Calendar('www.example.com');
// 2. Create an event
$vEvent = new \Eluceo\iCal\Component\Event('123456');
$vEvent->setDtStart(new \DateTime('2012-11-11 13:00:00'));
$vEvent->setDtEnd(new \DateTime('2012-11-11 14:30:00'));
$vEvent->setSummary('Weekly lunch with Markus');
// Set recurrence rule
$recurrenceRule = new \Eluceo\iCal\Property\Event\RecurrenceRule();
$recurrenceRule->setFreq(\Eluceo\iCal\Property\Event\RecurrenceRule::FREQ_WEEKLY);
$recurrenceRule->setInterval(1);
$vEvent->setRecurrenceRule($recurrenceRule);
// Adding Timezone (optional)
$vEvent->setUseTimezone(true);
$vEvent->setTimezoneString($timeZoneString);
// 3. Add event to calendar
$vCalendar->addComponent($vEvent);
$lines = array(
'/BEGIN:VCALENDAR/',
'/VERSION:2\.0/',
'/PRODID:www\.example\.com/',
'/X-PUBLISHED-TTL:P1W/',
'/BEGIN:VEVENT/',
'/UID:123456/',
'/DTSTART;TZID=\/example.com\/1.0.0-0\/Europe\/Berlin:20121111T130000/',
'/SEQUENCE:0/',
'/TRANSP:OPAQUE/',
'/DTEND;TZID=\/example.com\/1.0.0-0\/Europe\/Berlin:20121111T143000/',
'/SUMMARY:Weekly lunch with Markus/',
'/CLASS:PUBLIC/',
'/RRULE:FREQ=WEEKLY;INTERVAL=1/',
'/DTSTAMP:20\d{6}T\d{6}Z/',
'/END:VEVENT/',
'/END:VCALENDAR/',
);
foreach (explode("\n", $vCalendar->render()) as $key => $line)
{
$this->assertTrue(isset($lines[$key]), 'Too many lines... ' . $line);
$this->assertRegExp($lines[$key], $line);
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment