xref: /plugin/davcal/vendor/sabre/vobject/lib/Component/VEvent.php (revision a1a3b6794e0e143a4a8b51d3185ce2d339be61ab)
1*a1a3b679SAndreas Boehler<?php
2*a1a3b679SAndreas Boehler
3*a1a3b679SAndreas Boehlernamespace Sabre\VObject\Component;
4*a1a3b679SAndreas Boehler
5*a1a3b679SAndreas Boehleruse Sabre\VObject;
6*a1a3b679SAndreas Boehleruse Sabre\VObject\Recur\EventIterator;
7*a1a3b679SAndreas Boehleruse Sabre\VObject\Recur\NoInstancesException;
8*a1a3b679SAndreas Boehler
9*a1a3b679SAndreas Boehler/**
10*a1a3b679SAndreas Boehler * VEvent component
11*a1a3b679SAndreas Boehler *
12*a1a3b679SAndreas Boehler * This component contains some additional functionality specific for VEVENT's.
13*a1a3b679SAndreas Boehler *
14*a1a3b679SAndreas Boehler * @copyright Copyright (C) 2011-2015 fruux GmbH (https://fruux.com/).
15*a1a3b679SAndreas Boehler * @author Evert Pot (http://evertpot.com/)
16*a1a3b679SAndreas Boehler * @license http://sabre.io/license/ Modified BSD License
17*a1a3b679SAndreas Boehler */
18*a1a3b679SAndreas Boehlerclass VEvent extends VObject\Component {
19*a1a3b679SAndreas Boehler
20*a1a3b679SAndreas Boehler    /**
21*a1a3b679SAndreas Boehler     * Returns true or false depending on if the event falls in the specified
22*a1a3b679SAndreas Boehler     * time-range. This is used for filtering purposes.
23*a1a3b679SAndreas Boehler     *
24*a1a3b679SAndreas Boehler     * The rules used to determine if an event falls within the specified
25*a1a3b679SAndreas Boehler     * time-range is based on the CalDAV specification.
26*a1a3b679SAndreas Boehler     *
27*a1a3b679SAndreas Boehler     * @param \DateTime $start
28*a1a3b679SAndreas Boehler     * @param \DateTime $end
29*a1a3b679SAndreas Boehler     * @return bool
30*a1a3b679SAndreas Boehler     */
31*a1a3b679SAndreas Boehler    public function isInTimeRange(\DateTime $start, \DateTime $end) {
32*a1a3b679SAndreas Boehler
33*a1a3b679SAndreas Boehler        if ($this->RRULE) {
34*a1a3b679SAndreas Boehler
35*a1a3b679SAndreas Boehler            try {
36*a1a3b679SAndreas Boehler
37*a1a3b679SAndreas Boehler                $it = new EventIterator($this, null, $start->getTimezone());
38*a1a3b679SAndreas Boehler
39*a1a3b679SAndreas Boehler            } catch (NoInstancesException $e) {
40*a1a3b679SAndreas Boehler
41*a1a3b679SAndreas Boehler                // If we've catched this exception, there are no instances
42*a1a3b679SAndreas Boehler                // for the event that fall into the specified time-range.
43*a1a3b679SAndreas Boehler                return false;
44*a1a3b679SAndreas Boehler
45*a1a3b679SAndreas Boehler            }
46*a1a3b679SAndreas Boehler
47*a1a3b679SAndreas Boehler            $it->fastForward($start);
48*a1a3b679SAndreas Boehler
49*a1a3b679SAndreas Boehler            // We fast-forwarded to a spot where the end-time of the
50*a1a3b679SAndreas Boehler            // recurrence instance exceeded the start of the requested
51*a1a3b679SAndreas Boehler            // time-range.
52*a1a3b679SAndreas Boehler            //
53*a1a3b679SAndreas Boehler            // If the starttime of the recurrence did not exceed the
54*a1a3b679SAndreas Boehler            // end of the time range as well, we have a match.
55*a1a3b679SAndreas Boehler            return ($it->getDTStart() < $end && $it->getDTEnd() > $start);
56*a1a3b679SAndreas Boehler
57*a1a3b679SAndreas Boehler        }
58*a1a3b679SAndreas Boehler
59*a1a3b679SAndreas Boehler        $effectiveStart = $this->DTSTART->getDateTime($start->getTimezone());
60*a1a3b679SAndreas Boehler        if (isset($this->DTEND)) {
61*a1a3b679SAndreas Boehler
62*a1a3b679SAndreas Boehler            // The DTEND property is considered non inclusive. So for a 3 day
63*a1a3b679SAndreas Boehler            // event in july, dtstart and dtend would have to be July 1st and
64*a1a3b679SAndreas Boehler            // July 4th respectively.
65*a1a3b679SAndreas Boehler            //
66*a1a3b679SAndreas Boehler            // See:
67*a1a3b679SAndreas Boehler            // http://tools.ietf.org/html/rfc5545#page-54
68*a1a3b679SAndreas Boehler            $effectiveEnd = $this->DTEND->getDateTime($end->getTimezone());
69*a1a3b679SAndreas Boehler
70*a1a3b679SAndreas Boehler        } elseif (isset($this->DURATION)) {
71*a1a3b679SAndreas Boehler            $effectiveEnd = clone $effectiveStart;
72*a1a3b679SAndreas Boehler            $effectiveEnd->add(VObject\DateTimeParser::parseDuration($this->DURATION));
73*a1a3b679SAndreas Boehler        } elseif (!$this->DTSTART->hasTime()) {
74*a1a3b679SAndreas Boehler            $effectiveEnd = clone $effectiveStart;
75*a1a3b679SAndreas Boehler            $effectiveEnd->modify('+1 day');
76*a1a3b679SAndreas Boehler        } else {
77*a1a3b679SAndreas Boehler            $effectiveEnd = clone $effectiveStart;
78*a1a3b679SAndreas Boehler        }
79*a1a3b679SAndreas Boehler        return (
80*a1a3b679SAndreas Boehler            ($start < $effectiveEnd) && ($end > $effectiveStart)
81*a1a3b679SAndreas Boehler        );
82*a1a3b679SAndreas Boehler
83*a1a3b679SAndreas Boehler    }
84*a1a3b679SAndreas Boehler
85*a1a3b679SAndreas Boehler    /**
86*a1a3b679SAndreas Boehler     * This method should return a list of default property values.
87*a1a3b679SAndreas Boehler     *
88*a1a3b679SAndreas Boehler     * @return array
89*a1a3b679SAndreas Boehler     */
90*a1a3b679SAndreas Boehler    protected function getDefaults() {
91*a1a3b679SAndreas Boehler
92*a1a3b679SAndreas Boehler        return array(
93*a1a3b679SAndreas Boehler            'UID'     => 'sabre-vobject-' . VObject\UUIDUtil::getUUID(),
94*a1a3b679SAndreas Boehler            'DTSTAMP' => date('Ymd\\THis\\Z'),
95*a1a3b679SAndreas Boehler        );
96*a1a3b679SAndreas Boehler
97*a1a3b679SAndreas Boehler    }
98*a1a3b679SAndreas Boehler
99*a1a3b679SAndreas Boehler    /**
100*a1a3b679SAndreas Boehler     * A simple list of validation rules.
101*a1a3b679SAndreas Boehler     *
102*a1a3b679SAndreas Boehler     * This is simply a list of properties, and how many times they either
103*a1a3b679SAndreas Boehler     * must or must not appear.
104*a1a3b679SAndreas Boehler     *
105*a1a3b679SAndreas Boehler     * Possible values per property:
106*a1a3b679SAndreas Boehler     *   * 0 - Must not appear.
107*a1a3b679SAndreas Boehler     *   * 1 - Must appear exactly once.
108*a1a3b679SAndreas Boehler     *   * + - Must appear at least once.
109*a1a3b679SAndreas Boehler     *   * * - Can appear any number of times.
110*a1a3b679SAndreas Boehler     *   * ? - May appear, but not more than once.
111*a1a3b679SAndreas Boehler     *
112*a1a3b679SAndreas Boehler     * @var array
113*a1a3b679SAndreas Boehler     */
114*a1a3b679SAndreas Boehler    public function getValidationRules() {
115*a1a3b679SAndreas Boehler
116*a1a3b679SAndreas Boehler        $hasMethod = isset($this->parent->METHOD);
117*a1a3b679SAndreas Boehler        return array(
118*a1a3b679SAndreas Boehler            'UID' => 1,
119*a1a3b679SAndreas Boehler            'DTSTAMP' => 1,
120*a1a3b679SAndreas Boehler            'DTSTART' => $hasMethod?'?':'1',
121*a1a3b679SAndreas Boehler            'CLASS' => '?',
122*a1a3b679SAndreas Boehler            'CREATED' => '?',
123*a1a3b679SAndreas Boehler            'DESCRIPTION' => '?',
124*a1a3b679SAndreas Boehler            'GEO' => '?',
125*a1a3b679SAndreas Boehler            'LAST-MODIFIED' => '?',
126*a1a3b679SAndreas Boehler            'LOCATION' => '?',
127*a1a3b679SAndreas Boehler            'ORGANIZER' => '?',
128*a1a3b679SAndreas Boehler            'PRIORITY' => '?',
129*a1a3b679SAndreas Boehler            'SEQUENCE' => '?',
130*a1a3b679SAndreas Boehler            'STATUS' => '?',
131*a1a3b679SAndreas Boehler            'SUMMARY' => '?',
132*a1a3b679SAndreas Boehler            'TRANSP' => '?',
133*a1a3b679SAndreas Boehler            'URL' => '?',
134*a1a3b679SAndreas Boehler            'RECURRENCE-ID' => '?',
135*a1a3b679SAndreas Boehler            'RRULE' => '?',
136*a1a3b679SAndreas Boehler            'DTEND' => '?',
137*a1a3b679SAndreas Boehler            'DURATION' => '?',
138*a1a3b679SAndreas Boehler
139*a1a3b679SAndreas Boehler            'ATTACH' => '*',
140*a1a3b679SAndreas Boehler            'ATTENDEE' => '*',
141*a1a3b679SAndreas Boehler            'CATEGORIES' => '*',
142*a1a3b679SAndreas Boehler            'COMMENT' => '*',
143*a1a3b679SAndreas Boehler            'CONTACT' => '*',
144*a1a3b679SAndreas Boehler            'EXDATE' => '*',
145*a1a3b679SAndreas Boehler            'REQUEST-STATUS' => '*',
146*a1a3b679SAndreas Boehler            'RELATED-TO' => '*',
147*a1a3b679SAndreas Boehler            'RESOURCES' => '*',
148*a1a3b679SAndreas Boehler            'RDATE' => '*',
149*a1a3b679SAndreas Boehler        );
150*a1a3b679SAndreas Boehler
151*a1a3b679SAndreas Boehler    }
152*a1a3b679SAndreas Boehler
153*a1a3b679SAndreas Boehler}
154