Blame view

sources/3rdparty/sabre/vobject/lib/Sabre/VObject/FreeBusyGenerator.php 9.83 KB
03e52840d   Kload   Init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  <?php
  
  namespace Sabre\VObject;
  
  /**
   * 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.
   *
6d9380f96   Cédric Dupont   Update sources OC...
15
16
   * @copyright Copyright (C) 2007-2013 fruux GmbH (https://fruux.com/).
   * @author Evert Pot (http://evertpot.com/)
03e52840d   Kload   Init
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
   * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
   */
  class FreeBusyGenerator {
  
      /**
       * Input objects
       *
       * @var array
       */
      protected $objects;
  
      /**
       * Start of range
       *
       * @var DateTime|null
       */
      protected $start;
  
      /**
       * End of range
       *
       * @var DateTime|null
       */
      protected $end;
  
      /**
       * VCALENDAR object
       *
       * @var Component
       */
      protected $baseObject;
  
      /**
       * 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);
          }
  
      }
  
      /**
       * 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.
       *
       * @param Component $vcalendar
       * @return void
       */
      public function setBaseObject(Component $vcalendar) {
  
          $this->baseObject = $vcalendar;
  
      }
  
      /**
       * Sets the input objects
       *
       * 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.
       *
       * @param mixed $objects
       * @return void
       */
      public function setObjects($objects) {
  
          if (!is_array($objects)) {
              $objects = array($objects);
          }
  
          $this->objects = array();
          foreach($objects as $object) {
  
              if (is_string($object)) {
                  $this->objects[] = Reader::read($object);
              } elseif ($object instanceof Component) {
                  $this->objects[] = $object;
              } else {
                  throw new \InvalidArgumentException('You can only pass strings or \\Sabre\\VObject\\Component arguments to setObjects');
              }
  
          }
  
      }
  
      /**
       * Sets the time range
       *
       * Any freebusy object falling outside of this time range will be ignored.
       *
       * @param DateTime $start
       * @param DateTime $end
       * @return void
       */
      public function setTimeRange(\DateTime $start = null, \DateTime $end = null) {
  
          $this->start = $start;
          $this->end = $end;
  
      }
  
      /**
       * Parses the input data and returns a correct VFREEBUSY object, wrapped in
       * a VCALENDAR.
       *
       * @return Component
       */
      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) {
  
                              $iterator = new RecurrenceIterator($object, (string)$component->uid);
                              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)) {
                                  $duration = DateTimeParser::parseDuration((string)$component->DURATION);
                                  $endTime = clone $startTime;
                                  $endTime->add($duration);
                              } elseif ($component->DTSTART->getDateType() === Property\DateTime::DATE) {
                                  $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);
                                  $startTime = DateTimeParser::parseDateTime($startTime);
  
                                  if (substr($endTime,0,1)==='P' || substr($endTime,0,2)==='-P') {
                                      $duration = DateTimeParser::parseDuration($endTime);
                                      $endTime = clone $startTime;
                                      $endTime->add($duration);
                                  } else {
                                      $endTime = DateTimeParser::parseDateTime($endTime);
                                  }
  
                                  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 {
6d9380f96   Cédric Dupont   Update sources OC...
280
              $calendar = Component::create('VCALENDAR');
03e52840d   Kload   Init
281
282
283
284
              $calendar->version = '2.0';
              $calendar->prodid = '-//Sabre//Sabre VObject ' . Version::VERSION . '//EN';
              $calendar->calscale = 'GREGORIAN';
          }
6d9380f96   Cédric Dupont   Update sources OC...
285
          $vfreebusy = Component::create('VFREEBUSY');
03e52840d   Kload   Init
286
287
288
          $calendar->add($vfreebusy);
  
          if ($this->start) {
6d9380f96   Cédric Dupont   Update sources OC...
289
              $dtstart = Property::create('DTSTART');
03e52840d   Kload   Init
290
291
292
293
              $dtstart->setDateTime($this->start,Property\DateTime::UTC);
              $vfreebusy->add($dtstart);
          }
          if ($this->end) {
6d9380f96   Cédric Dupont   Update sources OC...
294
              $dtend = Property::create('DTEND');
03e52840d   Kload   Init
295
296
297
              $dtend->setDateTime($this->end,Property\DateTime::UTC);
              $vfreebusy->add($dtend);
          }
6d9380f96   Cédric Dupont   Update sources OC...
298
          $dtstamp = Property::create('DTSTAMP');
03e52840d   Kload   Init
299
300
301
302
303
304
305
          $dtstamp->setDateTime(new \DateTime('now'), Property\DateTime::UTC);
          $vfreebusy->add($dtstamp);
  
          foreach($busyTimes as $busyTime) {
  
              $busyTime[0]->setTimeZone(new \DateTimeZone('UTC'));
              $busyTime[1]->setTimeZone(new \DateTimeZone('UTC'));
6d9380f96   Cédric Dupont   Update sources OC...
306
              $prop = Property::create(
03e52840d   Kload   Init
307
308
309
310
311
312
313
314
315
316
317
318
319
                  'FREEBUSY',
                  $busyTime[0]->format('Ymd\\THis\\Z') . '/' . $busyTime[1]->format('Ymd\\THis\\Z')
              );
              $prop['FBTYPE'] = $busyTime[2];
              $vfreebusy->add($prop);
  
          }
  
          return $calendar;
  
      }
  
  }