]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/lib/Sabre/VObject/Component/VAlarm.php
Merge remote branch 'upstream/master'
[friendica-addons.git] / dav / SabreDAV / lib / Sabre / VObject / Component / VAlarm.php
1 <?php
2
3 /**
4  * VAlarm component
5  *
6  * This component contains some additional functionality specific for VALARMs.
7  *
8  * @package Sabre
9  * @subpackage VObject
10  * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
11  * @author Evert Pot (http://www.rooftopsolutions.nl/)
12  * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
13  */
14 class Sabre_VObject_Component_VAlarm extends Sabre_VObject_Component {
15
16     /**
17      * Returns a DateTime object when this alarm is going to trigger.
18      *
19      * This ignores repeated alarm, only the first trigger is returned.
20      *
21      * @return DateTime
22      */
23     public function getEffectiveTriggerTime() {
24
25         $trigger = $this->TRIGGER;
26         if(!isset($trigger['VALUE']) || strtoupper($trigger['VALUE']) === 'DURATION') {
27             $triggerDuration = Sabre_VObject_DateTimeParser::parseDuration($this->TRIGGER);
28             $related = (isset($trigger['RELATED']) && strtoupper($trigger['RELATED']) == 'END') ? 'END' : 'START';
29
30             $parentComponent = $this->parent;
31             if ($related === 'START') {
32                 $effectiveTrigger = clone $parentComponent->DTSTART->getDateTime();
33                 $effectiveTrigger->add($triggerDuration);
34             } else {
35                 if ($parentComponent->name === 'VTODO') {
36                     $endProp = 'DUE';
37                 } elseif ($parentComponent->name === 'VEVENT') {
38                     $endProp = 'DTEND';
39                 } else {
40                     throw new Sabre_DAV_Exception('time-range filters on VALARM components are only supported when they are a child of VTODO or VEVENT');
41                 }
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 = Sabre_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
63     /**
64      * Returns true or false depending on if the event falls in the specified
65      * time-range. This is used for filtering purposes.
66      *
67      * The rules used to determine if an event falls within the specified
68      * time-range is based on the CalDAV specification.
69      *
70      * @param DateTime $start
71      * @param DateTime $end
72      * @return bool
73      */
74     public function isInTimeRange(DateTime $start, DateTime $end) {
75
76         $effectiveTrigger = $this->getEffectiveTriggerTime();
77
78         if (isset($this->DURATION)) {
79             $duration = Sabre_VObject_DateTimeParser::parseDuration($this->DURATION);
80             $repeat = (string)$this->repeat;
81             if (!$repeat) {
82                 $repeat = 1;
83             }
84
85             $period = new DatePeriod($effectiveTrigger, $duration, (int)$repeat);
86
87             foreach($period as $occurrence) {
88
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
100 }