1: <?php
2:
3: namespace intouch\ical;
4:
5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17:
18: class VEvent {
19: const DEFAULT_CONFIRMED = true;
20:
21: protected $uid;
22:
23: protected $start;
24: protected $end;
25:
26: protected $summary;
27: protected $description;
28: protected $location;
29:
30: protected $laststart;
31: protected $lastend;
32:
33: public $recurrence;
34: public $recurex;
35: public $excluded;
36: public $added;
37:
38: public $freq;
39:
40: public $data;
41:
42: 43: 44: 45: 46: 47:
48: public function __construct($data, iCal $ical) {
49:
50: $this->uid = $data['uid']->getData();
51: unset($data['uid']);
52:
53: if ( isset($data['rrule']) ) {
54: $this->recurrence = new Recurrence($data['rrule']);
55: unset($data['rrule']);
56: }
57:
58: if ( isset($data['exrule']) ) {
59: $this->recurex = new Recurrence($data['exrule']);
60: unset($data['exrule']);
61: }
62:
63: if( isset($data['dtstart']) ) {
64: $this->start = $this->getTimestamp($data['dtstart'], $ical);
65: unset($data['dtstart']);
66: }
67:
68: if( isset($data['dtend']) ) {
69: $this->end = $this->getTimestamp($data['dtend'], $ical);
70: unset($data['dtend']);
71: } elseif( isset($data['duration']) ) {
72: $dur = new Duration( $data['duration']->getData() );
73: $this->end = $this->start + $dur->getDuration();
74: unset($data['duration']);
75: }
76:
77:
78: if ( isset($this->recurrence) ) {
79:
80:
81:
82: if ( isset($data['exdate']) ) {
83: foreach ($data['exdate'] as $exdate) {
84: foreach ($exdate->getDataAsArray() as $ts) {
85: $this->excluded[] = strtotime($ts);
86: }
87: }
88: unset($data['exdate']);
89: }
90:
91: if ( isset($data['rdate']) ) {
92: foreach ($data['rdate'] as $rdate) {
93: foreach ($rdate->getDataAsArray() as $ts) {
94: $this->added[] = strtotime($ts);
95: }
96: }
97: unset($data['rdate']);
98: }
99:
100: $until = $this->recurrence->getUntil();
101: $count = $this->recurrence->getCount();
102:
103: if ( $until ) {
104:
105: } elseif ($count) {
106:
107: $this->getFrequency();
108: $until = $this->freq->lastOccurrence($this->start);
109: } else {
110:
111: $this->recurrence->setUntil('+3 years');
112: $until = $this->recurrence->getUntil();
113: }
114:
115: $this->laststart = strtotime($until);
116: $this->lastend = $this->laststart + $this->getDuration();
117: }
118:
119: $imports = array('summary','description','location');
120: foreach( $imports AS $import ) {
121: if( isset($data[$import]) ) {
122: $this->$import = $data[$import]->getData();
123: unset($data[$import]);
124: }
125: }
126:
127: if( isset($this->previous_tz) ) {
128: date_default_timezone_set($this->previous_tz);
129: }
130:
131: $this->data = Line::Remove_Line($data);
132: }
133:
134:
135: 136: 137: 138:
139: public function getFrequency() {
140: if (! isset($this->freq)) {
141: if ( isset($this->recurrence) ) {
142: $this->freq = new Freq($this->recurrence->rrule, $this->start, $this->excluded, $this->added);
143: }
144: }
145: return $this->freq;
146: }
147:
148: 149: 150: 151:
152: public function getUID() {
153: return $this->uid;
154: }
155:
156: 157: 158: 159:
160: public function getSummary() {
161: return $this->summary;
162: }
163:
164: 165: 166: 167:
168: public function getDescription() {
169: return $this->description;
170: }
171:
172: 173: 174: 175:
176: public function getLocation() {
177: return $this->location;
178: }
179:
180: 181: 182: 183:
184: public function isBlocking() {
185: return !(isset($this->data['transp']) && $this->data['transp'] == 'TRANSPARENT');
186: }
187:
188: 189: 190: 191:
192: public function isConfirmed() {
193: if( !isset($this->data['status']) ) {
194: return self::DEFAULT_CONFIRMED;
195: } else {
196: return $this->data['status'] == 'CONFIRMED';
197: }
198: }
199:
200: 201: 202: 203:
204: public function isWholeDay() {
205: $dur = $this->getDuration();
206: if ($dur > 0 && ($dur % 86400) == 0) {
207: return true;
208: }
209: return false;
210: }
211:
212: 213: 214: 215:
216: public function getStart() {
217: return $this->start;
218: }
219:
220: 221: 222: 223:
224: public function getEnd() {
225: return $this->end;
226: }
227:
228: 229: 230: 231:
232: public function getRangeEnd() {
233: return max($this->end,$this->lastend);
234: }
235:
236: 237: 238: 239:
240: public function getDuration() {
241: return $this->end - $this->start;
242: }
243:
244: 245: 246: 247: 248:
249: public function getProperty( $prop ) {
250: if( isset($this->$prop) ) {
251: return $this->$prop;
252: } elseif( isset($this->data[$prop]) ) {
253: return $this->data[$prop];
254: } else {
255: return null;
256: }
257: }
258:
259:
260:
261: 262: 263: 264:
265: protected function setLineTimeZone( Line $line ) {
266: if( isset($line['tzid']) ) {
267: if (!isset($this->previous_tz)) {
268: $this->previous_tz = @ date_default_timezone_get();
269: }
270: $this->tzid = $line['tzid'];
271: date_default_timezone_set($this->tzid);
272: return true;
273: }
274: return false;
275: }
276:
277: 278: 279: 280: 281:
282: protected function getTimestamp( Line $line, iCal $ical ) {
283:
284: if( isset($line['tzid']) ) {
285: $this->setLineTimeZone($line);
286:
287:
288:
289: }
290: $ts = strtotime($line->getData());
291:
292: return $ts;
293: }
294: }
295: