Fix computation of recurrence end date (#2393)

This commit is contained in:
Thomas Bruederli 2013-10-21 17:14:11 +02:00
parent ed195f8f41
commit f001ae250b
2 changed files with 24 additions and 5 deletions

View file

@ -101,16 +101,35 @@ class kolab_date_recurrence
/** /**
* Get the end date of the occurence of this recurrence cycle * Get the end date of the occurence of this recurrence cycle
* *
* @param string Date limit (where infinite recurrences should abort)
* @return mixed Timestamp with end date of the last event or False if recurrence exceeds limit * @return mixed Timestamp with end date of the last event or False if recurrence exceeds limit
*/ */
public function end($limit = 'now +1 year') public function end()
{ {
$limit_dt = new DateTime($limit); $event = $this->object->to_array();
if ($this->engine && ($cend = $this->engine->getLastOccurrence()) && ($end_dt = kolab_format::php_datetime(new cDateTime($cend))) && $end_dt < $limit_dt) {
// recurrence end date is given
if ($event['recurrence']['UNTIL'] instanceof DateTime) {
return $event['recurrence']['UNTIL']->format('U');
}
// let libkolab do the work
if ($this->engine && ($cend = $this->engine->getLastOccurrence()) && ($end_dt = kolab_format::php_datetime(new cDateTime($cend)))) {
return $end_dt->format('U'); return $end_dt->format('U');
} }
// determine a reasonable end date if none given
if (!$event['recurrence']['COUNT']) {
switch ($event['recurrence']['FREQ']) {
case 'YEARLY': $intvl = 'P100Y'; break;
case 'MONTHLY': $intvl = 'P20Y'; break;
default: $intvl = 'P10Y'; break;
}
$end_dt = clone $event['start'];
$end_dt->add(new DateInterval($intvl));
return $end_dt->format('U');
}
return false; return false;
} }
} }

View file

@ -41,7 +41,7 @@ class kolab_storage_cache_event extends kolab_storage_cache
// extend date range for recurring events // extend date range for recurring events
if ($object['recurrence'] && $object['_formatobj']) { if ($object['recurrence'] && $object['_formatobj']) {
$recurrence = new kolab_date_recurrence($object['_formatobj']); $recurrence = new kolab_date_recurrence($object['_formatobj']);
$sql_data['dtend'] = date('Y-m-d 23:59:59', $recurrence->end() ?: strtotime('now +1 year')); $sql_data['dtend'] = date('Y-m-d 23:59:59', $recurrence->end() ?: strtotime('now +10 years'));
} }
return $sql_data; return $sql_data;