/
Event.php
3394 lines (3103 loc) · 125 KB
/
Event.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Kronolith_Event defines a generic API for events.
*
* Copyright 1999-2013 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (GPL). If you
* did not receive this file, see http://www.horde.org/licenses/gpl.
*
* @author Chuck Hagenbuch <chuck@horde.org>
* @author Jan Schneider <jan@horde.org>
* @package Kronolith
*/
abstract class Kronolith_Event
{
/**
* Flag that is set to true if this event has data from either a storage
* backend or a form or other import method.
*
* @var boolean
*/
public $initialized = false;
/**
* Flag that is set to true if this event exists in a storage driver.
*
* @var boolean
*/
public $stored = false;
/**
* The driver unique identifier for this event.
*
* @var string
*/
protected $_id = null;
/**
* The UID for this event.
*
* @var string
*/
public $uid = null;
/**
* The iCalendar SEQUENCE for this event.
*
* @var integer
*/
public $sequence = null;
/**
* The user id of the creator of the event.
*
* @var string
*/
protected $_creator = null;
/**
* The title of this event.
*
* For displaying in the interface use getTitle() instead.
*
* @var string
*/
public $title = '';
/**
* The location this event occurs at.
*
* @var string
*/
public $location = '';
/**
* The timezone of this event.
*
* @var string
*/
public $timezone;
/**
* The status of this event.
*
* @var integer
*/
public $status = Kronolith::STATUS_CONFIRMED;
/**
* URL to an icon of this event.
*
* @var string
*/
public $icon = '';
/**
* The description for this event.
*
* @var string
*/
public $description = '';
/**
* URL of this event.
*
* @var string
*/
public $url = '';
/**
* Whether the event is private.
*
* @var boolean
*/
public $private = false;
/**
* Event tags from the storage backend (e.g. Kolab)
*
* @var array
*/
protected $_internaltags;
/**
* This tag's events.
*
* @var array|string
*/
protected $_tags = null;
/**
* Geolocation
*
* @var array
*/
protected $_geoLocation;
/**
* Whether this is the event on the first day of a multi-day event.
*
* @var boolen
*/
public $first = true;
/**
* Whether this is the event on the last day of a multi-day event.
*
* @var boolen
*/
public $last = true;
/**
* All the attendees of this event.
*
* This is an associative array where the keys are the email addresses
* of the attendees, and the values are also associative arrays with
* keys 'attendance' and 'response' pointing to the attendees' attendance
* and response values, respectively.
*
* @var array
*/
public $attendees = array();
/**
* All resources of this event.
*
* This is an associative array where keys are resource uids values are
* associative arrays with keys attendance and response.
*
* @var unknown_type
*/
protected $_resources = array();
/**
* The start time of the event.
*
* @var Horde_Date
*/
public $start;
/**
* The end time of the event.
*
* @var Horde_Date
*/
public $end;
/**
* The duration of this event in minutes
*
* @var integer
*/
public $durMin = 0;
/**
* Whether this is an all-day event.
*
* @var boolean
*/
public $allday = false;
/**
* The creation time.
*
* @see loadHistory()
* @var Horde_Date
*/
public $created;
/**
* The creator string.
*
* @see loadHistory()
* @var string
*/
public $createdby;
/**
* The last modification time.
*
* @see loadHistory()
* @var Horde_Date
*/
public $modified;
/**
* The last-modifier string.
*
* @see loadHistory()
* @var string
*/
public $modifiedby;
/**
* Number of minutes before the event starts to trigger an alarm.
*
* @var integer
*/
public $alarm = 0;
/**
* Snooze minutes for this event's alarm.
*
* @see Horde_Alarm::snooze()
*
* @var integer
*/
protected $_snooze;
/**
* The particular alarm methods overridden for this event.
*
* @var array
*/
public $methods;
/**
* The identifier of the calender this event exists on.
*
* @var string
*/
public $calendar;
/**
* The type of the calender this event exists on.
*
* @var string
*/
public $calendarType;
/**
* The HTML background color to be used for this event.
*
* @var string
*/
protected $_backgroundColor = '#dddddd';
/**
* The HTML foreground color to be used for this event.
*
* @var string
*/
protected $_foregroundColor = '#000000';
/**
* The VarRenderer class to use for printing select elements.
*
* @var Horde_Core_Ui_VarRenderer
*/
private $_varRenderer;
/**
* The Horde_Date_Recurrence class for this event.
*
* @var Horde_Date_Recurrence
*/
public $recurrence;
/**
* Used in view renderers.
*
* @var integer
*/
protected $_overlap;
/**
* Used in view renderers.
*
* @var integer
*/
protected $_indent;
/**
* Used in view renderers.
*
* @var integer
*/
protected $_span;
/**
* Used in view renderers.
*
* @var integer
*/
protected $_rowspan;
/**
* The baseid. For events that represent exceptions this is the UID of the
* original, recurring event.
*
* @var string
*/
public $baseid;
/**
* For exceptions, the date of the original recurring event that this is an
* exception for.
*
* @var Horde_Date
*/
public $exceptionoriginaldate;
/**
* The cached event duration, split up in time units.
*
* @see getDuration()
* @var stdClass
*/
protected $_duration;
/**
* Constructor.
*
* @param Kronolith_Driver $driver The backend driver that this event is
* stored in.
* @param mixed $eventObject Backend specific event object
* that this will represent.
*/
public function __construct($driver, $eventObject = null)
{
$this->calendar = $driver->calendar;
list($this->_backgroundColor, $this->_foregroundColor) = $driver->colors();
if (!is_null($eventObject)) {
$this->fromDriver($eventObject);
}
}
/**
* Retrieves history information for this event from the history backend.
*/
public function loadHistory()
{
try {
$log = $GLOBALS['injector']->getInstance('Horde_History')
->getHistory('kronolith:' . $this->calendar . ':' . $this->uid);
$userId = $GLOBALS['registry']->getAuth();
foreach ($log as $entry) {
switch ($entry['action']) {
case 'add':
$this->created = new Horde_Date($entry['ts']);
if ($userId != $entry['who']) {
$this->createdby = sprintf(_("by %s"), Kronolith::getUserName($entry['who']));
} else {
$this->createdby = _("by me");
}
break;
case 'modify':
$this->modified = new Horde_Date($entry['ts']);
if ($userId != $entry['who']) {
$this->modifiedby = sprintf(_("by %s"), Kronolith::getUserName($entry['who']));
} else {
$this->modifiedby = _("by me");
}
break;
}
}
} catch (Horde_Exception $e) {
}
}
/**
* Setter.
*
* Sets the 'id' and 'creator' properties.
*
* @param string $name Property name.
* @param mixed $value Property value.
*/
public function __set($name, $value)
{
switch ($name) {
case 'id':
if (substr($value, 0, 10) == 'kronolith:') {
$value = substr($value, 10);
}
// Fall through.
case 'creator':
case 'overlap':
case 'indent':
case 'span':
case 'rowspan':
case 'geoLocation':
case 'tags':
$this->{'_' . $name} = $value;
return;
}
$trace = debug_backtrace();
trigger_error('Undefined property via __set(): ' . $name
. ' in ' . $trace[0]['file']
. ' on line ' . $trace[0]['line'],
E_USER_NOTICE);
}
/**
* Getter.
*
* Returns the 'id' and 'creator' properties.
*
* @param string $name Property name.
*
* @return mixed Property value.
*/
public function __get($name)
{
switch ($name) {
case 'creator':
if (empty($this->_creator)) {
$this->_creator = $GLOBALS['registry']->getAuth();
}
// Fall through.
case 'id':
case 'overlap':
case 'indent':
case 'span':
case 'rowspan':
return $this->{'_' . $name};
case 'tags':
if (!isset($this->_tags)) {
$this->synchronizeTags(Kronolith::getTagger()->getTags($this->uid, 'event'));
}
return $this->_tags;
case 'geoLocation':
if (!isset($this->_geoLocation)) {
try {
$this->_geoLocation = $GLOBALS['injector']->getInstance('Kronolith_Geo')->getLocation($this->id);
} catch (Kronolith_Exception $e) {}
}
return $this->_geoLocation;
}
$trace = debug_backtrace();
trigger_error('Undefined property via __set(): ' . $name
. ' in ' . $trace[0]['file']
. ' on line ' . $trace[0]['line'],
E_USER_NOTICE);
return null;
}
/**
* Returns a reference to a driver that's valid for this event.
*
* @return Kronolith_Driver A driver that this event can use to save
* itself, etc.
*/
public function getDriver()
{
return Kronolith::getDriver(str_replace('Kronolith_Event_', '', get_class($this)), $this->calendar);
}
/**
* Returns the share this event belongs to.
*
* @return Horde_Share This event's share.
* @throws Kronolith_Exception
*/
public function getShare()
{
if (isset($GLOBALS['all_calendars'][$this->calendar])) {
return $GLOBALS['all_calendars'][$this->calendar]->share();
}
throw new Kronolith_Exception('Share not found');
}
/**
* Encapsulates permissions checking.
*
* @param integer $permission The permission to check for.
* @param string $user The user to check permissions for.
*
* @return boolean
*/
public function hasPermission($permission, $user = null)
{
if ($user === null) {
$user = $GLOBALS['registry']->getAuth();
}
try {
$share = $this->getShare();
} catch (Exception $e) {
return false;
}
return $share->hasPermission($user, $permission, $this->creator);
}
/**
* Saves changes to this event.
*
* @return integer The event id.
* @throws Kronolith_Exception
*/
public function save()
{
if (!$this->initialized) {
throw new Kronolith_Exception('Event not yet initialized');
}
/* Check for acceptance/denial of this event's resources. */
$add_events = array();
$locks = $GLOBALS['injector']->getInstance('Horde_Lock');
$lock = array();
foreach (array_keys($this->getResources()) as $id) {
/* Get the resource and protect against infinite recursion in case
* someone is silly enough to add a resource to it's own event.*/
$resource = Kronolith::getDriver('Resource')->getResource($id);
$rcal = $resource->get('calendar');
if ($rcal == $this->calendar) {
continue;
}
Kronolith::getDriver('Resource')->open($rcal);
/* Lock the resource and get the response */
if ($resource->get('response_type') == Kronolith_Resource::RESPONSETYPE_AUTO) {
$principle = 'calendar/' . $rcal;
$lock[$resource->getId()] = $locks->setLock($GLOBALS['registry']->getAuth(), 'kronolith', $principle, 5, Horde_Lock::TYPE_EXCLUSIVE);
$haveLock = true;
} else {
$haveLock = false;
}
if ($haveLock && !$lock[$resource->getId()]) {
// Already locked
// For now, just fail. Not sure how else to capture the locked
// resources and notify the user.
throw new Kronolith_Exception(sprintf(_("The resource \"%s\" was locked. Please try again."), $resource->get('name')));
} else {
$response = $resource->getResponse($this);
}
/* Remember accepted resources so we can add the event to their
* calendars. Otherwise, clear the lock. */
if ($response == Kronolith::RESPONSE_ACCEPTED) {
$add_events[] = $resource;
} elseif ($haveLock) {
$locks->clearLock($lock[$resource->getId()]);
}
/* Add the resource to the event */
$this->addResource($resource, $response);
}
/* Save */
$result = $this->getDriver()->saveEvent($this);
/* Now that the event is definitely commited to storage, we can add
* the event to each resource that has accepted. Not very efficient,
* but this also solves the problem of not having a GUID for the event
* until after it's saved. If we add the event to the resources
* calendar before it is saved, they will have different GUIDs, and
* hence no longer refer to the same event. */
foreach ($add_events as $resource) {
$resource->addEvent($this);
if ($resource->get('response_type') == Kronolith_Resource::RESPONSETYPE_AUTO) {
$locks->clearLock($lock[$resource->getId()]);
}
}
$hordeAlarm = $GLOBALS['injector']->getInstance('Horde_Alarm');
if ($alarm = $this->toAlarm(new Horde_Date($_SERVER['REQUEST_TIME']))) {
$alarm['start'] = new Horde_Date($alarm['start']);
$alarm['end'] = new Horde_Date($alarm['end']);
$hordeAlarm->set($alarm);
if ($this->_snooze) {
$hordeAlarm->snooze($this->uid, $GLOBALS['registry']->getAuth(), $this->_snooze);
}
} else {
$hordeAlarm->delete($this->uid);
}
return $result;
}
/**
* Imports a backend specific event object.
*
* @param mixed $eventObject Backend specific event object that this
* object will represent.
*/
public function fromDriver($event)
{
}
/**
* Exports this event in iCalendar format.
*
* @param Horde_Icalendar $calendar A Horde_Icalendar object that acts as
* a container.
*
* @return array An array of Horde_Icalendar_Vevent objects for this event.
*/
public function toiCalendar($calendar)
{
$vEvent = Horde_Icalendar::newComponent('vevent', $calendar);
$v1 = $calendar->getAttribute('VERSION') == '1.0';
$vEvents = array();
// For certain recur types, we must output in the event's timezone
// so that the BYDAY values do not get out of sync with the UTC
// date-time. See Bug: 11339
if ($this->recurs()) {
switch ($this->recurrence->getRecurType()) {
case Horde_Date_Recurrence::RECUR_WEEKLY:
case Horde_Date_Recurrence::RECUR_YEARLY_WEEKDAY:
case Horde_Date_Recurrence::RECUR_MONTHLY_WEEKDAY:
if (!$this->timezone) {
$this->timezone = date_default_timezone_get();
}
}
}
if ($this->isAllDay()) {
/* DTEND is non-inclusive, but $this->end is inclusive. */
$end = clone $this->end;
$end->sec++;
$vEvent->setAttribute('DTSTART', $this->start, array('VALUE' => 'DATE'));
$vEvent->setAttribute('DTEND', $end, array('VALUE' => 'DATE'));
$vEvent->setAttribute('X-FUNAMBOL-ALLDAY', 1);
} else {
$this->setTimezone(true);
$params = array();
if ($this->timezone) {
try {
$tz = $GLOBALS['injector']->getInstance('Horde_Timezone');
$vEvents[] = $tz->getZone($this->timezone)->toVtimezone();
$params['TZID'] = $this->timezone;
} catch (Horde_Exception $e) {
}
}
/* DTEND is non-inclusive, but $this->end is inclusive. This needs
* to be done AFTER setting the timezone. */
$end = clone $this->end;
$end->sec++;
$vEvent->setAttribute('DTSTART', clone $this->start, $params);
$vEvent->setAttribute('DTEND', clone $end, $params);
}
$vEvent->setAttribute('DTSTAMP', $_SERVER['REQUEST_TIME']);
$vEvent->setAttribute('UID', $this->uid);
/* Get the event's create and last modify date. */
$created = $modified = null;
try {
$history = $GLOBALS['injector']->getInstance('Horde_History');
$created = $history->getActionTimestamp(
'kronolith:' . $this->calendar . ':' . $this->uid, 'add');
$modified = $history->getActionTimestamp(
'kronolith:' . $this->calendar . ':' . $this->uid, 'modify');
/* The history driver returns 0 for not found. If 0 or null does
* not matter, strip this. */
if ($created == 0) {
$created = null;
}
if ($modified == 0) {
$modified = null;
}
} catch (Exception $e) {
}
if (!empty($created)) {
$vEvent->setAttribute($v1 ? 'DCREATED' : 'CREATED', $created);
if (empty($modified)) {
$modified = $created;
}
}
if (!empty($modified)) {
$vEvent->setAttribute('LAST-MODIFIED', $modified);
}
$vEvent->setAttribute('SUMMARY', $this->getTitle());
// Organizer
if (count($this->attendees)) {
$name = Kronolith::getUserName($this->creator);
$email = Kronolith::getUserEmail($this->creator);
$params = array();
if ($v1) {
$tmp = new Horde_Mail_Rfc822_Address($email);
if (!empty($name)) {
$tmp->personal = $name;
}
$email = strval($tmp);
} else {
if (!empty($name)) {
$params['CN'] = $name;
}
if (!empty($email)) {
$email = 'mailto:' . $email;
}
}
$vEvent->setAttribute('ORGANIZER', $email, $params);
}
if (!$this->isPrivate()) {
if (!empty($this->description)) {
$vEvent->setAttribute('DESCRIPTION', $this->description);
}
// Tags
if ($this->tags) {
$tags = implode(', ', $this->tags);
$vEvent->setAttribute('CATEGORIES', $tags);
}
// Location
if (!empty($this->location)) {
$vEvent->setAttribute('LOCATION', $this->location);
}
if ($this->geoLocation) {
$vEvent->setAttribute('GEO', array('latitude' => $this->geoLocation['lat'], 'longitude' => $this->geoLocation['lon']));
}
// URL
if (!empty($this->url)) {
$vEvent->setAttribute('URL', $this->url);
}
}
$vEvent->setAttribute('CLASS', $this->private ? 'PRIVATE' : 'PUBLIC');
// Status.
switch ($this->status) {
case Kronolith::STATUS_FREE:
// This is not an official iCalendar value, but we need it for
// synchronization.
$vEvent->setAttribute('STATUS', 'FREE');
$vEvent->setAttribute('TRANSP', $v1 ? 1 : 'TRANSPARENT');
break;
case Kronolith::STATUS_TENTATIVE:
$vEvent->setAttribute('STATUS', 'TENTATIVE');
$vEvent->setAttribute('TRANSP', $v1 ? 0 : 'OPAQUE');
break;
case Kronolith::STATUS_CONFIRMED:
$vEvent->setAttribute('STATUS', 'CONFIRMED');
$vEvent->setAttribute('TRANSP', $v1 ? 0 : 'OPAQUE');
break;
case Kronolith::STATUS_CANCELLED:
if ($v1) {
$vEvent->setAttribute('STATUS', 'DECLINED');
$vEvent->setAttribute('TRANSP', 1);
} else {
$vEvent->setAttribute('STATUS', 'CANCELLED');
$vEvent->setAttribute('TRANSP', 'TRANSPARENT');
}
break;
}
// Attendees.
foreach ($this->attendees as $email => $status) {
$params = array();
switch ($status['attendance']) {
case Kronolith::PART_REQUIRED:
if ($v1) {
$params['EXPECT'] = 'REQUIRE';
} else {
$params['ROLE'] = 'REQ-PARTICIPANT';
}
break;
case Kronolith::PART_OPTIONAL:
if ($v1) {
$params['EXPECT'] = 'REQUEST';
} else {
$params['ROLE'] = 'OPT-PARTICIPANT';
}
break;
case Kronolith::PART_NONE:
if ($v1) {
$params['EXPECT'] = 'FYI';
} else {
$params['ROLE'] = 'NON-PARTICIPANT';
}
break;
}
switch ($status['response']) {
case Kronolith::RESPONSE_NONE:
if ($v1) {
$params['STATUS'] = 'NEEDS ACTION';
$params['RSVP'] = 'YES';
} else {
$params['PARTSTAT'] = 'NEEDS-ACTION';
$params['RSVP'] = 'TRUE';
}
break;
case Kronolith::RESPONSE_ACCEPTED:
if ($v1) {
$params['STATUS'] = 'ACCEPTED';
} else {
$params['PARTSTAT'] = 'ACCEPTED';
}
break;
case Kronolith::RESPONSE_DECLINED:
if ($v1) {
$params['STATUS'] = 'DECLINED';
} else {
$params['PARTSTAT'] = 'DECLINED';
}
break;
case Kronolith::RESPONSE_TENTATIVE:
if ($v1) {
$params['STATUS'] = 'TENTATIVE';
} else {
$params['PARTSTAT'] = 'TENTATIVE';
}
break;
}
if (strpos($email, '@') === false) {
$email = '';
}
if ($v1) {
if (empty($email)) {
if (!empty($status['name'])) {
$email = $status['name'];
}
} else {
$tmp = new Horde_Mail_Rfc822_Address($email);
if (!empty($status['name'])) {
$tmp->personal = $status['name'];
}
$email = strval($tmp);
}
} else {
if (!empty($status['name'])) {
$params['CN'] = $status['name'];
}
if (!empty($email)) {
$email = 'mailto:' . $email;
}
}
$vEvent->setAttribute('ATTENDEE', $email, $params);
}
// Alarms.
if (!empty($this->alarm)) {
if ($v1) {
$alarm = new Horde_Date($this->start);
$alarm->min -= $this->alarm;
$vEvent->setAttribute('AALARM', $alarm);
} else {
$vAlarm = Horde_Icalendar::newComponent('valarm', $vEvent);
$vAlarm->setAttribute('ACTION', 'DISPLAY');
$vAlarm->setAttribute('DESCRIPTION', $this->getTitle());
$vAlarm->setAttribute('TRIGGER;VALUE=DURATION', '-PT' . $this->alarm . 'M');
$vEvent->addComponent($vAlarm);
}
$hordeAlarm = $GLOBALS['injector']->getInstance('Horde_Alarm');
if ($hordeAlarm->exists($this->uid, $GLOBALS['registry']->getAuth()) &&
$hordeAlarm->isSnoozed($this->uid, $GLOBALS['registry']->getAuth())) {
$vEvent->setAttribute('X-MOZ-LASTACK', new Horde_Date($_SERVER['REQUEST_TIME']));
$alarm = $hordeAlarm->get($this->uid, $GLOBALS['registry']->getAuth());
if (!empty($alarm['snooze'])) {
$alarm['snooze']->setTimezone(date_default_timezone_get());
$vEvent->setAttribute('X-MOZ-SNOOZE-TIME', $alarm['snooze']);
}
}
}
// Recurrence.
if ($this->recurs()) {
if ($v1) {
$rrule = $this->recurrence->toRRule10($calendar);
} else {
$rrule = $this->recurrence->toRRule20($calendar);
}
if (!empty($rrule)) {
$vEvent->setAttribute('RRULE', $rrule);
}
// Exceptions. An exception with no replacement event is represented
// by EXDATE, and those with replacement events are represented by
// a new vEvent element. We get all known replacement events first,
// then remove the exceptionoriginaldate from the list of the event
// exceptions. Any exceptions left should represent exceptions with
// no replacement.
$exceptions = $this->recurrence->getExceptions();
$kronolith_driver = Kronolith::getDriver(null, $this->calendar);
$search = new stdClass();
$search->baseid = $this->uid;
$results = $kronolith_driver->search($search);
foreach ($results as $days) {
foreach ($days as $exceptionEvent) {
// Need to change the UID so it links to the original
// recurring event, but only if not using $v1. If using $v1,
// we add the date to EXDATE and do NOT change the UID.
if (!$v1) {
$exceptionEvent->uid = $this->uid;
}
$vEventException = $exceptionEvent->toiCalendar($calendar);
// This should never happen, but protect against it anyway.
if (count($vEventException) > 2 ||
(count($vEventException) > 1 &&
!($vEventException[0] instanceof Horde_Icalendar_Vtimezone) &&
!($vEventException[1] instanceof Horde_Icalendar_Vtimezone))) {
throw new Kronolith_Exception(_("Unable to parse event."));
}
$vEventException = array_pop($vEventException);
// If $v1, need to add to EXDATE
if (!$this->isAllDay()) {
$exceptionEvent->setTimezone(true);
}
if (!$v1) {
$vEventException->setAttribute('RECURRENCE-ID', $exceptionEvent->exceptionoriginaldate);
} else {
$vEvent->setAttribute('EXDATE', array($exceptionEvent->exceptionoriginaldate), array('VALUE' => 'DATE'));
}
$originaldate = $exceptionEvent->exceptionoriginaldate->format('Ymd');
$key = array_search($originaldate, $exceptions);
if ($key !== false) {
unset($exceptions[$key]);
}
$vEvents[] = $vEventException;
}
}
/* The remaining exceptions represent deleted recurrences */
foreach ($exceptions as $exception) {
if (!empty($exception)) {
// Use multiple EXDATE attributes instead of EXDATE
// attributes with multiple values to make Apple iCal
// happy.
list($year, $month, $mday) = sscanf($exception, '%04d%02d%02d');
if ($this->isAllDay()) {
$vEvent->setAttribute('EXDATE', array(new Horde_Date($year, $month, $mday)), array('VALUE' => 'DATE'));
} else {
// Another Apple iCal/Calendar fix. EXDATE is only
// recognized if the full datetime is present and matches
// the time part given in DTSTART.
$params = array();
if ($this->timezone) {
$params['TZID'] = $this->timezone;
}
$exdate = clone $this->start;
$exdate->year = $year;
$exdate->month = $month;
$exdate->mday = $mday;
$vEvent->setAttribute('EXDATE', array($exdate), $params);
}
}
}
}
array_unshift($vEvents, $vEvent);
$this->setTimezone(false);
return $vEvents;
}
/**
* Updates the properties of this event from a Horde_Icalendar_Vevent
* object.
*
* @param Horde_Icalendar_Vevent $vEvent The iCalendar data to update