VFreeBusy.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Sabre\VObject\Component;
  3. use Sabre\VObject;
  4. /**
  5. * The VFreeBusy component
  6. *
  7. * This component adds functionality to a component, specific for VFREEBUSY
  8. * components.
  9. *
  10. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  11. * @author Evert Pot (http://evertpot.com/)
  12. * @license http://sabre.io/license/ Modified BSD License
  13. */
  14. class VFreeBusy extends VObject\Component {
  15. /**
  16. * Checks based on the contained FREEBUSY information, if a timeslot is
  17. * available.
  18. *
  19. * @param DateTime $start
  20. * @param Datetime $end
  21. * @return bool
  22. */
  23. public function isFree(\DateTime $start, \Datetime $end) {
  24. foreach($this->select('FREEBUSY') as $freebusy) {
  25. // We are only interested in FBTYPE=BUSY (the default),
  26. // FBTYPE=BUSY-TENTATIVE or FBTYPE=BUSY-UNAVAILABLE.
  27. if (isset($freebusy['FBTYPE']) && strtoupper(substr((string)$freebusy['FBTYPE'],0,4))!=='BUSY') {
  28. continue;
  29. }
  30. // The freebusy component can hold more than 1 value, separated by
  31. // commas.
  32. $periods = explode(',', (string)$freebusy);
  33. foreach($periods as $period) {
  34. // Every period is formatted as [start]/[end]. The start is an
  35. // absolute UTC time, the end may be an absolute UTC time, or
  36. // duration (relative) value.
  37. list($busyStart, $busyEnd) = explode('/', $period);
  38. $busyStart = VObject\DateTimeParser::parse($busyStart);
  39. $busyEnd = VObject\DateTimeParser::parse($busyEnd);
  40. if ($busyEnd instanceof \DateInterval) {
  41. $tmp = clone $busyStart;
  42. $tmp->add($busyEnd);
  43. $busyEnd = $tmp;
  44. }
  45. if($start < $busyEnd && $end > $busyStart) {
  46. return false;
  47. }
  48. }
  49. }
  50. return true;
  51. }
  52. /**
  53. * A simple list of validation rules.
  54. *
  55. * This is simply a list of properties, and how many times they either
  56. * must or must not appear.
  57. *
  58. * Possible values per property:
  59. * * 0 - Must not appear.
  60. * * 1 - Must appear exactly once.
  61. * * + - Must appear at least once.
  62. * * * - Can appear any number of times.
  63. * * ? - May appear, but not more than once.
  64. *
  65. * @var array
  66. */
  67. public function getValidationRules() {
  68. return array(
  69. 'UID' => 1,
  70. 'DTSTAMP' => 1,
  71. 'CONTACT' => '?',
  72. 'DTSTART' => '?',
  73. 'DTEND' => '?',
  74. 'ORGANIZER' => '?',
  75. 'URL' => '?',
  76. 'ATTENDEE' => '*',
  77. 'COMMENT' => '*',
  78. 'FREEBUSY' => '*',
  79. 'REQUEST-STATUS' => '*',
  80. );
  81. }
  82. }