Fix calendar_driver::get_event() for birthday calendar entries

This commit is contained in:
Thomas Bruederli 2014-08-06 09:34:08 +02:00
parent e7dfea1e03
commit 46315c8ad5
3 changed files with 72 additions and 2 deletions

View file

@ -563,7 +563,11 @@ abstract class calendar_driver
$birthyear = $bday->format('Y');
}
catch (Exception $e) {
console('BIRTHDAY PARSE ERROR: ' . $e);
rcube::raise_error(array(
'code' => 600, 'type' => 'php',
'file' => __FILE__, 'line' => __LINE__,
'message' => 'BIRTHDAY PARSE ERROR: ' . $e),
true, false);
continue;
}
@ -598,7 +602,7 @@ abstract class calendar_driver
if ($bday <= $end && $bday >= $start) {
$age = $year - $birthyear;
$event = array(
'id' => md5('bday_' . $contact['ID'] . $year),
'id' => rcube_ldap::dn_encode('bday:' . $source . ':' . $contact['ID'] . ':' . $year),
'calendar' => self::BIRTHDAY_CALENDAR_ID,
'title' => $event_title,
'description' => $rcmail->gettext(array('name' => 'birthdayage', 'vars' => array('age' => $age)), 'calendar'),
@ -624,6 +628,62 @@ abstract class calendar_driver
return $events;
}
/**
* Get a single birthday calendar event
*/
public function get_birthday_event($id)
{
// decode $id
list(,$source,$contact_id,$year) = explode(':', rcube_ldap::dn_decode($id));
$rcmail = rcmail::get_instance();
if ($source && $contact_id && ($abook = $rcmail->get_address_book($source))) {
$contact = $abook->get_record($contact_id, true);
if (is_array($contact) && !empty($contact['birthday'])) {
try {
if (is_array($contact['birthday']))
$contact['birthday'] = reset($contact['birthday']);
$bday = $contact['birthday'] instanceof DateTime ? $contact['birthday'] :
new DateTime($contact['birthday'], new DateTimezone('UTC'));
$birthyear = $bday->format('Y');
}
catch (Exception $e) {
rcube::raise_error(array(
'code' => 600, 'type' => 'php',
'file' => __FILE__, 'line' => __LINE__,
'message' => 'BIRTHDAY PARSE ERROR: ' . $e),
true, false);
return null;
}
$display_name = rcube_addressbook::compose_display_name($contact);
$event_title = $rcmail->gettext(array('name' => 'birthdayeventtitle', 'vars' => array('name' => $display_name)), 'calendar');
$event = array(
'id' => rcube_ldap::dn_encode('bday:' . $source . ':' . $contact['ID'] . ':' . $year),
'uid' => rcube_ldap::dn_encode('bday:' . $source . ':' . $contact['ID'] . ':' . $birthyear),
'calendar' => self::BIRTHDAY_CALENDAR_ID,
'title' => $event_title,
'description' => '',
'allday' => true,
'start' => $bday,
'recurrence' => array('FREQ' => 'YEARLY', 'INTERVAL' => 1),
'free_busy' => 'free',
);
$event['end'] = clone $bday;
$event['end']->add(new DateInterval('PT1H'));
return $event;
}
}
return null;
}
/**
* Handler for user_delete plugin hook
*

View file

@ -744,11 +744,17 @@ class database_driver extends calendar_driver
public function get_event($event, $writeable = false, $active = false, $personal = false)
{
$id = is_array($event) ? ($event['id'] ? $event['id'] : $event['uid']) : $event;
$cal = is_array($event) ? $event['calendar'] : null;
$col = is_array($event) && is_numeric($id) ? 'event_id' : 'uid';
if ($this->cache[$id])
return $this->cache[$id];
// get event from the address books birthday calendar
if ($cal == self::BIRTHDAY_CALENDAR_ID) {
return $this->get_birthday_event($id);
}
if ($active) {
$calendars = $this->calendars;
foreach ($calendars as $idx => $cal) {

View file

@ -524,6 +524,10 @@ class kolab_driver extends calendar_driver
if ($storage = $this->get_calendar($cal)) {
return $storage->get_event($id);
}
// get event from the address books birthday calendar
else if ($cal == self::BIRTHDAY_CALENDAR_ID) {
return $this->get_birthday_event($id);
}
}
// iterate over all calendar folders and search for the event ID
else {