From 60205136eeeecc0be3cc62bc5652206d3ab2009e Mon Sep 17 00:00:00 2001 From: Thomas Bruederli Date: Tue, 10 Mar 2015 14:30:50 +0100 Subject: [PATCH] Code cleanup: get recurrence instance identifiers from utility functions --- plugins/calendar/drivers/calendar_driver.php | 2 +- .../drivers/database/database_driver.php | 11 +++---- .../calendar/drivers/kolab/kolab_calendar.php | 10 +++--- .../calendar/drivers/kolab/kolab_driver.php | 14 +++----- plugins/calendar/lib/calendar_recurrence.php | 3 +- plugins/libcalendaring/libcalendaring.php | 33 +++++++++++++++++-- .../libkolab/lib/kolab_date_recurrence.php | 2 +- plugins/libkolab/lib/kolab_format_event.php | 6 ++-- plugins/libkolab/libkolab.php | 11 +++++++ 9 files changed, 62 insertions(+), 30 deletions(-) diff --git a/plugins/calendar/drivers/calendar_driver.php b/plugins/calendar/drivers/calendar_driver.php index 659f4a0e..1cd69561 100644 --- a/plugins/calendar/drivers/calendar_driver.php +++ b/plugins/calendar/drivers/calendar_driver.php @@ -463,7 +463,7 @@ abstract class calendar_driver $rcmail = rcmail::get_instance(); $recurrence = new calendar_recurrence($rcmail->plugins->get_plugin('calendar'), $event); - $recurrence_id_format = $event['allday'] ? 'Ymd' : 'Ymd\THis'; + $recurrence_id_format = libcalendaring::recurrence_id_format($event); // determine a reasonable end date if none given if (!$end) { diff --git a/plugins/calendar/drivers/database/database_driver.php b/plugins/calendar/drivers/database/database_driver.php index 14a88914..402e0c30 100644 --- a/plugins/calendar/drivers/database/database_driver.php +++ b/plugins/calendar/drivers/database/database_driver.php @@ -388,10 +388,9 @@ class database_driver extends calendar_driver // set exception to first instance (= master) if ($event['id'] == $master['id']) { - $recurrence_id_format = $event['allday'] ? 'Ymd' : 'Ymd\THis'; $event += $old; $event['recurrence_id'] = $master['id']; - $event['_instance'] = $old['start']->format($recurrence_id_format); + $event['_instance'] = libcalendaring::recurrence_instance_identifier($old); $event['isexception'] = 1; $event_id = $this->_insert_event($event); return $event_id; @@ -463,7 +462,7 @@ class database_driver extends calendar_driver // adjust recurrence-id when start changed and therefore the entire recurrence chain changes if (is_array($event['recurrence']) && ($old_start_date != $new_start_date || $old_start_time != $new_start_time) && ($exceptions = $this->_load_exceptions($old))) { - $recurrence_id_format = $event['allday'] ? 'Ymd' : 'Ymd\THis'; + $recurrence_id_format = libcalendaring::recurrence_id_format($event); foreach ($exceptions as $exception) { $recurrence_id = rcube_utils::anytodatetime($exception['_instance'], $old['start']->getTimezone()); if (is_a($recurrence_id, 'DateTime')) { @@ -764,8 +763,9 @@ class database_driver extends calendar_driver $recurrence = new calendar_recurrence($this->cal, $event); $count = 0; + $event['allday'] = $event['all_day']; $duration = $event['start']->diff($event['end']); - $recurrence_id_format = $event['all_day'] ? 'Ymd' : 'Ymd\THis'; + $recurrence_id_format = libcalendaring::recurrence_id_format($event); while ($next_start = $recurrence->next_start()) { $instance = $next_start->format($recurrence_id_format); $datestr = substr($instance, 0, 8); @@ -1075,8 +1075,7 @@ class database_driver extends calendar_driver } // check for exception on first instance else { - $recurrence_id_format = $event['allday'] ? 'Ymd' : 'Ymd\THis'; - $instance = $event['start']->format($recurrence_id_format); + $instance = libcalendaring::recurrence_instance_identifier($event); $exceptions = $this->_load_exceptions($event, $instance); if ($exceptions && is_array($exceptions[$instance])) { $event = $exceptions[$instance]; diff --git a/plugins/calendar/drivers/kolab/kolab_calendar.php b/plugins/calendar/drivers/kolab/kolab_calendar.php index a443ee29..f3c26c90 100644 --- a/plugins/calendar/drivers/kolab/kolab_calendar.php +++ b/plugins/calendar/drivers/kolab/kolab_calendar.php @@ -616,14 +616,12 @@ class kolab_calendar extends kolab_storage_folder_api $events = array(); $exdata = array(); $futuredata = array(); - $recurrence_id_format = $event['allday'] ? 'Ymd' : 'Ymd\THis'; + $recurrence_id_format = libcalendaring::recurrence_id_format($event); if (is_array($event['recurrence']['EXCEPTIONS'])) { foreach ($event['recurrence']['EXCEPTIONS'] as $exception) { - if (!$exception['_instance'] && is_a($exception['recurrence_date'], 'DateTime')) - $exception['_instance'] = $exception['recurrence_date']->format($recurrence_id_format); - else if (!$exception['_instance'] && is_a($exception['start'], 'DateTime')) - $exception['_instance'] = $exception['start']->format($recurrence_id_format); + if (!$exception['_instance']) + $exception['_instance'] = libcalendaring::recurrence_instance_identifier($exception); $rec_event = $this->_to_driver_event($exception); $rec_event['id'] = $event['uid'] . '-' . $exception['_instance']; @@ -717,7 +715,7 @@ class kolab_calendar extends kolab_storage_folder_api } // add instance identifier to first occurrence (master event) - $recurrence_id_format = $record['allday'] ? 'Ymd' : 'Ymd\THis'; + $recurrence_id_format = libcalendaring::recurrence_id_format($record); if (!$noinst && $record['recurrence'] && !$record['recurrence_id'] && !$record['_instance']) { $record['_instance'] = $record['start']->format($recurrence_id_format); } diff --git a/plugins/calendar/drivers/kolab/kolab_driver.php b/plugins/calendar/drivers/kolab/kolab_driver.php index cc08e6b4..f494e397 100644 --- a/plugins/calendar/drivers/kolab/kolab_driver.php +++ b/plugins/calendar/drivers/kolab/kolab_driver.php @@ -791,8 +791,7 @@ class kolab_driver extends calendar_driver $_SESSION['calendar_restore_event_data'] = $master; // removing the first instance => just move to next occurence - $recurrence_id_format = $master['allday'] ? 'Ymd' : 'Ymd\THis'; - if ($master['recurrence'] && $event['_instance'] == $master['start']->format($recurrence_id_format)) { + if ($master['recurrence'] && $event['_instance'] == libcalendaring::recurrence_instance_identifier($master)) { $recurring = reset($storage->get_recurring_events($event, $event['start'], null, $event['id'].'-1')); // no future instances found: delete the master event (bug #1677) @@ -822,8 +821,7 @@ class kolab_driver extends calendar_driver break; case 'future': - $recurrence_id_format = $master['allday'] ? 'Ymd' : 'Ymd\THis'; - $master['_instance'] = $master['start']->format($recurrence_id_format); + $master['_instance'] = libcalendaring::recurrence_instance_identifier($master); if ($master['_instance'] != $event['_instance']) { $_SESSION['calendar_restore_event_data'] = $master; @@ -952,8 +950,7 @@ class kolab_driver extends calendar_driver $savemode = $event['_savemode'] ?: ($old['recurrence_id'] || $old['isexception'] ? 'current' : 'all'); // this-and-future on the first instance equals to 'all' - $recurrence_id_format = $master['allday'] ? 'Ymd' : 'Ymd\THis'; - if ($savemode == 'future' && $master['start'] && $old['_instance'] == $master['start']->format($recurrence_id_format)) + if ($savemode == 'future' && $master['start'] && $old['_instance'] == libcalendaring::recurrence_instance_identifier($master)) $savemode = 'all'; // force 'current' mode for single occurrences stored as exception else if (!$old['recurrence'] && !$old['recurrence_id'] && $old['isexception']) @@ -1176,7 +1173,7 @@ class kolab_driver extends calendar_driver // adjust recurrence-id when start changed and therefore the entire recurrence chain changes if ($old_start_date != $new_start_date || $old_start_time != $new_start_time) { - $recurrence_id_format = $event['allday'] ? 'Ymd' : 'Ymd\THis'; + $recurrence_id_format = libcalendaring::recurrence_id_format($event); foreach ($event['recurrence']['EXCEPTIONS'] as $i => $exception) { $recurrence_id = is_a($exception['recurrence_date'], 'DateTime') ? $exception['recurrence_date'] : rcube_utils::anytodatetime($exception['_instance'], $old['start']->getTimezone()); @@ -1337,8 +1334,7 @@ class kolab_driver extends calendar_driver } if (!$event['_instance'] && is_a($event['recurrence_date'], 'DateTime')) { - $recurrence_id_format = $event['allday'] ? 'Ymd' : 'Ymd\THis'; - $event['_instance'] = $event['recurrence_date']->format($recurrence_id_format); + $event['_instance'] = libcalendaring::recurrence_instance_identifier($event); } if (!is_array($master['exceptions']) && is_array($master['recurrence']['EXCEPTIONS'])) { diff --git a/plugins/calendar/lib/calendar_recurrence.php b/plugins/calendar/lib/calendar_recurrence.php index d3af94dc..44d5b08f 100644 --- a/plugins/calendar/lib/calendar_recurrence.php +++ b/plugins/calendar/lib/calendar_recurrence.php @@ -74,9 +74,8 @@ class calendar_recurrence extends libcalendaring_recurrence $next['end']->add($this->duration); } - $recurrence_id_format = $next['allday'] ? 'Ymd' : 'Ymd\THis'; $next['recurrence_date'] = clone $next_start; - $next['_instance'] = $next_start->format($recurrence_id_format); + $next['_instance'] = libcalendaring::recurrence_instance_identifier($next); unset($next['_formatobj']); diff --git a/plugins/libcalendaring/libcalendaring.php b/plugins/libcalendaring/libcalendaring.php index 4f277cff..f25818fa 100644 --- a/plugins/libcalendaring/libcalendaring.php +++ b/plugins/libcalendaring/libcalendaring.php @@ -1427,8 +1427,7 @@ class libcalendaring extends rcube_plugin } // set instance and 'savemode' according to recurrence-id else if (!empty($object['recurrence_date']) && is_a($object['recurrence_date'], 'DateTime')) { - $recurrence_id_format = $object['allday'] ? 'Ymd' : 'Ymd\THis'; - $object['_instance'] = $object['recurrence_date']->format($recurrence_id_format); + $object['_instance'] = self::recurrence_instance_identifier($object); $object['_savemode'] = $object['thisandfuture'] ? 'future' : 'current'; } else if (!empty($object['recurrence_id']) && !empty($object['_instance'])) { @@ -1441,6 +1440,36 @@ class libcalendaring extends rcube_plugin } } + /** + * Return a date() format string to render identifiers for recurrence instances + * + * @param array Hash array with event properties + * @return string Format string + */ + public static function recurrence_id_format($event) + { + return $event['allday'] ? 'Ymd' : 'Ymd\THis'; + } + + /** + * Return the identifer for the given instance of a recurring event + * + * @param array Hash array with event properties + * @return mixed Format string or null if identifier cannot be generated + */ + public static function recurrence_instance_identifier($event) + { + $instance_date = $event['recurrence_date'] ?: $event['start']; + + if ($instance_date && is_a($instance_date, 'DateTime')) { + $recurrence_id_format = $event['allday'] ? 'Ymd' : 'Ymd\THis'; + return $instance_date->format($recurrence_id_format); + } + + return null; + } + + /********* Attendee handling functions *********/ /** diff --git a/plugins/libkolab/lib/kolab_date_recurrence.php b/plugins/libkolab/lib/kolab_date_recurrence.php index b2511f2b..f2483c96 100644 --- a/plugins/libkolab/lib/kolab_date_recurrence.php +++ b/plugins/libkolab/lib/kolab_date_recurrence.php @@ -90,7 +90,7 @@ class kolab_date_recurrence $next['start'] = $next_start; $next['end'] = $next_end; - $recurrence_id_format = $next['allday'] ? 'Ymd' : 'Ymd\THis'; + $recurrence_id_format = libkolab::recurrence_id_format($next); $next['recurrence_date'] = clone $next_start; $next['_instance'] = $next_start->format($recurrence_id_format); diff --git a/plugins/libkolab/lib/kolab_format_event.php b/plugins/libkolab/lib/kolab_format_event.php index a1ff5b3e..e59ddcf8 100644 --- a/plugins/libkolab/lib/kolab_format_event.php +++ b/plugins/libkolab/lib/kolab_format_event.php @@ -102,7 +102,7 @@ class kolab_format_event extends kolab_format_xcal } if (is_array($object['exceptions'])) { - $recurrence_id_format = $object['allday'] ? 'Ymd' : 'Ymd\THis'; + $recurrence_id_format = libkolab::recurrence_id_format($object); $vexceptions = new vectorevent; foreach ($object['exceptions'] as $i => $exception) { $exevent = new kolab_format_event; @@ -210,7 +210,7 @@ class kolab_format_event extends kolab_format_xcal // read exception event objects if (($exceptions = $this->obj->exceptions()) && is_object($exceptions) && $exceptions->size()) { $recurrence_exceptions = array(); - $recurrence_id_format = $object['allday'] ? 'Ymd' : 'Ymd\THis'; + $recurrence_id_format = libkolab::recurrence_id_format($object); for ($i=0; $i < $exceptions->size(); $i++) { if (($exobj = $exceptions->get($i))) { $exception = new kolab_format_event($exobj); @@ -251,7 +251,7 @@ class kolab_format_event extends kolab_format_xcal $result = null; $object = $this->to_array(); - $recurrence_id_format = $object['allday'] ? 'Ymd' : 'Ymd\THis'; + $recurrence_id_format = libkolab::recurrence_id_format($object); $instance_id = $recurrence_id instanceof DateTime ? $recurrence_id->format($recurrence_id_format) : strval($recurrence_id); if ($object['recurrence_date'] instanceof DateTime) { diff --git a/plugins/libkolab/libkolab.php b/plugins/libkolab/libkolab.php index 20e7d37a..f1c2c878 100644 --- a/plugins/libkolab/libkolab.php +++ b/plugins/libkolab/libkolab.php @@ -139,4 +139,15 @@ class libkolab extends rcube_plugin $diff = new FineDiff($from, $to, FineDiff::$wordGranularity); return $diff->renderDiffToHTML(); } + + /** + * Return a date() format string to render identifiers for recurrence instances + * + * @param array Hash array with event properties + * @return string Format string + */ + public static function recurrence_id_format($event) + { + return $event['allday'] ? 'Ymd' : 'Ymd\THis'; + } }