]> git.mxchange.org Git - friendica-addons.git/blob - dav/sabre-vobject/lib/Sabre/VObject/Component/VAlarm.php
Second part of refactoring; should be runnable again, yet not thoroughly tested
[friendica-addons.git] / dav / sabre-vobject / lib / Sabre / VObject / Component / VAlarm.php
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-2012 Rooftop Solutions. All rights reserved.
12  * @author Evert Pot (http://www.rooftopsolutions.nl/)
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                 $effectiveTrigger = clone $parentComponent->DTSTART->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
44                 if (isset($parentComponent->$endProp)) {
45                     $effectiveTrigger = clone $parentComponent->$endProp->getDateTime();
46                     $effectiveTrigger->add($triggerDuration);
47                 } elseif (isset($parentComponent->DURATION)) {
48                     $effectiveTrigger = clone $parentComponent->DTSTART->getDateTime();
49                     $duration = VObject\DateTimeParser::parseDuration($parentComponent->DURATION);
50                     $effectiveTrigger->add($duration);
51                     $effectiveTrigger->add($triggerDuration);
52                 } else {
53                     $effectiveTrigger = clone $parentComponent->DTSTART->getDateTime();
54                     $effectiveTrigger->add($triggerDuration);
55                 }
56             }
57         } else {
58             $effectiveTrigger = $trigger->getDateTime();
59         }
60         return $effectiveTrigger;
61
62     }
63
64     /**
65      * Returns true or false depending on if the event falls in the specified
66      * time-range. This is used for filtering purposes.
67      *
68      * The rules used to determine if an event falls within the specified
69      * time-range is based on the CalDAV specification.
70      *
71      * @param \DateTime $start
72      * @param \DateTime $end
73      * @return bool
74      */
75     public function isInTimeRange(\DateTime $start, \DateTime $end) {
76
77         $effectiveTrigger = $this->getEffectiveTriggerTime();
78
79         if (isset($this->DURATION)) {
80             $duration = VObject\DateTimeParser::parseDuration($this->DURATION);
81             $repeat = (string)$this->repeat;
82             if (!$repeat) {
83                 $repeat = 1;
84             }
85
86             $period = new \DatePeriod($effectiveTrigger, $duration, (int)$repeat);
87
88             foreach($period as $occurrence) {
89
90                 if ($start <= $occurrence && $end > $occurrence) {
91                     return true;
92                 }
93             }
94             return false;
95         } else {
96             return ($start <= $effectiveTrigger && $end > $effectiveTrigger);
97         }
98
99     }
100
101 }