1<?php
2
3namespace Sabre\VObject\Component;
4use Sabre\VObject;
5
6/**
7 * VAlarm component
8 *
9 * This component contains some additional functionality specific for VALARMs.
10 *
11 * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
12 * @author Evert Pot (http://evertpot.com/)
13 * @license http://sabre.io/license/ Modified BSD License
14 */
15class 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    /**
109     * A simple list of validation rules.
110     *
111     * This is simply a list of properties, and how many times they either
112     * must or must not appear.
113     *
114     * Possible values per property:
115     *   * 0 - Must not appear.
116     *   * 1 - Must appear exactly once.
117     *   * + - Must appear at least once.
118     *   * * - Can appear any number of times.
119     *   * ? - May appear, but not more than once.
120     *
121     * @var array
122     */
123    public function getValidationRules() {
124
125        return array(
126            'ACTION' => 1,
127            'TRIGGER' => 1,
128
129            'DURATION' => '?',
130            'REPEAT' => '?',
131
132            'ATTACH' => '?',
133        );
134
135    }
136
137}
138