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