VTodo.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace Sabre\VObject\Component;
  3. use Sabre\VObject;
  4. /**
  5. * VTodo component
  6. *
  7. * This component contains some additional functionality specific for VTODOs.
  8. *
  9. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  10. * @author Evert Pot (http://evertpot.com/)
  11. * @license http://sabre.io/license/ Modified BSD License
  12. */
  13. class VTodo extends VObject\Component {
  14. /**
  15. * Returns true or false depending on if the event falls in the specified
  16. * time-range. This is used for filtering purposes.
  17. *
  18. * The rules used to determine if an event falls within the specified
  19. * time-range is based on the CalDAV specification.
  20. *
  21. * @param DateTime $start
  22. * @param DateTime $end
  23. * @return bool
  24. */
  25. public function isInTimeRange(\DateTime $start, \DateTime $end) {
  26. $dtstart = isset($this->DTSTART)?$this->DTSTART->getDateTime():null;
  27. $duration = isset($this->DURATION)?VObject\DateTimeParser::parseDuration($this->DURATION):null;
  28. $due = isset($this->DUE)?$this->DUE->getDateTime():null;
  29. $completed = isset($this->COMPLETED)?$this->COMPLETED->getDateTime():null;
  30. $created = isset($this->CREATED)?$this->CREATED->getDateTime():null;
  31. if ($dtstart) {
  32. if ($duration) {
  33. $effectiveEnd = clone $dtstart;
  34. $effectiveEnd->add($duration);
  35. return $start <= $effectiveEnd && $end > $dtstart;
  36. } elseif ($due) {
  37. return
  38. ($start < $due || $start <= $dtstart) &&
  39. ($end > $dtstart || $end >= $due);
  40. } else {
  41. return $start <= $dtstart && $end > $dtstart;
  42. }
  43. }
  44. if ($due) {
  45. return ($start < $due && $end >= $due);
  46. }
  47. if ($completed && $created) {
  48. return
  49. ($start <= $created || $start <= $completed) &&
  50. ($end >= $created || $end >= $completed);
  51. }
  52. if ($completed) {
  53. return ($start <= $completed && $end >= $completed);
  54. }
  55. if ($created) {
  56. return ($end > $created);
  57. }
  58. return true;
  59. }
  60. /**
  61. * A simple list of validation rules.
  62. *
  63. * This is simply a list of properties, and how many times they either
  64. * must or must not appear.
  65. *
  66. * Possible values per property:
  67. * * 0 - Must not appear.
  68. * * 1 - Must appear exactly once.
  69. * * + - Must appear at least once.
  70. * * * - Can appear any number of times.
  71. * * ? - May appear, but not more than once.
  72. *
  73. * @var array
  74. */
  75. public function getValidationRules() {
  76. return array(
  77. 'UID' => 1,
  78. 'DTSTAMP' => 1,
  79. 'CLASS' => '?',
  80. 'COMPLETED' => '?',
  81. 'CREATED' => '?',
  82. 'DESCRIPTION' => '?',
  83. 'DTSTART' => '?',
  84. 'GEO' => '?',
  85. 'LAST-MODIFIED' => '?',
  86. 'LOCATION' => '?',
  87. 'ORGANIZER' => '?',
  88. 'PERCENT' => '?',
  89. 'PRIORITY' => '?',
  90. 'RECURRENCE-ID' => '?',
  91. 'SEQUENCE' => '?',
  92. 'STATUS' => '?',
  93. 'SUMMARY' => '?',
  94. 'URL' => '?',
  95. 'RRULE' => '?',
  96. 'DUE' => '?',
  97. 'DURATION' => '?',
  98. 'ATTACH' => '*',
  99. 'ATTENDEE' => '*',
  100. 'CATEGORIES' => '*',
  101. 'COMMENT' => '*',
  102. 'CONTACT' => '*',
  103. 'EXDATE' => '*',
  104. 'REQUEST-STATUS' => '*',
  105. 'RELATED-TO' => '*',
  106. 'RESOURCES' => '*',
  107. 'RDATE' => '*',
  108. );
  109. }
  110. /**
  111. * Validates the node for correctness.
  112. *
  113. * The following options are supported:
  114. * Node::REPAIR - May attempt to automatically repair the problem.
  115. *
  116. * This method returns an array with detected problems.
  117. * Every element has the following properties:
  118. *
  119. * * level - problem level.
  120. * * message - A human-readable string describing the issue.
  121. * * node - A reference to the problematic node.
  122. *
  123. * The level means:
  124. * 1 - The issue was repaired (only happens if REPAIR was turned on)
  125. * 2 - An inconsequential issue
  126. * 3 - A severe issue.
  127. *
  128. * @param int $options
  129. * @return array
  130. */
  131. public function validate($options = 0) {
  132. $result = parent::validate($options);
  133. if (isset($this->DUE) && isset($this->DTSTART)) {
  134. $due = $this->DUE;
  135. $dtStart = $this->DTSTART;
  136. if ($due->getValueType() !== $dtStart->getValueType()) {
  137. $result[] = array(
  138. 'level' => 3,
  139. 'message' => 'The value type (DATE or DATE-TIME) must be identical for DUE and DTSTART',
  140. 'node' => $due,
  141. );
  142. } elseif ($due->getDateTime() < $dtStart->getDateTime()) {
  143. $result[] = array(
  144. 'level' => 3,
  145. 'message' => 'DUE must occur after DTSTART',
  146. 'node' => $due,
  147. );
  148. }
  149. }
  150. return $result;
  151. }
  152. }