Mercurial > hg > rc1
comparison plugins/libcalendaring/lib/Sabre/VObject/RecurrenceIterator.php @ 4:888e774ee983
libcalendar plugin as distributed
author | Charlie Root |
---|---|
date | Sat, 13 Jan 2018 08:57:56 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
3:f6fe4b6ae66a | 4:888e774ee983 |
---|---|
1 <?php | |
2 | |
3 namespace Sabre\VObject; | |
4 | |
5 /** | |
6 * This class is used to determine new for a recurring event, when the next | |
7 * events occur. | |
8 * | |
9 * This iterator may loop infinitely in the future, therefore it is important | |
10 * that if you use this class, you set hard limits for the amount of iterations | |
11 * you want to handle. | |
12 * | |
13 * Note that currently there is not full support for the entire iCalendar | |
14 * specification, as it's very complex and contains a lot of permutations | |
15 * that's not yet used very often in software. | |
16 * | |
17 * For the focus has been on features as they actually appear in Calendaring | |
18 * software, but this may well get expanded as needed / on demand | |
19 * | |
20 * The following RRULE properties are supported | |
21 * * UNTIL | |
22 * * INTERVAL | |
23 * * COUNT | |
24 * * FREQ=DAILY | |
25 * * BYDAY | |
26 * * BYHOUR | |
27 * * FREQ=WEEKLY | |
28 * * BYDAY | |
29 * * BYHOUR | |
30 * * WKST | |
31 * * FREQ=MONTHLY | |
32 * * BYMONTHDAY | |
33 * * BYDAY | |
34 * * BYSETPOS | |
35 * * FREQ=YEARLY | |
36 * * BYMONTH | |
37 * * BYMONTHDAY (only if BYMONTH is also set) | |
38 * * BYDAY (only if BYMONTH is also set) | |
39 * | |
40 * Anything beyond this is 'undefined', which means that it may get ignored, or | |
41 * you may get unexpected results. The effect is that in some applications the | |
42 * specified recurrence may look incorrect, or is missing. | |
43 * | |
44 * @copyright Copyright (C) 2007-2013 fruux GmbH (https://fruux.com/). | |
45 * @author Evert Pot (http://evertpot.com/) | |
46 * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License | |
47 */ | |
48 class RecurrenceIterator implements \Iterator { | |
49 | |
50 /** | |
51 * The initial event date | |
52 * | |
53 * @var DateTime | |
54 */ | |
55 public $startDate; | |
56 | |
57 /** | |
58 * The end-date of the initial event | |
59 * | |
60 * @var DateTime | |
61 */ | |
62 public $endDate; | |
63 | |
64 /** | |
65 * The 'current' recurrence. | |
66 * | |
67 * This will be increased for every iteration. | |
68 * | |
69 * @var DateTime | |
70 */ | |
71 public $currentDate; | |
72 | |
73 | |
74 /** | |
75 * List of dates that are excluded from the rules. | |
76 * | |
77 * This list contains the items that have been overriden by the EXDATE | |
78 * property. | |
79 * | |
80 * @var array | |
81 */ | |
82 public $exceptionDates = array(); | |
83 | |
84 /** | |
85 * Base event | |
86 * | |
87 * @var Component\VEvent | |
88 */ | |
89 public $baseEvent; | |
90 | |
91 /** | |
92 * List of dates that are overridden by other events. | |
93 * Similar to $overriddenEvents, but this just contains the original dates. | |
94 * | |
95 * @var array | |
96 */ | |
97 public $overriddenDates = array(); | |
98 | |
99 /** | |
100 * list of events that are 'overridden'. | |
101 * | |
102 * This is an array of Component\VEvent objects. | |
103 * | |
104 * @var array | |
105 */ | |
106 public $overriddenEvents = array(); | |
107 | |
108 /** | |
109 * Frequency is one of: secondly, minutely, hourly, daily, weekly, monthly, | |
110 * yearly. | |
111 * | |
112 * @var string | |
113 */ | |
114 public $frequency; | |
115 | |
116 /** | |
117 * The last instance of this recurrence, inclusively | |
118 * | |
119 * @var DateTime|null | |
120 */ | |
121 public $until; | |
122 | |
123 /** | |
124 * The number of recurrences, or 'null' if infinitely recurring. | |
125 * | |
126 * @var int | |
127 */ | |
128 public $count; | |
129 | |
130 /** | |
131 * The interval. | |
132 * | |
133 * If for example frequency is set to daily, interval = 2 would mean every | |
134 * 2 days. | |
135 * | |
136 * @var int | |
137 */ | |
138 public $interval = 1; | |
139 | |
140 /** | |
141 * Which seconds to recur. | |
142 * | |
143 * This is an array of integers (between 0 and 60) | |
144 * | |
145 * @var array | |
146 */ | |
147 public $bySecond; | |
148 | |
149 /** | |
150 * Which minutes to recur | |
151 * | |
152 * This is an array of integers (between 0 and 59) | |
153 * | |
154 * @var array | |
155 */ | |
156 public $byMinute; | |
157 | |
158 /** | |
159 * Which hours to recur | |
160 * | |
161 * This is an array of integers (between 0 and 23) | |
162 * | |
163 * @var array | |
164 */ | |
165 public $byHour; | |
166 | |
167 /** | |
168 * Which weekdays to recur. | |
169 * | |
170 * This is an array of weekdays | |
171 * | |
172 * This may also be preceeded by a positive or negative integer. If present, | |
173 * this indicates the nth occurrence of a specific day within the monthly or | |
174 * yearly rrule. For instance, -2TU indicates the second-last tuesday of | |
175 * the month, or year. | |
176 * | |
177 * @var array | |
178 */ | |
179 public $byDay; | |
180 | |
181 /** | |
182 * Which days of the month to recur | |
183 * | |
184 * This is an array of days of the months (1-31). The value can also be | |
185 * negative. -5 for instance means the 5th last day of the month. | |
186 * | |
187 * @var array | |
188 */ | |
189 public $byMonthDay; | |
190 | |
191 /** | |
192 * Which days of the year to recur. | |
193 * | |
194 * This is an array with days of the year (1 to 366). The values can also | |
195 * be negative. For instance, -1 will always represent the last day of the | |
196 * year. (December 31st). | |
197 * | |
198 * @var array | |
199 */ | |
200 public $byYearDay; | |
201 | |
202 /** | |
203 * Which week numbers to recur. | |
204 * | |
205 * This is an array of integers from 1 to 53. The values can also be | |
206 * negative. -1 will always refer to the last week of the year. | |
207 * | |
208 * @var array | |
209 */ | |
210 public $byWeekNo; | |
211 | |
212 /** | |
213 * Which months to recur | |
214 * | |
215 * This is an array of integers from 1 to 12. | |
216 * | |
217 * @var array | |
218 */ | |
219 public $byMonth; | |
220 | |
221 /** | |
222 * Which items in an existing st to recur. | |
223 * | |
224 * These numbers work together with an existing by* rule. It specifies | |
225 * exactly which items of the existing by-rule to filter. | |
226 * | |
227 * Valid values are 1 to 366 and -1 to -366. As an example, this can be | |
228 * used to recur the last workday of the month. | |
229 * | |
230 * This would be done by setting frequency to 'monthly', byDay to | |
231 * 'MO,TU,WE,TH,FR' and bySetPos to -1. | |
232 * | |
233 * @var array | |
234 */ | |
235 public $bySetPos; | |
236 | |
237 /** | |
238 * When a week starts | |
239 * | |
240 * @var string | |
241 */ | |
242 public $weekStart = 'MO'; | |
243 | |
244 /** | |
245 * The current item in the list | |
246 * | |
247 * @var int | |
248 */ | |
249 public $counter = 0; | |
250 | |
251 /** | |
252 * Simple mapping from iCalendar day names to day numbers | |
253 * | |
254 * @var array | |
255 */ | |
256 private $dayMap = array( | |
257 'SU' => 0, | |
258 'MO' => 1, | |
259 'TU' => 2, | |
260 'WE' => 3, | |
261 'TH' => 4, | |
262 'FR' => 5, | |
263 'SA' => 6, | |
264 ); | |
265 | |
266 /** | |
267 * Mappings between the day number and english day name. | |
268 * | |
269 * @var array | |
270 */ | |
271 private $dayNames = array( | |
272 0 => 'Sunday', | |
273 1 => 'Monday', | |
274 2 => 'Tuesday', | |
275 3 => 'Wednesday', | |
276 4 => 'Thursday', | |
277 5 => 'Friday', | |
278 6 => 'Saturday', | |
279 ); | |
280 | |
281 /** | |
282 * If the current iteration of the event is an overriden event, this | |
283 * property will hold the VObject | |
284 * | |
285 * @var Component | |
286 */ | |
287 private $currentOverriddenEvent; | |
288 | |
289 /** | |
290 * This property may contain the date of the next not-overridden event. | |
291 * This date is calculated sometimes a bit early, before overridden events | |
292 * are evaluated. | |
293 * | |
294 * @var DateTime | |
295 */ | |
296 private $nextDate; | |
297 | |
298 /** | |
299 * This counts the number of overridden events we've handled so far | |
300 * | |
301 * @var int | |
302 */ | |
303 private $handledOverridden = 0; | |
304 | |
305 /** | |
306 * Creates the iterator | |
307 * | |
308 * You should pass a VCALENDAR component, as well as the UID of the event | |
309 * we're going to traverse. | |
310 * | |
311 * @param Component $vcal | |
312 * @param string|null $uid | |
313 */ | |
314 public function __construct(Component $vcal, $uid=null) { | |
315 | |
316 if (is_null($uid)) { | |
317 if ($vcal->name === 'VCALENDAR') { | |
318 throw new \InvalidArgumentException('If you pass a VCALENDAR object, you must pass a uid argument as well'); | |
319 } | |
320 $components = array($vcal); | |
321 $uid = (string)$vcal->uid; | |
322 } else { | |
323 $components = $vcal->select('VEVENT'); | |
324 } | |
325 foreach($components as $component) { | |
326 if ((string)$component->uid == $uid) { | |
327 if (isset($component->{'RECURRENCE-ID'})) { | |
328 $this->overriddenEvents[$component->DTSTART->getDateTime()->getTimeStamp()] = $component; | |
329 $this->overriddenDates[] = $component->{'RECURRENCE-ID'}->getDateTime(); | |
330 } else { | |
331 $this->baseEvent = $component; | |
332 } | |
333 } | |
334 } | |
335 | |
336 ksort($this->overriddenEvents); | |
337 | |
338 if (!$this->baseEvent) { | |
339 throw new \InvalidArgumentException('Could not find a base event with uid: ' . $uid); | |
340 } | |
341 | |
342 $this->startDate = clone $this->baseEvent->DTSTART->getDateTime(); | |
343 | |
344 $this->endDate = null; | |
345 if (isset($this->baseEvent->DTEND)) { | |
346 $this->endDate = clone $this->baseEvent->DTEND->getDateTime(); | |
347 } else { | |
348 $this->endDate = clone $this->startDate; | |
349 if (isset($this->baseEvent->DURATION)) { | |
350 $this->endDate->add(DateTimeParser::parse($this->baseEvent->DURATION->value)); | |
351 } elseif ($this->baseEvent->DTSTART->getDateType()===Property\DateTime::DATE) { | |
352 $this->endDate->modify('+1 day'); | |
353 } | |
354 } | |
355 $this->currentDate = clone $this->startDate; | |
356 | |
357 $rrule = (string)$this->baseEvent->RRULE; | |
358 | |
359 $parts = explode(';', $rrule); | |
360 | |
361 // If no rrule was specified, we create a default setting | |
362 if (!$rrule) { | |
363 $this->frequency = 'daily'; | |
364 $this->count = 1; | |
365 } else foreach($parts as $part) { | |
366 | |
367 list($key, $value) = explode('=', $part, 2); | |
368 | |
369 switch(strtoupper($key)) { | |
370 | |
371 case 'FREQ' : | |
372 if (!in_array( | |
373 strtolower($value), | |
374 array('secondly','minutely','hourly','daily','weekly','monthly','yearly') | |
375 )) { | |
376 throw new \InvalidArgumentException('Unknown value for FREQ=' . strtoupper($value)); | |
377 | |
378 } | |
379 $this->frequency = strtolower($value); | |
380 break; | |
381 | |
382 case 'UNTIL' : | |
383 $this->until = DateTimeParser::parse($value); | |
384 | |
385 // In some cases events are generated with an UNTIL= | |
386 // parameter before the actual start of the event. | |
387 // | |
388 // Not sure why this is happening. We assume that the | |
389 // intention was that the event only recurs once. | |
390 // | |
391 // So we are modifying the parameter so our code doesn't | |
392 // break. | |
393 if($this->until < $this->baseEvent->DTSTART->getDateTime()) { | |
394 $this->until = $this->baseEvent->DTSTART->getDateTime(); | |
395 } | |
396 break; | |
397 | |
398 case 'COUNT' : | |
399 $this->count = (int)$value; | |
400 break; | |
401 | |
402 case 'INTERVAL' : | |
403 $this->interval = (int)$value; | |
404 if ($this->interval < 1) { | |
405 throw new \InvalidArgumentException('INTERVAL in RRULE must be a positive integer!'); | |
406 } | |
407 break; | |
408 | |
409 case 'BYSECOND' : | |
410 $this->bySecond = explode(',', $value); | |
411 break; | |
412 | |
413 case 'BYMINUTE' : | |
414 $this->byMinute = explode(',', $value); | |
415 break; | |
416 | |
417 case 'BYHOUR' : | |
418 $this->byHour = explode(',', $value); | |
419 break; | |
420 | |
421 case 'BYDAY' : | |
422 $this->byDay = explode(',', strtoupper($value)); | |
423 break; | |
424 | |
425 case 'BYMONTHDAY' : | |
426 $this->byMonthDay = explode(',', $value); | |
427 break; | |
428 | |
429 case 'BYYEARDAY' : | |
430 $this->byYearDay = explode(',', $value); | |
431 break; | |
432 | |
433 case 'BYWEEKNO' : | |
434 $this->byWeekNo = explode(',', $value); | |
435 break; | |
436 | |
437 case 'BYMONTH' : | |
438 $this->byMonth = explode(',', $value); | |
439 break; | |
440 | |
441 case 'BYSETPOS' : | |
442 $this->bySetPos = explode(',', $value); | |
443 break; | |
444 | |
445 case 'WKST' : | |
446 $this->weekStart = strtoupper($value); | |
447 break; | |
448 | |
449 } | |
450 | |
451 } | |
452 | |
453 // Parsing exception dates | |
454 if (isset($this->baseEvent->EXDATE)) { | |
455 foreach($this->baseEvent->EXDATE as $exDate) { | |
456 | |
457 foreach(explode(',', (string)$exDate) as $exceptionDate) { | |
458 | |
459 $this->exceptionDates[] = | |
460 DateTimeParser::parse($exceptionDate, $this->startDate->getTimeZone()); | |
461 | |
462 } | |
463 | |
464 } | |
465 | |
466 } | |
467 | |
468 } | |
469 | |
470 /** | |
471 * Returns the current item in the list | |
472 * | |
473 * @return DateTime | |
474 */ | |
475 public function current() { | |
476 | |
477 if (!$this->valid()) return null; | |
478 return clone $this->currentDate; | |
479 | |
480 } | |
481 | |
482 /** | |
483 * This method returns the startdate for the current iteration of the | |
484 * event. | |
485 * | |
486 * @return DateTime | |
487 */ | |
488 public function getDtStart() { | |
489 | |
490 if (!$this->valid()) return null; | |
491 return clone $this->currentDate; | |
492 | |
493 } | |
494 | |
495 /** | |
496 * This method returns the enddate for the current iteration of the | |
497 * event. | |
498 * | |
499 * @return DateTime | |
500 */ | |
501 public function getDtEnd() { | |
502 | |
503 if (!$this->valid()) return null; | |
504 $dtEnd = clone $this->currentDate; | |
505 $dtEnd->add( $this->startDate->diff( $this->endDate ) ); | |
506 return clone $dtEnd; | |
507 | |
508 } | |
509 | |
510 /** | |
511 * Returns a VEVENT object with the updated start and end date. | |
512 * | |
513 * Any recurrence information is removed, and this function may return an | |
514 * 'overridden' event instead. | |
515 * | |
516 * This method always returns a cloned instance. | |
517 * | |
518 * @return Component\VEvent | |
519 */ | |
520 public function getEventObject() { | |
521 | |
522 if ($this->currentOverriddenEvent) { | |
523 return clone $this->currentOverriddenEvent; | |
524 } | |
525 $event = clone $this->baseEvent; | |
526 unset($event->RRULE); | |
527 unset($event->EXDATE); | |
528 unset($event->RDATE); | |
529 unset($event->EXRULE); | |
530 | |
531 $event->DTSTART->setDateTime($this->getDTStart(), $event->DTSTART->getDateType()); | |
532 if (isset($event->DTEND)) { | |
533 $event->DTEND->setDateTime($this->getDtEnd(), $event->DTSTART->getDateType()); | |
534 } | |
535 if ($this->counter > 0) { | |
536 $event->{'RECURRENCE-ID'} = (string)$event->DTSTART; | |
537 } | |
538 | |
539 return $event; | |
540 | |
541 } | |
542 | |
543 /** | |
544 * Returns the current item number | |
545 * | |
546 * @return int | |
547 */ | |
548 public function key() { | |
549 | |
550 return $this->counter; | |
551 | |
552 } | |
553 | |
554 /** | |
555 * Whether or not there is a 'next item' | |
556 * | |
557 * @return bool | |
558 */ | |
559 public function valid() { | |
560 | |
561 if (!is_null($this->count)) { | |
562 return $this->counter < $this->count; | |
563 } | |
564 if (!is_null($this->until) && $this->currentDate > $this->until) { | |
565 | |
566 // Need to make sure there's no overridden events past the | |
567 // until date. | |
568 foreach($this->overriddenEvents as $overriddenEvent) { | |
569 | |
570 if ($overriddenEvent->DTSTART->getDateTime() >= $this->currentDate) { | |
571 | |
572 return true; | |
573 } | |
574 } | |
575 return false; | |
576 } | |
577 return true; | |
578 | |
579 } | |
580 | |
581 /** | |
582 * Resets the iterator | |
583 * | |
584 * @return void | |
585 */ | |
586 public function rewind() { | |
587 | |
588 $this->currentDate = clone $this->startDate; | |
589 $this->counter = 0; | |
590 | |
591 } | |
592 | |
593 /** | |
594 * This method allows you to quickly go to the next occurrence after the | |
595 * specified date. | |
596 * | |
597 * Note that this checks the current 'endDate', not the 'stardDate'. This | |
598 * means that if you forward to January 1st, the iterator will stop at the | |
599 * first event that ends *after* January 1st. | |
600 * | |
601 * @param DateTime $dt | |
602 * @return void | |
603 */ | |
604 public function fastForward(\DateTime $dt) { | |
605 | |
606 while($this->valid() && $this->getDTEnd() <= $dt) { | |
607 $this->next(); | |
608 } | |
609 | |
610 } | |
611 | |
612 /** | |
613 * Returns true if this recurring event never ends. | |
614 * | |
615 * @return bool | |
616 */ | |
617 public function isInfinite() { | |
618 | |
619 return !$this->count && !$this->until; | |
620 | |
621 } | |
622 | |
623 /** | |
624 * Goes on to the next iteration | |
625 * | |
626 * @return void | |
627 */ | |
628 public function next() { | |
629 | |
630 $previousStamp = $this->currentDate->getTimeStamp(); | |
631 | |
632 // Finding the next overridden event in line, and storing that for | |
633 // later use. | |
634 $overriddenEvent = null; | |
635 $overriddenDate = null; | |
636 $this->currentOverriddenEvent = null; | |
637 | |
638 foreach($this->overriddenEvents as $index=>$event) { | |
639 if ($index > $previousStamp) { | |
640 $overriddenEvent = $event; | |
641 $overriddenDate = clone $event->DTSTART->getDateTime(); | |
642 break; | |
643 } | |
644 } | |
645 | |
646 // If we have a stored 'next date', we will use that. | |
647 if ($this->nextDate) { | |
648 if (!$overriddenDate || $this->nextDate < $overriddenDate) { | |
649 $this->currentDate = $this->nextDate; | |
650 $currentStamp = $this->currentDate->getTimeStamp(); | |
651 $this->nextDate = null; | |
652 } else { | |
653 $this->currentDate = clone $overriddenDate; | |
654 $this->currentOverriddenEvent = $overriddenEvent; | |
655 } | |
656 $this->counter++; | |
657 return; | |
658 } | |
659 | |
660 while(true) { | |
661 | |
662 // Otherwise, we find the next event in the normal RRULE | |
663 // sequence. | |
664 switch($this->frequency) { | |
665 | |
666 case 'hourly' : | |
667 $this->nextHourly(); | |
668 break; | |
669 | |
670 case 'daily' : | |
671 $this->nextDaily(); | |
672 break; | |
673 | |
674 case 'weekly' : | |
675 $this->nextWeekly(); | |
676 break; | |
677 | |
678 case 'monthly' : | |
679 $this->nextMonthly(); | |
680 break; | |
681 | |
682 case 'yearly' : | |
683 $this->nextYearly(); | |
684 break; | |
685 | |
686 } | |
687 $currentStamp = $this->currentDate->getTimeStamp(); | |
688 | |
689 | |
690 // Checking exception dates | |
691 foreach($this->exceptionDates as $exceptionDate) { | |
692 if ($this->currentDate == $exceptionDate) { | |
693 $this->counter++; | |
694 continue 2; | |
695 } | |
696 } | |
697 foreach($this->overriddenDates as $check) { | |
698 if ($this->currentDate == $check) { | |
699 continue 2; | |
700 } | |
701 } | |
702 break; | |
703 | |
704 } | |
705 | |
706 | |
707 | |
708 // Is the date we have actually higher than the next overiddenEvent? | |
709 if ($overriddenDate && $this->currentDate > $overriddenDate) { | |
710 $this->nextDate = clone $this->currentDate; | |
711 $this->currentDate = clone $overriddenDate; | |
712 $this->currentOverriddenEvent = $overriddenEvent; | |
713 $this->handledOverridden++; | |
714 } | |
715 $this->counter++; | |
716 | |
717 | |
718 /* | |
719 * If we have overridden events left in the queue, but our counter is | |
720 * running out, we should grab one of those. | |
721 */ | |
722 if (!is_null($overriddenEvent) && !is_null($this->count) && count($this->overriddenEvents) - $this->handledOverridden >= ($this->count - $this->counter)) { | |
723 | |
724 $this->currentOverriddenEvent = $overriddenEvent; | |
725 $this->currentDate = clone $overriddenDate; | |
726 $this->handledOverridden++; | |
727 | |
728 } | |
729 | |
730 } | |
731 | |
732 /** | |
733 * Does the processing for advancing the iterator for hourly frequency. | |
734 * | |
735 * @return void | |
736 */ | |
737 protected function nextHourly() { | |
738 | |
739 if (!$this->byHour) { | |
740 $this->currentDate->modify('+' . $this->interval . ' hours'); | |
741 return; | |
742 } | |
743 } | |
744 | |
745 /** | |
746 * Does the processing for advancing the iterator for daily frequency. | |
747 * | |
748 * @return void | |
749 */ | |
750 protected function nextDaily() { | |
751 | |
752 if (!$this->byHour && !$this->byDay) { | |
753 $this->currentDate->modify('+' . $this->interval . ' days'); | |
754 return; | |
755 } | |
756 | |
757 if (isset($this->byHour)) { | |
758 $recurrenceHours = $this->getHours(); | |
759 } | |
760 | |
761 if (isset($this->byDay)) { | |
762 $recurrenceDays = $this->getDays(); | |
763 } | |
764 | |
765 do { | |
766 | |
767 if ($this->byHour) { | |
768 if ($this->currentDate->format('G') == '23') { | |
769 // to obey the interval rule | |
770 $this->currentDate->modify('+' . $this->interval-1 . ' days'); | |
771 } | |
772 | |
773 $this->currentDate->modify('+1 hours'); | |
774 | |
775 } else { | |
776 $this->currentDate->modify('+' . $this->interval . ' days'); | |
777 | |
778 } | |
779 | |
780 // Current day of the week | |
781 $currentDay = $this->currentDate->format('w'); | |
782 | |
783 // Current hour of the day | |
784 $currentHour = $this->currentDate->format('G'); | |
785 | |
786 } while (($this->byDay && !in_array($currentDay, $recurrenceDays)) || ($this->byHour && !in_array($currentHour, $recurrenceHours))); | |
787 | |
788 } | |
789 | |
790 /** | |
791 * Does the processing for advancing the iterator for weekly frequency. | |
792 * | |
793 * @return void | |
794 */ | |
795 protected function nextWeekly() { | |
796 | |
797 if (!$this->byHour && !$this->byDay) { | |
798 $this->currentDate->modify('+' . $this->interval . ' weeks'); | |
799 return; | |
800 } | |
801 | |
802 if ($this->byHour) { | |
803 $recurrenceHours = $this->getHours(); | |
804 } | |
805 | |
806 if ($this->byDay) { | |
807 $recurrenceDays = $this->getDays(); | |
808 } | |
809 | |
810 // First day of the week: | |
811 $firstDay = $this->dayMap[$this->weekStart]; | |
812 | |
813 do { | |
814 | |
815 if ($this->byHour) { | |
816 $this->currentDate->modify('+1 hours'); | |
817 } else { | |
818 $this->currentDate->modify('+1 days'); | |
819 } | |
820 | |
821 // Current day of the week | |
822 $currentDay = (int) $this->currentDate->format('w'); | |
823 | |
824 // Current hour of the day | |
825 $currentHour = (int) $this->currentDate->format('G'); | |
826 | |
827 // We need to roll over to the next week | |
828 if ($currentDay === $firstDay && (!$this->byHour || $currentHour == '0')) { | |
829 $this->currentDate->modify('+' . $this->interval-1 . ' weeks'); | |
830 | |
831 // We need to go to the first day of this week, but only if we | |
832 // are not already on this first day of this week. | |
833 if($this->currentDate->format('w') != $firstDay) { | |
834 $this->currentDate->modify('last ' . $this->dayNames[$this->dayMap[$this->weekStart]]); | |
835 } | |
836 } | |
837 | |
838 // We have a match | |
839 } while (($this->byDay && !in_array($currentDay, $recurrenceDays)) || ($this->byHour && !in_array($currentHour, $recurrenceHours))); | |
840 } | |
841 | |
842 /** | |
843 * Does the processing for advancing the iterator for monthly frequency. | |
844 * | |
845 * @return void | |
846 */ | |
847 protected function nextMonthly() { | |
848 | |
849 $currentDayOfMonth = $this->currentDate->format('j'); | |
850 if (!$this->byMonthDay && !$this->byDay) { | |
851 | |
852 // If the current day is higher than the 28th, rollover can | |
853 // occur to the next month. We Must skip these invalid | |
854 // entries. | |
855 if ($currentDayOfMonth < 29) { | |
856 $this->currentDate->modify('+' . $this->interval . ' months'); | |
857 } else { | |
858 $increase = 0; | |
859 do { | |
860 $increase++; | |
861 $tempDate = clone $this->currentDate; | |
862 $tempDate->modify('+ ' . ($this->interval*$increase) . ' months'); | |
863 } while ($tempDate->format('j') != $currentDayOfMonth); | |
864 $this->currentDate = $tempDate; | |
865 } | |
866 return; | |
867 } | |
868 | |
869 while(true) { | |
870 | |
871 $occurrences = $this->getMonthlyOccurrences(); | |
872 | |
873 foreach($occurrences as $occurrence) { | |
874 | |
875 // The first occurrence thats higher than the current | |
876 // day of the month wins. | |
877 if ($occurrence > $currentDayOfMonth) { | |
878 break 2; | |
879 } | |
880 | |
881 } | |
882 | |
883 // If we made it all the way here, it means there were no | |
884 // valid occurrences, and we need to advance to the next | |
885 // month. | |
886 $this->currentDate->modify('first day of this month'); | |
887 $this->currentDate->modify('+ ' . $this->interval . ' months'); | |
888 | |
889 // This goes to 0 because we need to start counting at hte | |
890 // beginning. | |
891 $currentDayOfMonth = 0; | |
892 | |
893 } | |
894 | |
895 $this->currentDate->setDate($this->currentDate->format('Y'), $this->currentDate->format('n'), $occurrence); | |
896 | |
897 } | |
898 | |
899 /** | |
900 * Does the processing for advancing the iterator for yearly frequency. | |
901 * | |
902 * @return void | |
903 */ | |
904 protected function nextYearly() { | |
905 | |
906 $currentMonth = $this->currentDate->format('n'); | |
907 $currentYear = $this->currentDate->format('Y'); | |
908 $currentDayOfMonth = $this->currentDate->format('j'); | |
909 | |
910 // No sub-rules, so we just advance by year | |
911 if (!$this->byMonth) { | |
912 | |
913 // Unless it was a leap day! | |
914 if ($currentMonth==2 && $currentDayOfMonth==29) { | |
915 | |
916 $counter = 0; | |
917 do { | |
918 $counter++; | |
919 // Here we increase the year count by the interval, until | |
920 // we hit a date that's also in a leap year. | |
921 // | |
922 // We could just find the next interval that's dividable by | |
923 // 4, but that would ignore the rule that there's no leap | |
924 // year every year that's dividable by a 100, but not by | |
925 // 400. (1800, 1900, 2100). So we just rely on the datetime | |
926 // functions instead. | |
927 $nextDate = clone $this->currentDate; | |
928 $nextDate->modify('+ ' . ($this->interval*$counter) . ' years'); | |
929 } while ($nextDate->format('n')!=2); | |
930 $this->currentDate = $nextDate; | |
931 | |
932 return; | |
933 | |
934 } | |
935 | |
936 // The easiest form | |
937 $this->currentDate->modify('+' . $this->interval . ' years'); | |
938 return; | |
939 | |
940 } | |
941 | |
942 $currentMonth = $this->currentDate->format('n'); | |
943 $currentYear = $this->currentDate->format('Y'); | |
944 $currentDayOfMonth = $this->currentDate->format('j'); | |
945 | |
946 $advancedToNewMonth = false; | |
947 | |
948 // If we got a byDay or getMonthDay filter, we must first expand | |
949 // further. | |
950 if ($this->byDay || $this->byMonthDay) { | |
951 | |
952 while(true) { | |
953 | |
954 $occurrences = $this->getMonthlyOccurrences(); | |
955 | |
956 foreach($occurrences as $occurrence) { | |
957 | |
958 // The first occurrence that's higher than the current | |
959 // day of the month wins. | |
960 // If we advanced to the next month or year, the first | |
961 // occurrence is always correct. | |
962 if ($occurrence > $currentDayOfMonth || $advancedToNewMonth) { | |
963 break 2; | |
964 } | |
965 | |
966 } | |
967 | |
968 // If we made it here, it means we need to advance to | |
969 // the next month or year. | |
970 $currentDayOfMonth = 1; | |
971 $advancedToNewMonth = true; | |
972 do { | |
973 | |
974 $currentMonth++; | |
975 if ($currentMonth>12) { | |
976 $currentYear+=$this->interval; | |
977 $currentMonth = 1; | |
978 } | |
979 } while (!in_array($currentMonth, $this->byMonth)); | |
980 | |
981 $this->currentDate->setDate($currentYear, $currentMonth, $currentDayOfMonth); | |
982 | |
983 } | |
984 | |
985 // If we made it here, it means we got a valid occurrence | |
986 $this->currentDate->setDate($currentYear, $currentMonth, $occurrence); | |
987 return; | |
988 | |
989 } else { | |
990 | |
991 // These are the 'byMonth' rules, if there are no byDay or | |
992 // byMonthDay sub-rules. | |
993 do { | |
994 | |
995 $currentMonth++; | |
996 if ($currentMonth>12) { | |
997 $currentYear+=$this->interval; | |
998 $currentMonth = 1; | |
999 } | |
1000 } while (!in_array($currentMonth, $this->byMonth)); | |
1001 $this->currentDate->setDate($currentYear, $currentMonth, $currentDayOfMonth); | |
1002 | |
1003 return; | |
1004 | |
1005 } | |
1006 | |
1007 } | |
1008 | |
1009 /** | |
1010 * Returns all the occurrences for a monthly frequency with a 'byDay' or | |
1011 * 'byMonthDay' expansion for the current month. | |
1012 * | |
1013 * The returned list is an array of integers with the day of month (1-31). | |
1014 * | |
1015 * @return array | |
1016 */ | |
1017 protected function getMonthlyOccurrences() { | |
1018 | |
1019 $startDate = clone $this->currentDate; | |
1020 | |
1021 $byDayResults = array(); | |
1022 | |
1023 // Our strategy is to simply go through the byDays, advance the date to | |
1024 // that point and add it to the results. | |
1025 if ($this->byDay) foreach($this->byDay as $day) { | |
1026 | |
1027 $dayName = $this->dayNames[$this->dayMap[substr($day,-2)]]; | |
1028 | |
1029 // Dayname will be something like 'wednesday'. Now we need to find | |
1030 // all wednesdays in this month. | |
1031 $dayHits = array(); | |
1032 | |
1033 $checkDate = clone $startDate; | |
1034 $checkDate->modify('first day of this month'); | |
1035 $checkDate->modify($dayName); | |
1036 | |
1037 do { | |
1038 $dayHits[] = $checkDate->format('j'); | |
1039 $checkDate->modify('next ' . $dayName); | |
1040 } while ($checkDate->format('n') === $startDate->format('n')); | |
1041 | |
1042 // So now we have 'all wednesdays' for month. It is however | |
1043 // possible that the user only really wanted the 1st, 2nd or last | |
1044 // wednesday. | |
1045 if (strlen($day)>2) { | |
1046 $offset = (int)substr($day,0,-2); | |
1047 | |
1048 if ($offset>0) { | |
1049 // It is possible that the day does not exist, such as a | |
1050 // 5th or 6th wednesday of the month. | |
1051 if (isset($dayHits[$offset-1])) { | |
1052 $byDayResults[] = $dayHits[$offset-1]; | |
1053 } | |
1054 } else { | |
1055 | |
1056 // if it was negative we count from the end of the array | |
1057 $byDayResults[] = $dayHits[count($dayHits) + $offset]; | |
1058 } | |
1059 } else { | |
1060 // There was no counter (first, second, last wednesdays), so we | |
1061 // just need to add the all to the list). | |
1062 $byDayResults = array_merge($byDayResults, $dayHits); | |
1063 | |
1064 } | |
1065 | |
1066 } | |
1067 | |
1068 $byMonthDayResults = array(); | |
1069 if ($this->byMonthDay) foreach($this->byMonthDay as $monthDay) { | |
1070 | |
1071 // Removing values that are out of range for this month | |
1072 if ($monthDay > $startDate->format('t') || | |
1073 $monthDay < 0-$startDate->format('t')) { | |
1074 continue; | |
1075 } | |
1076 if ($monthDay>0) { | |
1077 $byMonthDayResults[] = $monthDay; | |
1078 } else { | |
1079 // Negative values | |
1080 $byMonthDayResults[] = $startDate->format('t') + 1 + $monthDay; | |
1081 } | |
1082 } | |
1083 | |
1084 // If there was just byDay or just byMonthDay, they just specify our | |
1085 // (almost) final list. If both were provided, then byDay limits the | |
1086 // list. | |
1087 if ($this->byMonthDay && $this->byDay) { | |
1088 $result = array_intersect($byMonthDayResults, $byDayResults); | |
1089 } elseif ($this->byMonthDay) { | |
1090 $result = $byMonthDayResults; | |
1091 } else { | |
1092 $result = $byDayResults; | |
1093 } | |
1094 $result = array_unique($result); | |
1095 sort($result, SORT_NUMERIC); | |
1096 | |
1097 // The last thing that needs checking is the BYSETPOS. If it's set, it | |
1098 // means only certain items in the set survive the filter. | |
1099 if (!$this->bySetPos) { | |
1100 return $result; | |
1101 } | |
1102 | |
1103 $filteredResult = array(); | |
1104 foreach($this->bySetPos as $setPos) { | |
1105 | |
1106 if ($setPos<0) { | |
1107 $setPos = count($result)-($setPos+1); | |
1108 } | |
1109 if (isset($result[$setPos-1])) { | |
1110 $filteredResult[] = $result[$setPos-1]; | |
1111 } | |
1112 } | |
1113 | |
1114 sort($filteredResult, SORT_NUMERIC); | |
1115 return $filteredResult; | |
1116 | |
1117 } | |
1118 | |
1119 protected function getHours() | |
1120 { | |
1121 $recurrenceHours = array(); | |
1122 foreach($this->byHour as $byHour) { | |
1123 $recurrenceHours[] = $byHour; | |
1124 } | |
1125 | |
1126 return $recurrenceHours; | |
1127 } | |
1128 | |
1129 protected function getDays() | |
1130 { | |
1131 $recurrenceDays = array(); | |
1132 foreach($this->byDay as $byDay) { | |
1133 | |
1134 // The day may be preceeded with a positive (+n) or | |
1135 // negative (-n) integer. However, this does not make | |
1136 // sense in 'weekly' so we ignore it here. | |
1137 $recurrenceDays[] = $this->dayMap[substr($byDay,-2)]; | |
1138 | |
1139 } | |
1140 | |
1141 return $recurrenceDays; | |
1142 } | |
1143 } | |
1144 |