VEvent.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace Sabre\VObject\Component;
  3. use DateTimeInterface;
  4. use Sabre\VObject;
  5. use Sabre\VObject\Recur\EventIterator;
  6. use Sabre\VObject\Recur\NoInstancesException;
  7. /**
  8. * VEvent component.
  9. *
  10. * This component contains some additional functionality specific for VEVENT's.
  11. *
  12. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  13. * @author Evert Pot (http://evertpot.com/)
  14. * @license http://sabre.io/license/ Modified BSD License
  15. */
  16. class VEvent extends VObject\Component
  17. {
  18. /**
  19. * Returns true or false depending on if the event falls in the specified
  20. * time-range. This is used for filtering purposes.
  21. *
  22. * The rules used to determine if an event falls within the specified
  23. * time-range is based on the CalDAV specification.
  24. *
  25. * @return bool
  26. */
  27. public function isInTimeRange(DateTimeInterface $start, DateTimeInterface $end)
  28. {
  29. if ($this->RRULE) {
  30. try {
  31. $it = new EventIterator($this, null, $start->getTimezone());
  32. } catch (NoInstancesException $e) {
  33. // If we've caught this exception, there are no instances
  34. // for the event that fall into the specified time-range.
  35. return false;
  36. }
  37. $it->fastForward($start);
  38. // We fast-forwarded to a spot where the end-time of the
  39. // recurrence instance exceeded the start of the requested
  40. // time-range.
  41. //
  42. // If the starttime of the recurrence did not exceed the
  43. // end of the time range as well, we have a match.
  44. return $it->getDTStart() < $end && $it->getDTEnd() > $start;
  45. }
  46. $effectiveStart = $this->DTSTART->getDateTime($start->getTimezone());
  47. if (isset($this->DTEND)) {
  48. // The DTEND property is considered non inclusive. So for a 3 day
  49. // event in july, dtstart and dtend would have to be July 1st and
  50. // July 4th respectively.
  51. //
  52. // See:
  53. // http://tools.ietf.org/html/rfc5545#page-54
  54. $effectiveEnd = $this->DTEND->getDateTime($end->getTimezone());
  55. } elseif (isset($this->DURATION)) {
  56. $effectiveEnd = $effectiveStart->add(VObject\DateTimeParser::parseDuration($this->DURATION));
  57. } elseif (!$this->DTSTART->hasTime()) {
  58. $effectiveEnd = $effectiveStart->modify('+1 day');
  59. } else {
  60. $effectiveEnd = $effectiveStart;
  61. }
  62. return
  63. ($start < $effectiveEnd) && ($end > $effectiveStart)
  64. ;
  65. }
  66. /**
  67. * This method should return a list of default property values.
  68. *
  69. * @return array
  70. */
  71. protected function getDefaults()
  72. {
  73. return [
  74. 'UID' => 'sabre-vobject-'.VObject\UUIDUtil::getUUID(),
  75. 'DTSTAMP' => gmdate('Ymd\\THis\\Z'),
  76. ];
  77. }
  78. /**
  79. * A simple list of validation rules.
  80. *
  81. * This is simply a list of properties, and how many times they either
  82. * must or must not appear.
  83. *
  84. * Possible values per property:
  85. * * 0 - Must not appear.
  86. * * 1 - Must appear exactly once.
  87. * * + - Must appear at least once.
  88. * * * - Can appear any number of times.
  89. * * ? - May appear, but not more than once.
  90. *
  91. * @var array
  92. */
  93. public function getValidationRules()
  94. {
  95. $hasMethod = isset($this->parent->METHOD);
  96. return [
  97. 'UID' => 1,
  98. 'DTSTAMP' => 1,
  99. 'DTSTART' => $hasMethod ? '?' : '1',
  100. 'CLASS' => '?',
  101. 'CREATED' => '?',
  102. 'DESCRIPTION' => '?',
  103. 'GEO' => '?',
  104. 'LAST-MODIFIED' => '?',
  105. 'LOCATION' => '?',
  106. 'ORGANIZER' => '?',
  107. 'PRIORITY' => '?',
  108. 'SEQUENCE' => '?',
  109. 'STATUS' => '?',
  110. 'SUMMARY' => '?',
  111. 'TRANSP' => '?',
  112. 'URL' => '?',
  113. 'RECURRENCE-ID' => '?',
  114. 'RRULE' => '?',
  115. 'DTEND' => '?',
  116. 'DURATION' => '?',
  117. 'ATTACH' => '*',
  118. 'ATTENDEE' => '*',
  119. 'CATEGORIES' => '*',
  120. 'COMMENT' => '*',
  121. 'CONTACT' => '*',
  122. 'EXDATE' => '*',
  123. 'REQUEST-STATUS' => '*',
  124. 'RELATED-TO' => '*',
  125. 'RESOURCES' => '*',
  126. 'RDATE' => '*',
  127. ];
  128. }
  129. }