1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
/**
|
3 |
|
|
* @file
|
4 |
|
|
* Code to compute the dates that match an iCal RRULE.
|
5 |
|
|
*
|
6 |
|
|
* Moved to a separate file since it is not used on most pages
|
7 |
|
|
* so the code is not parsed unless needed.
|
8 |
|
|
*
|
9 |
|
|
* Extensive simpletests have been created to test the RRULE calculation
|
10 |
|
|
* results against official examples from RFC 2445.
|
11 |
|
|
*
|
12 |
|
|
* These calculations are expensive and results should be stored or cached
|
13 |
|
|
* so the calculation code is not called more often than necessary.
|
14 |
|
|
*
|
15 |
|
|
* Currently implemented:
|
16 |
|
|
* INTERVAL, UNTIL, COUNT, EXDATE, RDATE, BYDAY, BYMONTHDAY, BYMONTH,
|
17 |
|
|
* YEARLY, MONTHLY, WEEKLY, DAILY
|
18 |
|
|
*
|
19 |
|
|
* Currently not implemented:
|
20 |
|
|
*
|
21 |
|
|
* BYYEARDAY, MINUTELY, HOURLY, SECONDLY, BYMINUTE, BYHOUR, BYSECOND
|
22 |
|
|
* These could be implemented in the future.
|
23 |
|
|
*
|
24 |
|
|
* BYSETPOS
|
25 |
|
|
* Seldom used anywhere, so no reason to complicated the code.
|
26 |
|
|
*/
|
27 |
|
|
|
28 |
|
|
/**
|
29 |
|
|
* Private implementation of date_repeat_calc().
|
30 |
|
|
*
|
31 |
|
|
* Compute dates that match the requested rule, within a specified date range.
|
32 |
|
|
*/
|
33 |
|
|
function _date_repeat_calc($rrule, $start, $end, $exceptions, $timezone, $additions) {
|
34 |
|
|
module_load_include('inc', 'date_api', 'date_api_ical');
|
35 |
|
|
|
36 |
|
|
if (empty($timezone)) {
|
37 |
|
|
$timezone = date_default_timezone();
|
38 |
|
|
}
|
39 |
|
|
|
40 |
|
|
// Make sure the 'EXCEPTIONS' string isn't appended to the rule.
|
41 |
|
|
$parts = explode("\n", $rrule);
|
42 |
|
|
if (count($parts)) {
|
43 |
|
|
$rrule = $parts[0];
|
44 |
|
|
}
|
45 |
|
|
// Get the parsed array of rule values.
|
46 |
|
|
$rrule = date_ical_parse_rrule('RRULE:', $rrule);
|
47 |
|
|
|
48 |
|
|
// These default values indicate there is no RRULE here.
|
49 |
|
|
if ($rrule['FREQ'] == 'NONE' || (isset($rrule['INTERVAL']) && $rrule['INTERVAL'] == 0)) {
|
50 |
|
|
return array();
|
51 |
|
|
}
|
52 |
|
|
|
53 |
|
|
// Create a date object for the start and end dates.
|
54 |
|
|
$start_date = new DateObject($start, $timezone);
|
55 |
|
|
|
56 |
|
|
// Versions of PHP greater than PHP 5.3.5 require that we set an explicit time when
|
57 |
|
|
// using date_modify() or the time may not match the original value. Adding this
|
58 |
|
|
// modifier gives us the same results in both older and newer versions of PHP.
|
59 |
|
|
$modify_time = ' ' . $start_date->format('g:ia');
|
60 |
|
|
|
61 |
|
|
// If the rule has an UNTIL, see if that is earlier than the end date.
|
62 |
|
|
if (!empty($rrule['UNTIL'])) {
|
63 |
|
|
$end_date = new DateObject($end, $timezone);
|
64 |
|
|
$until_date = date_ical_date($rrule['UNTIL'], $timezone);
|
65 |
|
|
if (date_format($until_date, 'U') < date_format($end_date, 'U')) {
|
66 |
|
|
$end_date = $until_date;
|
67 |
|
|
}
|
68 |
|
|
}
|
69 |
|
|
// The only valid option for an empty end date is when we have a count.
|
70 |
|
|
elseif (empty($end)) {
|
71 |
|
|
if (!empty($rrule['COUNT'])) {
|
72 |
|
|
$end_date = NULL;
|
73 |
|
|
}
|
74 |
|
|
else {
|
75 |
|
|
return array();
|
76 |
|
|
}
|
77 |
|
|
}
|
78 |
|
|
else {
|
79 |
|
|
$end_date = new DateObject($end, $timezone);
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
// Get an integer value for the interval, if none given, '1' is implied.
|
83 |
|
|
if (empty($rrule['INTERVAL'])) {
|
84 |
|
|
$rrule['INTERVAL'] = 1;
|
85 |
|
|
}
|
86 |
|
|
$interval = max(1, $rrule['INTERVAL']);
|
87 |
|
|
$count = isset($rrule['COUNT']) ? $rrule['COUNT'] : NULL;
|
88 |
|
|
|
89 |
|
|
if (empty($rrule['FREQ'])) {
|
90 |
|
|
$rrule['FREQ'] = 'DAILY';
|
91 |
|
|
}
|
92 |
|
|
|
93 |
|
|
// Make sure DAILY frequency isn't used in places it won't work;
|
94 |
|
|
if (!empty($rrule['BYMONTHDAY']) && !in_array($rrule['FREQ'], array('MONTHLY', 'YEARLY'))) {
|
95 |
|
|
$rrule['FREQ'] = 'MONTHLY';
|
96 |
|
|
}
|
97 |
|
|
elseif (!empty($rrule['BYDAY']) && !in_array($rrule['FREQ'], array('MONTHLY', 'WEEKLY', 'YEARLY'))) {
|
98 |
|
|
$rrule['FREQ'] = 'WEEKLY';
|
99 |
|
|
}
|
100 |
|
|
|
101 |
|
|
// Find the time period to jump forward between dates.
|
102 |
|
|
switch ($rrule['FREQ']) {
|
103 |
|
|
case 'DAILY':
|
104 |
|
|
$jump = $interval . ' days';
|
105 |
|
|
break;
|
106 |
|
|
case 'WEEKLY':
|
107 |
|
|
$jump = $interval . ' weeks';
|
108 |
|
|
break;
|
109 |
|
|
case 'MONTHLY':
|
110 |
|
|
$jump = $interval . ' months';
|
111 |
|
|
break;
|
112 |
|
|
case 'YEARLY':
|
113 |
|
|
$jump = $interval . ' years';
|
114 |
|
|
break;
|
115 |
|
|
}
|
116 |
|
|
$rrule = date_repeat_adjust_rrule($rrule, $start_date);
|
117 |
|
|
|
118 |
|
|
// The start date always goes into the results, whether or not it meets
|
119 |
|
|
// the rules. RFC 2445 includes examples where the start date DOES NOT
|
120 |
|
|
// meet the rules, but the expected results always include the start date.
|
121 |
|
|
$days = array(date_format($start_date, DATE_FORMAT_DATETIME));
|
122 |
|
|
|
123 |
|
|
// BYMONTHDAY will look for specific days of the month in one or more months.
|
124 |
|
|
// This process is only valid when frequency is monthly or yearly.
|
125 |
|
|
|
126 |
|
|
if (!empty($rrule['BYMONTHDAY'])) {
|
127 |
|
|
$finished = FALSE;
|
128 |
|
|
$current_day = clone($start_date);
|
129 |
|
|
$direction_days = array();
|
130 |
|
|
// Deconstruct the day in case it has a negative modifier.
|
131 |
|
|
foreach ($rrule['BYMONTHDAY'] as $day) {
|
132 |
|
|
preg_match("@(-)?([0-9]{1,2})@", $day, $regs);
|
133 |
|
|
if (!empty($regs[2])) {
|
134 |
|
|
// Convert parameters into full day name, count, and direction.
|
135 |
|
|
$direction_days[$day] = array(
|
136 |
|
|
'direction' => !empty($regs[1]) ? $regs[1] : '+',
|
137 |
|
|
'direction_count' => $regs[2],
|
138 |
|
|
);
|
139 |
|
|
}
|
140 |
|
|
}
|
141 |
|
|
while (!$finished) {
|
142 |
|
|
$period_finished = FALSE;
|
143 |
|
|
while (!$period_finished) {
|
144 |
|
|
foreach ($rrule['BYMONTHDAY'] as $monthday) {
|
145 |
|
|
$day = $direction_days[$monthday];
|
146 |
|
|
$current_day = date_repeat_set_month_day($current_day, NULL, $day['direction_count'], $day['direction'], $timezone, $modify_time);
|
147 |
|
|
date_repeat_add_dates($days, $current_day, $start_date, $end_date, $exceptions, $rrule);
|
148 |
|
|
if ($finished = date_repeat_is_finished($current_day, $days, $count, $end_date)) {
|
149 |
|
|
$period_finished = TRUE;
|
150 |
|
|
}
|
151 |
|
|
}
|
152 |
|
|
// If it's monthly, keep looping through months, one INTERVAL at a time.
|
153 |
|
|
if ($rrule['FREQ'] == 'MONTHLY') {
|
154 |
|
|
if ($finished = date_repeat_is_finished($current_day, $days, $count, $end_date)) {
|
155 |
|
|
$period_finished = TRUE;
|
156 |
|
|
}
|
157 |
|
|
// Back up to first of month and jump.
|
158 |
|
|
$current_day = date_repeat_set_month_day($current_day, NULL, 1, '+', $timezone, $modify_time);
|
159 |
|
|
date_modify($current_day, '+' . $jump . $modify_time);
|
160 |
|
|
}
|
161 |
|
|
// If it's yearly, break out of the loop at the
|
162 |
|
|
// end of every year, and jump one INTERVAL in years.
|
163 |
|
|
else {
|
164 |
|
|
if (date_format($current_day, 'n') == 12) {
|
165 |
|
|
$period_finished = TRUE;
|
166 |
|
|
}
|
167 |
|
|
else {
|
168 |
|
|
// Back up to first of month and jump.
|
169 |
|
|
$current_day = date_repeat_set_month_day($current_day, NULL, 1, '+', $timezone, $modify_time);
|
170 |
|
|
date_modify($current_day, '+1 month' . $modify_time);
|
171 |
|
|
}
|
172 |
|
|
}
|
173 |
|
|
}
|
174 |
|
|
if ($rrule['FREQ'] == 'YEARLY') {
|
175 |
|
|
// Back up to first of year and jump.
|
176 |
db9ffd17
|
Assos Assos
|
$current_day = date_repeat_set_year_day($current_day, NULL, NULL, 1, '+', $timezone, $modify_time);
|
177 |
85ad3d82
|
Assos Assos
|
date_modify($current_day, '+' . $jump . $modify_time);
|
178 |
|
|
}
|
179 |
|
|
$finished = date_repeat_is_finished($current_day, $days, $count, $end_date);
|
180 |
|
|
}
|
181 |
|
|
}
|
182 |
|
|
|
183 |
|
|
// This is the simple fallback case, not looking for any BYDAY,
|
184 |
|
|
// just repeating the start date. Because of imputed BYDAY above, this
|
185 |
|
|
// will only test TRUE for a DAILY or less frequency (like HOURLY).
|
186 |
|
|
|
187 |
|
|
elseif (empty($rrule['BYDAY'])) {
|
188 |
|
|
// $current_day will keep track of where we are in the calculation.
|
189 |
|
|
$current_day = clone($start_date);
|
190 |
|
|
$finished = FALSE;
|
191 |
|
|
$months = !empty($rrule['BYMONTH']) ? $rrule['BYMONTH'] : array();
|
192 |
|
|
while (!$finished) {
|
193 |
|
|
date_repeat_add_dates($days, $current_day, $start_date, $end_date, $exceptions, $rrule);
|
194 |
|
|
$finished = date_repeat_is_finished($current_day, $days, $count, $end_date);
|
195 |
|
|
date_modify($current_day, '+' . $jump . $modify_time);
|
196 |
|
|
}
|
197 |
|
|
}
|
198 |
|
|
|
199 |
|
|
else {
|
200 |
|
|
|
201 |
|
|
// More complex searches for day names and criteria like '-1SU' or '2TU,2TH',
|
202 |
|
|
// require that we interate through the whole time period checking each BYDAY.
|
203 |
|
|
|
204 |
|
|
// Create helper array to pull day names out of iCal day strings.
|
205 |
|
|
$day_names = date_repeat_dow_day_options(FALSE);
|
206 |
|
|
$days_of_week = array_keys($day_names);
|
207 |
|
|
|
208 |
|
|
// Parse out information about the BYDAYs and separate them
|
209 |
|
|
// depending on whether they have directional parameters like -1SU or 2TH.
|
210 |
|
|
$month_days = array();
|
211 |
|
|
$week_days = array();
|
212 |
|
|
|
213 |
|
|
// Find the right first day of the week to use, iCal rules say Monday
|
214 |
|
|
// should be used if none is specified.
|
215 |
|
|
$week_start_rule = !empty($rrule['WKST']) ? trim($rrule['WKST']) : 'MO';
|
216 |
|
|
$week_start_day = $day_names[$week_start_rule];
|
217 |
|
|
|
218 |
|
|
// Make sure the week days array is sorted into week order,
|
219 |
|
|
// we use the $ordered_keys to get the right values into the key
|
220 |
|
|
// and force the array to that order. Needed later when we
|
221 |
|
|
// iterate through each week looking for days so we don't
|
222 |
|
|
// jump to the next week when we hit a day out of order.
|
223 |
|
|
$ordered = date_repeat_days_ordered($week_start_rule);
|
224 |
|
|
$ordered_keys = array_flip($ordered);
|
225 |
|
|
|
226 |
db9ffd17
|
Assos Assos
|
if ($rrule['FREQ'] == 'YEARLY' && !empty($rrule['BYMONTH'])) {
|
227 |
|
|
// Additional cycle to apply month preferences.
|
228 |
|
|
foreach ($rrule['BYMONTH'] as $month) {
|
229 |
|
|
foreach ($rrule['BYDAY'] as $day) {
|
230 |
|
|
preg_match("@(-)?([0-9]+)?([SU|MO|TU|WE|TH|FR|SA]{2})@", trim($day), $regs);
|
231 |
|
|
// Convert parameters into full day name, count, and direction.
|
232 |
|
|
// Add leading zero to first 9 months.
|
233 |
|
|
if (!empty($regs[2])) {
|
234 |
|
|
$direction_days[] = array(
|
235 |
|
|
'day' => $day_names[$regs[3]],
|
236 |
|
|
'direction' => !empty($regs[1]) ? $regs[1] : '+',
|
237 |
|
|
'direction_count' => $regs[2],
|
238 |
|
|
'month' => strlen($month) > 1 ? $month : '0' . $month,
|
239 |
|
|
);
|
240 |
|
|
}
|
241 |
|
|
else {
|
242 |
|
|
$week_days[$ordered_keys[$regs[3]]] = $day_names[$regs[3]];
|
243 |
|
|
}
|
244 |
|
|
}
|
245 |
85ad3d82
|
Assos Assos
|
}
|
246 |
db9ffd17
|
Assos Assos
|
}
|
247 |
|
|
else {
|
248 |
|
|
foreach ($rrule['BYDAY'] as $day) {
|
249 |
|
|
preg_match("@(-)?([0-9]+)?([SU|MO|TU|WE|TH|FR|SA]{2})@", trim($day), $regs);
|
250 |
|
|
if (!empty($regs[2])) {
|
251 |
|
|
// Convert parameters into full day name, count, and direction.
|
252 |
|
|
$direction_days[] = array(
|
253 |
|
|
'day' => $day_names[$regs[3]],
|
254 |
|
|
'direction' => !empty($regs[1]) ? $regs[1] : '+',
|
255 |
|
|
'direction_count' => $regs[2],
|
256 |
|
|
'month' => NULL,
|
257 |
|
|
);
|
258 |
|
|
}
|
259 |
|
|
else {
|
260 |
|
|
$week_days[$ordered_keys[$regs[3]]] = $day_names[$regs[3]];
|
261 |
|
|
}
|
262 |
85ad3d82
|
Assos Assos
|
}
|
263 |
|
|
}
|
264 |
|
|
ksort($week_days);
|
265 |
|
|
|
266 |
|
|
// BYDAYs with parameters like -1SU (last Sun) or 2TH (second Thur)
|
267 |
|
|
// need to be processed one month or year at a time.
|
268 |
|
|
if (!empty($direction_days) && in_array($rrule['FREQ'], array('MONTHLY', 'YEARLY'))) {
|
269 |
|
|
$finished = FALSE;
|
270 |
|
|
$current_day = clone($start_date);
|
271 |
|
|
while (!$finished) {
|
272 |
|
|
foreach ($direction_days as $day) {
|
273 |
|
|
// Find the BYDAY date in the current month.
|
274 |
|
|
if ($rrule['FREQ'] == 'MONTHLY') {
|
275 |
|
|
$current_day = date_repeat_set_month_day($current_day, $day['day'], $day['direction_count'], $day['direction'], $timezone, $modify_time);
|
276 |
|
|
}
|
277 |
|
|
else {
|
278 |
db9ffd17
|
Assos Assos
|
$current_day = date_repeat_set_year_day($current_day, $day['month'], $day['day'], $day['direction_count'], $day['direction'], $timezone, $modify_time);
|
279 |
85ad3d82
|
Assos Assos
|
}
|
280 |
|
|
date_repeat_add_dates($days, $current_day, $start_date, $end_date, $exceptions, $rrule);
|
281 |
|
|
}
|
282 |
|
|
$finished = date_repeat_is_finished($current_day, $days, $count, $end_date);
|
283 |
|
|
// Reset to beginning of period before jumping to next period.
|
284 |
|
|
// Needed especially when working with values like 'last Saturday'
|
285 |
|
|
// to be sure we don't skip months like February.
|
286 |
|
|
$year = date_format($current_day, 'Y');
|
287 |
|
|
$month = date_format($current_day, 'n');
|
288 |
|
|
if ($rrule['FREQ'] == 'MONTHLY') {
|
289 |
|
|
date_date_set($current_day, $year, $month, 1);
|
290 |
|
|
}
|
291 |
|
|
else {
|
292 |
|
|
date_date_set($current_day, $year, 1, 1);
|
293 |
|
|
}
|
294 |
|
|
// Jump to the next period.
|
295 |
|
|
date_modify($current_day, '+' . $jump . $modify_time);
|
296 |
|
|
}
|
297 |
|
|
}
|
298 |
|
|
|
299 |
|
|
// For BYDAYs without parameters,like TU,TH (every Tues and Thur),
|
300 |
|
|
// we look for every one of those days during the frequency period.
|
301 |
|
|
// Iterate through periods of a WEEK, MONTH, or YEAR, checking for
|
302 |
|
|
// the days of the week that match our criteria for each week in the
|
303 |
|
|
// period, then jumping ahead to the next week, month, or year,
|
304 |
|
|
// an INTERVAL at a time.
|
305 |
|
|
|
306 |
|
|
if (!empty($week_days) && in_array($rrule['FREQ'], array('MONTHLY', 'WEEKLY', 'YEARLY'))) {
|
307 |
|
|
$finished = FALSE;
|
308 |
|
|
$current_day = clone($start_date);
|
309 |
|
|
$format = $rrule['FREQ'] == 'YEARLY' ? 'Y' : 'n';
|
310 |
|
|
$current_period = date_format($current_day, $format);
|
311 |
|
|
|
312 |
|
|
// Back up to the beginning of the week in case we are somewhere in the
|
313 |
|
|
// middle of the possible week days, needed so we don't prematurely
|
314 |
|
|
// jump to the next week. The date_repeat_add_dates() function will
|
315 |
|
|
// keep dates outside the range from getting added.
|
316 |
|
|
if (date_format($current_day, 'l') != $day_names[$day]) {
|
317 |
|
|
date_modify($current_day, '-1 ' . $week_start_day . $modify_time);
|
318 |
|
|
}
|
319 |
|
|
while (!$finished) {
|
320 |
|
|
$period_finished = FALSE;
|
321 |
|
|
while (!$period_finished) {
|
322 |
|
|
$moved = FALSE;
|
323 |
|
|
foreach ($week_days as $delta => $day) {
|
324 |
|
|
// Find the next occurence of each day in this week, only add it
|
325 |
|
|
// if we are still in the current month or year. The date_repeat_add_dates
|
326 |
|
|
// function is insufficient to test whether to include this date
|
327 |
|
|
// if we are using a rule like 'every other month', so we must
|
328 |
|
|
// explicitly test it here.
|
329 |
|
|
|
330 |
|
|
// If we're already on the right day, don't jump or we
|
331 |
|
|
// will prematurely move into the next week.
|
332 |
|
|
if (date_format($current_day, 'l') != $day) {
|
333 |
|
|
date_modify($current_day, '+1 ' . $day . $modify_time);
|
334 |
|
|
$moved = TRUE;
|
335 |
|
|
}
|
336 |
|
|
if ($rrule['FREQ'] == 'WEEKLY' || date_format($current_day, $format) == $current_period) {
|
337 |
|
|
date_repeat_add_dates($days, $current_day, $start_date, $end_date, $exceptions, $rrule);
|
338 |
|
|
}
|
339 |
|
|
}
|
340 |
|
|
$finished = date_repeat_is_finished($current_day, $days, $count, $end_date);
|
341 |
|
|
|
342 |
|
|
// Make sure we don't get stuck in endless loop if the current
|
343 |
|
|
// day never got changed above.
|
344 |
|
|
if (!$moved) {
|
345 |
|
|
date_modify($current_day, '+1 day' . $modify_time);
|
346 |
|
|
}
|
347 |
|
|
|
348 |
|
|
// If this is a WEEKLY frequency, stop after each week,
|
349 |
|
|
// otherwise, stop when we've moved outside the current period.
|
350 |
|
|
// Jump to the end of the week, then test the period.
|
351 |
|
|
if ($finished || $rrule['FREQ'] == 'WEEKLY') {
|
352 |
|
|
$period_finished = TRUE;
|
353 |
|
|
}
|
354 |
|
|
elseif ($rrule['FREQ'] != 'WEEKLY' && date_format($current_day, $format) != $current_period) {
|
355 |
|
|
$period_finished = TRUE;
|
356 |
|
|
}
|
357 |
|
|
}
|
358 |
|
|
|
359 |
|
|
if ($finished) {
|
360 |
|
|
continue;
|
361 |
|
|
}
|
362 |
|
|
|
363 |
|
|
// We'll be at the end of a week, month, or year when
|
364 |
|
|
// we get to this point in the code.
|
365 |
|
|
|
366 |
|
|
// Go back to the beginning of this period before we jump, to
|
367 |
|
|
// ensure we jump to the first day of the next period.
|
368 |
|
|
switch ($rrule['FREQ']) {
|
369 |
|
|
case 'WEEKLY':
|
370 |
|
|
date_modify($current_day, '+1 ' . $week_start_day . $modify_time);
|
371 |
|
|
date_modify($current_day, '-1 week' . $modify_time);
|
372 |
|
|
break;
|
373 |
|
|
case 'MONTHLY':
|
374 |
|
|
date_modify($current_day, '-' . (date_format($current_day, 'j') - 1) . ' days' . $modify_time);
|
375 |
|
|
date_modify($current_day, '-1 month' . $modify_time);
|
376 |
|
|
break;
|
377 |
|
|
case 'YEARLY':
|
378 |
|
|
date_modify($current_day, '-' . date_format($current_day, 'z') . ' days' . $modify_time);
|
379 |
|
|
date_modify($current_day, '-1 year' . $modify_time);
|
380 |
|
|
break;
|
381 |
|
|
}
|
382 |
|
|
// Jump ahead to the next period to be evaluated.
|
383 |
|
|
date_modify($current_day, '+' . $jump . $modify_time);
|
384 |
|
|
$current_period = date_format($current_day, $format);
|
385 |
|
|
$finished = date_repeat_is_finished($current_day, $days, $count, $end_date);
|
386 |
|
|
}
|
387 |
|
|
}
|
388 |
|
|
}
|
389 |
|
|
|
390 |
|
|
// add additional dates
|
391 |
|
|
foreach ($additions as $addition) {
|
392 |
|
|
$date = new dateObject($addition . ' ' . $start_date->format('H:i:s'), $timezone);
|
393 |
|
|
$days[] = date_format($date, DATE_FORMAT_DATETIME);
|
394 |
|
|
}
|
395 |
|
|
|
396 |
|
|
sort($days);
|
397 |
|
|
return $days;
|
398 |
|
|
}
|
399 |
|
|
|
400 |
|
|
/**
|
401 |
|
|
* See if the RRULE needs some imputed values added to it.
|
402 |
|
|
*/
|
403 |
|
|
function date_repeat_adjust_rrule($rrule, $start_date) {
|
404 |
|
|
// If this is not a valid value, do nothing;
|
405 |
|
|
if (empty($rrule) || empty($rrule['FREQ'])) {
|
406 |
|
|
return array();
|
407 |
|
|
}
|
408 |
|
|
|
409 |
|
|
// RFC 2445 says if no day or monthday is specified when creating repeats for
|
410 |
|
|
// weeks, months, or years, impute the value from the start date.
|
411 |
|
|
|
412 |
|
|
if (empty($rrule['BYDAY']) && $rrule['FREQ'] == 'WEEKLY') {
|
413 |
|
|
$rrule['BYDAY'] = array(date_repeat_dow2day(date_format($start_date, 'w')));
|
414 |
|
|
}
|
415 |
|
|
elseif (empty($rrule['BYDAY']) && empty($rrule['BYMONTHDAY']) && $rrule['FREQ'] == 'MONTHLY') {
|
416 |
|
|
$rrule['BYMONTHDAY'] = array(date_format($start_date, 'j'));
|
417 |
|
|
}
|
418 |
|
|
elseif (empty($rrule['BYDAY']) && empty($rrule['BYMONTHDAY']) && empty($rrule['BYYEARDAY']) && $rrule['FREQ'] == 'YEARLY') {
|
419 |
|
|
$rrule['BYMONTHDAY'] = array(date_format($start_date, 'j'));
|
420 |
|
|
if (empty($rrule['BYMONTH'])) {
|
421 |
|
|
$rrule['BYMONTH'] = array(date_format($start_date, 'n'));
|
422 |
|
|
}
|
423 |
|
|
}
|
424 |
|
|
// If we are processing rules for period other than YEARLY or MONTHLY
|
425 |
|
|
// and have BYDAYS like 2SU or -1SA, simplify them to SU or SA since the
|
426 |
|
|
// position rules make no sense in other periods and just add complexity.
|
427 |
|
|
|
428 |
|
|
elseif (!empty($rrule['BYDAY']) && !in_array($rrule['FREQ'], array('MONTHLY', 'YEARLY'))) {
|
429 |
|
|
foreach ($rrule['BYDAY'] as $delta => $BYDAY) {
|
430 |
|
|
$rrule['BYDAY'][$delta] = substr($BYDAY, -2);
|
431 |
|
|
}
|
432 |
|
|
}
|
433 |
|
|
|
434 |
|
|
return $rrule;
|
435 |
|
|
}
|
436 |
|
|
|
437 |
|
|
/**
|
438 |
|
|
* Helper function to add found date to the $dates array.
|
439 |
|
|
*
|
440 |
|
|
* Check that the date to be added is between the start and end date
|
441 |
|
|
* and that it is not in the $exceptions, nor already in the $days array,
|
442 |
|
|
* and that it meets other criteria in the RRULE.
|
443 |
|
|
*/
|
444 |
|
|
function date_repeat_add_dates(&$days, $current_day, $start_date, $end_date, $exceptions, $rrule) {
|
445 |
|
|
if (isset($rrule['COUNT']) && sizeof($days) >= $rrule['COUNT']) {
|
446 |
|
|
return FALSE;
|
447 |
|
|
}
|
448 |
|
|
$formatted = date_format($current_day, DATE_FORMAT_DATETIME);
|
449 |
|
|
if (!empty($end_date) && $formatted > date_format($end_date, DATE_FORMAT_DATETIME)) {
|
450 |
|
|
return FALSE;
|
451 |
|
|
}
|
452 |
|
|
if ($formatted < date_format($start_date, DATE_FORMAT_DATETIME)) {
|
453 |
|
|
return FALSE;
|
454 |
|
|
}
|
455 |
|
|
if (in_array(date_format($current_day, 'Y-m-d'), $exceptions)) {
|
456 |
|
|
return FALSE;
|
457 |
|
|
}
|
458 |
|
|
if (!empty($rrule['BYDAY'])) {
|
459 |
|
|
$BYDAYS = $rrule['BYDAY'];
|
460 |
|
|
foreach ($BYDAYS as $delta => $BYDAY) {
|
461 |
|
|
$BYDAYS[$delta] = substr($BYDAY, -2);
|
462 |
|
|
}
|
463 |
|
|
if (!in_array(date_repeat_dow2day(date_format($current_day, 'w')), $BYDAYS)) {
|
464 |
|
|
return FALSE;
|
465 |
|
|
}}
|
466 |
|
|
if (!empty($rrule['BYYEAR']) && !in_array(date_format($current_day, 'Y'), $rrule['BYYEAR'])) {
|
467 |
|
|
return FALSE;
|
468 |
|
|
}
|
469 |
|
|
if (!empty($rrule['BYMONTH']) && !in_array(date_format($current_day, 'n'), $rrule['BYMONTH'])) {
|
470 |
|
|
return FALSE;
|
471 |
|
|
}
|
472 |
|
|
if (!empty($rrule['BYMONTHDAY'])) {
|
473 |
|
|
// Test month days, but only if there are no negative numbers.
|
474 |
|
|
$test = TRUE;
|
475 |
|
|
$BYMONTHDAYS = array();
|
476 |
|
|
foreach ($rrule['BYMONTHDAY'] as $day) {
|
477 |
|
|
if ($day > 0) {
|
478 |
|
|
$BYMONTHDAYS[] = $day;
|
479 |
|
|
}
|
480 |
|
|
else {
|
481 |
|
|
$test = FALSE;
|
482 |
|
|
break;
|
483 |
|
|
}
|
484 |
|
|
}
|
485 |
|
|
if ($test && !empty($BYMONTHDAYS) && !in_array(date_format($current_day, 'j'), $BYMONTHDAYS)) {
|
486 |
|
|
return FALSE;
|
487 |
|
|
}
|
488 |
|
|
}
|
489 |
|
|
// Don't add a day if it is already saved so we don't throw the count off.
|
490 |
|
|
if (in_array($formatted, $days)) {
|
491 |
|
|
return TRUE;
|
492 |
|
|
}
|
493 |
|
|
else {
|
494 |
|
|
$days[] = $formatted;
|
495 |
|
|
}
|
496 |
|
|
}
|
497 |
|
|
|
498 |
|
|
/**
|
499 |
|
|
* Stop when $current_day is greater than $end_date or $count is reached.
|
500 |
|
|
*/
|
501 |
|
|
function date_repeat_is_finished($current_day, $days, $count, $end_date) {
|
502 |
|
|
if (($count && sizeof($days) >= $count)
|
503 |
|
|
|| (!empty($end_date) && date_format($current_day, 'U') > date_format($end_date, 'U'))) {
|
504 |
|
|
return TRUE;
|
505 |
|
|
}
|
506 |
|
|
else {
|
507 |
|
|
return FALSE;
|
508 |
|
|
}
|
509 |
|
|
}
|
510 |
|
|
|
511 |
|
|
/**
|
512 |
|
|
* Set a date object to a specific day of the month.
|
513 |
|
|
*
|
514 |
|
|
* Example,
|
515 |
|
|
* date_set_month_day($date, 'Sunday', 2, '-')
|
516 |
|
|
* will reset $date to the second to last Sunday in the month.
|
517 |
|
|
* If $day is empty, will set to the number of days from the
|
518 |
|
|
* beginning or end of the month.
|
519 |
|
|
*/
|
520 |
|
|
function date_repeat_set_month_day($date_in, $day, $count = 1, $direction = '+', $timezone = 'UTC', $modify_time) {
|
521 |
|
|
if (is_object($date_in)) {
|
522 |
|
|
$current_month = date_format($date_in, 'n');
|
523 |
|
|
|
524 |
|
|
// Reset to the start of the month.
|
525 |
|
|
// We should be able to do this with date_date_set(), but
|
526 |
|
|
// for some reason the date occasionally gets confused if run
|
527 |
|
|
// through this function multiple times. It seems to work
|
528 |
|
|
// reliably if we create a new object each time.
|
529 |
|
|
$datetime = date_format($date_in, DATE_FORMAT_DATETIME);
|
530 |
|
|
$datetime = substr_replace($datetime, '01', 8, 2);
|
531 |
|
|
$date = new DateObject($datetime, $timezone);
|
532 |
|
|
if ($direction == '-') {
|
533 |
|
|
// For negative search, start from the end of the month.
|
534 |
|
|
date_modify($date, '+1 month' . $modify_time);
|
535 |
|
|
}
|
536 |
|
|
else {
|
537 |
|
|
// For positive search, back up one day to get outside the
|
538 |
|
|
// current month, so we can catch the first of the month.
|
539 |
|
|
date_modify($date, '-1 day' . $modify_time);
|
540 |
|
|
}
|
541 |
|
|
|
542 |
|
|
if (empty($day)) {
|
543 |
|
|
date_modify($date, $direction . $count . ' days' . $modify_time);
|
544 |
|
|
}
|
545 |
|
|
else {
|
546 |
|
|
// Use the English text for order, like First Sunday
|
547 |
|
|
// instead of +1 Sunday to overcome PHP5 bug, (see #369020).
|
548 |
|
|
$order = date_order();
|
549 |
|
|
$step = $count <= 5 ? $order[$direction . $count] : $count;
|
550 |
|
|
date_modify($date, $step . ' ' . $day . $modify_time);
|
551 |
|
|
}
|
552 |
|
|
|
553 |
|
|
// If that takes us outside the current month, don't go there.
|
554 |
|
|
if (date_format($date, 'n') == $current_month) {
|
555 |
|
|
return $date;
|
556 |
|
|
}
|
557 |
|
|
}
|
558 |
|
|
return $date_in;
|
559 |
|
|
}
|
560 |
|
|
|
561 |
|
|
/**
|
562 |
|
|
* Set a date object to a specific day of the year.
|
563 |
|
|
*
|
564 |
|
|
* Example,
|
565 |
|
|
* date_set_year_day($date, 'Sunday', 2, '-')
|
566 |
|
|
* will reset $date to the second to last Sunday in the year.
|
567 |
|
|
* If $day is empty, will set to the number of days from the
|
568 |
|
|
* beginning or end of the year.
|
569 |
|
|
*/
|
570 |
db9ffd17
|
Assos Assos
|
function date_repeat_set_year_day($date_in, $month, $day, $count = 1, $direction = '+', $timezone = 'UTC', $modify_time) {
|
571 |
85ad3d82
|
Assos Assos
|
if (is_object($date_in)) {
|
572 |
|
|
$current_year = date_format($date_in, 'Y');
|
573 |
|
|
|
574 |
|
|
// Reset to the start of the month.
|
575 |
|
|
// See note above.
|
576 |
|
|
$datetime = date_format($date_in, DATE_FORMAT_DATETIME);
|
577 |
db9ffd17
|
Assos Assos
|
$month_key = isset($month) ? $month : '01';
|
578 |
|
|
$datetime = substr_replace($datetime, $month_key . '-01', 5, 5);
|
579 |
85ad3d82
|
Assos Assos
|
$date = new DateObject($datetime, $timezone);
|
580 |
db9ffd17
|
Assos Assos
|
|
581 |
|
|
if (isset($month)) {
|
582 |
|
|
if ($direction == '-') {
|
583 |
|
|
// For negative search, start from the end of the month.
|
584 |
|
|
$modifier = '+1 month';
|
585 |
|
|
}
|
586 |
|
|
else {
|
587 |
|
|
// For positive search, back up one day to get outside the
|
588 |
|
|
// current month, so we can catch the first of the month.
|
589 |
|
|
$modifier = '-1 day';
|
590 |
|
|
}
|
591 |
85ad3d82
|
Assos Assos
|
}
|
592 |
|
|
else {
|
593 |
db9ffd17
|
Assos Assos
|
if ($direction == '-') {
|
594 |
|
|
// For negative search, start from the end of the year.
|
595 |
|
|
$modifier = '+1 year';
|
596 |
|
|
}
|
597 |
|
|
else {
|
598 |
|
|
// For positive search, back up one day to get outside the
|
599 |
|
|
// current year, so we can catch the first of the year.
|
600 |
|
|
$modifier = '-1 day';
|
601 |
|
|
}
|
602 |
85ad3d82
|
Assos Assos
|
}
|
603 |
db9ffd17
|
Assos Assos
|
|
604 |
|
|
date_modify($date, $modifier . $modify_time);
|
605 |
|
|
|
606 |
85ad3d82
|
Assos Assos
|
if (empty($day)) {
|
607 |
|
|
date_modify($date, $direction . $count . ' days' . $modify_time);
|
608 |
|
|
}
|
609 |
|
|
else {
|
610 |
|
|
// Use the English text for order, like First Sunday
|
611 |
|
|
// instead of +1 Sunday to overcome PHP5 bug, (see #369020).
|
612 |
|
|
$order = date_order();
|
613 |
|
|
$step = $count <= 5 ? $order[$direction . $count] : $count;
|
614 |
|
|
date_modify($date, $step . ' ' . $day . $modify_time);
|
615 |
|
|
}
|
616 |
|
|
|
617 |
|
|
// If that takes us outside the current year, don't go there.
|
618 |
|
|
if (date_format($date, 'Y') == $current_year) {
|
619 |
|
|
return $date;
|
620 |
|
|
}
|
621 |
|
|
}
|
622 |
|
|
return $date_in;
|
623 |
|
|
} |