/
Kronolith.php
3016 lines (2736 loc) · 112 KB
/
Kronolith.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 base library.
*
* Copyright 1999-2015 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.
*
* @category Horde
* @license http://www.horde.org/licenses/gpl GPL
* @package Kronolith
*/
/**
* The Kronolith:: class provides functionality common to all of Kronolith.
*
* @author Chuck Hagenbuch <chuck@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/gpl GPL
* @package Kronolith
*/
class Kronolith
{
/** Event status */
const STATUS_NONE = 0;
const STATUS_TENTATIVE = 1;
const STATUS_CONFIRMED = 2;
const STATUS_CANCELLED = 3;
const STATUS_FREE = 4;
/** Invitation responses */
const RESPONSE_NONE = 1;
const RESPONSE_ACCEPTED = 2;
const RESPONSE_DECLINED = 3;
const RESPONSE_TENTATIVE = 4;
/** Attendee status */
const PART_REQUIRED = 1;
const PART_OPTIONAL = 2;
const PART_NONE = 3;
const PART_IGNORE = 4;
/** iTip requests */
const ITIP_REQUEST = 1;
const ITIP_CANCEL = 2;
const RANGE_THISANDFUTURE = 'THISANDFUTURE';
/** The event can be delegated. */
const PERMS_DELEGATE = 1024;
/** Calendar Manager Constants */
const DISPLAY_CALENDARS = 'displayCalendars';
const DISPLAY_REMOTE_CALENDARS = 'displayRemote';
const DISPLAY_EXTERNAL_CALENDARS= 'displayExternal';
const DISPLAY_RESOURCE_CALENDARS= 'displayResource';
const DISPLAY_HOLIDAYS = 'displayHolidays';
const ALL_CALENDARS = 'allCalendars';
const ALL_REMOTE_CALENDARS = 'allRemote';
const ALL_EXTERNAL_CALENDARS = 'allExternal';
const ALL_HOLIDAYS = 'allHolidays';
const ALL_RESOURCE_CALENDARS = 'allResource';
/** Share Types */
const SHARE_TYPE_USER = 1;
const SHARE_TYPE_RESOURCE = 2;
/**
* @var Kronolith_Tagger
*/
private static $_tagger;
/**
* Converts a permission object to a json object.
*
* This methods filters out any permissions for the owner and converts the
* user name if necessary.
*
* @param Horde_Perms_Permission $perm A permission object.
*
* @return array A hash suitable for json.
*/
public static function permissionToJson(Horde_Perms_Permission $perm)
{
$json = $perm->data;
if (isset($json['users'])) {
$users = array();
foreach ($json['users'] as $user => $value) {
if ($user == $GLOBALS['registry']->getAuth()) {
continue;
}
$user = $GLOBALS['registry']->convertUsername($user, false);
$users[$user] = $value;
}
if ($users) {
$json['users'] = $users;
} else {
unset($json['users']);
}
}
return $json;
}
/**
* Returns all the alarms active on a specific date.
*
* @param Horde_Date $date The date to check for alarms.
* @param array $calendars The calendars to check for events.
* @param boolean $fullevent Whether to return complete alarm objects or
* only alarm IDs.
*
* @return array The alarms active on the date. A hash with calendar names
* as keys and arrays of events or event ids as values.
* @throws Kronolith_Exception
*/
public static function listAlarms($date, $calendars, $fullevent = false)
{
$kronolith_driver = self::getDriver();
$alarms = array();
foreach ($calendars as $cal) {
$kronolith_driver->open($cal);
$alarms[$cal] = $kronolith_driver->listAlarms($date, $fullevent);
}
return $alarms;
}
/**
* Searches for events with the given properties.
*
* @param object $query The search query.
* @param string $calendar The calendar to search in the form
* "Driver|calendar_id".
*
* @return array The events.
* @throws Kronolith_Exception
*/
public static function search($query, $calendar = null)
{
if ($calendar) {
$driver = explode('|', $calendar, 2);
$calendars = array($driver[0] => array($driver[1]));
} elseif (!empty($query->calendars)) {
$calendars = $query->calendars;
} else {
$calendars = array(
Horde_String::ucfirst($GLOBALS['conf']['calendar']['driver']) => $GLOBALS['calendar_manager']->get(Kronolith::DISPLAY_CALENDARS),
'Horde' => $GLOBALS['calendar_manager']->get(Kronolith::DISPLAY_EXTERNAL_CALENDARS),
'Ical' => $GLOBALS['calendar_manager']->get(Kronolith::DISPLAY_REMOTE_CALENDARS),
'Holidays' => $GLOBALS['calendar_manager']->get(Kronolith::DISPLAY_HOLIDAYS));
}
$events = array();
foreach ($calendars as $type => $list) {
if (!empty($list)) {
$kronolith_driver = self::getDriver($type);
}
foreach ($list as $cal) {
$kronolith_driver->open($cal);
$retevents = $kronolith_driver->search($query);
self::mergeEvents($events, $retevents);
}
}
return $events;
}
/**
* Returns all the events that happen each day within a time period
*
* @deprecated
*
* @param Horde_Date $startDate The start of the time range.
* @param Horde_Date $endDate The end of the time range.
* @param array $calendars The calendars to check for events.
* @param array $options Additional options:
* - show_recurrence: (boolean) Return every instance of a recurring
* event?
* DEFAULT: false (Only return recurring events once
* inside $startDate - $endDate range)
* - has_alarm: (boolean) Only return events with alarms.
* DEFAULT: false (Return all events)
* - json: (boolean) Store the results of the event's toJson()
* method?
* DEFAULT: false
* - cover_dates: (boolean) Add the events to all days that they
* cover?
* DEFAULT: true
* - hide_exceptions: (boolean) Hide events that represent exceptions to
* a recurring event.
* DEFAULT: false (Do not hide exception events)
* - fetch_tags: (boolean) Fetch tags for all events.
* DEFAULT: false (Do not fetch event tags)
*
* @return array The events happening in this time period.
*/
public static function listEvents(
$startDate, $endDate, $calendars = null, array $options = array())
{
$options = array_merge(array(
'show_recurrence' => true,
'has_alarm' => false,
'show_remote' => true,
'hide_exceptions' => false,
'cover_dates' => true,
'fetch_tags' => false), $options);
$results = array();
/* Internal calendars. */
if (!isset($calendars)) {
$calendars = $GLOBALS['calendar_manager']->get(Kronolith::DISPLAY_CALENDARS);
}
$driver = self::getDriver();
foreach ($calendars as $calendar) {
try {
$driver->open($calendar);
$events = $driver->listEvents($startDate, $endDate, $options);
self::mergeEvents($results, $events);
} catch (Kronolith_Exception $e) {
$GLOBALS['notification']->push($e);
}
}
// Resource calendars
if (count($GLOBALS['calendar_manager']->get(Kronolith::DISPLAY_RESOURCE_CALENDARS)) &&
!empty($GLOBALS['conf']['resources']['enabled'])) {
$driver = self::getDriver('Resource');
foreach ($GLOBALS['calendar_manager']->get(Kronolith::DISPLAY_RESOURCE_CALENDARS) as $calendar) {
try {
$driver->open($calendar);
$events = $driver->listEvents(
$startDate, $endDate, array('show_recurrence' => $options['show_recurrence']));
self::mergeEvents($results, $events);
} catch (Kronolith_Exception $e) {
$GLOBALS['notification']->push($e);
}
}
}
if ($options['show_remote']) {
/* Horde applications providing listTimeObjects. */
if (count($GLOBALS['calendar_manager']->get(Kronolith::DISPLAY_EXTERNAL_CALENDARS))) {
$driver = self::getDriver('Horde');
foreach ($GLOBALS['calendar_manager']->get(Kronolith::DISPLAY_EXTERNAL_CALENDARS) as $external_cal) {
try {
$driver->open($external_cal);
$events = $driver->listEvents(
$startDate, $endDate, array('show_recurrence' => $options['show_recurrence']));
self::mergeEvents($results, $events);
} catch (Kronolith_Exception $e) {
$GLOBALS['notification']->push($e);
}
}
}
/* Remote Calendars. */
foreach ($GLOBALS['calendar_manager']->get(Kronolith::DISPLAY_REMOTE_CALENDARS) as $url) {
try {
$driver = self::getDriver('Ical', $url);
$events = $driver->listEvents(
$startDate, $endDate, array('show_recurrence' => $options['show_recurrence']));
self::mergeEvents($results, $events);
} catch (Kronolith_Exception $e) {
$GLOBALS['notification']->push($e);
}
}
/* Holidays. */
$display_holidays = $GLOBALS['calendar_manager']->get(Kronolith::DISPLAY_HOLIDAYS);
if (count($display_holidays) && !empty($GLOBALS['conf']['holidays']['enable'])) {
$driver = self::getDriver('Holidays');
foreach ($display_holidays as $holiday) {
try {
$driver->open($holiday);
$events = $driver->listEvents(
$startDate, $endDate, array('show_recurrence' => $options['show_recurrence']));
self::mergeEvents($results, $events);
} catch (Kronolith_Exception $e) {
$GLOBALS['notification']->push($e);
}
}
}
}
/* Sort events. */
$results = self::sortEvents($results);
return $results;
}
/**
* Merges results from two listEvents() result sets.
*
* @param array $results First list of events.
* @param array $events List of events to be merged into the first one.
*/
public static function mergeEvents(&$results, $events)
{
foreach ($events as $day => $day_events) {
if (isset($results[$day])) {
$results[$day] = array_merge($results[$day], $day_events);
} else {
$results[$day] = $day_events;
}
}
ksort($results);
}
/**
* Calculates recurrences of an event during a certain period.
*/
public static function addEvents(&$results, &$event, $startDate, $endDate,
$showRecurrence, $json, $coverDates = true)
{
/* If the event has a custom timezone, we need to convert the
* recurrence object to the event's timezone while calculating next
* recurrences, to take DST changes in both the event's and the local
* timezone into account. */
$convert = $event->timezone &&
$event->getDriver()->supportsTimezones();
if ($convert) {
$timezone = date_default_timezone_get();
}
if ($event->recurs() && $showRecurrence) {
/* Recurring Event. */
/* If the event ends at 12am and does not end at the same time
* that it starts (0 duration), set the end date to the previous
* day's end date. */
if ($event->end->hour == 0 &&
$event->end->min == 0 &&
$event->end->sec == 0 &&
$event->start->compareDateTime($event->end) != 0) {
$event->end = new Horde_Date(
array('hour' => 23,
'min' => 59,
'sec' => 59,
'month' => $event->end->month,
'mday' => $event->end->mday - 1,
'year' => $event->end->year));
}
/* We can't use the event duration here because we might cover a
* daylight saving time switch. */
$diff = array($event->end->year - $event->start->year,
$event->end->month - $event->start->month,
$event->end->mday - $event->start->mday,
$event->end->hour - $event->start->hour,
$event->end->min - $event->start->min);
if ($event->start->compareDateTime($startDate) < 0) {
/* The first time the event happens was before the period
* started. Start searching for recurrences from the start of
* the period. */
$next = new Horde_Date(array('year' => $startDate->year,
'month' => $startDate->month,
'mday' => $startDate->mday),
$event->timezone);
} else {
/* The first time the event happens is in the range; unless
* there is an exception for this ocurrence, add it. */
if (!$event->recurrence->hasException($event->start->year,
$event->start->month,
$event->start->mday)) {
if ($coverDates) {
self::addCoverDates($results, $event, $event->start, $event->end, $json);
} else {
$results[$event->start->dateString()][$event->id] = $json ? $event->toJson() : $event;
}
}
/* Start searching for recurrences from the day after it
* starts. */
$next = clone $event->start;
++$next->mday;
}
if ($convert) {
$event->recurrence->start->setTimezone($event->timezone);
if ($event->recurrence->hasRecurEnd()) {
$event->recurrence->recurEnd->setTimezone($event->timezone);
}
}
/* Add all recurrences of the event. */
$next = $event->recurrence->nextRecurrence($next);
if ($next && $convert) {
/* Resetting after the nextRecurrence() call, because
* we need to test if the next recurrence in the
* event's timezone actually matches the interval we
* check in the local timezone. This is done on each
* nextRecurrence() further below. */
$next->setTimezone($timezone);
}
while ($next !== false && $next->compareDate($endDate) <= 0) {
if (!$event->recurrence->hasException($next->year, $next->month, $next->mday)) {
/* Add the event to all the days it covers. */
$nextEnd = clone $next;
$nextEnd->year += $diff[0];
$nextEnd->month += $diff[1];
$nextEnd->mday += $diff[2];
$nextEnd->hour += $diff[3];
$nextEnd->min += $diff[4];
$addEvent = clone $event;
$addEvent->start = $addEvent->originalStart = $next;
$addEvent->end = $addEvent->originalEnd = $nextEnd;
if ($coverDates) {
self::addCoverDates($results, $addEvent, $next, $nextEnd, $json);
} else {
$addEvent->start = $next;
$addEvent->end = $nextEnd;
$results[$addEvent->start->dateString()][$addEvent->id] = $json ? $addEvent->toJson() : $addEvent;
}
}
if ($convert) {
$next->setTimezone($event->timezone);
}
$next = $event->recurrence->nextRecurrence(
array('year' => $next->year,
'month' => $next->month,
'mday' => $next->mday + 1,
'hour' => $next->hour,
'min' => $next->min,
'sec' => $next->sec));
if ($next && $convert) {
$next->setTimezone($timezone);
}
}
} else {
/* Event only occurs once. */
if (!$coverDates) {
$results[$event->start->dateString()][$event->id] = $json ? $event->toJson() : $event;
} else {
$allDay = $event->isAllDay();
/* Work out what day it starts on. */
if ($startDate &&
$event->start->compareDateTime($startDate) < 0) {
/* It started before the beginning of the period. */
if ($event->recurs()) {
$eventStart = $event->recurrence->nextRecurrence($startDate);
$originalStart = clone $eventStart;
} else {
$eventStart = clone $startDate;
$originalStart = clone $event->start;
}
} else {
$eventStart = clone $event->start;
$originalStart = clone $event->start;
}
/* Work out what day it ends on. */
if ($endDate &&
$event->end->compareDateTime($endDate) > 0) {
/* Ends after the end of the period. */
if (is_object($endDate)) {
$eventEnd = clone $endDate;
$originalEnd = clone $event->end;
} else {
$eventEnd = $endDate;
$originalEnd = new Horde_Date($endDate);
}
} else {
/* Need to perform some magic if this is a single instance
* of a recurring event since $event->end would be the
* original end date, not the recurrence's end date. */
if ($event->recurs()) {
$diff = array($event->end->year - $event->start->year,
$event->end->month - $event->start->month,
$event->end->mday - $event->start->mday,
$event->end->hour - $event->start->hour,
$event->end->min - $event->start->min);
$theEnd = $event->recurrence->nextRecurrence($eventStart);
$theEnd->year += $diff[0];
$theEnd->month += $diff[1];
$theEnd->mday += $diff[2];
$theEnd->hour += $diff[3];
$theEnd->min += $diff[4];
if ($convert) {
$eventStart->setTimezone($timezone);
$theEnd->setTimezone($timezone);
}
} else {
$theEnd = clone $event->end;
}
$originalEnd = clone $theEnd;
/* If the event doesn't end at 12am set the end date to
* the current end date. If it ends at 12am and does not
* end at the same time that it starts (0 duration), set
* the end date to the previous day's end date. */
if ($theEnd->hour != 0 ||
$theEnd->min != 0 ||
$theEnd->sec != 0 ||
$event->start->compareDateTime($theEnd) == 0 ||
$allDay) {
$eventEnd = clone $theEnd;
} else {
$eventEnd = new Horde_Date(
array('hour' => 23,
'min' => 59,
'sec' => 59,
'month' => $theEnd->month,
'mday' => $theEnd->mday - 1,
'year' => $theEnd->year));
}
}
/* Add the event to all the days it covers. This is similar to
* Kronolith::addCoverDates(), but for days in between the
* start and end day, the range is midnight to midnight, and
* for the edge days it's start to midnight, and midnight to
* end. */
$i = $eventStart->mday;
$loopDate = new Horde_Date(array('month' => $eventStart->month,
'mday' => $i,
'year' => $eventStart->year));
while ($loopDate->compareDateTime($eventEnd) <= 0) {
if (!$allDay ||
$loopDate->compareDateTime($eventEnd) != 0) {
$addEvent = clone $event;
$addEvent->originalStart = $originalStart;
$addEvent->originalEnd = $originalEnd;
/* If this is the start day, set the start time to
* the real start time, otherwise set it to
* 00:00 */
if ($loopDate->compareDate($eventStart) == 0) {
$addEvent->start = $eventStart;
} else {
$addEvent->start = clone $loopDate;
$addEvent->start->hour = $addEvent->start->min = $addEvent->start->sec = 0;
$addEvent->first = false;
}
/* If this is the end day, set the end time to the
* real event end, otherwise set it to 23:59. */
if ($loopDate->compareDate($eventEnd) == 0) {
$addEvent->end = $eventEnd;
} else {
$addEvent->end = clone $loopDate;
$addEvent->end->hour = 23;
$addEvent->end->min = $addEvent->end->sec = 59;
$addEvent->last = false;
}
$results[$loopDate->dateString()][$addEvent->id] = $json ? $addEvent->toJson(array('all_day' => $allDay)) : $addEvent;
}
$loopDate = new Horde_Date(
array('month' => $eventStart->month,
'mday' => ++$i,
'year' => $eventStart->year));
}
}
}
ksort($results);
}
/**
* Adds an event to all the days it covers.
*
* @param array $result The current result list.
* @param Kronolith_Event $event An event object.
* @param Horde_Date $eventStart The event's start at the actual
* recurrence.
* @param Horde_Date $eventEnd The event's end at the actual recurrence.
* @param boolean $json Store the results of the events' toJson()
* method?
*/
public static function addCoverDates(&$results, $event, $eventStart,
$eventEnd, $json)
{
$loopDate = new Horde_Date($eventStart->year, $eventStart->month, $eventStart->mday);
$allDay = $event->isAllDay();
while ($loopDate->compareDateTime($eventEnd) <= 0) {
if (!$allDay ||
$loopDate->compareDateTime($eventEnd) != 0) {
$addEvent = clone $event;
$addEvent->start = $eventStart;
$addEvent->end = $eventEnd;
if ($loopDate->compareDate($eventStart) != 0) {
$addEvent->first = false;
}
if ($loopDate->compareDate($eventEnd) != 0) {
$addEvent->last = false;
}
if ($addEvent->recurs() &&
$addEvent->recurrence->hasCompletion($loopDate->year, $loopDate->month, $loopDate->mday)) {
$addEvent->status = Kronolith::STATUS_CANCELLED;
}
$results[$loopDate->dateString()][$addEvent->id] = $json ? $addEvent->toJson(array('all_day' => $allDay)) : $addEvent;
}
$loopDate->mday++;
}
}
/**
* Adds an event to set of search results.
*
* @param array $events The list of events to update with the new
* event.
* @param Kronolith_Event $event An event from a search result.
* @param stdClass $query A search query.
* @param boolean $json Store the results of the events' toJson()
* method?
*/
public static function addSearchEvents(&$events, $event, $query, $json)
{
static $now;
if (!isset($now)) {
$now = new Horde_Date($_SERVER['REQUEST_TIME']);
}
$showRecurrence = true;
if ($event->recurs()) {
if (empty($query->start) && empty($query->end)) {
$eventStart = $event->start;
$eventEnd = $event->end;
} else {
if (empty($query->end)) {
$convert = $event->timezone &&
$event->getDriver()->supportsTimezones();
if ($convert) {
$timezone = date_default_timezone_get();
$event->recurrence->start->setTimezone($event->timezone);
if ($event->recurrence->hasRecurEnd()) {
$event->recurrence->recurEnd->setTimezone($event->timezone);
}
}
$eventEnd = $event->recurrence->nextRecurrence($now);
if (!$eventEnd) {
return;
}
if ($convert) {
$eventEnd->setTimezone($timezone);
}
} else {
$eventEnd = $query->end;
}
if (empty($query->start)) {
$eventStart = $event->start;
$showRecurrence = false;
} else {
$eventStart = $query->start;
}
}
} else {
$eventStart = $event->start;
$eventEnd = $event->end;
}
self::addEvents($events, $event, $eventStart, $eventEnd, $showRecurrence, $json, false);
}
/**
* Returns the number of events in calendars that the current user owns.
*
* @return integer The number of events.
*/
public static function countEvents()
{
static $count;
if (isset($count)) {
return $count;
}
$kronolith_driver = self::getDriver();
$calendars = self::listInternalCalendars(true, Horde_Perms::ALL);
$current_calendar = $kronolith_driver->calendar;
$count = 0;
foreach (array_keys($calendars) as $calendar) {
$kronolith_driver->open($calendar);
try {
$count += $kronolith_driver->countEvents();
} catch (Exception $e) {
}
}
/* Reopen last calendar. */
$kronolith_driver->open($current_calendar);
return $count;
}
/**
* Imports an event parsed from a string.
*
* @param string $text The text to parse into an event
* @param string $calendar The calendar into which the event will be
* imported. If 'null', the user's default
* calendar will be used.
*
* @return array The UID of all events that were added.
* @throws Kronolith_Exception
*/
public function quickAdd($text, $calendar = null)
{
$text = trim($text);
if (strpos($text, "\n") !== false) {
list($title, $description) = explode($text, "\n", 2);
} else {
$title = $text;
$description = '';
}
$title = trim($title);
$description = trim($description);
$dateParser = Horde_Date_Parser::factory(array('locale' => $GLOBALS['language']));
$r = $dateParser->parse($title, array('return' => 'result'));
if (!($d = $r->guess())) {
throw new Horde_Exception(sprintf(_("Cannot parse event description \"%s\""), Horde_String::truncate($text)));
}
$title = $r->untaggedText();
$kronolith_driver = self::getDriver(null, $calendar);
$event = $kronolith_driver->getEvent();
$event->initialized = true;
$event->title = $title;
$event->description = $description;
$event->start = $d;
$event->end = $d->add(array('hour' => 1));
$event->save();
return $event;
}
/**
* Initial app setup code.
*
* Globals defined:
*/
public static function initialize()
{
$GLOBALS['calendar_manager'] = $GLOBALS['injector']->createInstance('Kronolith_CalendarsManager');
}
/**
* Returns the real name, if available, of a user.
*/
public static function getUserName($uid)
{
static $names = array();
if (!isset($names[$uid])) {
$ident = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Identity')->create($uid);
$ident->setDefault($ident->getDefault());
$names[$uid] = $ident->getValue('fullname');
if (empty($names[$uid])) {
$names[$uid] = $uid;
}
}
return $names[$uid];
}
/**
* Returns the email address, if available, of a user.
*/
public static function getUserEmail($uid)
{
static $emails = array();
if (!isset($emails[$uid])) {
$ident = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Identity')->create($uid);
$emails[$uid] = $ident->getValue('from_addr');
if (empty($emails[$uid])) {
$emails[$uid] = $uid;
}
}
return $emails[$uid];
}
/**
* Checks if an email address belongs to a user.
*/
public static function isUserEmail($uid, $email)
{
static $emails = array();
if (!isset($emails[$uid])) {
$ident = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Identity')->create($uid);
$addrs = $ident->getAll('from_addr');
$addrs[] = $uid;
$emails[$uid] = $addrs;
}
return in_array($email, $emails[$uid]);
}
/**
* Maps a Kronolith recurrence value to a translated string suitable for
* display.
*
* @param integer $type The recurrence value; one of the
* Horde_Date_Recurrence::RECUR_XXX constants.
*
* @return string The translated displayable recurrence value string.
*/
public static function recurToString($type)
{
switch ($type) {
case Horde_Date_Recurrence::RECUR_NONE:
return _("Does not recur");
case Horde_Date_Recurrence::RECUR_DAILY:
return _("Recurs daily");
case Horde_Date_Recurrence::RECUR_WEEKLY:
return _("Recurs weekly");
case Horde_Date_Recurrence::RECUR_MONTHLY_DATE:
case Horde_Date_Recurrence::RECUR_MONTHLY_WEEKDAY:
case Horde_Date_Recurrence::RECUR_MONTHLY_LAST_WEEKDAY:
return _("Recurs monthly");
case Horde_Date_Recurrence::RECUR_YEARLY_DATE:
case Horde_Date_Recurrence::RECUR_YEARLY_DAY:
case Horde_Date_Recurrence::RECUR_YEARLY_WEEKDAY:
return _("Recurs yearly");
}
}
/**
* Maps a Kronolith meeting status string to a translated string suitable
* for display.
*
* @param integer $status The meeting status; one of the
* Kronolith::STATUS_XXX constants.
*
* @return string The translated displayable meeting status string.
*/
public static function statusToString($status)
{
switch ($status) {
case self::STATUS_CONFIRMED:
return _("Confirmed");
case self::STATUS_CANCELLED:
return _("Cancelled");
case self::STATUS_FREE:
return _("Free");
case self::STATUS_TENTATIVE:
default:
return _("Tentative");
}
}
/**
* Maps a Kronolith attendee response string to a translated string
* suitable for display.
*
* @param integer $response The attendee response; one of the
* Kronolith::RESPONSE_XXX constants.
*
* @return string The translated displayable attendee response string.
*/
public static function responseToString($response)
{
switch ($response) {
case self::RESPONSE_ACCEPTED:
return _("Accepted");
case self::RESPONSE_DECLINED:
return _("Declined");
case self::RESPONSE_TENTATIVE:
return _("Tentative");
case self::RESPONSE_NONE:
default:
return _("None");
}
}
/**
* Maps a Kronolith attendee participation string to a translated string
* suitable for display.
*
* @param integer $part The attendee participation; one of the
* Kronolith::PART_XXX constants.
*
* @return string The translated displayable attendee participation
* string.
*/
public static function partToString($part)
{
switch ($part) {
case self::PART_OPTIONAL:
return _("Optional");
case self::PART_NONE:
return _("None");
case self::PART_REQUIRED:
default:
return _("Required");
}
}
/**
* Maps an iCalendar attendee response string to the corresponding
* Kronolith value.
*
* @param string $response The attendee response.
*
* @return string The Kronolith response value.
*/
public static function responseFromICal($response)
{
switch (Horde_String::upper($response)) {
case 'ACCEPTED':
return self::RESPONSE_ACCEPTED;
case 'DECLINED':
return self::RESPONSE_DECLINED;
case 'TENTATIVE':
return self::RESPONSE_TENTATIVE;
case 'NEEDS-ACTION':
default:
return self::RESPONSE_NONE;
}
}
/**
* Builds the HTML for an event status widget.
*
* @param string $name The name of the widget.
* @param string $current The selected status value.
* @param string $any Whether an 'any' item should be added
*
* @return string The HTML <select> widget.
*/
public static function buildStatusWidget($name,
$current = self::STATUS_CONFIRMED,
$any = false)
{
$html = "<select id=\"$name\" name=\"$name\">";
$statii = array(
self::STATUS_FREE,
self::STATUS_TENTATIVE,
self::STATUS_CONFIRMED,
self::STATUS_CANCELLED
);
if (!isset($current)) {
$current = self::STATUS_NONE;
}
if ($any) {
$html .= "<option value=\"" . self::STATUS_NONE . "\"";
$html .= ($current == self::STATUS_NONE) ? ' selected="selected">' : '>';
$html .= _("Any") . "</option>";
}
foreach ($statii as $status) {
$html .= "<option value=\"$status\"";
$html .= ($status == $current) ? ' selected="selected">' : '>';
$html .= self::statusToString($status) . "</option>";
}
$html .= '</select>';
return $html;
}
/**
* Returns all internal calendars a user has access to, according
* to several parameters/permission levels.
*
* This method takes the $conf['share']['hidden'] setting into account. If
* this setting is enabled, even if requesting permissions different than
* SHOW, it will only return calendars that the user owns or has SHOW
* permissions for. For checking individual calendar's permissions, use
* hasPermission() instead.
*
* @param boolean $owneronly Only return calenders that this user owns?
* Defaults to false.
* @param integer $permission The permission to filter calendars by.
* @param string $user The user to list calendars for, if not
* the current.
*
* @return array The calendar list.
*/
public static function listInternalCalendars($owneronly = false,