7
|
1 <?php
|
|
2
|
|
3 namespace Sabre\VObject;
|
|
4
|
|
5 use DateTimeZone;
|
|
6 use Sabre\VObject\Component\VCalendar;
|
|
7 use Sabre\VObject\Recur\EventIterator;
|
|
8
|
|
9 /**
|
|
10 * This class helps with generating FREEBUSY reports based on existing sets of
|
|
11 * objects.
|
|
12 *
|
|
13 * It only looks at VEVENT and VFREEBUSY objects from the sourcedata, and
|
|
14 * generates a single VFREEBUSY object.
|
|
15 *
|
|
16 * VFREEBUSY components are described in RFC5545, The rules for what should
|
|
17 * go in a single freebusy report is taken from RFC4791, section 7.10.
|
|
18 *
|
|
19 * @copyright Copyright (C) 2011-2015 fruux GmbH (https://fruux.com/).
|
|
20 * @author Evert Pot (http://evertpot.com/)
|
|
21 * @license http://sabre.io/license/ Modified BSD License
|
|
22 */
|
|
23 class FreeBusyGenerator {
|
|
24
|
|
25 /**
|
|
26 * Input objects
|
|
27 *
|
|
28 * @var array
|
|
29 */
|
|
30 protected $objects;
|
|
31
|
|
32 /**
|
|
33 * Start of range
|
|
34 *
|
|
35 * @var DateTime|null
|
|
36 */
|
|
37 protected $start;
|
|
38
|
|
39 /**
|
|
40 * End of range
|
|
41 *
|
|
42 * @var DateTime|null
|
|
43 */
|
|
44 protected $end;
|
|
45
|
|
46 /**
|
|
47 * VCALENDAR object
|
|
48 *
|
|
49 * @var Component
|
|
50 */
|
|
51 protected $baseObject;
|
|
52
|
|
53 /**
|
|
54 * Reference timezone.
|
|
55 *
|
|
56 * When we are calculating busy times, and we come across so-called
|
|
57 * floating times (times without a timezone), we use the reference timezone
|
|
58 * instead.
|
|
59 *
|
|
60 * This is also used for all-day events.
|
|
61 *
|
|
62 * This defaults to UTC.
|
|
63 *
|
|
64 * @var DateTimeZone
|
|
65 */
|
|
66 protected $timeZone;
|
|
67
|
|
68 /**
|
|
69 * Creates the generator.
|
|
70 *
|
|
71 * Check the setTimeRange and setObjects methods for details about the
|
|
72 * arguments.
|
|
73 *
|
|
74 * @param DateTime $start
|
|
75 * @param DateTime $end
|
|
76 * @param mixed $objects
|
|
77 * @param DateTimeZone $timeZone
|
|
78 * @return void
|
|
79 */
|
|
80 public function __construct(\DateTime $start = null, \DateTime $end = null, $objects = null, DateTimeZone $timeZone = null) {
|
|
81
|
|
82 if ($start && $end) {
|
|
83 $this->setTimeRange($start, $end);
|
|
84 }
|
|
85
|
|
86 if ($objects) {
|
|
87 $this->setObjects($objects);
|
|
88 }
|
|
89 if (is_null($timeZone)) {
|
|
90 $timeZone = new DateTimeZone('UTC');
|
|
91 }
|
|
92 $this->setTimeZone($timeZone);
|
|
93
|
|
94 }
|
|
95
|
|
96 /**
|
|
97 * Sets the VCALENDAR object.
|
|
98 *
|
|
99 * If this is set, it will not be generated for you. You are responsible
|
|
100 * for setting things like the METHOD, CALSCALE, VERSION, etc..
|
|
101 *
|
|
102 * The VFREEBUSY object will be automatically added though.
|
|
103 *
|
|
104 * @param Component $vcalendar
|
|
105 * @return void
|
|
106 */
|
|
107 public function setBaseObject(Component $vcalendar) {
|
|
108
|
|
109 $this->baseObject = $vcalendar;
|
|
110
|
|
111 }
|
|
112
|
|
113 /**
|
|
114 * Sets the input objects
|
|
115 *
|
|
116 * You must either specify a valendar object as a strong, or as the parse
|
|
117 * Component.
|
|
118 * It's also possible to specify multiple objects as an array.
|
|
119 *
|
|
120 * @param mixed $objects
|
|
121 * @return void
|
|
122 */
|
|
123 public function setObjects($objects) {
|
|
124
|
|
125 if (!is_array($objects)) {
|
|
126 $objects = array($objects);
|
|
127 }
|
|
128
|
|
129 $this->objects = array();
|
|
130 foreach($objects as $object) {
|
|
131
|
|
132 if (is_string($object)) {
|
|
133 $this->objects[] = Reader::read($object);
|
|
134 } elseif ($object instanceof Component) {
|
|
135 $this->objects[] = $object;
|
|
136 } else {
|
|
137 throw new \InvalidArgumentException('You can only pass strings or \\Sabre\\VObject\\Component arguments to setObjects');
|
|
138 }
|
|
139
|
|
140 }
|
|
141
|
|
142 }
|
|
143
|
|
144 /**
|
|
145 * Sets the time range
|
|
146 *
|
|
147 * Any freebusy object falling outside of this time range will be ignored.
|
|
148 *
|
|
149 * @param DateTime $start
|
|
150 * @param DateTime $end
|
|
151 * @return void
|
|
152 */
|
|
153 public function setTimeRange(\DateTime $start = null, \DateTime $end = null) {
|
|
154
|
|
155 $this->start = $start;
|
|
156 $this->end = $end;
|
|
157
|
|
158 }
|
|
159
|
|
160 /**
|
|
161 * Sets the reference timezone for floating times.
|
|
162 *
|
|
163 * @param DateTimeZone $timeZone
|
|
164 * @return void
|
|
165 */
|
|
166 public function setTimeZone(DateTimeZone $timeZone) {
|
|
167
|
|
168 $this->timeZone = $timeZone;
|
|
169
|
|
170 }
|
|
171
|
|
172 /**
|
|
173 * Parses the input data and returns a correct VFREEBUSY object, wrapped in
|
|
174 * a VCALENDAR.
|
|
175 *
|
|
176 * @return Component
|
|
177 */
|
|
178 public function getResult() {
|
|
179
|
|
180 $busyTimes = array();
|
|
181
|
|
182 foreach($this->objects as $object) {
|
|
183
|
|
184 foreach($object->getBaseComponents() as $component) {
|
|
185
|
|
186 switch($component->name) {
|
|
187
|
|
188 case 'VEVENT' :
|
|
189
|
|
190 $FBTYPE = 'BUSY';
|
|
191 if (isset($component->TRANSP) && (strtoupper($component->TRANSP) === 'TRANSPARENT')) {
|
|
192 break;
|
|
193 }
|
|
194 if (isset($component->STATUS)) {
|
|
195 $status = strtoupper($component->STATUS);
|
|
196 if ($status==='CANCELLED') {
|
|
197 break;
|
|
198 }
|
|
199 if ($status==='TENTATIVE') {
|
|
200 $FBTYPE = 'BUSY-TENTATIVE';
|
|
201 }
|
|
202 }
|
|
203
|
|
204 $times = array();
|
|
205
|
|
206 if ($component->RRULE) {
|
|
207
|
|
208 $iterator = new EventIterator($object, (string)$component->uid, $this->timeZone);
|
|
209 if ($this->start) {
|
|
210 $iterator->fastForward($this->start);
|
|
211 }
|
|
212
|
|
213 $maxRecurrences = 200;
|
|
214
|
|
215 while($iterator->valid() && --$maxRecurrences) {
|
|
216
|
|
217 $startTime = $iterator->getDTStart();
|
|
218 if ($this->end && $startTime > $this->end) {
|
|
219 break;
|
|
220 }
|
|
221 $times[] = array(
|
|
222 $iterator->getDTStart(),
|
|
223 $iterator->getDTEnd(),
|
|
224 );
|
|
225
|
|
226 $iterator->next();
|
|
227
|
|
228 }
|
|
229
|
|
230 } else {
|
|
231
|
|
232 $startTime = $component->DTSTART->getDateTime($this->timeZone);
|
|
233 if ($this->end && $startTime > $this->end) {
|
|
234 break;
|
|
235 }
|
|
236 $endTime = null;
|
|
237 if (isset($component->DTEND)) {
|
|
238 $endTime = $component->DTEND->getDateTime($this->timeZone);
|
|
239 } elseif (isset($component->DURATION)) {
|
|
240 $duration = DateTimeParser::parseDuration((string)$component->DURATION);
|
|
241 $endTime = clone $startTime;
|
|
242 $endTime->add($duration);
|
|
243 } elseif (!$component->DTSTART->hasTime()) {
|
|
244 $endTime = clone $startTime;
|
|
245 $endTime->modify('+1 day');
|
|
246 } else {
|
|
247 // The event had no duration (0 seconds)
|
|
248 break;
|
|
249 }
|
|
250
|
|
251 $times[] = array($startTime, $endTime);
|
|
252
|
|
253 }
|
|
254
|
|
255 foreach($times as $time) {
|
|
256
|
|
257 if ($this->end && $time[0] > $this->end) break;
|
|
258 if ($this->start && $time[1] < $this->start) break;
|
|
259
|
|
260 $busyTimes[] = array(
|
|
261 $time[0],
|
|
262 $time[1],
|
|
263 $FBTYPE,
|
|
264 );
|
|
265 }
|
|
266 break;
|
|
267
|
|
268 case 'VFREEBUSY' :
|
|
269 foreach($component->FREEBUSY as $freebusy) {
|
|
270
|
|
271 $fbType = isset($freebusy['FBTYPE'])?strtoupper($freebusy['FBTYPE']):'BUSY';
|
|
272
|
|
273 // Skipping intervals marked as 'free'
|
|
274 if ($fbType==='FREE')
|
|
275 continue;
|
|
276
|
|
277 $values = explode(',', $freebusy);
|
|
278 foreach($values as $value) {
|
|
279 list($startTime, $endTime) = explode('/', $value);
|
|
280 $startTime = DateTimeParser::parseDateTime($startTime);
|
|
281
|
|
282 if (substr($endTime,0,1)==='P' || substr($endTime,0,2)==='-P') {
|
|
283 $duration = DateTimeParser::parseDuration($endTime);
|
|
284 $endTime = clone $startTime;
|
|
285 $endTime->add($duration);
|
|
286 } else {
|
|
287 $endTime = DateTimeParser::parseDateTime($endTime);
|
|
288 }
|
|
289
|
|
290 if($this->start && $this->start > $endTime) continue;
|
|
291 if($this->end && $this->end < $startTime) continue;
|
|
292 $busyTimes[] = array(
|
|
293 $startTime,
|
|
294 $endTime,
|
|
295 $fbType
|
|
296 );
|
|
297
|
|
298 }
|
|
299
|
|
300
|
|
301 }
|
|
302 break;
|
|
303
|
|
304
|
|
305
|
|
306 }
|
|
307
|
|
308
|
|
309 }
|
|
310
|
|
311 }
|
|
312
|
|
313 if ($this->baseObject) {
|
|
314 $calendar = $this->baseObject;
|
|
315 } else {
|
|
316 $calendar = new VCalendar();
|
|
317 }
|
|
318
|
|
319 $vfreebusy = $calendar->createComponent('VFREEBUSY');
|
|
320 $calendar->add($vfreebusy);
|
|
321
|
|
322 if ($this->start) {
|
|
323 $dtstart = $calendar->createProperty('DTSTART');
|
|
324 $dtstart->setDateTime($this->start);
|
|
325 $vfreebusy->add($dtstart);
|
|
326 }
|
|
327 if ($this->end) {
|
|
328 $dtend = $calendar->createProperty('DTEND');
|
|
329 $dtend->setDateTime($this->end);
|
|
330 $vfreebusy->add($dtend);
|
|
331 }
|
|
332 $dtstamp = $calendar->createProperty('DTSTAMP');
|
|
333 $dtstamp->setDateTime(new \DateTime('now', new \DateTimeZone('UTC')));
|
|
334 $vfreebusy->add($dtstamp);
|
|
335
|
|
336 foreach($busyTimes as $busyTime) {
|
|
337
|
|
338 $busyTime[0]->setTimeZone(new \DateTimeZone('UTC'));
|
|
339 $busyTime[1]->setTimeZone(new \DateTimeZone('UTC'));
|
|
340
|
|
341 $prop = $calendar->createProperty(
|
|
342 'FREEBUSY',
|
|
343 $busyTime[0]->format('Ymd\\THis\\Z') . '/' . $busyTime[1]->format('Ymd\\THis\\Z')
|
|
344 );
|
|
345 $prop['FBTYPE'] = $busyTime[2];
|
|
346 $vfreebusy->add($prop);
|
|
347
|
|
348 }
|
|
349
|
|
350 return $calendar;
|
|
351
|
|
352 }
|
|
353
|
|
354 }
|
|
355
|