From 16770f3612d79b913926ea7b1f27faadb815f970 Mon Sep 17 00:00:00 2001 From: Thomas Bruederli Date: Wed, 4 Jul 2012 15:57:19 +0200 Subject: [PATCH] Compute the real end date-time of a recurring event for caching --- .../libkolab/lib/kolab_date_recurrence.php | 28 ++++++++++++++++++- plugins/libkolab/lib/kolab_storage_cache.php | 3 +- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/plugins/libkolab/lib/kolab_date_recurrence.php b/plugins/libkolab/lib/kolab_date_recurrence.php index 95dfedc7..55cc8c35 100644 --- a/plugins/libkolab/lib/kolab_date_recurrence.php +++ b/plugins/libkolab/lib/kolab_date_recurrence.php @@ -45,7 +45,7 @@ class kolab_date_recurrence $this->next = new Horde_Date($object['start'], kolab_format::$timezone->getName()); if (is_object($object['start']) && is_object($object['end'])) - $this->duration = $object['end']->diff($object['start']); + $this->duration = $object['start']->diff($object['end']); else $this->duration = new DateInterval('PT' . ($object['end'] - $object['start']) . 'S'); @@ -115,6 +115,32 @@ class kolab_date_recurrence return false; } + /** + * 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 + */ + public function end($limit = 'now +1 year') + { + if ($this->object['recurrence']['UNTIL']) + return $this->object['recurrence']['UNTIL']; + + $limit_time = strtotime($limit); + while ($next_start = $this->next_start(true)) { + if ($next_start > $limit_time) + break; + } + + if ($this->next) { + $next_end = $this->next->toDateTime(); + $next_end->add($this->duration); + return $next_end->format('U'); + } + + return false; + } + /** * Convert the internal structured data into a vcalendar RRULE 2.0 string */ diff --git a/plugins/libkolab/lib/kolab_storage_cache.php b/plugins/libkolab/lib/kolab_storage_cache.php index 969d400c..80a9342a 100644 --- a/plugins/libkolab/lib/kolab_storage_cache.php +++ b/plugins/libkolab/lib/kolab_storage_cache.php @@ -514,7 +514,8 @@ class kolab_storage_cache // extend date range for recurring events if ($object['recurrence']) { - $sql_data['dtend'] = date('Y-m-d H:i:s', $object['recurrence']['UNTIL'] ?: strtotime('now + 2 years')); + $recurrence = new kolab_date_recurrence($object); + $sql_data['dtend'] = date('Y-m-d H:i:s', $recurrence->end() ?: strtotime('now +1 year')); } } else if ($objtype == 'task') {