annotate plugins/libcalendaring/lib/Horde_Date_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 * This is a modified copy of Horde/Date/Recurrence.php (2015-01-05)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
5 * Pull the latest version of this file from the PEAR channel of the Horde
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
6 * project at http://pear.horde.org by installing the Horde_Date package.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
7 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
8
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
9 if (!class_exists('Horde_Date')) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
10 require_once(__DIR__ . '/Horde_Date.php');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
11 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
12
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
13 // minimal required implementation of Horde_Date_Translation to avoid a huge dependency nightmare
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
14 class Horde_Date_Translation
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
15 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
16 function t($arg) { return $arg; }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
17 function ngettext($sing, $plur, $num) { return ($num > 1 ? $plur : $sing); }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
18 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
19
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
20
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
21 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
22 * This file contains the Horde_Date_Recurrence class and according constants.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
23 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
24 * Copyright 2007-2015 Horde LLC (http://www.horde.org/)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
25 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
26 * See the enclosed file COPYING for license information (LGPL). If you
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
27 * did not receive this file, see http://www.horde.org/licenses/lgpl21.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
28 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
29 * @category Horde
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
30 * @package Date
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
31 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
32
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
33 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
34 * The Horde_Date_Recurrence class implements algorithms for calculating
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
35 * recurrences of events, including several recurrence types, intervals,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
36 * exceptions, and conversion from and to vCalendar and iCalendar recurrence
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
37 * rules.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
38 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
39 * All methods expecting dates as parameters accept all values that the
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
40 * Horde_Date constructor accepts, i.e. a timestamp, another Horde_Date
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
41 * object, an ISO time string or a hash.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
42 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
43 * @author Jan Schneider <jan@horde.org>
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
44 * @category Horde
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
45 * @package Date
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
46 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
47 class Horde_Date_Recurrence
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
48 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
49 /** No Recurrence **/
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
50 const RECUR_NONE = 0;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
51
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
52 /** Recurs daily. */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
53 const RECUR_DAILY = 1;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
54
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
55 /** Recurs weekly. */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
56 const RECUR_WEEKLY = 2;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
57
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
58 /** Recurs monthly on the same date. */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
59 const RECUR_MONTHLY_DATE = 3;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
60
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
61 /** Recurs monthly on the same week day. */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
62 const RECUR_MONTHLY_WEEKDAY = 4;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
63
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
64 /** Recurs yearly on the same date. */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
65 const RECUR_YEARLY_DATE = 5;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
66
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
67 /** Recurs yearly on the same day of the year. */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
68 const RECUR_YEARLY_DAY = 6;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
69
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
70 /** Recurs yearly on the same week day. */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
71 const RECUR_YEARLY_WEEKDAY = 7;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
72
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
73 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
74 * The start time of the event.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
75 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
76 * @var Horde_Date
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
77 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
78 public $start;
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 * The end date of the recurrence interval.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
82 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
83 * @var Horde_Date
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
84 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
85 public $recurEnd = null;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
86
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
87 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
88 * The number of recurrences.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
89 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
90 * @var integer
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
91 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
92 public $recurCount = null;
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 * The type of recurrence this event follows. RECUR_* constant.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
96 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
97 * @var integer
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
98 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
99 public $recurType = self::RECUR_NONE;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
100
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
101 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
102 * The length of time between recurrences. The time unit depends on the
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
103 * recurrence type.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
104 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
105 * @var integer
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
106 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
107 public $recurInterval = 1;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
108
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
109 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
110 * Any additional recurrence data.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
111 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
112 * @var integer
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
113 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
114 public $recurData = null;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
115
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
116 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
117 * BYDAY recurrence number
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
118 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
119 * @var integer
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
120 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
121 public $recurNthDay = null;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
122
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
123 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
124 * BYMONTH recurrence data
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
125 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
126 * @var array
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
127 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
128 public $recurMonths = array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
129
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
130 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
131 * RDATE recurrence values
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
132 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
133 * @var array
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
134 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
135 public $rdates = array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
136
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
137 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
138 * All the exceptions from recurrence for this event.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
139 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
140 * @var array
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
141 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
142 public $exceptions = array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
143
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
144 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
145 * All the dates this recurrence has been marked as completed.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
146 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
147 * @var array
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
148 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
149 public $completions = array();
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 * Constructor.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
153 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
154 * @param Horde_Date $start Start of the recurring event.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
155 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
156 public function __construct($start)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
157 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
158 $this->start = new Horde_Date($start);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
159 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
160
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
161 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
162 * Resets the class properties.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
163 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
164 public function reset()
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
165 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
166 $this->recurEnd = null;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
167 $this->recurCount = null;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
168 $this->recurType = self::RECUR_NONE;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
169 $this->recurInterval = 1;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
170 $this->recurData = null;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
171 $this->exceptions = array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
172 $this->completions = array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
173 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
174
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
175 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
176 * Checks if this event recurs on a given day of the week.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
177 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
178 * @param integer $dayMask A mask consisting of Horde_Date::MASK_*
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
179 * constants specifying the day(s) to check.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
180 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
181 * @return boolean True if this event recurs on the given day(s).
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
182 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
183 public function recurOnDay($dayMask)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
184 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
185 return ($this->recurData & $dayMask);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
186 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
187
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
188 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
189 * Specifies the days this event recurs on.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
190 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
191 * @param integer $dayMask A mask consisting of Horde_Date::MASK_*
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
192 * constants specifying the day(s) to recur on.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
193 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
194 public function setRecurOnDay($dayMask)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
195 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
196 $this->recurData = $dayMask;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
197 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
198
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
199 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
200 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
201 * @param integer $nthDay The nth weekday of month to repeat events on
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
202 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
203 public function setRecurNthWeekday($nth)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
204 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
205 $this->recurNthDay = (int)$nth;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
206 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
207
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
208 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
209 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
210 * @return integer The nth weekday of month to repeat events.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
211 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
212 public function getRecurNthWeekday()
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
213 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
214 return isset($this->recurNthDay) ? $this->recurNthDay : ceil($this->start->mday / 7);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
215 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
216
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
217 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
218 * Specifies the months for yearly (weekday) recurrence
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
219 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
220 * @param array $months List of months (integers) this event recurs on.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
221 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
222 function setRecurByMonth($months)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
223 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
224 $this->recurMonths = (array)$months;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
225 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
226
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
227 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
228 * Returns a list of months this yearly event recurs on
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
229 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
230 * @return array List of months (integers) this event recurs on.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
231 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
232 function getRecurByMonth()
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
233 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
234 return $this->recurMonths;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
235 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
236
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
237 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
238 * Returns the days this event recurs on.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
239 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
240 * @return integer A mask consisting of Horde_Date::MASK_* constants
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
241 * specifying the day(s) this event recurs on.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
242 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
243 public function getRecurOnDays()
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
244 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
245 return $this->recurData;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
246 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
247
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
248 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
249 * Returns whether this event has a specific recurrence type.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
250 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
251 * @param integer $recurrence RECUR_* constant of the
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
252 * recurrence type to check for.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
253 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
254 * @return boolean True if the event has the specified recurrence type.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
255 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
256 public function hasRecurType($recurrence)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
257 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
258 return ($recurrence == $this->recurType);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
259 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
260
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
261 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
262 * Sets a recurrence type for this event.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
263 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
264 * @param integer $recurrence A RECUR_* constant.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
265 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
266 public function setRecurType($recurrence)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
267 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
268 $this->recurType = $recurrence;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
269 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
270
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
271 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
272 * Returns recurrence type of this event.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
273 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
274 * @return integer A RECUR_* constant.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
275 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
276 public function getRecurType()
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
277 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
278 return $this->recurType;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
279 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
280
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
281 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
282 * Returns a description of this event's recurring type.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
283 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
284 * @return string Human readable recurring type.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
285 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
286 public function getRecurName()
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
287 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
288 switch ($this->getRecurType()) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
289 case self::RECUR_NONE:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
290 return Horde_Date_Translation::t("No recurrence");
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
291 case self::RECUR_DAILY:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
292 return Horde_Date_Translation::t("Daily");
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
293 case self::RECUR_WEEKLY:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
294 return Horde_Date_Translation::t("Weekly");
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
295 case self::RECUR_MONTHLY_DATE:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
296 case self::RECUR_MONTHLY_WEEKDAY:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
297 return Horde_Date_Translation::t("Monthly");
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
298 case self::RECUR_YEARLY_DATE:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
299 case self::RECUR_YEARLY_DAY:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
300 case self::RECUR_YEARLY_WEEKDAY:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
301 return Horde_Date_Translation::t("Yearly");
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
302 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
303 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
304
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
305 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
306 * Sets the length of time between recurrences of this event.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
307 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
308 * @param integer $interval The time between recurrences.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
309 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
310 public function setRecurInterval($interval)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
311 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
312 if ($interval > 0) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
313 $this->recurInterval = $interval;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
314 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
315 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
316
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
317 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
318 * Retrieves the length of time between recurrences of this event.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
319 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
320 * @return integer The number of seconds between recurrences.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
321 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
322 public function getRecurInterval()
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
323 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
324 return $this->recurInterval;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
325 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
326
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
327 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
328 * Sets the number of recurrences of this event.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
329 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
330 * @param integer $count The number of recurrences.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
331 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
332 public function setRecurCount($count)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
333 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
334 if ($count > 0) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
335 $this->recurCount = (int)$count;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
336 // Recurrence counts and end dates are mutually exclusive.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
337 $this->recurEnd = null;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
338 } else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
339 $this->recurCount = null;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
340 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
341 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
342
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
343 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
344 * Retrieves the number of recurrences of this event.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
345 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
346 * @return integer The number recurrences.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
347 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
348 public function getRecurCount()
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
349 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
350 return $this->recurCount;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
351 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
352
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
353 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
354 * Returns whether this event has a recurrence with a fixed count.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
355 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
356 * @return boolean True if this recurrence has a fixed count.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
357 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
358 public function hasRecurCount()
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
359 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
360 return isset($this->recurCount);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
361 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
362
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
363 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
364 * Sets the start date of the recurrence interval.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
365 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
366 * @param Horde_Date $start The recurrence start.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
367 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
368 public function setRecurStart($start)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
369 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
370 $this->start = clone $start;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
371 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
372
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
373 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
374 * Retrieves the start date of the recurrence interval.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
375 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
376 * @return Horde_Date The recurrence start.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
377 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
378 public function getRecurStart()
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
379 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
380 return $this->start;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
381 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
382
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
383 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
384 * Sets the end date of the recurrence interval.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
385 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
386 * @param Horde_Date $end The recurrence end.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
387 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
388 public function setRecurEnd($end)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
389 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
390 if (!empty($end)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
391 // Recurrence counts and end dates are mutually exclusive.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
392 $this->recurCount = null;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
393 $this->recurEnd = clone $end;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
394 } else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
395 $this->recurEnd = $end;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
396 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
397 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
398
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
399 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
400 * Retrieves the end date of the recurrence interval.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
401 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
402 * @return Horde_Date The recurrence end.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
403 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
404 public function getRecurEnd()
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
405 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
406 return $this->recurEnd;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
407 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
408
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
409 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
410 * Returns whether this event has a recurrence end.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
411 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
412 * @return boolean True if this recurrence ends.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
413 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
414 public function hasRecurEnd()
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
415 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
416 return isset($this->recurEnd) && isset($this->recurEnd->year) &&
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
417 $this->recurEnd->year != 9999;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
418 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
419
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
420 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
421 * Finds the next recurrence of this event that's after $afterDate.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
422 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
423 * @param Horde_Date|string $after Return events after this date.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
424 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
425 * @return Horde_Date|boolean The date of the next recurrence or false
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
426 * if the event does not recur after
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
427 * $afterDate.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
428 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
429 public function nextRecurrence($after)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
430 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
431 if (!($after instanceof Horde_Date)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
432 $after = new Horde_Date($after);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
433 } else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
434 $after = clone($after);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
435 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
436
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
437 // Make sure $after and $this->start are in the same TZ
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
438 $after->setTimezone($this->start->timezone);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
439 if ($this->start->compareDateTime($after) >= 0) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
440 return clone $this->start;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
441 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
442
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
443 if ($this->recurInterval == 0 && empty($this->rdates)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
444 return false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
445 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
446
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
447 switch ($this->getRecurType()) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
448 case self::RECUR_DAILY:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
449 $diff = $this->start->diff($after);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
450 $recur = ceil($diff / $this->recurInterval);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
451 if ($this->recurCount && $recur >= $this->recurCount) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
452 return false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
453 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
454
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
455 $recur *= $this->recurInterval;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
456 $next = $this->start->add(array('day' => $recur));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
457 if ((!$this->hasRecurEnd() ||
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
458 $next->compareDateTime($this->recurEnd) <= 0) &&
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
459 $next->compareDateTime($after) >= 0) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
460 return $next;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
461 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
462
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
463 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
464
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
465 case self::RECUR_WEEKLY:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
466 if (empty($this->recurData)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
467 return false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
468 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
469
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
470 $start_week = Horde_Date_Utils::firstDayOfWeek($this->start->format('W'),
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
471 $this->start->year);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
472 $start_week->timezone = $this->start->timezone;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
473 $start_week->hour = $this->start->hour;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
474 $start_week->min = $this->start->min;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
475 $start_week->sec = $this->start->sec;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
476
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
477 // Make sure we are not at the ISO-8601 first week of year while
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
478 // still in month 12...OR in the ISO-8601 last week of year while
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
479 // in month 1 and adjust the year accordingly.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
480 $week = $after->format('W');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
481 if ($week == 1 && $after->month == 12) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
482 $theYear = $after->year + 1;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
483 } elseif ($week >= 52 && $after->month == 1) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
484 $theYear = $after->year - 1;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
485 } else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
486 $theYear = $after->year;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
487 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
488
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
489 $after_week = Horde_Date_Utils::firstDayOfWeek($week, $theYear);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
490 $after_week->timezone = $this->start->timezone;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
491 $after_week_end = clone $after_week;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
492 $after_week_end->mday += 7;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
493
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
494 $diff = $start_week->diff($after_week);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
495 $interval = $this->recurInterval * 7;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
496 $repeats = floor($diff / $interval);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
497 if ($diff % $interval < 7) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
498 $recur = $diff;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
499 } else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
500 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
501 * If the after_week is not in the first week interval the
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
502 * search needs to skip ahead a complete interval. The way it is
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
503 * calculated here means that an event that occurs every second
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
504 * week on Monday and Wednesday with the event actually starting
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
505 * on Tuesday or Wednesday will only have one incidence in the
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
506 * first week.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
507 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
508 $recur = $interval * ($repeats + 1);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
509 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
510
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
511 if ($this->hasRecurCount()) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
512 $recurrences = 0;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
513 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
514 * Correct the number of recurrences by the number of events
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
515 * that lay between the start of the start week and the
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
516 * recurrence start.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
517 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
518 $next = clone $start_week;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
519 while ($next->compareDateTime($this->start) < 0) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
520 if ($this->recurOnDay((int)pow(2, $next->dayOfWeek()))) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
521 $recurrences--;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
522 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
523 ++$next->mday;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
524 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
525 if ($repeats > 0) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
526 $weekdays = $this->recurData;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
527 $total_recurrences_per_week = 0;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
528 while ($weekdays > 0) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
529 if ($weekdays % 2) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
530 $total_recurrences_per_week++;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
531 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
532 $weekdays = ($weekdays - ($weekdays % 2)) / 2;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
533 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
534 $recurrences += $total_recurrences_per_week * $repeats;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
535 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
536 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
537
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
538 $next = clone $start_week;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
539 $next->mday += $recur;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
540 while ($next->compareDateTime($after) < 0 &&
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
541 $next->compareDateTime($after_week_end) < 0) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
542 if ($this->hasRecurCount()
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
543 && $next->compareDateTime($after) < 0
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
544 && $this->recurOnDay((int)pow(2, $next->dayOfWeek()))) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
545 $recurrences++;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
546 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
547 ++$next->mday;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
548 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
549 if ($this->hasRecurCount() &&
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
550 $recurrences >= $this->recurCount) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
551 return false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
552 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
553 if (!$this->hasRecurEnd() ||
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
554 $next->compareDateTime($this->recurEnd) <= 0) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
555 if ($next->compareDateTime($after_week_end) >= 0) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
556 return $this->nextRecurrence($after_week_end);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
557 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
558 while (!$this->recurOnDay((int)pow(2, $next->dayOfWeek())) &&
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
559 $next->compareDateTime($after_week_end) < 0) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
560 ++$next->mday;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
561 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
562 if (!$this->hasRecurEnd() ||
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
563 $next->compareDateTime($this->recurEnd) <= 0) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
564 if ($next->compareDateTime($after_week_end) >= 0) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
565 return $this->nextRecurrence($after_week_end);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
566 } else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
567 return $next;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
568 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
569 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
570 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
571 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
572
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
573 case self::RECUR_MONTHLY_DATE:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
574 $start = clone $this->start;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
575 if ($after->compareDateTime($start) < 0) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
576 $after = clone $start;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
577 } else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
578 $after = clone $after;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
579 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
580
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
581 // If we're starting past this month's recurrence of the event,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
582 // look in the next month on the day the event recurs.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
583 if ($after->mday > $start->mday) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
584 ++$after->month;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
585 $after->mday = $start->mday;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
586 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
587
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
588 // Adjust $start to be the first match.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
589 $offset = ($after->month - $start->month) + ($after->year - $start->year) * 12;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
590 $offset = floor(($offset + $this->recurInterval - 1) / $this->recurInterval) * $this->recurInterval;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
591
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
592 if ($this->recurCount &&
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
593 ($offset / $this->recurInterval) >= $this->recurCount) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
594 return false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
595 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
596 $start->month += $offset;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
597 $count = $offset / $this->recurInterval;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
598
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
599 do {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
600 if ($this->recurCount &&
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
601 $count++ >= $this->recurCount) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
602 return false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
603 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
604
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
605 // Bail if we've gone past the end of recurrence.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
606 if ($this->hasRecurEnd() &&
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
607 $this->recurEnd->compareDateTime($start) < 0) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
608 return false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
609 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
610 if ($start->isValid()) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
611 return $start;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
612 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
613
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
614 // If the interval is 12, and the date isn't valid, then we
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
615 // need to see if February 29th is an option. If not, then the
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
616 // event will _never_ recur, and we need to stop checking to
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
617 // avoid an infinite loop.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
618 if ($this->recurInterval == 12 && ($start->month != 2 || $start->mday > 29)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
619 return false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
620 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
621
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
622 // Add the recurrence interval.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
623 $start->month += $this->recurInterval;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
624 } while (true);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
625
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
626 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
627
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
628 case self::RECUR_MONTHLY_WEEKDAY:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
629 // Start with the start date of the event.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
630 $estart = clone $this->start;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
631
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
632 // What day of the week, and week of the month, do we recur on?
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
633 if (isset($this->recurNthDay)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
634 $nth = $this->recurNthDay;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
635 $weekday = log($this->recurData, 2);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
636 } else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
637 $nth = ceil($this->start->mday / 7);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
638 $weekday = $estart->dayOfWeek();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
639 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
640
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
641 // Adjust $estart to be the first candidate.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
642 $offset = ($after->month - $estart->month) + ($after->year - $estart->year) * 12;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
643 $offset = floor(($offset + $this->recurInterval - 1) / $this->recurInterval) * $this->recurInterval;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
644
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
645 // Adjust our working date until it's after $after.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
646 $estart->month += $offset - $this->recurInterval;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
647
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
648 $count = $offset / $this->recurInterval;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
649 do {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
650 if ($this->recurCount &&
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
651 $count++ >= $this->recurCount) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
652 return false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
653 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
654
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
655 $estart->month += $this->recurInterval;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
656
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
657 $next = clone $estart;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
658 $next->setNthWeekday($weekday, $nth);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
659
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
660 if ($next->month != $estart->month) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
661 // We're already in the next month.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
662 continue;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
663 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
664 if ($next->compareDateTime($after) < 0) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
665 // We haven't made it past $after yet, try again.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
666 continue;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
667 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
668 if ($this->hasRecurEnd() &&
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
669 $next->compareDateTime($this->recurEnd) > 0) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
670 // We've gone past the end of recurrence; we can give up
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
671 // now.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
672 return false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
673 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
674
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
675 // We have a candidate to return.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
676 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
677 } while (true);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
678
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
679 return $next;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
680
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
681 case self::RECUR_YEARLY_DATE:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
682 // Start with the start date of the event.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
683 $estart = clone $this->start;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
684 $after = clone $after;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
685
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
686 if ($after->month > $estart->month ||
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
687 ($after->month == $estart->month && $after->mday > $estart->mday)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
688 ++$after->year;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
689 $after->month = $estart->month;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
690 $after->mday = $estart->mday;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
691 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
692
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
693 // Seperate case here for February 29th
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
694 if ($estart->month == 2 && $estart->mday == 29) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
695 while (!Horde_Date_Utils::isLeapYear($after->year)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
696 ++$after->year;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
697 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
698 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
699
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
700 // Adjust $estart to be the first candidate.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
701 $offset = $after->year - $estart->year;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
702 if ($offset > 0) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
703 $offset = floor(($offset + $this->recurInterval - 1) / $this->recurInterval) * $this->recurInterval;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
704 $estart->year += $offset;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
705 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
706
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
707 // We've gone past the end of recurrence; give up.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
708 if ($this->recurCount &&
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
709 $offset >= $this->recurCount) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
710 return false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
711 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
712 if ($this->hasRecurEnd() &&
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
713 $this->recurEnd->compareDateTime($estart) < 0) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
714 return false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
715 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
716
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
717 return $estart;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
718
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
719 case self::RECUR_YEARLY_DAY:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
720 // Check count first.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
721 $dayofyear = $this->start->dayOfYear();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
722 $count = ($after->year - $this->start->year) / $this->recurInterval + 1;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
723 if ($this->recurCount &&
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
724 ($count > $this->recurCount ||
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
725 ($count == $this->recurCount &&
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
726 $after->dayOfYear() > $dayofyear))) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
727 return false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
728 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
729
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
730 // Start with a rough interval.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
731 $estart = clone $this->start;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
732 $estart->year += floor($count - 1) * $this->recurInterval;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
733
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
734 // Now add the difference to the required day of year.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
735 $estart->mday += $dayofyear - $estart->dayOfYear();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
736
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
737 // Add an interval if the estimation was wrong.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
738 if ($estart->compareDate($after) < 0) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
739 $estart->year += $this->recurInterval;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
740 $estart->mday += $dayofyear - $estart->dayOfYear();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
741 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
742
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
743 // We've gone past the end of recurrence; give up.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
744 if ($this->hasRecurEnd() &&
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
745 $this->recurEnd->compareDateTime($estart) < 0) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
746 return false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
747 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
748
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
749 return $estart;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
750
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
751 case self::RECUR_YEARLY_WEEKDAY:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
752 // Start with the start date of the event.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
753 $estart = clone $this->start;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
754
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
755 // What day of the week, and week of the month, do we recur on?
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
756 if (isset($this->recurNthDay)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
757 $nth = $this->recurNthDay;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
758 $weekday = log($this->recurData, 2);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
759 } else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
760 $nth = ceil($this->start->mday / 7);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
761 $weekday = $estart->dayOfWeek();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
762 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
763
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
764 // Adjust $estart to be the first candidate.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
765 $offset = floor(($after->year - $estart->year + $this->recurInterval - 1) / $this->recurInterval) * $this->recurInterval;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
766
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
767 // Adjust our working date until it's after $after.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
768 $estart->year += $offset - $this->recurInterval;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
769
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
770 $count = $offset / $this->recurInterval;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
771 do {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
772 if ($this->recurCount &&
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
773 $count++ >= $this->recurCount) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
774 return false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
775 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
776
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
777 $estart->year += $this->recurInterval;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
778
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
779 $next = clone $estart;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
780 $next->setNthWeekday($weekday, $nth);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
781
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
782 if ($next->compareDateTime($after) < 0) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
783 // We haven't made it past $after yet, try again.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
784 continue;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
785 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
786 if ($this->hasRecurEnd() &&
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
787 $next->compareDateTime($this->recurEnd) > 0) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
788 // We've gone past the end of recurrence; we can give up
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
789 // now.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
790 return false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
791 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
792
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
793 // We have a candidate to return.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
794 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
795 } while (true);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
796
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
797 return $next;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
798 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
799
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
800 // fall-back to RDATE properties
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
801 if (!empty($this->rdates)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
802 $next = clone $this->start;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
803 foreach ($this->rdates as $rdate) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
804 $next->year = $rdate->year;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
805 $next->month = $rdate->month;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
806 $next->mday = $rdate->mday;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
807 if ($next->compareDateTime($after) >= 0) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
808 return $next;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
809 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
810 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
811 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
812
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
813 // We didn't find anything, the recurType was bad, or something else
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
814 // went wrong - return false.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
815 return false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
816 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
817
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
818 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
819 * Returns whether this event has any date that matches the recurrence
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
820 * rules and is not an exception.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
821 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
822 * @return boolean True if an active recurrence exists.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
823 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
824 public function hasActiveRecurrence()
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
825 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
826 if (!$this->hasRecurEnd()) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
827 return true;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
828 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
829
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
830 $next = $this->nextRecurrence(new Horde_Date($this->start));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
831 while (is_object($next)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
832 if (!$this->hasException($next->year, $next->month, $next->mday) &&
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
833 !$this->hasCompletion($next->year, $next->month, $next->mday)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
834 return true;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
835 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
836
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
837 $next = $this->nextRecurrence($next->add(array('day' => 1)));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
838 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
839
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
840 return false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
841 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
842
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
843 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
844 * Returns the next active recurrence.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
845 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
846 * @param Horde_Date $afterDate Return events after this date.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
847 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
848 * @return Horde_Date|boolean The date of the next active
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
849 * recurrence or false if the event
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
850 * has no active recurrence after
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
851 * $afterDate.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
852 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
853 public function nextActiveRecurrence($afterDate)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
854 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
855 $next = $this->nextRecurrence($afterDate);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
856 while (is_object($next)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
857 if (!$this->hasException($next->year, $next->month, $next->mday) &&
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
858 !$this->hasCompletion($next->year, $next->month, $next->mday)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
859 return $next;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
860 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
861 $next->mday++;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
862 $next = $this->nextRecurrence($next);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
863 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
864
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
865 return false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
866 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
867
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
868 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
869 * Adds an absolute recurrence date.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
870 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
871 * @param integer $year The year of the instance.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
872 * @param integer $month The month of the instance.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
873 * @param integer $mday The day of the month of the instance.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
874 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
875 public function addRDate($year, $month, $mday)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
876 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
877 $this->rdates[] = new Horde_Date($year, $month, $mday);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
878 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
879
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
880 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
881 * Adds an exception to a recurring event.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
882 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
883 * @param integer $year The year of the execption.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
884 * @param integer $month The month of the execption.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
885 * @param integer $mday The day of the month of the exception.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
886 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
887 public function addException($year, $month, $mday)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
888 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
889 $key = sprintf('%04d%02d%02d', $year, $month, $mday);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
890 if (array_search($key, $this->exceptions) === false) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
891 $this->exceptions[] = sprintf('%04d%02d%02d', $year, $month, $mday);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
892 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
893 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
894
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
895 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
896 * Deletes an exception from a recurring event.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
897 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
898 * @param integer $year The year of the execption.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
899 * @param integer $month The month of the execption.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
900 * @param integer $mday The day of the month of the exception.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
901 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
902 public function deleteException($year, $month, $mday)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
903 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
904 $key = array_search(sprintf('%04d%02d%02d', $year, $month, $mday), $this->exceptions);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
905 if ($key !== false) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
906 unset($this->exceptions[$key]);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
907 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
908 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
909
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
910 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
911 * Checks if an exception exists for a given reccurence of an event.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
912 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
913 * @param integer $year The year of the reucrance.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
914 * @param integer $month The month of the reucrance.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
915 * @param integer $mday The day of the month of the reucrance.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
916 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
917 * @return boolean True if an exception exists for the given date.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
918 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
919 public function hasException($year, $month, $mday)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
920 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
921 return in_array(sprintf('%04d%02d%02d', $year, $month, $mday),
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
922 $this->getExceptions());
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
923 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
924
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
925 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
926 * Retrieves all the exceptions for this event.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
927 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
928 * @return array Array containing the dates of all the exceptions in
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
929 * YYYYMMDD form.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
930 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
931 public function getExceptions()
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
932 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
933 return $this->exceptions;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
934 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
935
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
936 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
937 * Adds a completion to a recurring event.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
938 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
939 * @param integer $year The year of the execption.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
940 * @param integer $month The month of the execption.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
941 * @param integer $mday The day of the month of the completion.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
942 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
943 public function addCompletion($year, $month, $mday)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
944 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
945 $this->completions[] = sprintf('%04d%02d%02d', $year, $month, $mday);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
946 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
947
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
948 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
949 * Deletes a completion from a recurring event.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
950 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
951 * @param integer $year The year of the execption.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
952 * @param integer $month The month of the execption.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
953 * @param integer $mday The day of the month of the completion.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
954 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
955 public function deleteCompletion($year, $month, $mday)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
956 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
957 $key = array_search(sprintf('%04d%02d%02d', $year, $month, $mday), $this->completions);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
958 if ($key !== false) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
959 unset($this->completions[$key]);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
960 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
961 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
962
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
963 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
964 * Checks if a completion exists for a given reccurence of an event.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
965 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
966 * @param integer $year The year of the reucrance.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
967 * @param integer $month The month of the recurrance.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
968 * @param integer $mday The day of the month of the recurrance.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
969 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
970 * @return boolean True if a completion exists for the given date.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
971 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
972 public function hasCompletion($year, $month, $mday)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
973 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
974 return in_array(sprintf('%04d%02d%02d', $year, $month, $mday),
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
975 $this->getCompletions());
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
976 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
977
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
978 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
979 * Retrieves all the completions for this event.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
980 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
981 * @return array Array containing the dates of all the completions in
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
982 * YYYYMMDD form.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
983 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
984 public function getCompletions()
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
985 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
986 return $this->completions;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
987 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
988
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
989 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
990 * Parses a vCalendar 1.0 recurrence rule.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
991 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
992 * @link http://www.imc.org/pdi/vcal-10.txt
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
993 * @link http://www.shuchow.com/vCalAddendum.html
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
994 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
995 * @param string $rrule A vCalendar 1.0 conform RRULE value.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
996 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
997 public function fromRRule10($rrule)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
998 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
999 $this->reset();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1000
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1001 if (!$rrule) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1002 return;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1003 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1004
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1005 if (!preg_match('/([A-Z]+)(\d+)?(.*)/', $rrule, $matches)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1006 // No recurrence data - event does not recur.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1007 $this->setRecurType(self::RECUR_NONE);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1008 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1009
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1010 // Always default the recurInterval to 1.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1011 $this->setRecurInterval(!empty($matches[2]) ? $matches[2] : 1);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1012
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1013 $remainder = trim($matches[3]);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1014
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1015 switch ($matches[1]) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1016 case 'D':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1017 $this->setRecurType(self::RECUR_DAILY);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1018 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1019
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1020 case 'W':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1021 $this->setRecurType(self::RECUR_WEEKLY);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1022 if (!empty($remainder)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1023 $mask = 0;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1024 while (preg_match('/^ ?[A-Z]{2} ?/', $remainder, $matches)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1025 $day = trim($matches[0]);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1026 $remainder = substr($remainder, strlen($matches[0]));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1027 $mask |= $maskdays[$day];
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1028 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1029 $this->setRecurOnDay($mask);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1030 } else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1031 // Recur on the day of the week of the original recurrence.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1032 $maskdays = array(
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1033 Horde_Date::DATE_SUNDAY => Horde_Date::MASK_SUNDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1034 Horde_Date::DATE_MONDAY => Horde_Date::MASK_MONDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1035 Horde_Date::DATE_TUESDAY => Horde_Date::MASK_TUESDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1036 Horde_Date::DATE_WEDNESDAY => Horde_Date::MASK_WEDNESDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1037 Horde_Date::DATE_THURSDAY => Horde_Date::MASK_THURSDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1038 Horde_Date::DATE_FRIDAY => Horde_Date::MASK_FRIDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1039 Horde_Date::DATE_SATURDAY => Horde_Date::MASK_SATURDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1040 );
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1041 $this->setRecurOnDay($maskdays[$this->start->dayOfWeek()]);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1042 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1043 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1044
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1045 case 'MP':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1046 $this->setRecurType(self::RECUR_MONTHLY_WEEKDAY);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1047 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1048
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1049 case 'MD':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1050 $this->setRecurType(self::RECUR_MONTHLY_DATE);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1051 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1052
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1053 case 'YM':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1054 $this->setRecurType(self::RECUR_YEARLY_DATE);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1055 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1056
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1057 case 'YD':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1058 $this->setRecurType(self::RECUR_YEARLY_DAY);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1059 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1060 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1061
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1062 // We don't support modifiers at the moment, strip them.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1063 while ($remainder && !preg_match('/^(#\d+|\d{8})($| |T\d{6})/', $remainder)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1064 $remainder = substr($remainder, 1);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1065 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1066 if (!empty($remainder)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1067 if (strpos($remainder, '#') === 0) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1068 $this->setRecurCount(substr($remainder, 1));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1069 } else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1070 list($year, $month, $mday, $hour, $min, $sec, $tz) =
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1071 sscanf($remainder, '%04d%02d%02dT%02d%02d%02d%s');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1072 $this->setRecurEnd(new Horde_Date(array('year' => $year,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1073 'month' => $month,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1074 'mday' => $mday,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1075 'hour' => $hour,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1076 'min' => $min,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1077 'sec' => $sec),
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1078 $tz == 'Z' ? 'UTC' : $this->start->timezone));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1079 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1080 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1081 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1082
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1083 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1084 * Creates a vCalendar 1.0 recurrence rule.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1085 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1086 * @link http://www.imc.org/pdi/vcal-10.txt
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1087 * @link http://www.shuchow.com/vCalAddendum.html
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1088 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1089 * @param Horde_Icalendar $calendar A Horde_Icalendar object instance.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1090 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1091 * @return string A vCalendar 1.0 conform RRULE value.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1092 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1093 public function toRRule10($calendar)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1094 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1095 switch ($this->recurType) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1096 case self::RECUR_NONE:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1097 return '';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1098
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1099 case self::RECUR_DAILY:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1100 $rrule = 'D' . $this->recurInterval;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1101 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1102
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1103 case self::RECUR_WEEKLY:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1104 $rrule = 'W' . $this->recurInterval;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1105 $vcaldays = array('SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1106
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1107 for ($i = 0; $i <= 7; ++$i) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1108 if ($this->recurOnDay(pow(2, $i))) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1109 $rrule .= ' ' . $vcaldays[$i];
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1110 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1111 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1112 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1113
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1114 case self::RECUR_MONTHLY_DATE:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1115 $rrule = 'MD' . $this->recurInterval . ' ' . trim($this->start->mday);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1116 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1117
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1118 case self::RECUR_MONTHLY_WEEKDAY:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1119 $nth_weekday = (int)($this->start->mday / 7);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1120 if (($this->start->mday % 7) > 0) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1121 $nth_weekday++;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1122 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1123
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1124 $vcaldays = array('SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1125 $rrule = 'MP' . $this->recurInterval . ' ' . $nth_weekday . '+ ' . $vcaldays[$this->start->dayOfWeek()];
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1126
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1127 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1128
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1129 case self::RECUR_YEARLY_DATE:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1130 $rrule = 'YM' . $this->recurInterval . ' ' . trim($this->start->month);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1131 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1132
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1133 case self::RECUR_YEARLY_DAY:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1134 $rrule = 'YD' . $this->recurInterval . ' ' . $this->start->dayOfYear();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1135 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1136
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1137 default:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1138 return '';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1139 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1140
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1141 if ($this->hasRecurEnd()) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1142 $recurEnd = clone $this->recurEnd;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1143 return $rrule . ' ' . $calendar->_exportDateTime($recurEnd);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1144 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1145
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1146 return $rrule . ' #' . (int)$this->getRecurCount();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1147 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1148
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1149 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1150 * Parses an iCalendar 2.0 recurrence rule.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1151 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1152 * @link http://rfc.net/rfc2445.html#s4.3.10
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1153 * @link http://rfc.net/rfc2445.html#s4.8.5
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1154 * @link http://www.shuchow.com/vCalAddendum.html
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1155 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1156 * @param string $rrule An iCalendar 2.0 conform RRULE value.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1157 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1158 public function fromRRule20($rrule)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1159 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1160 $this->reset();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1161
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1162 // Parse the recurrence rule into keys and values.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1163 $rdata = array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1164 $parts = explode(';', $rrule);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1165 foreach ($parts as $part) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1166 list($key, $value) = explode('=', $part, 2);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1167 $rdata[strtoupper($key)] = $value;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1168 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1169
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1170 if (isset($rdata['FREQ'])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1171 // Always default the recurInterval to 1.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1172 $this->setRecurInterval(isset($rdata['INTERVAL']) ? $rdata['INTERVAL'] : 1);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1173
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1174 $maskdays = array(
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1175 'SU' => Horde_Date::MASK_SUNDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1176 'MO' => Horde_Date::MASK_MONDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1177 'TU' => Horde_Date::MASK_TUESDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1178 'WE' => Horde_Date::MASK_WEDNESDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1179 'TH' => Horde_Date::MASK_THURSDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1180 'FR' => Horde_Date::MASK_FRIDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1181 'SA' => Horde_Date::MASK_SATURDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1182 );
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1183
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1184 switch (strtoupper($rdata['FREQ'])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1185 case 'DAILY':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1186 $this->setRecurType(self::RECUR_DAILY);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1187 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1188
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1189 case 'WEEKLY':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1190 $this->setRecurType(self::RECUR_WEEKLY);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1191 if (isset($rdata['BYDAY'])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1192 $days = explode(',', $rdata['BYDAY']);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1193 $mask = 0;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1194 foreach ($days as $day) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1195 $mask |= $maskdays[$day];
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1196 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1197 $this->setRecurOnDay($mask);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1198 } else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1199 // Recur on the day of the week of the original
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1200 // recurrence.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1201 $maskdays = array(
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1202 Horde_Date::DATE_SUNDAY => Horde_Date::MASK_SUNDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1203 Horde_Date::DATE_MONDAY => Horde_Date::MASK_MONDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1204 Horde_Date::DATE_TUESDAY => Horde_Date::MASK_TUESDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1205 Horde_Date::DATE_WEDNESDAY => Horde_Date::MASK_WEDNESDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1206 Horde_Date::DATE_THURSDAY => Horde_Date::MASK_THURSDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1207 Horde_Date::DATE_FRIDAY => Horde_Date::MASK_FRIDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1208 Horde_Date::DATE_SATURDAY => Horde_Date::MASK_SATURDAY);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1209 $this->setRecurOnDay($maskdays[$this->start->dayOfWeek()]);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1210 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1211 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1212
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1213 case 'MONTHLY':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1214 if (isset($rdata['BYDAY'])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1215 $this->setRecurType(self::RECUR_MONTHLY_WEEKDAY);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1216 if (preg_match('/(-?[1-4])([A-Z]+)/', $rdata['BYDAY'], $m)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1217 $this->setRecurOnDay($maskdays[$m[2]]);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1218 $this->setRecurNthWeekday($m[1]);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1219 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1220 } else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1221 $this->setRecurType(self::RECUR_MONTHLY_DATE);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1222 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1223 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1224
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1225 case 'YEARLY':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1226 if (isset($rdata['BYYEARDAY'])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1227 $this->setRecurType(self::RECUR_YEARLY_DAY);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1228 } elseif (isset($rdata['BYDAY'])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1229 $this->setRecurType(self::RECUR_YEARLY_WEEKDAY);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1230 if (preg_match('/(-?[1-4])([A-Z]+)/', $rdata['BYDAY'], $m)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1231 $this->setRecurOnDay($maskdays[$m[2]]);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1232 $this->setRecurNthWeekday($m[1]);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1233 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1234 if ($rdata['BYMONTH']) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1235 $months = explode(',', $rdata['BYMONTH']);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1236 $this->setRecurByMonth($months);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1237 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1238 } else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1239 $this->setRecurType(self::RECUR_YEARLY_DATE);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1240 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1241 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1242 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1243
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1244 // MUST take into account the time portion if it is present.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1245 // See Bug: 12869 and Bug: 2813
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1246 if (isset($rdata['UNTIL'])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1247 if (preg_match('/^(\d{4})-?(\d{2})-?(\d{2})T? ?(\d{2}):?(\d{2}):?(\d{2})(?:\.\d+)?(Z?)$/', $rdata['UNTIL'], $parts)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1248 $until = new Horde_Date($rdata['UNTIL'], 'UTC');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1249 $until->setTimezone($this->start->timezone);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1250 } else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1251 list($year, $month, $mday) = sscanf($rdata['UNTIL'],
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1252 '%04d%02d%02d');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1253 $until = new Horde_Date(
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1254 array('year' => $year,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1255 'month' => $month,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1256 'mday' => $mday + 1),
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1257 $this->start->timezone
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1258 );
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1259 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1260 $this->setRecurEnd($until);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1261 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1262 if (isset($rdata['COUNT'])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1263 $this->setRecurCount($rdata['COUNT']);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1264 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1265 } else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1266 // No recurrence data - event does not recur.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1267 $this->setRecurType(self::RECUR_NONE);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1268 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1269 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1270
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1271 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1272 * Creates an iCalendar 2.0 recurrence rule.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1273 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1274 * @link http://rfc.net/rfc2445.html#s4.3.10
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1275 * @link http://rfc.net/rfc2445.html#s4.8.5
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1276 * @link http://www.shuchow.com/vCalAddendum.html
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1277 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1278 * @param Horde_Icalendar $calendar A Horde_Icalendar object instance.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1279 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1280 * @return string An iCalendar 2.0 conform RRULE value.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1281 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1282 public function toRRule20($calendar)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1283 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1284 switch ($this->recurType) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1285 case self::RECUR_NONE:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1286 return '';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1287
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1288 case self::RECUR_DAILY:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1289 $rrule = 'FREQ=DAILY;INTERVAL=' . $this->recurInterval;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1290 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1291
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1292 case self::RECUR_WEEKLY:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1293 $rrule = 'FREQ=WEEKLY;INTERVAL=' . $this->recurInterval;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1294 $vcaldays = array('SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1295
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1296 for ($i = $flag = 0; $i <= 7; ++$i) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1297 if ($this->recurOnDay(pow(2, $i))) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1298 if ($flag == 0) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1299 $rrule .= ';BYDAY=';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1300 $flag = 1;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1301 } else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1302 $rrule .= ',';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1303 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1304 $rrule .= $vcaldays[$i];
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1305 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1306 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1307 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1308
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1309 case self::RECUR_MONTHLY_DATE:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1310 $rrule = 'FREQ=MONTHLY;INTERVAL=' . $this->recurInterval;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1311 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1312
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1313 case self::RECUR_MONTHLY_WEEKDAY:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1314 if (isset($this->recurNthDay)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1315 $nth_weekday = $this->recurNthDay;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1316 $day_of_week = log($this->recurData, 2);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1317 } else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1318 $day_of_week = $this->start->dayOfWeek();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1319 $nth_weekday = (int)($this->start->mday / 7);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1320 if (($this->start->mday % 7) > 0) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1321 $nth_weekday++;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1322 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1323 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1324 $vcaldays = array('SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1325 $rrule = 'FREQ=MONTHLY;INTERVAL=' . $this->recurInterval
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1326 . ';BYDAY=' . $nth_weekday . $vcaldays[$day_of_week];
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1327 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1328
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1329 case self::RECUR_YEARLY_DATE:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1330 $rrule = 'FREQ=YEARLY;INTERVAL=' . $this->recurInterval;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1331 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1332
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1333 case self::RECUR_YEARLY_DAY:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1334 $rrule = 'FREQ=YEARLY;INTERVAL=' . $this->recurInterval
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1335 . ';BYYEARDAY=' . $this->start->dayOfYear();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1336 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1337
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1338 case self::RECUR_YEARLY_WEEKDAY:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1339 if (isset($this->recurNthDay)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1340 $nth_weekday = $this->recurNthDay;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1341 $day_of_week = log($this->recurData, 2);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1342 } else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1343 $day_of_week = $this->start->dayOfWeek();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1344 $nth_weekday = (int)($this->start->mday / 7);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1345 if (($this->start->mday % 7) > 0) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1346 $nth_weekday++;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1347 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1348 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1349 $months = !empty($this->recurMonths) ? join(',', $this->recurMonths) : $this->start->month;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1350 $vcaldays = array('SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1351 $rrule = 'FREQ=YEARLY;INTERVAL=' . $this->recurInterval
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1352 . ';BYDAY='
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1353 . $nth_weekday
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1354 . $vcaldays[$day_of_week]
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1355 . ';BYMONTH=' . $this->start->month;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1356 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1357 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1358
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1359 if ($this->hasRecurEnd()) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1360 $recurEnd = clone $this->recurEnd;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1361 $rrule .= ';UNTIL=' . $calendar->_exportDateTime($recurEnd);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1362 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1363 if ($count = $this->getRecurCount()) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1364 $rrule .= ';COUNT=' . $count;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1365 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1366 return $rrule;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1367 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1368
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1369 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1370 * Parses the recurrence data from a Kolab hash.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1371 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1372 * @param array $hash The hash to convert.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1373 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1374 * @return boolean True if the hash seemed valid, false otherwise.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1375 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1376 public function fromKolab($hash)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1377 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1378 $this->reset();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1379
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1380 if (!isset($hash['interval']) || !isset($hash['cycle'])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1381 $this->setRecurType(self::RECUR_NONE);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1382 return false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1383 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1384
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1385 $this->setRecurInterval((int)$hash['interval']);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1386
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1387 $month2number = array(
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1388 'january' => 1,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1389 'february' => 2,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1390 'march' => 3,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1391 'april' => 4,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1392 'may' => 5,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1393 'june' => 6,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1394 'july' => 7,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1395 'august' => 8,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1396 'september' => 9,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1397 'october' => 10,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1398 'november' => 11,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1399 'december' => 12,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1400 );
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1401
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1402 $parse_day = false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1403 $set_daymask = false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1404 $update_month = false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1405 $update_daynumber = false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1406 $update_weekday = false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1407 $nth_weekday = -1;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1408
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1409 switch ($hash['cycle']) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1410 case 'daily':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1411 $this->setRecurType(self::RECUR_DAILY);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1412 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1413
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1414 case 'weekly':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1415 $this->setRecurType(self::RECUR_WEEKLY);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1416 $parse_day = true;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1417 $set_daymask = true;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1418 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1419
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1420 case 'monthly':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1421 if (!isset($hash['daynumber'])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1422 $this->setRecurType(self::RECUR_NONE);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1423 return false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1424 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1425
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1426 switch ($hash['type']) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1427 case 'daynumber':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1428 $this->setRecurType(self::RECUR_MONTHLY_DATE);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1429 $update_daynumber = true;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1430 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1431
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1432 case 'weekday':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1433 $this->setRecurType(self::RECUR_MONTHLY_WEEKDAY);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1434 $this->setRecurNthWeekday($hash['daynumber']);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1435 $parse_day = true;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1436 $set_daymask = true;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1437 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1438 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1439 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1440
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1441 case 'yearly':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1442 if (!isset($hash['type'])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1443 $this->setRecurType(self::RECUR_NONE);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1444 return false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1445 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1446
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1447 switch ($hash['type']) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1448 case 'monthday':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1449 $this->setRecurType(self::RECUR_YEARLY_DATE);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1450 $update_month = true;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1451 $update_daynumber = true;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1452 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1453
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1454 case 'yearday':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1455 if (!isset($hash['daynumber'])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1456 $this->setRecurType(self::RECUR_NONE);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1457 return false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1458 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1459
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1460 $this->setRecurType(self::RECUR_YEARLY_DAY);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1461 // Start counting days in January.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1462 $hash['month'] = 'january';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1463 $update_month = true;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1464 $update_daynumber = true;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1465 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1466
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1467 case 'weekday':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1468 if (!isset($hash['daynumber'])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1469 $this->setRecurType(self::RECUR_NONE);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1470 return false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1471 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1472
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1473 $this->setRecurType(self::RECUR_YEARLY_WEEKDAY);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1474 $this->setRecurNthWeekday($hash['daynumber']);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1475 $parse_day = true;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1476 $set_daymask = true;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1477
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1478 if ($hash['month'] && isset($month2number[$hash['month']])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1479 $this->setRecurByMonth($month2number[$hash['month']]);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1480 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1481 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1482 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1483 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1484
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1485 if (isset($hash['range-type']) && isset($hash['range'])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1486 switch ($hash['range-type']) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1487 case 'number':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1488 $this->setRecurCount((int)$hash['range']);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1489 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1490
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1491 case 'date':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1492 $recur_end = new Horde_Date($hash['range']);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1493 $recur_end->hour = 23;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1494 $recur_end->min = 59;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1495 $recur_end->sec = 59;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1496 $this->setRecurEnd($recur_end);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1497 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1498 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1499 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1500
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1501 // Need to parse <day>?
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1502 $last_found_day = -1;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1503 if ($parse_day) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1504 if (!isset($hash['day'])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1505 $this->setRecurType(self::RECUR_NONE);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1506 return false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1507 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1508
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1509 $mask = 0;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1510 $bits = array(
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1511 'monday' => Horde_Date::MASK_MONDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1512 'tuesday' => Horde_Date::MASK_TUESDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1513 'wednesday' => Horde_Date::MASK_WEDNESDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1514 'thursday' => Horde_Date::MASK_THURSDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1515 'friday' => Horde_Date::MASK_FRIDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1516 'saturday' => Horde_Date::MASK_SATURDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1517 'sunday' => Horde_Date::MASK_SUNDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1518 );
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1519 $days = array(
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1520 'monday' => Horde_Date::DATE_MONDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1521 'tuesday' => Horde_Date::DATE_TUESDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1522 'wednesday' => Horde_Date::DATE_WEDNESDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1523 'thursday' => Horde_Date::DATE_THURSDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1524 'friday' => Horde_Date::DATE_FRIDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1525 'saturday' => Horde_Date::DATE_SATURDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1526 'sunday' => Horde_Date::DATE_SUNDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1527 );
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1528
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1529 foreach ($hash['day'] as $day) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1530 // Validity check.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1531 if (empty($day) || !isset($bits[$day])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1532 continue;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1533 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1534
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1535 $mask |= $bits[$day];
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1536 $last_found_day = $days[$day];
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1537 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1538
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1539 if ($set_daymask) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1540 $this->setRecurOnDay($mask);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1541 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1542 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1543
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1544 if ($update_month || $update_daynumber || $update_weekday) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1545 if ($update_month) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1546 if (isset($month2number[$hash['month']])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1547 $this->start->month = $month2number[$hash['month']];
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1548 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1549 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1550
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1551 if ($update_daynumber) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1552 if (!isset($hash['daynumber'])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1553 $this->setRecurType(self::RECUR_NONE);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1554 return false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1555 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1556
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1557 $this->start->mday = $hash['daynumber'];
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1558 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1559
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1560 if ($update_weekday) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1561 $this->setNthWeekday($nth_weekday);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1562 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1563 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1564
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1565 // Exceptions.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1566 if (isset($hash['exclusion'])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1567 foreach ($hash['exclusion'] as $exception) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1568 if ($exception instanceof DateTime) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1569 $this->exceptions[] = $exception->format('Ymd');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1570 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1571 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1572 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1573
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1574 if (isset($hash['complete'])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1575 foreach ($hash['complete'] as $completion) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1576 if ($exception instanceof DateTime) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1577 $this->completions[] = $completion->format('Ymd');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1578 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1579 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1580 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1581
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1582 return true;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1583 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1584
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1585 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1586 * Export this object into a Kolab hash.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1587 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1588 * @return array The recurrence hash.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1589 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1590 public function toKolab()
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1591 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1592 if ($this->getRecurType() == self::RECUR_NONE) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1593 return array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1594 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1595
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1596 $day2number = array(
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1597 0 => 'sunday',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1598 1 => 'monday',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1599 2 => 'tuesday',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1600 3 => 'wednesday',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1601 4 => 'thursday',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1602 5 => 'friday',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1603 6 => 'saturday'
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1604 );
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1605 $month2number = array(
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1606 1 => 'january',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1607 2 => 'february',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1608 3 => 'march',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1609 4 => 'april',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1610 5 => 'may',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1611 6 => 'june',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1612 7 => 'july',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1613 8 => 'august',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1614 9 => 'september',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1615 10 => 'october',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1616 11 => 'november',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1617 12 => 'december'
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1618 );
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1619
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1620 $hash = array('interval' => $this->getRecurInterval());
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1621 $start = $this->getRecurStart();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1622
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1623 switch ($this->getRecurType()) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1624 case self::RECUR_DAILY:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1625 $hash['cycle'] = 'daily';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1626 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1627
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1628 case self::RECUR_WEEKLY:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1629 $hash['cycle'] = 'weekly';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1630 $bits = array(
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1631 'monday' => Horde_Date::MASK_MONDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1632 'tuesday' => Horde_Date::MASK_TUESDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1633 'wednesday' => Horde_Date::MASK_WEDNESDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1634 'thursday' => Horde_Date::MASK_THURSDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1635 'friday' => Horde_Date::MASK_FRIDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1636 'saturday' => Horde_Date::MASK_SATURDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1637 'sunday' => Horde_Date::MASK_SUNDAY,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1638 );
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1639 $days = array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1640 foreach ($bits as $name => $bit) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1641 if ($this->recurOnDay($bit)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1642 $days[] = $name;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1643 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1644 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1645 $hash['day'] = $days;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1646 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1647
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1648 case self::RECUR_MONTHLY_DATE:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1649 $hash['cycle'] = 'monthly';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1650 $hash['type'] = 'daynumber';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1651 $hash['daynumber'] = $start->mday;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1652 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1653
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1654 case self::RECUR_MONTHLY_WEEKDAY:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1655 $hash['cycle'] = 'monthly';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1656 $hash['type'] = 'weekday';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1657 $hash['daynumber'] = $start->weekOfMonth();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1658 $hash['day'] = array ($day2number[$start->dayOfWeek()]);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1659 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1660
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1661 case self::RECUR_YEARLY_DATE:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1662 $hash['cycle'] = 'yearly';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1663 $hash['type'] = 'monthday';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1664 $hash['daynumber'] = $start->mday;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1665 $hash['month'] = $month2number[$start->month];
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1666 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1667
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1668 case self::RECUR_YEARLY_DAY:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1669 $hash['cycle'] = 'yearly';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1670 $hash['type'] = 'yearday';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1671 $hash['daynumber'] = $start->dayOfYear();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1672 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1673
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1674 case self::RECUR_YEARLY_WEEKDAY:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1675 $hash['cycle'] = 'yearly';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1676 $hash['type'] = 'weekday';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1677 $hash['daynumber'] = $start->weekOfMonth();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1678 $hash['day'] = array ($day2number[$start->dayOfWeek()]);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1679 $hash['month'] = $month2number[$start->month];
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1680 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1681
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1682 if ($this->hasRecurCount()) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1683 $hash['range-type'] = 'number';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1684 $hash['range'] = $this->getRecurCount();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1685 } elseif ($this->hasRecurEnd()) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1686 $date = $this->getRecurEnd();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1687 $hash['range-type'] = 'date';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1688 $hash['range'] = $date->toDateTime();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1689 } else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1690 $hash['range-type'] = 'none';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1691 $hash['range'] = '';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1692 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1693
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1694 // Recurrence exceptions
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1695 $hash['exclusion'] = $hash['complete'] = array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1696 foreach ($this->exceptions as $exception) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1697 $hash['exclusion'][] = new DateTime($exception);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1698 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1699 foreach ($this->completions as $completionexception) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1700 $hash['complete'][] = new DateTime($completionexception);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1701 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1702
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1703 return $hash;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1704 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1705
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1706 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1707 * Returns a simple object suitable for json transport representing this
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1708 * object.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1709 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1710 * Possible properties are:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1711 * - t: type
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1712 * - i: interval
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1713 * - e: end date
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1714 * - c: count
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1715 * - d: data
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1716 * - co: completions
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1717 * - ex: exceptions
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1718 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1719 * @return object A simple object.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1720 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1721 public function toJson()
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1722 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1723 $json = new stdClass;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1724 $json->t = $this->recurType;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1725 $json->i = $this->recurInterval;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1726 if ($this->hasRecurEnd()) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1727 $json->e = $this->recurEnd->toJson();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1728 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1729 if ($this->recurCount) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1730 $json->c = $this->recurCount;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1731 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1732 if ($this->recurData) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1733 $json->d = $this->recurData;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1734 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1735 if ($this->completions) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1736 $json->co = $this->completions;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1737 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1738 if ($this->exceptions) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1739 $json->ex = $this->exceptions;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1740 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1741 return $json;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1742 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1743
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1744 }