VEvent.php 4.4 KB

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