VAlarm.php 4.4 KB

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