Code cleanup: get recurrence instance identifiers from utility functions

This commit is contained in:
Thomas Bruederli 2015-03-10 14:30:50 +01:00
parent 738cdb7017
commit 60205136ee
9 changed files with 62 additions and 30 deletions

View file

@ -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) {

View file

@ -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];

View file

@ -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);
}

View file

@ -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'])) {

View file

@ -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']);

View file

@ -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 *********/
/**

View file

@ -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);

View file

@ -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) {

View file

@ -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';
}
}