FreeBusyGenerator.php 11 KB

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