annotate plugins/libcalendaring/lib/Sabre/VObject/FreeBusyGenerator.php @ 43:771f6803cc4b default tip

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