Mercurial > hg > rc1
comparison plugins/libcalendaring/lib/Sabre/VObject/Component/VAlarm.php @ 4:888e774ee983
libcalendar plugin as distributed
| author | Charlie Root |
|---|---|
| date | Sat, 13 Jan 2018 08:57:56 -0500 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 3:f6fe4b6ae66a | 4:888e774ee983 |
|---|---|
| 1 <?php | |
| 2 | |
| 3 namespace Sabre\VObject\Component; | |
| 4 use Sabre\VObject; | |
| 5 | |
| 6 /** | |
| 7 * VAlarm component | |
| 8 * | |
| 9 * This component contains some additional functionality specific for VALARMs. | |
| 10 * | |
| 11 * @copyright Copyright (C) 2007-2013 fruux GmbH (https://fruux.com/). | |
| 12 * @author Evert Pot (http://evertpot.com/) | |
| 13 * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License | |
| 14 */ | |
| 15 class VAlarm extends VObject\Component { | |
| 16 | |
| 17 /** | |
| 18 * Returns a DateTime object when this alarm is going to trigger. | |
| 19 * | |
| 20 * This ignores repeated alarm, only the first trigger is returned. | |
| 21 * | |
| 22 * @return DateTime | |
| 23 */ | |
| 24 public function getEffectiveTriggerTime() { | |
| 25 | |
| 26 $trigger = $this->TRIGGER; | |
| 27 if(!isset($trigger['VALUE']) || strtoupper($trigger['VALUE']) === 'DURATION') { | |
| 28 $triggerDuration = VObject\DateTimeParser::parseDuration($this->TRIGGER); | |
| 29 $related = (isset($trigger['RELATED']) && strtoupper($trigger['RELATED']) == 'END') ? 'END' : 'START'; | |
| 30 | |
| 31 $parentComponent = $this->parent; | |
| 32 if ($related === 'START') { | |
| 33 | |
| 34 if ($parentComponent->name === 'VTODO') { | |
| 35 $propName = 'DUE'; | |
| 36 } else { | |
| 37 $propName = 'DTSTART'; | |
| 38 } | |
| 39 | |
| 40 $effectiveTrigger = clone $parentComponent->$propName->getDateTime(); | |
| 41 $effectiveTrigger->add($triggerDuration); | |
| 42 } else { | |
| 43 if ($parentComponent->name === 'VTODO') { | |
| 44 $endProp = 'DUE'; | |
| 45 } elseif ($parentComponent->name === 'VEVENT') { | |
| 46 $endProp = 'DTEND'; | |
| 47 } else { | |
| 48 throw new \LogicException('time-range filters on VALARM components are only supported when they are a child of VTODO or VEVENT'); | |
| 49 } | |
| 50 | |
| 51 if (isset($parentComponent->$endProp)) { | |
| 52 $effectiveTrigger = clone $parentComponent->$endProp->getDateTime(); | |
| 53 $effectiveTrigger->add($triggerDuration); | |
| 54 } elseif (isset($parentComponent->DURATION)) { | |
| 55 $effectiveTrigger = clone $parentComponent->DTSTART->getDateTime(); | |
| 56 $duration = VObject\DateTimeParser::parseDuration($parentComponent->DURATION); | |
| 57 $effectiveTrigger->add($duration); | |
| 58 $effectiveTrigger->add($triggerDuration); | |
| 59 } else { | |
| 60 $effectiveTrigger = clone $parentComponent->DTSTART->getDateTime(); | |
| 61 $effectiveTrigger->add($triggerDuration); | |
| 62 } | |
| 63 } | |
| 64 } else { | |
| 65 $effectiveTrigger = $trigger->getDateTime(); | |
| 66 } | |
| 67 return $effectiveTrigger; | |
| 68 | |
| 69 } | |
| 70 | |
| 71 /** | |
| 72 * Returns true or false depending on if the event falls in the specified | |
| 73 * time-range. This is used for filtering purposes. | |
| 74 * | |
| 75 * The rules used to determine if an event falls within the specified | |
| 76 * time-range is based on the CalDAV specification. | |
| 77 * | |
| 78 * @param \DateTime $start | |
| 79 * @param \DateTime $end | |
| 80 * @return bool | |
| 81 */ | |
| 82 public function isInTimeRange(\DateTime $start, \DateTime $end) { | |
| 83 | |
| 84 $effectiveTrigger = $this->getEffectiveTriggerTime(); | |
| 85 | |
| 86 if (isset($this->DURATION)) { | |
| 87 $duration = VObject\DateTimeParser::parseDuration($this->DURATION); | |
| 88 $repeat = (string)$this->repeat; | |
| 89 if (!$repeat) { | |
| 90 $repeat = 1; | |
| 91 } | |
| 92 | |
| 93 $period = new \DatePeriod($effectiveTrigger, $duration, (int)$repeat); | |
| 94 | |
| 95 foreach($period as $occurrence) { | |
| 96 | |
| 97 if ($start <= $occurrence && $end > $occurrence) { | |
| 98 return true; | |
| 99 } | |
| 100 } | |
| 101 return false; | |
| 102 } else { | |
| 103 return ($start <= $effectiveTrigger && $end > $effectiveTrigger); | |
| 104 } | |
| 105 | |
| 106 } | |
| 107 | |
| 108 } |
