3 namespace Sabre\VObject;
6 * This class helps with generating FREEBUSY reports based on existing sets of
9 * It only looks at VEVENT and VFREEBUSY objects from the sourcedata, and
10 * generates a single VFREEBUSY object.
12 * VFREEBUSY components are described in RFC5545, The rules for what should
13 * go in a single freebusy report is taken from RFC4791, section 7.10.
15 * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
16 * @author Evert Pot (http://www.rooftopsolutions.nl/)
17 * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
19 class FreeBusyGenerator {
47 protected $baseObject;
50 * Creates the generator.
52 * Check the setTimeRange and setObjects methods for details about the
55 * @param DateTime $start
56 * @param DateTime $end
57 * @param mixed $objects
60 public function __construct(\DateTime $start = null, \DateTime $end = null, $objects = null) {
63 $this->setTimeRange($start, $end);
67 $this->setObjects($objects);
73 * Sets the VCALENDAR object.
75 * If this is set, it will not be generated for you. You are responsible
76 * for setting things like the METHOD, CALSCALE, VERSION, etc..
78 * The VFREEBUSY object will be automatically added though.
80 * @param Component $vcalendar
83 public function setBaseObject(Component $vcalendar) {
85 $this->baseObject = $vcalendar;
90 * Sets the input objects
92 * You must either specify a valendar object as a strong, or as the parse
94 * It's also possible to specify multiple objects as an array.
96 * @param mixed $objects
99 public function setObjects($objects) {
101 if (!is_array($objects)) {
102 $objects = array($objects);
105 $this->objects = array();
106 foreach($objects as $object) {
108 if (is_string($object)) {
109 $this->objects[] = Reader::read($object);
110 } elseif ($object instanceof Component) {
111 $this->objects[] = $object;
113 throw new \InvalidArgumentException('You can only pass strings or \\Sabre\\VObject\\Component arguments to setObjects');
121 * Sets the time range
123 * Any freebusy object falling outside of this time range will be ignored.
125 * @param DateTime $start
126 * @param DateTime $end
129 public function setTimeRange(\DateTime $start = null, \DateTime $end = null) {
131 $this->start = $start;
137 * Parses the input data and returns a correct VFREEBUSY object, wrapped in
142 public function getResult() {
144 $busyTimes = array();
146 foreach($this->objects as $object) {
148 foreach($object->getBaseComponents() as $component) {
150 switch($component->name) {
155 if (isset($component->TRANSP) && (strtoupper($component->TRANSP) === 'TRANSPARENT')) {
158 if (isset($component->STATUS)) {
159 $status = strtoupper($component->STATUS);
160 if ($status==='CANCELLED') {
163 if ($status==='TENTATIVE') {
164 $FBTYPE = 'BUSY-TENTATIVE';
170 if ($component->RRULE) {
172 $iterator = new RecurrenceIterator($object, (string)$component->uid);
174 $iterator->fastForward($this->start);
177 $maxRecurrences = 200;
179 while($iterator->valid() && --$maxRecurrences) {
181 $startTime = $iterator->getDTStart();
182 if ($this->end && $startTime > $this->end) {
186 $iterator->getDTStart(),
187 $iterator->getDTEnd(),
196 $startTime = $component->DTSTART->getDateTime();
197 if ($this->end && $startTime > $this->end) {
201 if (isset($component->DTEND)) {
202 $endTime = $component->DTEND->getDateTime();
203 } elseif (isset($component->DURATION)) {
204 $duration = DateTimeParser::parseDuration((string)$component->DURATION);
205 $endTime = clone $startTime;
206 $endTime->add($duration);
207 } elseif ($component->DTSTART->getDateType() === Property\DateTime::DATE) {
208 $endTime = clone $startTime;
209 $endTime->modify('+1 day');
211 // The event had no duration (0 seconds)
215 $times[] = array($startTime, $endTime);
219 foreach($times as $time) {
221 if ($this->end && $time[0] > $this->end) break;
222 if ($this->start && $time[1] < $this->start) break;
224 $busyTimes[] = array(
233 foreach($component->FREEBUSY as $freebusy) {
235 $fbType = isset($freebusy['FBTYPE'])?strtoupper($freebusy['FBTYPE']):'BUSY';
237 // Skipping intervals marked as 'free'
238 if ($fbType==='FREE')
241 $values = explode(',', $freebusy);
242 foreach($values as $value) {
243 list($startTime, $endTime) = explode('/', $value);
244 $startTime = DateTimeParser::parseDateTime($startTime);
246 if (substr($endTime,0,1)==='P' || substr($endTime,0,2)==='-P') {
247 $duration = DateTimeParser::parseDuration($endTime);
248 $endTime = clone $startTime;
249 $endTime->add($duration);
251 $endTime = DateTimeParser::parseDateTime($endTime);
254 if($this->start && $this->start > $endTime) continue;
255 if($this->end && $this->end < $startTime) continue;
256 $busyTimes[] = array(
277 if ($this->baseObject) {
278 $calendar = $this->baseObject;
280 $calendar = new Component('VCALENDAR');
281 $calendar->version = '2.0';
282 $calendar->prodid = '-//Sabre//Sabre VObject ' . Version::VERSION . '//EN';
283 $calendar->calscale = 'GREGORIAN';
286 $vfreebusy = new Component('VFREEBUSY');
287 $calendar->add($vfreebusy);
290 $dtstart = new Property\DateTime('DTSTART');
291 $dtstart->setDateTime($this->start,Property\DateTime::UTC);
292 $vfreebusy->add($dtstart);
295 $dtend = new Property\DateTime('DTEND');
296 $dtend->setDateTime($this->start,Property\DateTime::UTC);
297 $vfreebusy->add($dtend);
299 $dtstamp = new Property\DateTime('DTSTAMP');
300 $dtstamp->setDateTime(new \DateTime('now'), Property\DateTime::UTC);
301 $vfreebusy->add($dtstamp);
303 foreach($busyTimes as $busyTime) {
305 $busyTime[0]->setTimeZone(new \DateTimeZone('UTC'));
306 $busyTime[1]->setTimeZone(new \DateTimeZone('UTC'));
308 $prop = new Property(
310 $busyTime[0]->format('Ymd\\THis\\Z') . '/' . $busyTime[1]->format('Ymd\\THis\\Z')
312 $prop['FBTYPE'] = $busyTime[2];
313 $vfreebusy->add($prop);