]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/lib/Sabre/VObject/Component/VCalendar.php
Merge branch 'master' of git://github.com/friendica/friendica-addons
[friendica-addons.git] / dav / SabreDAV / lib / Sabre / VObject / Component / VCalendar.php
1 <?php
2
3 /**
4  * The VCalendar component
5  *
6  * This component adds functionality to a component, specific for a VCALENDAR.
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_VCalendar extends Sabre_VObject_Component {
15
16     /**
17      * Returns a list of all 'base components'. For instance, if an Event has 
18      * a recurrence rule, and one instance is overridden, the overridden event 
19      * will have the same UID, but will be excluded from this list.
20      *
21      * VTIMEZONE components will always be excluded. 
22      *
23      * @param string $componentName filter by component name 
24      * @return array 
25      */
26     public function getBaseComponents($componentName = null) {
27
28         $components = array();
29         foreach($this->children as $component) {
30
31             if (!$component instanceof Sabre_VObject_Component)
32                 continue;
33
34             if (isset($component->{'RECURRENCE-ID'})) 
35                 continue;
36
37             if ($componentName && $component->name !== strtoupper($componentName)) 
38                 continue;
39
40             if ($component->name === 'VTIMEZONE')
41                 continue;
42
43             $components[] = $component;
44
45         }
46
47         return $components;
48
49     }
50
51     /**
52      * If this calendar object, has events with recurrence rules, this method 
53      * can be used to expand the event into multiple sub-events.
54      *
55      * Each event will be stripped from it's recurrence information, and only 
56      * the instances of the event in the specified timerange will be left 
57      * alone.
58      *
59      * In addition, this method will cause timezone information to be stripped, 
60      * and normalized to UTC.
61      *
62      * This method will alter the VCalendar. This cannot be reversed.
63      *
64      * This functionality is specifically used by the CalDAV standard. It is 
65      * possible for clients to request expand events, if they are rather simple 
66      * clients and do not have the possibility to calculate recurrences.
67      *
68      * @param DateTime $start
69      * @param DateTime $end 
70      * @return void
71      */
72     public function expand(DateTime $start, DateTime $end) {
73
74         $newEvents = array();
75
76         foreach($this->select('VEVENT') as $key=>$vevent) {
77
78             if (isset($vevent->{'RECURRENCE-ID'})) {
79                 unset($this->children[$key]);
80                 continue;
81             } 
82
83
84             if (!$vevent->rrule) {
85                 unset($this->children[$key]);
86                 if ($vevent->isInTimeRange($start, $end)) {
87                     $newEvents[] = $vevent;
88                 }
89                 continue;
90             }
91
92             $uid = (string)$vevent->uid;
93             if (!$uid) {
94                 throw new LogicException('Event did not have a UID!');
95             }
96
97             $it = new Sabre_VObject_RecurrenceIterator($this, $vevent->uid);
98             $it->fastForward($start);
99
100             while($it->valid() && $it->getDTStart() < $end) {
101
102                 if ($it->getDTEnd() > $start) {
103
104                     $newEvents[] = $it->getEventObject();
105
106                 }
107                 $it->next();
108
109             }
110             unset($this->children[$key]);
111
112         }
113
114         foreach($newEvents as $newEvent) {
115
116             foreach($newEvent->children as $child) {
117                 if ($child instanceof Sabre_VObject_Property_DateTime &&
118                     $child->getDateType() == Sabre_VObject_Property_DateTime::LOCALTZ) {
119                         $child->setDateTime($child->getDateTime(),Sabre_VObject_Property_DateTime::UTC);
120                     }
121             }
122
123             $this->add($newEvent);
124
125         }
126
127         // Removing all VTIMEZONE components
128         unset($this->VTIMEZONE);
129
130     } 
131
132 }
133