2012-07-03 08:25:55 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recurrence computation class for xcal-based Kolab format objects
|
|
|
|
*
|
2012-10-23 15:05:38 +02:00
|
|
|
* Utility class to compute instances of recurring events.
|
|
|
|
* It requires the libcalendaring PHP module to be installed and loaded.
|
2012-07-03 08:25:55 +02:00
|
|
|
*
|
|
|
|
* @version @package_version@
|
|
|
|
* @author Thomas Bruederli <bruederli@kolabsys.com>
|
|
|
|
*
|
2016-04-07 10:46:24 +02:00
|
|
|
* Copyright (C) 2012-2016, Kolab Systems AG <contact@kolabsys.com>
|
2012-07-03 08:25:55 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
class kolab_date_recurrence
|
|
|
|
{
|
2012-10-23 15:05:38 +02:00
|
|
|
private /* EventCal */ $engine;
|
|
|
|
private /* kolab_format_xcal */ $object;
|
|
|
|
private /* DateTime */ $start;
|
|
|
|
private /* DateTime */ $next;
|
2012-11-08 15:41:23 +01:00
|
|
|
private /* cDateTime */ $cnext;
|
2012-10-23 15:05:38 +02:00
|
|
|
private /* DateInterval */ $duration;
|
2012-07-04 15:10:36 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Default constructor
|
|
|
|
*
|
2016-04-07 10:46:24 +02:00
|
|
|
* @param kolab_format_xcal The Kolab object to operate on
|
2012-07-04 15:10:36 +02:00
|
|
|
*/
|
|
|
|
function __construct($object)
|
|
|
|
{
|
2012-10-23 15:05:38 +02:00
|
|
|
$data = $object->to_array();
|
|
|
|
|
2012-07-04 15:10:36 +02:00
|
|
|
$this->object = $object;
|
2012-10-23 15:05:38 +02:00
|
|
|
$this->engine = $object->to_libcal();
|
2016-04-07 10:46:24 +02:00
|
|
|
$this->start = $this->next = $data['start'];
|
|
|
|
$this->cnext = kolab_format::get_datetime($this->next);
|
2012-07-04 15:10:36 +02:00
|
|
|
|
2016-04-07 10:46:24 +02:00
|
|
|
if (is_object($data['start']) && is_object($data['end'])) {
|
2012-10-23 15:05:38 +02:00
|
|
|
$this->duration = $data['start']->diff($data['end']);
|
2016-04-07 10:46:24 +02:00
|
|
|
}
|
2016-03-10 16:43:10 +01:00
|
|
|
else {
|
|
|
|
// Prevent from errors when end date is not set (#5307) RFC5545 3.6.1
|
|
|
|
$seconds = !empty($data['end']) ? ($data['end'] - $data['start']) : 0;
|
|
|
|
$this->duration = new DateInterval('PT' . $seconds . 'S');
|
|
|
|
}
|
2012-07-04 15:10:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get date/time of the next occurence of this event
|
|
|
|
*
|
|
|
|
* @param boolean Return a Unix timestamp instead of a DateTime object
|
|
|
|
* @return mixed DateTime object/unix timestamp or False if recurrence ended
|
|
|
|
*/
|
|
|
|
public function next_start($timestamp = false)
|
|
|
|
{
|
|
|
|
$time = false;
|
2012-10-23 15:05:38 +02:00
|
|
|
|
|
|
|
if ($this->engine && $this->next) {
|
2012-11-08 15:41:23 +01:00
|
|
|
if (($cnext = new cDateTime($this->engine->getNextOccurence($this->cnext))) && $cnext->isValid()) {
|
|
|
|
$next = kolab_format::php_datetime($cnext);
|
2012-10-23 15:05:38 +02:00
|
|
|
$time = $timestamp ? $next->format('U') : $next;
|
2016-04-07 10:46:24 +02:00
|
|
|
|
2012-11-08 15:41:23 +01:00
|
|
|
$this->cnext = $cnext;
|
2016-04-07 10:46:24 +02:00
|
|
|
$this->next = $next;
|
2012-07-04 15:10:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $time;
|
2012-07-03 08:25:55 +02:00
|
|
|
}
|
|
|
|
|
2012-07-04 15:10:36 +02:00
|
|
|
/**
|
|
|
|
* Get the next recurring instance of this event
|
|
|
|
*
|
|
|
|
* @return mixed Array with event properties or False if recurrence ended
|
|
|
|
*/
|
|
|
|
public function next_instance()
|
|
|
|
{
|
|
|
|
if ($next_start = $this->next_start()) {
|
|
|
|
$next_end = clone $next_start;
|
|
|
|
$next_end->add($this->duration);
|
|
|
|
|
2016-04-07 10:46:24 +02:00
|
|
|
$next = $this->object->to_array();
|
2012-07-04 15:10:36 +02:00
|
|
|
$next['start'] = $next_start;
|
2016-04-07 10:46:24 +02:00
|
|
|
$next['end'] = $next_end;
|
2015-02-15 14:32:31 +01:00
|
|
|
|
2016-04-07 10:46:24 +02:00
|
|
|
$recurrence_id_format = libkolab::recurrence_id_format($next);
|
2015-02-15 14:32:31 +01:00
|
|
|
$next['recurrence_date'] = clone $next_start;
|
2016-04-07 10:46:24 +02:00
|
|
|
$next['_instance'] = $next_start->format($recurrence_id_format);
|
2015-02-15 14:32:31 +01:00
|
|
|
|
2012-07-04 15:10:36 +02:00
|
|
|
unset($next['_formatobj']);
|
|
|
|
|
|
|
|
return $next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-04 15:57:19 +02:00
|
|
|
/**
|
|
|
|
* Get the end date of the occurence of this recurrence cycle
|
|
|
|
*
|
2014-01-22 11:22:23 +01:00
|
|
|
* @return DateTime|bool End datetime of the last event or False if recurrence exceeds limit
|
2012-07-04 15:57:19 +02:00
|
|
|
*/
|
2013-10-21 17:14:11 +02:00
|
|
|
public function end()
|
2012-07-04 15:57:19 +02:00
|
|
|
{
|
2013-10-21 17:14:11 +02:00
|
|
|
$event = $this->object->to_array();
|
|
|
|
|
|
|
|
// recurrence end date is given
|
|
|
|
if ($event['recurrence']['UNTIL'] instanceof DateTime) {
|
2014-01-22 11:22:23 +01:00
|
|
|
return $event['recurrence']['UNTIL'];
|
2013-10-21 17:14:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// let libkolab do the work
|
2016-04-07 10:46:24 +02:00
|
|
|
if ($this->engine && ($cend = $this->engine->getLastOccurrence())
|
|
|
|
&& ($end_dt = kolab_format::php_datetime(new cDateTime($cend)))
|
|
|
|
) {
|
2014-01-22 11:22:23 +01:00
|
|
|
return $end_dt;
|
2012-07-04 15:57:19 +02:00
|
|
|
}
|
|
|
|
|
2013-10-21 17:14:11 +02:00
|
|
|
// determine a reasonable end date if none given
|
2014-01-22 11:22:23 +01:00
|
|
|
if (!$event['recurrence']['COUNT'] && $event['end'] instanceof DateTime) {
|
2016-04-07 10:46:24 +02:00
|
|
|
$end_dt = clone $event['end'];
|
2016-10-18 09:04:54 +02:00
|
|
|
$end_dt->add(new DateInterval('P100Y'));
|
2016-04-07 10:46:24 +02:00
|
|
|
|
|
|
|
return $end_dt;
|
2013-10-21 17:14:11 +02:00
|
|
|
}
|
|
|
|
|
2012-07-04 15:57:19 +02:00
|
|
|
return false;
|
|
|
|
}
|
2012-07-03 08:25:55 +02:00
|
|
|
}
|