VAlarm.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace Sabre\VObject\Component;
  3. use DateTimeImmutable;
  4. use DateTimeInterface;
  5. use Sabre\VObject;
  6. use Sabre\VObject\InvalidDataException;
  7. /**
  8. * VAlarm component.
  9. *
  10. * This component contains some additional functionality specific for VALARMs.
  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 VAlarm extends VObject\Component
  17. {
  18. /**
  19. * Returns a DateTime object when this alarm is going to trigger.
  20. *
  21. * This ignores repeated alarm, only the first trigger is returned.
  22. *
  23. * @return DateTimeImmutable
  24. */
  25. public function getEffectiveTriggerTime()
  26. {
  27. $trigger = $this->TRIGGER;
  28. if (!isset($trigger['VALUE']) || 'DURATION' === strtoupper($trigger['VALUE'])) {
  29. $triggerDuration = VObject\DateTimeParser::parseDuration($this->TRIGGER);
  30. $related = (isset($trigger['RELATED']) && 'END' == strtoupper($trigger['RELATED'])) ? 'END' : 'START';
  31. $parentComponent = $this->parent;
  32. if ('START' === $related) {
  33. if ('VTODO' === $parentComponent->name) {
  34. $propName = 'DUE';
  35. } else {
  36. $propName = 'DTSTART';
  37. }
  38. $effectiveTrigger = $parentComponent->$propName->getDateTime();
  39. $effectiveTrigger = $effectiveTrigger->add($triggerDuration);
  40. } else {
  41. if ('VTODO' === $parentComponent->name) {
  42. $endProp = 'DUE';
  43. } elseif ('VEVENT' === $parentComponent->name) {
  44. $endProp = 'DTEND';
  45. } else {
  46. throw new InvalidDataException('time-range filters on VALARM components are only supported when they are a child of VTODO or VEVENT');
  47. }
  48. if (isset($parentComponent->$endProp)) {
  49. $effectiveTrigger = $parentComponent->$endProp->getDateTime();
  50. $effectiveTrigger = $effectiveTrigger->add($triggerDuration);
  51. } elseif (isset($parentComponent->DURATION)) {
  52. $effectiveTrigger = $parentComponent->DTSTART->getDateTime();
  53. $duration = VObject\DateTimeParser::parseDuration($parentComponent->DURATION);
  54. $effectiveTrigger = $effectiveTrigger->add($duration);
  55. $effectiveTrigger = $effectiveTrigger->add($triggerDuration);
  56. } else {
  57. $effectiveTrigger = $parentComponent->DTSTART->getDateTime();
  58. $effectiveTrigger = $effectiveTrigger->add($triggerDuration);
  59. }
  60. }
  61. } else {
  62. $effectiveTrigger = $trigger->getDateTime();
  63. }
  64. return $effectiveTrigger;
  65. }
  66. /**
  67. * Returns true or false depending on if the event falls in the specified
  68. * time-range. This is used for filtering purposes.
  69. *
  70. * The rules used to determine if an event falls within the specified
  71. * time-range is based on the CalDAV specification.
  72. *
  73. * @param DateTime $start
  74. * @param DateTime $end
  75. *
  76. * @return bool
  77. */
  78. public function isInTimeRange(DateTimeInterface $start, DateTimeInterface $end)
  79. {
  80. $effectiveTrigger = $this->getEffectiveTriggerTime();
  81. if (isset($this->DURATION)) {
  82. $duration = VObject\DateTimeParser::parseDuration($this->DURATION);
  83. $repeat = (string) $this->REPEAT;
  84. if (!$repeat) {
  85. $repeat = 1;
  86. }
  87. $period = new \DatePeriod($effectiveTrigger, $duration, (int) $repeat);
  88. foreach ($period as $occurrence) {
  89. if ($start <= $occurrence && $end > $occurrence) {
  90. return true;
  91. }
  92. }
  93. return false;
  94. } else {
  95. return $start <= $effectiveTrigger && $end > $effectiveTrigger;
  96. }
  97. }
  98. /**
  99. * A simple list of validation rules.
  100. *
  101. * This is simply a list of properties, and how many times they either
  102. * must or must not appear.
  103. *
  104. * Possible values per property:
  105. * * 0 - Must not appear.
  106. * * 1 - Must appear exactly once.
  107. * * + - Must appear at least once.
  108. * * * - Can appear any number of times.
  109. * * ? - May appear, but not more than once.
  110. *
  111. * @var array
  112. */
  113. public function getValidationRules()
  114. {
  115. return [
  116. 'ACTION' => 1,
  117. 'TRIGGER' => 1,
  118. 'DURATION' => '?',
  119. 'REPEAT' => '?',
  120. 'ATTACH' => '?',
  121. ];
  122. }
  123. }