2012-06-03 14:19:28 -04:00
|
|
|
<?php
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
namespace Sabre\VObject;
|
|
|
|
|
2012-06-03 14:19:28 -04:00
|
|
|
/**
|
|
|
|
* This class helps with generating FREEBUSY reports based on existing sets of
|
|
|
|
* objects.
|
|
|
|
*
|
|
|
|
* It only looks at VEVENT and VFREEBUSY objects from the sourcedata, and
|
|
|
|
* generates a single VFREEBUSY object.
|
|
|
|
*
|
|
|
|
* VFREEBUSY components are described in RFC5545, The rules for what should
|
|
|
|
* go in a single freebusy report is taken from RFC4791, section 7.10.
|
|
|
|
*
|
|
|
|
* @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
|
|
|
|
* @author Evert Pot (http://www.rooftopsolutions.nl/)
|
|
|
|
* @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
|
|
|
|
*/
|
2012-08-11 04:07:19 -04:00
|
|
|
class FreeBusyGenerator {
|
2012-06-03 14:19:28 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Input objects
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $objects;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start of range
|
|
|
|
*
|
|
|
|
* @var DateTime|null
|
|
|
|
*/
|
|
|
|
protected $start;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* End of range
|
|
|
|
*
|
|
|
|
* @var DateTime|null
|
|
|
|
*/
|
|
|
|
protected $end;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* VCALENDAR object
|
|
|
|
*
|
2012-08-11 04:07:19 -04:00
|
|
|
* @var Component
|
2012-06-03 14:19:28 -04:00
|
|
|
*/
|
|
|
|
protected $baseObject;
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
/**
|
|
|
|
* Creates the generator.
|
|
|
|
*
|
|
|
|
* Check the setTimeRange and setObjects methods for details about the
|
|
|
|
* arguments.
|
|
|
|
*
|
|
|
|
* @param DateTime $start
|
|
|
|
* @param DateTime $end
|
|
|
|
* @param mixed $objects
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct(\DateTime $start = null, \DateTime $end = null, $objects = null) {
|
|
|
|
|
|
|
|
if ($start && $end) {
|
|
|
|
$this->setTimeRange($start, $end);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($objects) {
|
|
|
|
$this->setObjects($objects);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-06-03 14:19:28 -04:00
|
|
|
/**
|
|
|
|
* Sets the VCALENDAR object.
|
|
|
|
*
|
|
|
|
* If this is set, it will not be generated for you. You are responsible
|
|
|
|
* for setting things like the METHOD, CALSCALE, VERSION, etc..
|
|
|
|
*
|
|
|
|
* The VFREEBUSY object will be automatically added though.
|
|
|
|
*
|
2012-08-11 04:07:19 -04:00
|
|
|
* @param Component $vcalendar
|
2012-06-03 14:19:28 -04:00
|
|
|
* @return void
|
|
|
|
*/
|
2012-08-11 04:07:19 -04:00
|
|
|
public function setBaseObject(Component $vcalendar) {
|
2012-06-03 14:19:28 -04:00
|
|
|
|
|
|
|
$this->baseObject = $vcalendar;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the input objects
|
|
|
|
*
|
2012-08-11 04:07:19 -04:00
|
|
|
* You must either specify a valendar object as a strong, or as the parse
|
|
|
|
* Component.
|
|
|
|
* It's also possible to specify multiple objects as an array.
|
2012-06-03 14:19:28 -04:00
|
|
|
*
|
2012-08-11 04:07:19 -04:00
|
|
|
* @param mixed $objects
|
2012-06-03 14:19:28 -04:00
|
|
|
* @return void
|
|
|
|
*/
|
2012-08-11 04:07:19 -04:00
|
|
|
public function setObjects($objects) {
|
|
|
|
|
|
|
|
if (!is_array($objects)) {
|
|
|
|
$objects = array($objects);
|
|
|
|
}
|
2012-06-03 14:19:28 -04:00
|
|
|
|
|
|
|
$this->objects = array();
|
|
|
|
foreach($objects as $object) {
|
|
|
|
|
|
|
|
if (is_string($object)) {
|
2012-08-11 04:07:19 -04:00
|
|
|
$this->objects[] = Reader::read($object);
|
|
|
|
} elseif ($object instanceof Component) {
|
2012-06-03 14:19:28 -04:00
|
|
|
$this->objects[] = $object;
|
|
|
|
} else {
|
2012-08-11 04:07:19 -04:00
|
|
|
throw new \InvalidArgumentException('You can only pass strings or \\Sabre\\VObject\\Component arguments to setObjects');
|
2012-06-03 14:19:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the time range
|
|
|
|
*
|
|
|
|
* Any freebusy object falling outside of this time range will be ignored.
|
|
|
|
*
|
|
|
|
* @param DateTime $start
|
|
|
|
* @param DateTime $end
|
|
|
|
* @return void
|
|
|
|
*/
|
2012-08-11 04:07:19 -04:00
|
|
|
public function setTimeRange(\DateTime $start = null, \DateTime $end = null) {
|
2012-06-03 14:19:28 -04:00
|
|
|
|
|
|
|
$this->start = $start;
|
|
|
|
$this->end = $end;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses the input data and returns a correct VFREEBUSY object, wrapped in
|
|
|
|
* a VCALENDAR.
|
|
|
|
*
|
2012-08-11 04:07:19 -04:00
|
|
|
* @return Component
|
2012-06-03 14:19:28 -04:00
|
|
|
*/
|
|
|
|
public function getResult() {
|
|
|
|
|
|
|
|
$busyTimes = array();
|
|
|
|
|
|
|
|
foreach($this->objects as $object) {
|
|
|
|
|
|
|
|
foreach($object->getBaseComponents() as $component) {
|
|
|
|
|
|
|
|
switch($component->name) {
|
|
|
|
|
|
|
|
case 'VEVENT' :
|
|
|
|
|
|
|
|
$FBTYPE = 'BUSY';
|
|
|
|
if (isset($component->TRANSP) && (strtoupper($component->TRANSP) === 'TRANSPARENT')) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (isset($component->STATUS)) {
|
|
|
|
$status = strtoupper($component->STATUS);
|
|
|
|
if ($status==='CANCELLED') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ($status==='TENTATIVE') {
|
|
|
|
$FBTYPE = 'BUSY-TENTATIVE';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$times = array();
|
|
|
|
|
|
|
|
if ($component->RRULE) {
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$iterator = new RecurrenceIterator($object, (string)$component->uid);
|
2012-06-03 14:19:28 -04:00
|
|
|
if ($this->start) {
|
|
|
|
$iterator->fastForward($this->start);
|
|
|
|
}
|
|
|
|
|
|
|
|
$maxRecurrences = 200;
|
|
|
|
|
|
|
|
while($iterator->valid() && --$maxRecurrences) {
|
|
|
|
|
|
|
|
$startTime = $iterator->getDTStart();
|
|
|
|
if ($this->end && $startTime > $this->end) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$times[] = array(
|
|
|
|
$iterator->getDTStart(),
|
|
|
|
$iterator->getDTEnd(),
|
|
|
|
);
|
|
|
|
|
|
|
|
$iterator->next();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
$startTime = $component->DTSTART->getDateTime();
|
|
|
|
if ($this->end && $startTime > $this->end) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$endTime = null;
|
|
|
|
if (isset($component->DTEND)) {
|
|
|
|
$endTime = $component->DTEND->getDateTime();
|
|
|
|
} elseif (isset($component->DURATION)) {
|
2012-08-11 04:07:19 -04:00
|
|
|
$duration = DateTimeParser::parseDuration((string)$component->DURATION);
|
2012-06-03 14:19:28 -04:00
|
|
|
$endTime = clone $startTime;
|
|
|
|
$endTime->add($duration);
|
2012-08-11 04:07:19 -04:00
|
|
|
} elseif ($component->DTSTART->getDateType() === Property\DateTime::DATE) {
|
2012-06-03 14:19:28 -04:00
|
|
|
$endTime = clone $startTime;
|
|
|
|
$endTime->modify('+1 day');
|
|
|
|
} else {
|
|
|
|
// The event had no duration (0 seconds)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$times[] = array($startTime, $endTime);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach($times as $time) {
|
|
|
|
|
|
|
|
if ($this->end && $time[0] > $this->end) break;
|
|
|
|
if ($this->start && $time[1] < $this->start) break;
|
|
|
|
|
|
|
|
$busyTimes[] = array(
|
|
|
|
$time[0],
|
|
|
|
$time[1],
|
|
|
|
$FBTYPE,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'VFREEBUSY' :
|
|
|
|
foreach($component->FREEBUSY as $freebusy) {
|
|
|
|
|
|
|
|
$fbType = isset($freebusy['FBTYPE'])?strtoupper($freebusy['FBTYPE']):'BUSY';
|
|
|
|
|
|
|
|
// Skipping intervals marked as 'free'
|
|
|
|
if ($fbType==='FREE')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
$values = explode(',', $freebusy);
|
|
|
|
foreach($values as $value) {
|
|
|
|
list($startTime, $endTime) = explode('/', $value);
|
2012-08-11 04:07:19 -04:00
|
|
|
$startTime = DateTimeParser::parseDateTime($startTime);
|
2012-06-03 14:19:28 -04:00
|
|
|
|
|
|
|
if (substr($endTime,0,1)==='P' || substr($endTime,0,2)==='-P') {
|
2012-08-11 04:07:19 -04:00
|
|
|
$duration = DateTimeParser::parseDuration($endTime);
|
2012-06-03 14:19:28 -04:00
|
|
|
$endTime = clone $startTime;
|
|
|
|
$endTime->add($duration);
|
|
|
|
} else {
|
2012-08-11 04:07:19 -04:00
|
|
|
$endTime = DateTimeParser::parseDateTime($endTime);
|
2012-06-03 14:19:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if($this->start && $this->start > $endTime) continue;
|
|
|
|
if($this->end && $this->end < $startTime) continue;
|
|
|
|
$busyTimes[] = array(
|
|
|
|
$startTime,
|
|
|
|
$endTime,
|
|
|
|
$fbType
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->baseObject) {
|
|
|
|
$calendar = $this->baseObject;
|
|
|
|
} else {
|
2012-08-11 04:07:19 -04:00
|
|
|
$calendar = new Component('VCALENDAR');
|
2012-06-03 14:19:28 -04:00
|
|
|
$calendar->version = '2.0';
|
2012-08-11 04:07:19 -04:00
|
|
|
$calendar->prodid = '-//Sabre//Sabre VObject ' . Version::VERSION . '//EN';
|
2012-06-03 14:19:28 -04:00
|
|
|
$calendar->calscale = 'GREGORIAN';
|
|
|
|
}
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$vfreebusy = new Component('VFREEBUSY');
|
2012-06-03 14:19:28 -04:00
|
|
|
$calendar->add($vfreebusy);
|
|
|
|
|
|
|
|
if ($this->start) {
|
2012-08-11 04:07:19 -04:00
|
|
|
$dtstart = new Property\DateTime('DTSTART');
|
|
|
|
$dtstart->setDateTime($this->start,Property\DateTime::UTC);
|
2012-06-03 14:19:28 -04:00
|
|
|
$vfreebusy->add($dtstart);
|
|
|
|
}
|
|
|
|
if ($this->end) {
|
2012-08-11 04:07:19 -04:00
|
|
|
$dtend = new Property\DateTime('DTEND');
|
|
|
|
$dtend->setDateTime($this->start,Property\DateTime::UTC);
|
2012-06-03 14:19:28 -04:00
|
|
|
$vfreebusy->add($dtend);
|
|
|
|
}
|
2012-08-11 04:07:19 -04:00
|
|
|
$dtstamp = new Property\DateTime('DTSTAMP');
|
|
|
|
$dtstamp->setDateTime(new \DateTime('now'), Property\DateTime::UTC);
|
2012-06-03 14:19:28 -04:00
|
|
|
$vfreebusy->add($dtstamp);
|
|
|
|
|
|
|
|
foreach($busyTimes as $busyTime) {
|
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$busyTime[0]->setTimeZone(new \DateTimeZone('UTC'));
|
|
|
|
$busyTime[1]->setTimeZone(new \DateTimeZone('UTC'));
|
2012-06-03 14:19:28 -04:00
|
|
|
|
2012-08-11 04:07:19 -04:00
|
|
|
$prop = new Property(
|
2012-06-03 14:19:28 -04:00
|
|
|
'FREEBUSY',
|
|
|
|
$busyTime[0]->format('Ymd\\THis\\Z') . '/' . $busyTime[1]->format('Ymd\\THis\\Z')
|
|
|
|
);
|
|
|
|
$prop['FBTYPE'] = $busyTime[2];
|
|
|
|
$vfreebusy->add($prop);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return $calendar;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|