VTodo.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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) 2007-2014 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. *
  72. * @var array
  73. */
  74. public function getValidationRules() {
  75. return array(
  76. 'UID' => 1,
  77. 'DTSTAMP' => 1,
  78. 'CLASS' => '?',
  79. 'COMPLETED' => '?',
  80. 'CREATED' => '?',
  81. 'DESCRIPTION' => '?',
  82. 'DTSTART' => '?',
  83. 'GEO' => '?',
  84. 'LAST-MODIFICATION' => '?',
  85. 'LOCATION' => '?',
  86. 'ORGANIZER' => '?',
  87. 'PERCENT' => '?',
  88. 'PRIORITY' => '?',
  89. 'RECURRENCE-ID' => '?',
  90. 'SEQUENCE' => '?',
  91. 'STATUS' => '?',
  92. 'SUMMARY' => '?',
  93. 'URL' => '?',
  94. 'RRULE' => '?',
  95. 'DUE' => '?',
  96. 'DURATION' => '?',
  97. 'ATTACH' => '*',
  98. 'ATTENDEE' => '*',
  99. 'CATEGORIES' => '*',
  100. 'COMMENT' => '*',
  101. 'CONTACT' => '*',
  102. 'EXDATE' => '*',
  103. 'REQUEST-STATUS' => '*',
  104. 'RELATED-TO' => '*',
  105. 'RESOURCES' => '*',
  106. 'RDATE' => '*',
  107. );
  108. }
  109. /**
  110. * Validates the node for correctness.
  111. *
  112. * The following options are supported:
  113. * Node::REPAIR - May attempt to automatically repair the problem.
  114. *
  115. * This method returns an array with detected problems.
  116. * Every element has the following properties:
  117. *
  118. * * level - problem level.
  119. * * message - A human-readable string describing the issue.
  120. * * node - A reference to the problematic node.
  121. *
  122. * The level means:
  123. * 1 - The issue was repaired (only happens if REPAIR was turned on)
  124. * 2 - An inconsequential issue
  125. * 3 - A severe issue.
  126. *
  127. * @param int $options
  128. * @return array
  129. */
  130. public function validate($options = 0) {
  131. $result = parent::validate($options);
  132. if (isset($this->DUE) && isset($this->DTSTART)) {
  133. $due = $this->DUE;
  134. $dtStart = $this->DTSTART;
  135. if ($due->getValueType() !== $dtStart->getValueType()) {
  136. $result[] = array(
  137. 'level' => 3,
  138. 'message' => 'The value type (DATE or DATE-TIME) must be identical for DUE and DTSTART',
  139. 'node' => $due,
  140. );
  141. } elseif ($due->getDateTime() < $dtStart->getDateTime()) {
  142. $result[] = array(
  143. 'level' => 3,
  144. 'message' => 'DUE must occur after DTSTART',
  145. 'node' => $due,
  146. );
  147. }
  148. }
  149. return $result;
  150. }
  151. }