2011-05-20 19:04:25 +02:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
+-------------------------------------------------------------------------+
|
|
|
|
| Kolab driver for the Calendar Plugin |
|
|
|
|
| Version 0.3 beta |
|
|
|
|
| |
|
|
|
|
| This program is free software; you can redistribute it and/or modify |
|
|
|
|
| it under the terms of the GNU General Public License version 2 |
|
|
|
|
| as published by the Free Software Foundation. |
|
|
|
|
| |
|
|
|
|
| PURPOSE: |
|
|
|
|
| Kolab bindings for the calendar backend |
|
|
|
|
| |
|
|
|
|
+-------------------------------------------------------------------------+
|
|
|
|
| Author: Thomas Bruederli <roundcube@gmail.com> |
|
|
|
|
+-------------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
require_once(dirname(__FILE__) . '/kolab_calendar.php');
|
|
|
|
|
|
|
|
class kolab_driver extends calendar_driver
|
|
|
|
{
|
|
|
|
// features this backend supports
|
2011-05-22 17:29:09 +02:00
|
|
|
public $alarms = true;
|
2011-05-20 19:04:25 +02:00
|
|
|
public $attendees = false;
|
|
|
|
public $attachments = false;
|
2011-05-26 15:44:46 +02:00
|
|
|
public $categoriesimmutable = true;
|
2011-05-20 19:04:25 +02:00
|
|
|
|
|
|
|
private $rc;
|
|
|
|
private $cal;
|
|
|
|
private $calendars;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default constructor
|
|
|
|
*/
|
|
|
|
public function __construct($cal)
|
|
|
|
{
|
|
|
|
$this->cal = $cal;
|
|
|
|
$this->rc = $cal->rc;
|
|
|
|
$this->_read_calendars();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read available calendars from server
|
|
|
|
*/
|
|
|
|
private function _read_calendars()
|
|
|
|
{
|
|
|
|
// already read sources
|
|
|
|
if (isset($this->calendars))
|
2011-06-28 10:32:52 +02:00
|
|
|
return $this->calendars;
|
2011-05-20 19:04:25 +02:00
|
|
|
|
|
|
|
// get all folders that have "event" type
|
|
|
|
$folders = rcube_kolab::get_folders('event');
|
2011-06-28 10:32:52 +02:00
|
|
|
$this->calendars = array();
|
2011-05-20 19:04:25 +02:00
|
|
|
|
|
|
|
if (PEAR::isError($folders)) {
|
2011-06-28 10:32:52 +02:00
|
|
|
raise_error(array(
|
|
|
|
'code' => 600, 'type' => 'php',
|
|
|
|
'file' => __FILE__, 'line' => __LINE__,
|
|
|
|
'message' => "Failed to list calendar folders from Kolab server:" . $folders->getMessage()),
|
|
|
|
true, false);
|
2011-05-20 19:04:25 +02:00
|
|
|
}
|
|
|
|
else {
|
2011-06-28 10:32:52 +02:00
|
|
|
// convert to UTF8 and sort
|
|
|
|
$names = array();
|
|
|
|
foreach ($folders as $folder)
|
|
|
|
$names[$folder->name] = rcube_charset_convert($folder->name, 'UTF7-IMAP');
|
2011-05-20 19:04:25 +02:00
|
|
|
|
2011-06-28 10:32:52 +02:00
|
|
|
asort($names, SORT_LOCALE_STRING);
|
2011-05-20 19:04:25 +02:00
|
|
|
|
2011-06-28 10:32:52 +02:00
|
|
|
foreach ($names as $utf7name => $name) {
|
|
|
|
$calendar = new kolab_calendar($utf7name, $this->cal);
|
|
|
|
$this->calendars[$calendar->id] = $calendar;
|
|
|
|
}
|
|
|
|
}
|
2011-05-20 19:04:25 +02:00
|
|
|
|
2011-06-28 10:32:52 +02:00
|
|
|
return $this->calendars;
|
2011-05-20 19:04:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of available calendars from this source
|
|
|
|
*/
|
|
|
|
public function list_calendars()
|
|
|
|
{
|
|
|
|
// attempt to create a default calendar for this user
|
|
|
|
if (empty($this->calendars)) {
|
2011-06-28 10:32:52 +02:00
|
|
|
if ($this->create_calendar(array('name' => 'Calendar', 'color' => 'cc0000')))
|
2011-05-20 19:04:25 +02:00
|
|
|
$this->_read_calendars();
|
|
|
|
}
|
2011-06-27 15:49:51 +02:00
|
|
|
|
2011-06-28 10:32:52 +02:00
|
|
|
$calendars = $names = array();
|
|
|
|
|
|
|
|
foreach ($this->calendars as $id => $cal) {
|
|
|
|
if ($cal->ready) {
|
|
|
|
$name = $origname = $cal->get_name();
|
|
|
|
|
|
|
|
// find folder prefix to truncate (the same code as in kolab_addressbook plugin)
|
|
|
|
for ($i = count($names)-1; $i >= 0; $i--) {
|
|
|
|
if (strpos($name, $names[$i].' » ') === 0) {
|
|
|
|
$length = strlen($names[$i].' » ');
|
|
|
|
$prefix = substr($name, 0, $length);
|
|
|
|
$count = count(explode(' » ', $prefix));
|
|
|
|
$name = str_repeat(' ', $count-1) . '» ' . substr($name, $length);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$names[] = $origname;
|
|
|
|
|
|
|
|
$calendars[$cal->id] = array(
|
|
|
|
'id' => $cal->id,
|
|
|
|
'name' => $name,
|
|
|
|
'editname' => $cal->get_foldername(),
|
|
|
|
'color' => $cal->get_color(),
|
|
|
|
'readonly' => $cal->readonly,
|
|
|
|
'class_name' => $cal->get_namespace(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $calendars;
|
2011-05-20 19:04:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new calendar assigned to the current user
|
|
|
|
*
|
|
|
|
* @param array Hash array with calendar properties
|
|
|
|
* name: Calendar name
|
|
|
|
* color: The color of the calendar
|
|
|
|
* @return mixed ID of the calendar on success, False on error
|
|
|
|
*/
|
|
|
|
public function create_calendar($prop)
|
|
|
|
{
|
2011-06-27 15:49:51 +02:00
|
|
|
$folder = rcube_charset_convert($prop['name'], RCMAIL_CHARSET, 'UTF7-IMAP');
|
|
|
|
|
|
|
|
// add namespace prefix (when needed)
|
|
|
|
$this->rc->imap_init();
|
|
|
|
$folder = $this->rc->imap->mod_mailbox($folder, 'in');
|
|
|
|
|
|
|
|
// create ID
|
|
|
|
$id = rcube_kolab::folder_id($folder);
|
|
|
|
|
|
|
|
// create IMAP folder
|
|
|
|
if (rcube_kolab::folder_create($folder, 'event')) {
|
|
|
|
// save color in user prefs (temp. solution)
|
|
|
|
$prefs['kolab_calendars'] = $this->rc->config->get('kolab_calendars', array());
|
|
|
|
$prefs['kolab_calendars'][$id]['color'] = $prop['color'];
|
|
|
|
|
|
|
|
$this->rc->user->save_prefs($prefs);
|
|
|
|
|
|
|
|
return $id;
|
|
|
|
}
|
|
|
|
|
2011-05-20 19:04:25 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-06-27 15:49:51 +02:00
|
|
|
|
2011-06-08 18:48:06 -06:00
|
|
|
/**
|
|
|
|
* Update properties of an existing calendar
|
|
|
|
*
|
|
|
|
* @see calendar_driver::edit_calendar()
|
|
|
|
*/
|
|
|
|
public function edit_calendar($prop)
|
|
|
|
{
|
2011-06-28 10:32:52 +02:00
|
|
|
if ($prop['id'] && ($cal = $this->calendars[$prop['id']])) {
|
2011-06-27 15:49:51 +02:00
|
|
|
$newfolder = rcube_charset_convert($prop['name'], RCMAIL_CHARSET, 'UTF7-IMAP');
|
|
|
|
$oldfolder = $cal->get_realname();
|
|
|
|
// add namespace prefix (when needed)
|
|
|
|
$this->rc->imap_init();
|
|
|
|
$newfolder = $this->rc->imap->mod_mailbox($newfolder, 'in');
|
|
|
|
|
|
|
|
if ($newfolder != $oldfolder)
|
|
|
|
$result = rcube_kolab::folder_rename($oldfolder, $newfolder);
|
|
|
|
else
|
|
|
|
$result = true;
|
|
|
|
|
|
|
|
if ($result) {
|
|
|
|
// create ID
|
|
|
|
$id = rcube_kolab::folder_id($newfolder);
|
|
|
|
// save color in user prefs (temp. solution)
|
|
|
|
$prefs['kolab_calendars'] = $this->rc->config->get('kolab_calendars', array());
|
|
|
|
$prefs['kolab_calendars'][$id]['color'] = $prop['color'];
|
|
|
|
unset($prefs['kolab_calendars'][$prop['id']]);
|
|
|
|
|
|
|
|
$this->rc->user->save_prefs($prefs);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-14 14:52:20 +03:00
|
|
|
return false;
|
2011-06-08 18:48:06 -06:00
|
|
|
}
|
|
|
|
|
2011-06-27 15:49:51 +02:00
|
|
|
|
2011-06-08 18:48:06 -06:00
|
|
|
/**
|
|
|
|
* Delete the given calendar with all its contents
|
|
|
|
*
|
|
|
|
* @see calendar_driver::remove_calendar()
|
|
|
|
*/
|
|
|
|
public function remove_calendar($prop)
|
|
|
|
{
|
2011-06-28 10:32:52 +02:00
|
|
|
if ($prop['id'] && ($cal = $this->calendars[$prop['id']])) {
|
2011-06-27 15:49:51 +02:00
|
|
|
$folder = $cal->get_realname();
|
|
|
|
if (rcube_kolab::folder_delete($folder)) {
|
|
|
|
// remove color in user prefs (temp. solution)
|
|
|
|
$prefs['kolab_calendars'] = $this->rc->config->get('kolab_calendars', array());
|
|
|
|
unset($prefs['kolab_calendars'][$prop['id']]);
|
|
|
|
|
|
|
|
$this->rc->user->save_prefs($prefs);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-08 18:48:06 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-20 19:04:25 +02:00
|
|
|
/**
|
|
|
|
* Add a single event to the database
|
|
|
|
*
|
|
|
|
* @see Driver:new_event()
|
|
|
|
*/
|
|
|
|
public function new_event($event)
|
|
|
|
{
|
|
|
|
$cid = $event['calendar'] ? $event['calendar'] : reset(array_keys($this->calendars));
|
2011-06-28 10:32:52 +02:00
|
|
|
if ($storage = $this->calendars($cid))
|
2011-05-20 19:04:25 +02:00
|
|
|
return $storage->insert_event($event);
|
2011-06-27 15:49:51 +02:00
|
|
|
|
2011-05-20 19:04:25 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update an event entry with the given data
|
|
|
|
*
|
|
|
|
* @see Driver:new_event()
|
|
|
|
* @return boolean True on success, False on error
|
|
|
|
*/
|
|
|
|
public function edit_event($event)
|
|
|
|
{
|
2011-06-28 10:32:52 +02:00
|
|
|
if ($storage = $this->calendars[$event['calendar']])
|
2011-05-20 19:04:25 +02:00
|
|
|
return $storage->update_event($event);
|
2011-06-27 15:49:51 +02:00
|
|
|
|
2011-05-20 19:04:25 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move a single event
|
|
|
|
*
|
|
|
|
* @see Driver:move_event()
|
|
|
|
* @return boolean True on success, False on error
|
|
|
|
*/
|
|
|
|
public function move_event($event)
|
|
|
|
{
|
2011-06-28 10:32:52 +02:00
|
|
|
if (($storage = $this->calendars[$event['calendar']]) && ($ev = $storage->get_event($event['id'])))
|
2011-05-20 19:04:25 +02:00
|
|
|
return $storage->update_event($event + $ev);
|
2011-06-27 15:49:51 +02:00
|
|
|
|
2011-05-20 19:04:25 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resize a single event
|
|
|
|
*
|
|
|
|
* @see Driver:resize_event()
|
|
|
|
* @return boolean True on success, False on error
|
|
|
|
*/
|
|
|
|
public function resize_event($event)
|
|
|
|
{
|
|
|
|
if (($storage = $this->_get_storage($event['calendar'])) && ($ev = $storage->get_event($event['id'])))
|
|
|
|
return $storage->update_event($event + $ev);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a single event from the database
|
|
|
|
*
|
|
|
|
* @param array Hash array with event properties:
|
|
|
|
* id: Event identifier
|
|
|
|
* @return boolean True on success, False on error
|
|
|
|
*/
|
|
|
|
public function remove_event($event)
|
|
|
|
{
|
2011-06-28 10:32:52 +02:00
|
|
|
if (($storage = $this->calendars[$event['calendar']]) && ($ev = $storage->get_event($event['id'])))
|
2011-06-14 15:40:47 +03:00
|
|
|
return $storage->delete_event($event);
|
2011-06-28 10:32:52 +02:00
|
|
|
|
2011-05-20 19:04:25 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get events from source.
|
|
|
|
*
|
|
|
|
* @param integer Event's new start (unix timestamp)
|
|
|
|
* @param integer Event's new end (unix timestamp)
|
|
|
|
* @param mixed List of calendar IDs to load events from (either as array or comma-separated string)
|
2011-06-13 18:41:32 -06:00
|
|
|
* @param string Search query (optional)
|
2011-05-20 19:04:25 +02:00
|
|
|
* @return array A list of event records
|
|
|
|
*/
|
2011-06-13 18:41:32 -06:00
|
|
|
public function load_events($start, $end, $calendars = null, $search = null)
|
2011-05-20 19:04:25 +02:00
|
|
|
{
|
|
|
|
if ($calendars && is_string($calendars))
|
|
|
|
$calendars = explode(',', $calendars);
|
2011-06-28 10:32:52 +02:00
|
|
|
|
2011-05-20 19:04:25 +02:00
|
|
|
$events = array();
|
|
|
|
foreach ($this->calendars as $cid => $calendar) {
|
|
|
|
if ($calendars && !in_array($cid, $calendars))
|
|
|
|
continue;
|
2011-06-28 10:32:52 +02:00
|
|
|
|
2011-06-28 10:36:11 +02:00
|
|
|
$events = array_merge($events, $this->calendars[$cid]->list_events($start, $end, $search));
|
2011-05-20 19:04:25 +02:00
|
|
|
}
|
2011-06-28 10:32:52 +02:00
|
|
|
|
2011-05-20 19:04:25 +02:00
|
|
|
return $events;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search events using the given query
|
|
|
|
*
|
|
|
|
* @see Driver::search_events()
|
|
|
|
* @return array A list of event records
|
|
|
|
*/
|
|
|
|
public function search_events($start, $end, $query, $calendars = null)
|
|
|
|
{
|
2011-06-13 18:41:32 -06:00
|
|
|
// delegate request to load_events()
|
|
|
|
return $this->load_events($start, $end, $calendars, $query);
|
2011-05-20 19:04:25 +02:00
|
|
|
}
|
|
|
|
|
2011-05-22 18:45:04 +02:00
|
|
|
/**
|
|
|
|
* Get a list of pending alarms to be displayed to the user
|
|
|
|
*
|
|
|
|
* @see Driver:pending_alarms()
|
|
|
|
*/
|
|
|
|
public function pending_alarms($time, $calendars = null)
|
|
|
|
{
|
2011-06-27 11:47:52 -04:00
|
|
|
$events = array();
|
2011-06-28 08:45:05 +02:00
|
|
|
foreach ($this->load_events($time, $time + 86400 * 365, $calendars) as $e) {
|
2011-06-27 11:47:52 -04:00
|
|
|
// add to list if alarm is set
|
2011-06-28 08:45:05 +02:00
|
|
|
if ($e['_alarm'] && ($notifyat = $e['start'] - $e['_alarm'] * 60) <= $time) {
|
2011-06-27 11:47:52 -04:00
|
|
|
$id = $e['id'];
|
|
|
|
$events[$id] = $e;
|
|
|
|
$events[$id]['notifyat'] = $notifyat;
|
|
|
|
}
|
|
|
|
}
|
2011-06-28 10:36:11 +02:00
|
|
|
|
2011-06-27 11:47:52 -04:00
|
|
|
// get alarm information stored in local database
|
|
|
|
if (!empty($events)) {
|
2011-06-28 08:45:05 +02:00
|
|
|
$event_ids = array_map(array($this->rc->db, 'quote'), array_keys($events));
|
2011-06-27 11:47:52 -04:00
|
|
|
$result = $this->rc->db->query(sprintf(
|
|
|
|
"SELECT * FROM kolab_alarms
|
2011-06-28 08:45:05 +02:00
|
|
|
WHERE event_id IN (%s)",
|
2011-06-27 11:47:52 -04:00
|
|
|
join(',', $event_ids),
|
|
|
|
$this->rc->db->now()
|
|
|
|
));
|
|
|
|
|
|
|
|
while ($result && ($e = $this->rc->db->fetch_assoc($result))) {
|
|
|
|
$dbdata[$e['event_id']] = $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$alarms = array();
|
|
|
|
foreach ($events as $id => $e) {
|
|
|
|
// skip dismissed
|
|
|
|
if ($dbdata[$id]['dismissed'])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// snooze function may have shifted alarm time
|
2011-06-28 08:45:05 +02:00
|
|
|
$notifyat = $dbdata[$id]['notifyat'] ? strtotime($dbdata[$id]['notifyat']) : $e['notifyat'];
|
2011-06-27 11:47:52 -04:00
|
|
|
if ($notifyat <= $time)
|
|
|
|
$alarms[] = $e;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $alarms;
|
2011-05-22 18:45:04 +02:00
|
|
|
}
|
|
|
|
|
2011-05-23 21:00:14 +02:00
|
|
|
/**
|
|
|
|
* Feedback after showing/sending an alarm notification
|
|
|
|
*
|
2011-05-25 23:16:13 +02:00
|
|
|
* @see Driver:dismiss_alarm()
|
2011-05-23 21:00:14 +02:00
|
|
|
*/
|
2011-05-25 23:16:13 +02:00
|
|
|
public function dismiss_alarm($event_id, $snooze = 0)
|
2011-05-23 21:00:14 +02:00
|
|
|
{
|
2011-06-27 11:47:52 -04:00
|
|
|
// set new notifyat time or unset if not snoozed
|
|
|
|
$notifyat = $snooze > 0 ? date('Y-m-d H:i:s', time() + $snooze) : null;
|
|
|
|
|
|
|
|
$query = $this->rc->db->query(
|
2011-06-28 08:45:05 +02:00
|
|
|
"REPLACE INTO kolab_alarms
|
|
|
|
(event_id, dismissed, notifyat)
|
|
|
|
VALUES(?, ?, ?)",
|
|
|
|
$event_id,
|
2011-06-27 11:47:52 -04:00
|
|
|
$snooze > 0 ? 0 : 1,
|
2011-06-28 08:45:05 +02:00
|
|
|
$notifyat
|
2011-06-27 11:47:52 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
return $this->rc->db->affected_rows($query);
|
2011-05-23 21:00:14 +02:00
|
|
|
}
|
|
|
|
|
2011-05-20 19:04:25 +02:00
|
|
|
/**
|
|
|
|
* Save an attachment related to the given event
|
|
|
|
*/
|
|
|
|
public function add_attachment($attachment, $event_id)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a specific attachment from the given event
|
|
|
|
*/
|
|
|
|
public function remove_attachment($attachment, $event_id)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2011-05-26 15:44:46 +02:00
|
|
|
* List availabale categories
|
|
|
|
* The default implementation reads them from config/user prefs
|
2011-05-20 19:04:25 +02:00
|
|
|
*/
|
2011-05-26 15:44:46 +02:00
|
|
|
public function list_categories()
|
2011-05-20 19:04:25 +02:00
|
|
|
{
|
2011-05-26 15:44:46 +02:00
|
|
|
# fixed list according to http://www.kolab.org/doc/kolabformat-2.0rc7-html/c300.html
|
|
|
|
return array(
|
|
|
|
'important' => 'cc0000',
|
|
|
|
'business' => '333333',
|
|
|
|
'personal' => '333333',
|
|
|
|
'vacation' => '333333',
|
|
|
|
'must-attend' => '333333',
|
|
|
|
'travel-required' => '333333',
|
|
|
|
'needs-preparation' => '333333',
|
|
|
|
'birthday' => '333333',
|
|
|
|
'anniversary' => '333333',
|
|
|
|
'phone-call' => '333333',
|
|
|
|
);
|
2011-05-20 19:04:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch free/busy information from a person within the given range
|
|
|
|
*/
|
|
|
|
public function get_freebusy_list($email, $start, $end)
|
|
|
|
{
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|