annotate plugins/libcalendaring/lib/libcalendaring_recurrence.php @ 4:888e774ee983

libcalendar plugin as distributed
author Charlie Root
date Sat, 13 Jan 2018 08:57:56 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1 <?php
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
2
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
3 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
4 * Recurrence computation class for shared use
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
5 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
6 * Uitility class to compute reccurrence dates from the given rules
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
7 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
8 * @author Thomas Bruederli <bruederli@kolabsys.com>
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
9 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
10 * Copyright (C) 2012-2014, Kolab Systems AG <contact@kolabsys.com>
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
11 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
12 * This program is free software: you can redistribute it and/or modify
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
13 * it under the terms of the GNU Affero General Public License as
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
14 * published by the Free Software Foundation, either version 3 of the
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
15 * License, or (at your option) any later version.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
16 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
17 * This program is distributed in the hope that it will be useful,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
20 * GNU Affero General Public License for more details.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
21 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
22 * You should have received a copy of the GNU Affero General Public License
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
24 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
25 class libcalendaring_recurrence
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
26 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
27 protected $lib;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
28 protected $start;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
29 protected $next;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
30 protected $engine;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
31 protected $recurrence;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
32 protected $dateonly = false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
33 protected $hour = 0;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
34
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
35 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
36 * Default constructor
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
37 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
38 * @param object calendar The calendar plugin instance
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
39 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
40 function __construct($lib)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
41 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
42 // use Horde classes to compute recurring instances
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
43 // TODO: replace with something that has less than 6'000 lines of code
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
44 require_once(__DIR__ . '/Horde_Date_Recurrence.php');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
45
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
46 $this->lib = $lib;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
47 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
48
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
49 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
50 * Initialize recurrence engine
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
51 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
52 * @param array The recurrence properties
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
53 * @param object DateTime The recurrence start date
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
54 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
55 public function init($recurrence, $start = null)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
56 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
57 $this->recurrence = $recurrence;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
58
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
59 $this->engine = new Horde_Date_Recurrence($start);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
60 $this->engine->fromRRule20(libcalendaring::to_rrule($recurrence));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
61
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
62 $this->set_start($start);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
63
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
64 if (is_array($recurrence['EXDATE'])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
65 foreach ($recurrence['EXDATE'] as $exdate) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
66 if (is_a($exdate, 'DateTime')) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
67 $this->engine->addException($exdate->format('Y'), $exdate->format('n'), $exdate->format('j'));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
68 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
69 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
70 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
71 if (is_array($recurrence['RDATE'])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
72 foreach ($recurrence['RDATE'] as $rdate) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
73 if (is_a($rdate, 'DateTime')) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
74 $this->engine->addRDate($rdate->format('Y'), $rdate->format('n'), $rdate->format('j'));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
75 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
76 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
77 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
78 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
79
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
80 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
81 * Setter for (new) recurrence start date
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
82 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
83 * @param object DateTime The recurrence start date
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
84 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
85 public function set_start($start)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
86 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
87 $this->start = $start;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
88 $this->dateonly = $start->_dateonly;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
89 $this->next = new Horde_Date($start, $this->lib->timezone->getName());
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
90 $this->hour = $this->next->hour;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
91 $this->engine->setRecurStart($this->next);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
92 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
93
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
94 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
95 * Get date/time of the next occurence of this event
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
96 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
97 * @return mixed DateTime object or False if recurrence ended
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
98 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
99 public function next()
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
100 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
101 $time = false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
102 $after = clone $this->next;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
103 $after->mday = $after->mday + 1;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
104 if ($this->next && ($next = $this->engine->nextActiveRecurrence($after))) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
105 // avoid endless loops if recurrence computation fails
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
106 if (!$next->after($this->next)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
107 return false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
108 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
109 // fix time for all-day events
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
110 if ($this->dateonly) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
111 $next->hour = $this->hour;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
112 $next->min = 0;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
113 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
114
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
115 $time = $next->toDateTime();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
116 $this->next = $next;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
117 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
118
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
119 return $time;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
120 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
121
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
122 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
123 * Get the end date of the occurence of this recurrence cycle
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
124 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
125 * @return DateTime|bool End datetime of the last occurence or False if recurrence exceeds limit
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
126 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
127 public function end()
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
128 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
129 // recurrence end date is given
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
130 if ($this->recurrence['UNTIL'] instanceof DateTime) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
131 return $this->recurrence['UNTIL'];
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
132 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
133
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
134 // take the last RDATE entry if set
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
135 if (is_array($this->recurrence['RDATE']) && !empty($this->recurrence['RDATE'])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
136 $last = end($this->recurrence['RDATE']);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
137 if ($last instanceof DateTime) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
138 return $last;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
139 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
140 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
141
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
142 // run through all items till we reach the end
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
143 if ($this->recurrence['COUNT']) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
144 $last = $this->start;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
145 $this->next = new Horde_Date($this->start, $this->lib->timezone->getName());
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
146 while (($next = $this->next()) && $c < 1000) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
147 $last = $next;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
148 $c++;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
149 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
150 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
151
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
152 return $last;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
153 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
154
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
155 }