2011-05-20 19:04:25 +02:00
|
|
|
<?php
|
2011-08-21 12:48:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Database driver for the Calendar plugin
|
|
|
|
*
|
|
|
|
* @author Lazlo Westerhof <hello@lazlo.me>
|
2011-12-07 12:51:23 +01:00
|
|
|
* @author Thomas Bruederli <bruederli@kolabsys.com>
|
2011-08-21 12:48:33 +02:00
|
|
|
*
|
2011-10-27 10:20:46 +02:00
|
|
|
* Copyright (C) 2010, Lazlo Westerhof <hello@lazlo.me>
|
2015-03-01 18:54:54 +01:00
|
|
|
* Copyright (C) 2012-2015, Kolab Systems AG <contact@kolabsys.com>
|
2011-08-21 12:48:33 +02:00
|
|
|
*
|
2011-10-27 10:20:46 +02:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
2011-08-21 12:48:33 +02:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2011-10-27 10:20:46 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2011-08-21 12:48:33 +02:00
|
|
|
*
|
2011-10-27 10:20:46 +02:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2011-08-21 12:48:33 +02:00
|
|
|
*/
|
|
|
|
|
2011-05-20 19:04:25 +02:00
|
|
|
|
|
|
|
class database_driver extends calendar_driver
|
|
|
|
{
|
2024-01-24 11:24:41 +01:00
|
|
|
public const DB_DATE_FORMAT = 'Y-m-d H:i:s';
|
2019-09-28 09:35:22 +02:00
|
|
|
|
2024-01-24 11:24:41 +01:00
|
|
|
public static $scheduling_properties = ['start', 'end', 'allday', 'recurrence', 'location', 'cancelled'];
|
2019-09-28 09:35:22 +02:00
|
|
|
|
|
|
|
// features this backend supports
|
|
|
|
public $alarms = true;
|
|
|
|
public $attendees = true;
|
|
|
|
public $freebusy = false;
|
|
|
|
public $attachments = true;
|
2024-01-24 11:24:41 +01:00
|
|
|
public $alarm_types = ['DISPLAY'];
|
2019-09-28 09:35:22 +02:00
|
|
|
|
|
|
|
private $rc;
|
|
|
|
private $cal;
|
2024-01-24 11:24:41 +01:00
|
|
|
private $cache = [];
|
|
|
|
private $calendars = [];
|
2019-09-28 09:35:22 +02:00
|
|
|
private $calendar_ids = '';
|
2024-01-24 11:24:41 +01:00
|
|
|
private $free_busy_map = ['free' => 0, 'busy' => 1, 'out-of-office' => 2, 'outofoffice' => 2, 'tentative' => 3];
|
|
|
|
private $sensitivity_map = ['public' => 0, 'private' => 1, 'confidential' => 2];
|
2019-09-28 09:35:22 +02:00
|
|
|
private $server_timezone;
|
|
|
|
|
|
|
|
private $db_events = 'events';
|
|
|
|
private $db_calendars = 'calendars';
|
|
|
|
private $db_attachments = 'attachments';
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default constructor
|
|
|
|
*/
|
|
|
|
public function __construct($cal)
|
|
|
|
{
|
|
|
|
$this->cal = $cal;
|
|
|
|
$this->rc = $cal->rc;
|
|
|
|
$this->server_timezone = new DateTimeZone(date_default_timezone_get());
|
|
|
|
|
|
|
|
// read database config
|
|
|
|
$db = $this->rc->get_dbh();
|
|
|
|
$this->db_events = $db->table_name($this->rc->config->get('db_table_events', $this->db_events));
|
|
|
|
$this->db_calendars = $db->table_name($this->rc->config->get('db_table_calendars', $this->db_calendars));
|
|
|
|
$this->db_attachments = $db->table_name($this->rc->config->get('db_table_attachments', $this->db_attachments));
|
|
|
|
|
2011-05-20 19:04:25 +02:00
|
|
|
$this->_read_calendars();
|
|
|
|
}
|
2012-12-04 20:10:04 +01:00
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
/**
|
|
|
|
* Read available calendars for the current user and store them internally
|
|
|
|
*/
|
|
|
|
private function _read_calendars()
|
|
|
|
{
|
|
|
|
$hidden = array_filter(explode(',', $this->rc->config->get('hidden_calendars', '')));
|
2024-01-25 13:47:41 +01:00
|
|
|
$db = $this->rc->get_dbh();
|
2019-09-28 09:35:22 +02:00
|
|
|
|
|
|
|
if (!empty($this->rc->user->ID)) {
|
2024-01-24 11:24:41 +01:00
|
|
|
$calendar_ids = [];
|
2024-01-25 13:47:41 +01:00
|
|
|
$result = $db->query(
|
2019-09-28 09:35:22 +02:00
|
|
|
"SELECT *, `calendar_id` AS id FROM `{$this->db_calendars}`"
|
|
|
|
. " WHERE `user_id` = ?"
|
|
|
|
. " ORDER BY `name`",
|
|
|
|
$this->rc->user->ID
|
|
|
|
);
|
|
|
|
|
2024-01-25 13:47:41 +01:00
|
|
|
while ($result && ($arr = $db->fetch_assoc($result))) {
|
2019-09-28 09:35:22 +02:00
|
|
|
$arr['showalarms'] = intval($arr['showalarms']);
|
|
|
|
$arr['active'] = !in_array($arr['id'], $hidden);
|
|
|
|
$arr['name'] = html::quote($arr['name']);
|
|
|
|
$arr['listname'] = html::quote($arr['name']);
|
|
|
|
$arr['rights'] = 'lrswikxteav';
|
|
|
|
$arr['editable'] = true;
|
|
|
|
|
|
|
|
$this->calendars[$arr['calendar_id']] = $arr;
|
2024-01-25 13:47:41 +01:00
|
|
|
$calendar_ids[] = $db->quote($arr['calendar_id']);
|
2019-09-28 09:35:22 +02:00
|
|
|
}
|
|
|
|
|
2024-01-24 11:24:41 +01:00
|
|
|
$this->calendar_ids = implode(',', $calendar_ids);
|
2019-09-28 09:35:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of available calendars from this source
|
|
|
|
*
|
|
|
|
* @param integer Bitmask defining filter criterias
|
|
|
|
*
|
|
|
|
* @return array List of calendars
|
|
|
|
*/
|
|
|
|
public function list_calendars($filter = 0)
|
|
|
|
{
|
|
|
|
// attempt to create a default calendar for this user
|
|
|
|
if (empty($this->calendars)) {
|
2024-01-24 11:24:41 +01:00
|
|
|
if ($this->create_calendar(['name' => 'Default', 'color' => 'cc0000', 'showalarms' => true])) {
|
2019-09-28 09:35:22 +02:00
|
|
|
$this->_read_calendars();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$calendars = $this->calendars;
|
|
|
|
|
|
|
|
// filter active calendars
|
|
|
|
if ($filter & self::FILTER_ACTIVE) {
|
|
|
|
foreach ($calendars as $idx => $cal) {
|
|
|
|
if (!$cal['active']) {
|
|
|
|
unset($calendars[$idx]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 'personal' is unsupported in this driver
|
|
|
|
|
|
|
|
// append the virtual birthdays calendar
|
|
|
|
if ($this->rc->config->get('calendar_contact_birthdays', false)) {
|
2024-01-24 11:24:41 +01:00
|
|
|
$prefs = $this->rc->config->get('birthday_calendar', ['color' => '87CEFA']);
|
2019-09-28 09:35:22 +02:00
|
|
|
$hidden = array_filter(explode(',', $this->rc->config->get('hidden_calendars', '')));
|
|
|
|
$id = self::BIRTHDAY_CALENDAR_ID;
|
|
|
|
|
2024-01-24 10:59:25 +01:00
|
|
|
if (!in_array($id, $hidden)) {
|
2024-01-24 11:24:41 +01:00
|
|
|
$calendars[$id] = [
|
2019-09-28 09:35:22 +02:00
|
|
|
'id' => $id,
|
|
|
|
'name' => $this->cal->gettext('birthdays'),
|
|
|
|
'listname' => $this->cal->gettext('birthdays'),
|
|
|
|
'color' => $prefs['color'],
|
|
|
|
'showalarms' => (bool)$this->rc->config->get('calendar_birthdays_alarm_type'),
|
|
|
|
'active' => !in_array($id, $hidden),
|
|
|
|
'group' => 'x-birthdays',
|
|
|
|
'editable' => false,
|
|
|
|
'default' => false,
|
|
|
|
'children' => false,
|
2024-01-24 11:24:41 +01:00
|
|
|
];
|
2019-09-28 09:35:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $calendars;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
$result = $this->rc->db->query(
|
|
|
|
"INSERT INTO `{$this->db_calendars}`"
|
|
|
|
. " (`user_id`, `name`, `color`, `showalarms`)"
|
|
|
|
. " VALUES (?, ?, ?, ?)",
|
|
|
|
$this->rc->user->ID,
|
|
|
|
$prop['name'],
|
|
|
|
strval($prop['color']),
|
2021-02-01 08:30:34 +01:00
|
|
|
!empty($prop['showalarms']) ? 1 : 0
|
2019-09-28 09:35:22 +02:00
|
|
|
);
|
2012-12-04 20:10:04 +01:00
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
if ($result) {
|
|
|
|
return $this->rc->db->insert_id($this->db_calendars);
|
2012-12-04 20:10:04 +01:00
|
|
|
}
|
2019-09-28 09:35:22 +02:00
|
|
|
|
|
|
|
return false;
|
2012-12-04 20:10:04 +01:00
|
|
|
}
|
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
/**
|
|
|
|
* Update properties of an existing calendar
|
|
|
|
*
|
|
|
|
* @see calendar_driver::edit_calendar()
|
|
|
|
*/
|
|
|
|
public function edit_calendar($prop)
|
|
|
|
{
|
|
|
|
// birthday calendar properties are saved in user prefs
|
|
|
|
if ($prop['id'] == self::BIRTHDAY_CALENDAR_ID) {
|
2024-01-24 11:24:41 +01:00
|
|
|
$prefs['birthday_calendar'] = $this->rc->config->get('birthday_calendar', ['color' => '87CEFA']);
|
2019-09-28 09:35:22 +02:00
|
|
|
if (isset($prop['color'])) {
|
|
|
|
$prefs['birthday_calendar']['color'] = $prop['color'];
|
|
|
|
}
|
|
|
|
if (isset($prop['showalarms'])) {
|
|
|
|
$prefs['calendar_birthdays_alarm_type'] = $prop['showalarms'] ? $this->alarm_types[0] : '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->rc->user->save_prefs($prefs);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = $this->rc->db->query(
|
|
|
|
"UPDATE `{$this->db_calendars}`"
|
|
|
|
. " SET `name` = ?, `color` = ?, `showalarms` = ?"
|
|
|
|
. " WHERE `calendar_id` = ? AND `user_id` = ?",
|
|
|
|
$prop['name'],
|
|
|
|
strval($prop['color']),
|
|
|
|
$prop['showalarms'] ? 1 : 0,
|
|
|
|
$prop['id'],
|
|
|
|
$this->rc->user->ID
|
2014-01-27 19:12:29 +01:00
|
|
|
);
|
2019-09-28 09:35:22 +02:00
|
|
|
|
|
|
|
return $this->rc->db->affected_rows($query);
|
2014-01-27 19:12:29 +01:00
|
|
|
}
|
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
/**
|
|
|
|
* Set active/subscribed state of a calendar
|
|
|
|
* Save a list of hidden calendars in user prefs
|
|
|
|
*
|
|
|
|
* @see calendar_driver::subscribe_calendar()
|
|
|
|
*/
|
|
|
|
public function subscribe_calendar($prop)
|
|
|
|
{
|
|
|
|
$hidden = array_flip(explode(',', $this->rc->config->get('hidden_calendars', '')));
|
|
|
|
|
|
|
|
if ($prop['active']) {
|
|
|
|
unset($hidden[$prop['id']]);
|
2024-01-24 11:24:41 +01:00
|
|
|
} else {
|
2019-09-28 09:35:22 +02:00
|
|
|
$hidden[$prop['id']] = 1;
|
|
|
|
}
|
|
|
|
|
2024-01-24 11:24:41 +01:00
|
|
|
return $this->rc->user->save_prefs(['hidden_calendars' => implode(',', array_keys($hidden))]);
|
2014-01-27 19:12:29 +01:00
|
|
|
}
|
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
/**
|
|
|
|
* Delete the given calendar with all its contents
|
|
|
|
*
|
|
|
|
* @see calendar_driver::delete_calendar()
|
|
|
|
*/
|
|
|
|
public function delete_calendar($prop)
|
|
|
|
{
|
|
|
|
if (!$this->calendars[$prop['id']]) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-07 11:31:30 +02:00
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
// events and attachments will be deleted by foreign key cascade
|
2011-07-01 21:08:12 +02:00
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
$query = $this->rc->db->query(
|
|
|
|
"DELETE FROM `{$this->db_calendars}` WHERE `calendar_id` = ? AND `user_id` = ?",
|
|
|
|
$prop['id'],
|
|
|
|
$this->rc->user->ID
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->rc->db->affected_rows($query);
|
2015-03-01 18:54:54 +01:00
|
|
|
}
|
2019-09-28 09:35:22 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Search for shared or otherwise not listed calendars the user has access
|
|
|
|
*
|
|
|
|
* @param string Search string
|
|
|
|
* @param string Section/source to search
|
|
|
|
*
|
|
|
|
* @return array List of calendars
|
|
|
|
*/
|
|
|
|
public function search_calendars($query, $source)
|
|
|
|
{
|
|
|
|
// not implemented
|
2024-01-24 11:24:41 +01:00
|
|
|
return [];
|
2011-05-20 19:04:25 +02:00
|
|
|
}
|
2015-03-01 18:54:54 +01:00
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
/**
|
|
|
|
* Add a single event to the database
|
|
|
|
*
|
|
|
|
* @param array Hash array with event properties
|
|
|
|
* @see calendar_driver::new_event()
|
|
|
|
*/
|
|
|
|
public function new_event($event)
|
|
|
|
{
|
|
|
|
if (!$this->validate($event)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->calendars)) {
|
|
|
|
if ($event['calendar'] && !$this->calendars[$event['calendar']]) {
|
|
|
|
return false;
|
2011-06-01 18:35:10 +02:00
|
|
|
}
|
2019-09-28 09:35:22 +02:00
|
|
|
|
|
|
|
if (!$event['calendar']) {
|
|
|
|
$event['calendar'] = reset(array_keys($this->calendars));
|
2011-06-01 18:35:10 +02:00
|
|
|
}
|
2019-09-28 09:35:22 +02:00
|
|
|
|
|
|
|
if ($event_id = $this->_insert_event($event)) {
|
|
|
|
$this->_update_recurring($event);
|
2013-11-20 12:36:17 +01:00
|
|
|
}
|
2019-09-28 09:35:22 +02:00
|
|
|
|
|
|
|
return $event_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private function _insert_event(&$event)
|
|
|
|
{
|
|
|
|
$event = $this->_save_preprocess($event);
|
|
|
|
$now = $this->rc->db->now();
|
|
|
|
|
|
|
|
$this->rc->db->query(
|
|
|
|
"INSERT INTO `{$this->db_events}`"
|
|
|
|
. " (`calendar_id`, `created`, `changed`, `uid`, `recurrence_id`, `instance`,"
|
|
|
|
. " `isexception`, `start`, `end`, `all_day`, `recurrence`, `title`, `description`,"
|
|
|
|
. " `location`, `categories`, `url`, `free_busy`, `priority`, `sensitivity`,"
|
|
|
|
. " `status`, `attendees`, `alarms`, `notifyat`)"
|
|
|
|
. " VALUES (?, $now, $now, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
|
|
|
$event['calendar'],
|
|
|
|
strval($event['uid']),
|
2021-02-01 08:30:34 +01:00
|
|
|
isset($event['recurrence_id']) ? intval($event['recurrence_id']) : 0,
|
|
|
|
isset($event['_instance']) ? strval($event['_instance']) : '',
|
|
|
|
isset($event['isexception']) ? intval($event['isexception']) : 0,
|
2019-09-28 09:35:22 +02:00
|
|
|
$event['start']->format(self::DB_DATE_FORMAT),
|
|
|
|
$event['end']->format(self::DB_DATE_FORMAT),
|
|
|
|
intval($event['all_day']),
|
|
|
|
$event['_recurrence'],
|
|
|
|
strval($event['title']),
|
2021-02-01 08:30:34 +01:00
|
|
|
isset($event['description']) ? strval($event['description']) : '',
|
|
|
|
isset($event['location']) ? strval($event['location']) : '',
|
2024-01-24 11:24:41 +01:00
|
|
|
isset($event['categories']) ? implode(',', (array) $event['categories']) : '',
|
2021-02-01 08:30:34 +01:00
|
|
|
isset($event['url']) ? strval($event['url']) : '',
|
2019-09-28 09:35:22 +02:00
|
|
|
intval($event['free_busy']),
|
|
|
|
intval($event['priority']),
|
|
|
|
intval($event['sensitivity']),
|
2021-02-01 08:30:34 +01:00
|
|
|
isset($event['status']) ? strval($event['status']) : '',
|
2019-09-28 09:35:22 +02:00
|
|
|
$event['attendees'],
|
2024-01-24 11:24:41 +01:00
|
|
|
$event['alarms'] ?? null,
|
2019-09-28 09:35:22 +02:00
|
|
|
$event['notifyat']
|
|
|
|
);
|
|
|
|
|
|
|
|
$event_id = $this->rc->db->insert_id($this->db_events);
|
|
|
|
|
|
|
|
if ($event_id) {
|
|
|
|
$event['id'] = $event_id;
|
|
|
|
|
|
|
|
// add attachments
|
|
|
|
if (!empty($event['attachments'])) {
|
|
|
|
foreach ($event['attachments'] as $attachment) {
|
|
|
|
$this->add_attachment($attachment, $event_id);
|
|
|
|
unset($attachment);
|
2015-03-01 18:54:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
return $event_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2015-03-01 18:54:54 +01:00
|
|
|
}
|
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
/**
|
|
|
|
* Update an event entry with the given data
|
|
|
|
*
|
|
|
|
* @param array Hash array with event properties
|
|
|
|
* @see calendar_driver::edit_event()
|
|
|
|
*/
|
|
|
|
public function edit_event($event)
|
|
|
|
{
|
|
|
|
if (!empty($this->calendars)) {
|
|
|
|
$update_master = false;
|
|
|
|
$update_recurring = true;
|
|
|
|
|
|
|
|
$old = $this->get_event($event);
|
|
|
|
$ret = true;
|
|
|
|
|
|
|
|
// check if update affects scheduling and update attendee status accordingly
|
|
|
|
$reschedule = $this->_check_scheduling($event, $old, true);
|
|
|
|
|
|
|
|
// increment sequence number
|
|
|
|
if (empty($event['sequence']) && $reschedule) {
|
2021-02-01 08:30:34 +01:00
|
|
|
$event['sequence'] = $old['sequence'] + 1;
|
2019-09-28 09:35:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// modify a recurring event, check submitted savemode to do the right things
|
|
|
|
if ($old['recurrence'] || $old['recurrence_id']) {
|
2024-01-24 11:24:41 +01:00
|
|
|
$master = $old['recurrence_id'] ? $this->get_event(['id' => $old['recurrence_id']]) : $old;
|
2019-09-28 09:35:22 +02:00
|
|
|
|
|
|
|
// keep saved exceptions (not submitted by the client)
|
2021-02-01 08:30:34 +01:00
|
|
|
if (!empty($old['recurrence']['EXDATE'])) {
|
2019-09-28 09:35:22 +02:00
|
|
|
$event['recurrence']['EXDATE'] = $old['recurrence']['EXDATE'];
|
|
|
|
}
|
|
|
|
|
2024-01-24 11:24:41 +01:00
|
|
|
$savemode = $event['_savemode'] ?? null;
|
2021-02-01 08:30:34 +01:00
|
|
|
switch ($savemode) {
|
2024-01-24 11:24:41 +01:00
|
|
|
case 'new':
|
|
|
|
$event['uid'] = $this->cal->generate_uid();
|
|
|
|
return $this->new_event($event);
|
|
|
|
|
|
|
|
case 'current':
|
|
|
|
// save as exception
|
|
|
|
$event['isexception'] = 1;
|
|
|
|
$update_recurring = false;
|
|
|
|
|
|
|
|
// set exception to first instance (= master)
|
|
|
|
if ($event['id'] == $master['id']) {
|
|
|
|
$event += $old;
|
|
|
|
$event['recurrence_id'] = $master['id'];
|
|
|
|
$event['_instance'] = libcalendaring::recurrence_instance_identifier($old, $master['allday']);
|
|
|
|
$event['isexception'] = 1;
|
|
|
|
$event_id = $this->_insert_event($event);
|
|
|
|
|
|
|
|
return $event_id;
|
|
|
|
}
|
|
|
|
break;
|
2019-09-28 09:35:22 +02:00
|
|
|
|
2024-01-24 11:24:41 +01:00
|
|
|
case 'future':
|
|
|
|
if ($master['id'] != $event['id']) {
|
|
|
|
// set until-date on master event, then save this instance as new recurring event
|
|
|
|
$master['recurrence']['UNTIL'] = clone $event['start'];
|
|
|
|
$master['recurrence']['UNTIL']->sub(new DateInterval('P1D'));
|
|
|
|
unset($master['recurrence']['COUNT']);
|
|
|
|
$update_master = true;
|
|
|
|
|
|
|
|
// if recurrence COUNT, update value to the correct number of future occurences
|
|
|
|
if ($event['recurrence']['COUNT']) {
|
|
|
|
$fromdate = clone $event['start'];
|
|
|
|
$fromdate->setTimezone($this->server_timezone);
|
|
|
|
|
|
|
|
$query = $this->rc->db->query(
|
|
|
|
"SELECT `event_id` FROM `{$this->db_events}`"
|
|
|
|
. " WHERE `calendar_id` IN ({$this->calendar_ids})"
|
|
|
|
. " AND `start` >= ? AND `recurrence_id` = ?",
|
|
|
|
$fromdate->format(self::DB_DATE_FORMAT),
|
|
|
|
$master['id']
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($count = $this->rc->db->num_rows($query)) {
|
|
|
|
$event['recurrence']['COUNT'] = $count;
|
|
|
|
}
|
2019-09-28 09:35:22 +02:00
|
|
|
}
|
2024-01-24 11:24:41 +01:00
|
|
|
|
|
|
|
$update_recurring = true;
|
|
|
|
$event['recurrence_id'] = 0;
|
|
|
|
$event['isexception'] = 0;
|
|
|
|
$event['_instance'] = '';
|
|
|
|
break;
|
2019-09-28 09:35:22 +02:00
|
|
|
}
|
2024-01-24 11:24:41 +01:00
|
|
|
// else: 'future' == 'all' if modifying the master event
|
2019-09-28 09:35:22 +02:00
|
|
|
|
2024-01-24 11:24:41 +01:00
|
|
|
// no break
|
|
|
|
default: // 'all' is default
|
|
|
|
$event['id'] = $master['id'];
|
2019-09-28 09:35:22 +02:00
|
|
|
$event['recurrence_id'] = 0;
|
|
|
|
|
2024-01-24 11:24:41 +01:00
|
|
|
// use start date from master but try to be smart on time or duration changes
|
|
|
|
$old_start_date = $old['start']->format('Y-m-d');
|
|
|
|
$old_start_time = $old['allday'] ? '' : $old['start']->format('H:i');
|
|
|
|
$old_duration = $old['end']->format('U') - $old['start']->format('U');
|
|
|
|
|
|
|
|
$new_start_date = $event['start']->format('Y-m-d');
|
|
|
|
$new_start_time = $event['allday'] ? '' : $event['start']->format('H:i');
|
|
|
|
$new_duration = $event['end']->format('U') - $event['start']->format('U');
|
|
|
|
|
|
|
|
$diff = $old_start_date != $new_start_date || $old_start_time != $new_start_time || $old_duration != $new_duration;
|
|
|
|
$date_shift = $old['start']->diff($event['start']);
|
|
|
|
|
|
|
|
// shifted or resized
|
|
|
|
if ($diff && ($old_start_date == $new_start_date || $old_duration == $new_duration)) {
|
|
|
|
$event['start'] = $master['start']->add($old['start']->diff($event['start']));
|
|
|
|
$event['end'] = clone $event['start'];
|
|
|
|
$event['end']->add(new DateInterval('PT' . $new_duration . 'S'));
|
|
|
|
}
|
|
|
|
// dates did not change, use the ones from master
|
|
|
|
elseif ($new_start_date . $new_start_time == $old_start_date . $old_start_time) {
|
|
|
|
$event['start'] = $master['start'];
|
|
|
|
$event['end'] = $master['end'];
|
|
|
|
}
|
2019-09-28 09:35:22 +02:00
|
|
|
|
2024-01-24 11:24:41 +01:00
|
|
|
// 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 = 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')) {
|
|
|
|
$recurrence_id->add($date_shift);
|
|
|
|
$exception['_instance'] = $recurrence_id->format($recurrence_id_format);
|
|
|
|
$this->_update_event($exception, false);
|
|
|
|
}
|
2019-09-28 09:35:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-24 11:24:41 +01:00
|
|
|
$ret = $event['id']; // return master ID
|
|
|
|
break;
|
2019-09-28 09:35:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$success = $this->_update_event($event, $update_recurring);
|
|
|
|
|
2024-01-24 10:59:25 +01:00
|
|
|
if ($success && $update_master && !empty($master)) {
|
2019-09-28 09:35:22 +02:00
|
|
|
$this->_update_event($master, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $success ? $ret : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2015-03-01 18:54:54 +01:00
|
|
|
}
|
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
/**
|
|
|
|
* Extended event editing with possible changes to the argument
|
|
|
|
*
|
|
|
|
* @param array Hash array with event properties
|
|
|
|
* @param string New participant status
|
|
|
|
* @param array List of hash arrays with updated attendees
|
|
|
|
*
|
|
|
|
* @return boolean True on success, False on error
|
|
|
|
*/
|
|
|
|
public function edit_rsvp(&$event, $status, $attendees)
|
|
|
|
{
|
|
|
|
$update_event = $event;
|
|
|
|
|
|
|
|
// apply changes to master (and all exceptions)
|
|
|
|
if ($event['_savemode'] == 'all' && $event['recurrence_id']) {
|
2024-01-24 11:24:41 +01:00
|
|
|
$update_event = $this->get_event(['id' => $event['recurrence_id']]);
|
2019-09-28 09:35:22 +02:00
|
|
|
$update_event['_savemode'] = $event['_savemode'];
|
|
|
|
calendar::merge_attendee_data($update_event, $attendees);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($ret = $this->update_attendees($update_event, $attendees)) {
|
|
|
|
// replace $event with effectively updated event (for iTip reply)
|
2024-01-24 11:24:41 +01:00
|
|
|
if ($ret !== true && $ret != $update_event['id'] && ($new_event = $this->get_event(['id' => $ret]))) {
|
2019-09-28 09:35:22 +02:00
|
|
|
$event = $new_event;
|
2024-01-24 11:24:41 +01:00
|
|
|
} else {
|
2019-09-28 09:35:22 +02:00
|
|
|
$event = $update_event;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ret;
|
2015-03-01 18:54:54 +01:00
|
|
|
}
|
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
/**
|
|
|
|
* Update the participant status for the given attendees
|
|
|
|
*
|
|
|
|
* @see calendar_driver::update_attendees()
|
|
|
|
*/
|
|
|
|
public function update_attendees(&$event, $attendees)
|
|
|
|
{
|
2024-01-24 10:59:25 +01:00
|
|
|
$success = $this->edit_event($event);
|
2019-09-28 09:35:22 +02:00
|
|
|
|
|
|
|
// apply attendee updates to recurrence exceptions too
|
|
|
|
if ($success && $event['_savemode'] == 'all'
|
|
|
|
&& !empty($event['recurrence'])
|
|
|
|
&& empty($event['recurrence_id'])
|
|
|
|
&& ($exceptions = $this->_load_exceptions($event))
|
|
|
|
) {
|
|
|
|
foreach ($exceptions as $exception) {
|
|
|
|
calendar::merge_attendee_data($exception, $attendees);
|
|
|
|
$this->_update_event($exception, false);
|
|
|
|
}
|
|
|
|
}
|
2015-03-01 18:54:54 +01:00
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
return $success;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine whether the current change affects scheduling and reset attendee status accordingly
|
|
|
|
*/
|
|
|
|
private function _check_scheduling(&$event, $old, $update = true)
|
|
|
|
{
|
|
|
|
// skip this check when importing iCal/iTip events
|
|
|
|
if (isset($event['sequence']) || !empty($event['_method'])) {
|
|
|
|
return false;
|
2015-03-01 18:54:54 +01:00
|
|
|
}
|
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
$reschedule = false;
|
|
|
|
|
|
|
|
// iterate through the list of properties considered 'significant' for scheduling
|
|
|
|
foreach (self::$scheduling_properties as $prop) {
|
2024-01-24 11:24:41 +01:00
|
|
|
$a = $old[$prop] ?? null;
|
|
|
|
$b = $event[$prop] ?? null;
|
2019-09-28 09:35:22 +02:00
|
|
|
|
2021-02-01 08:30:34 +01:00
|
|
|
if (!empty($event['allday']) && ($prop == 'start' || $prop == 'end')
|
2022-11-29 15:54:43 +01:00
|
|
|
&& $a instanceof DateTimeInterface
|
|
|
|
&& $b instanceof DateTimeInterface
|
2021-02-01 08:30:34 +01:00
|
|
|
) {
|
2019-09-28 09:35:22 +02:00
|
|
|
$a = $a->format('Y-m-d');
|
|
|
|
$b = $b->format('Y-m-d');
|
2015-03-01 18:54:54 +01:00
|
|
|
}
|
2019-09-28 09:35:22 +02:00
|
|
|
|
|
|
|
if ($prop == 'recurrence' && is_array($a) && is_array($b)) {
|
|
|
|
unset($a['EXCEPTIONS'], $b['EXCEPTIONS']);
|
|
|
|
$a = array_filter($a);
|
|
|
|
$b = array_filter($b);
|
|
|
|
|
|
|
|
// advanced rrule comparison: no rescheduling if series was shortened
|
2021-02-01 08:30:34 +01:00
|
|
|
if (!empty($a['COUNT']) && !empty($b['COUNT']) && $b['COUNT'] < $a['COUNT']) {
|
2019-09-28 09:35:22 +02:00
|
|
|
unset($a['COUNT'], $b['COUNT']);
|
2024-01-24 11:24:41 +01:00
|
|
|
} elseif (!empty($a['UNTIL']) && !empty($b['UNTIL']) && $b['UNTIL'] < $a['UNTIL']) {
|
2019-09-28 09:35:22 +02:00
|
|
|
unset($a['UNTIL'], $b['UNTIL']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($a != $b) {
|
|
|
|
$reschedule = true;
|
|
|
|
break;
|
2015-03-01 18:54:54 +01:00
|
|
|
}
|
|
|
|
}
|
2019-09-28 09:35:22 +02:00
|
|
|
|
|
|
|
// reset all attendee status to needs-action (#4360)
|
|
|
|
if ($update && $reschedule && is_array($event['attendees'])) {
|
|
|
|
$is_organizer = false;
|
|
|
|
$emails = $this->cal->get_user_emails();
|
|
|
|
$attendees = $event['attendees'];
|
|
|
|
|
|
|
|
foreach ($attendees as $i => $attendee) {
|
|
|
|
if ($attendee['role'] == 'ORGANIZER' && $attendee['email'] && in_array(strtolower($attendee['email']), $emails)) {
|
|
|
|
$is_organizer = true;
|
2024-01-24 11:24:41 +01:00
|
|
|
} elseif ($attendee['role'] != 'ORGANIZER'
|
2019-09-28 09:35:22 +02:00
|
|
|
&& $attendee['role'] != 'NON-PARTICIPANT'
|
|
|
|
&& $attendee['status'] != 'DELEGATED'
|
|
|
|
) {
|
|
|
|
$attendees[$i]['status'] = 'NEEDS-ACTION';
|
|
|
|
$attendees[$i]['rsvp'] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update attendees only if I'm the organizer
|
|
|
|
if ($is_organizer || ($event['organizer'] && in_array(strtolower($event['organizer']['email']), $emails))) {
|
|
|
|
$event['attendees'] = $attendees;
|
|
|
|
}
|
2015-03-01 18:54:54 +01:00
|
|
|
}
|
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
return $reschedule;
|
2015-03-01 18:54:54 +01:00
|
|
|
}
|
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
/**
|
|
|
|
* Convert save data to be used in SQL statements
|
|
|
|
*/
|
|
|
|
private function _save_preprocess($event)
|
|
|
|
{
|
|
|
|
// shift dates to server's timezone (except for all-day events)
|
|
|
|
if (!$event['allday']) {
|
|
|
|
$event['start'] = clone $event['start'];
|
|
|
|
$event['start']->setTimezone($this->server_timezone);
|
|
|
|
$event['end'] = clone $event['end'];
|
|
|
|
$event['end']->setTimezone($this->server_timezone);
|
|
|
|
}
|
2014-04-09 13:54:04 +02:00
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
// compose vcalendar-style recurrencue rule from structured data
|
2021-02-01 08:30:34 +01:00
|
|
|
$rrule = !empty($event['recurrence']) ? libcalendaring::to_rrule($event['recurrence']) : '';
|
|
|
|
|
2022-09-14 12:45:30 +02:00
|
|
|
$sensitivity = isset($event['sensitivity']) ? strtolower($event['sensitivity']) : '';
|
|
|
|
$free_busy = isset($event['free_busy']) ? strtolower($event['free_busy']) : '';
|
2014-04-17 17:49:00 +02:00
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
$event['_recurrence'] = rtrim($rrule, ';');
|
2024-01-24 11:24:41 +01:00
|
|
|
$event['free_busy'] = $this->free_busy_map[$free_busy] ?? null;
|
|
|
|
$event['sensitivity'] = $this->sensitivity_map[$sensitivity] ?? 0;
|
2021-02-01 08:30:34 +01:00
|
|
|
$event['all_day'] = !empty($event['allday']) ? 1 : 0;
|
2014-04-17 17:49:00 +02:00
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
if ($event['free_busy'] == 'tentative') {
|
|
|
|
$event['status'] = 'TENTATIVE';
|
|
|
|
}
|
2014-04-17 17:49:00 +02:00
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
// compute absolute time to notify the user
|
|
|
|
$event['notifyat'] = $this->_get_notification($event);
|
2011-07-01 21:08:12 +02:00
|
|
|
|
2021-02-01 08:30:34 +01:00
|
|
|
if (!empty($event['valarms'])) {
|
2019-09-28 09:35:22 +02:00
|
|
|
$event['alarms'] = $this->serialize_alarms($event['valarms']);
|
|
|
|
}
|
2011-07-01 21:08:12 +02:00
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
// process event attendees
|
|
|
|
if (!empty($event['attendees'])) {
|
|
|
|
$event['attendees'] = json_encode((array)$event['attendees']);
|
2024-01-24 11:24:41 +01:00
|
|
|
} else {
|
2019-09-28 09:35:22 +02:00
|
|
|
$event['attendees'] = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $event;
|
2011-08-04 23:55:41 +02:00
|
|
|
}
|
2011-07-01 21:08:12 +02:00
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
/**
|
|
|
|
* Compute absolute time to notify the user
|
|
|
|
*/
|
|
|
|
private function _get_notification($event)
|
|
|
|
{
|
2021-02-01 08:30:34 +01:00
|
|
|
if (!empty($event['valarms']) && $event['start'] > new DateTime()) {
|
2019-09-28 09:35:22 +02:00
|
|
|
$alarm = libcalendaring::get_next_alarm($event);
|
|
|
|
|
|
|
|
if ($alarm['time'] && in_array($alarm['action'], $this->alarm_types)) {
|
|
|
|
return date('Y-m-d H:i:s', $alarm['time']);
|
|
|
|
}
|
|
|
|
}
|
2015-03-01 18:54:54 +01:00
|
|
|
}
|
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
/**
|
|
|
|
* Save the given event record to database
|
|
|
|
*
|
|
|
|
* @param array Event data
|
|
|
|
* @param boolean True if recurring events instances should be updated, too
|
|
|
|
*/
|
|
|
|
private function _update_event($event, $update_recurring = true)
|
|
|
|
{
|
|
|
|
$event = $this->_save_preprocess($event);
|
2024-01-24 11:24:41 +01:00
|
|
|
$sql_args = [];
|
|
|
|
$set_cols = ['start', 'end', 'all_day', 'recurrence_id', 'isexception', 'sequence',
|
2019-09-28 09:35:22 +02:00
|
|
|
'title', 'description', 'location', 'categories', 'url', 'free_busy', 'priority',
|
2024-01-24 11:24:41 +01:00
|
|
|
'sensitivity', 'status', 'attendees', 'alarms', 'notifyat',
|
|
|
|
];
|
2019-09-28 09:35:22 +02:00
|
|
|
|
|
|
|
foreach ($set_cols as $col) {
|
2021-02-01 08:30:34 +01:00
|
|
|
if (!empty($event[$col]) && is_a($event[$col], 'DateTime')) {
|
2019-09-28 09:35:22 +02:00
|
|
|
$sql_args[$col] = $event[$col]->format(self::DB_DATE_FORMAT);
|
2024-01-24 11:24:41 +01:00
|
|
|
} elseif (array_key_exists($col, $event)) {
|
|
|
|
$sql_args[$col] = is_array($event[$col]) ? implode(',', $event[$col]) : $event[$col];
|
2019-09-28 09:35:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-01 08:30:34 +01:00
|
|
|
if (!empty($event['_recurrence'])) {
|
2019-09-28 09:35:22 +02:00
|
|
|
$sql_args['recurrence'] = $event['_recurrence'];
|
|
|
|
}
|
|
|
|
|
2021-02-01 08:30:34 +01:00
|
|
|
if (!empty($event['_instance'])) {
|
2019-09-28 09:35:22 +02:00
|
|
|
$sql_args['instance'] = $event['_instance'];
|
|
|
|
}
|
|
|
|
|
2021-02-01 08:30:34 +01:00
|
|
|
if (!empty($event['_fromcalendar']) && $event['_fromcalendar'] != $event['calendar']) {
|
2019-09-28 09:35:22 +02:00
|
|
|
$sql_args['calendar_id'] = $event['calendar'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql_set = '';
|
|
|
|
foreach (array_keys($sql_args) as $col) {
|
|
|
|
$sql_set .= ", `$col` = ?";
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql_args = array_values($sql_args);
|
|
|
|
$sql_args[] = $event['id'];
|
|
|
|
|
|
|
|
$query = $this->rc->db->query(
|
|
|
|
"UPDATE `{$this->db_events}`"
|
|
|
|
. " SET `changed` = " . $this->rc->db->now() . $sql_set
|
|
|
|
. " WHERE `event_id` = ? AND `calendar_id` IN ({$this->calendar_ids})",
|
|
|
|
$sql_args
|
2015-03-01 18:54:54 +01:00
|
|
|
);
|
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
$success = $this->rc->db->affected_rows($query);
|
|
|
|
|
|
|
|
// add attachments
|
|
|
|
if ($success && !empty($event['attachments'])) {
|
|
|
|
foreach ($event['attachments'] as $attachment) {
|
|
|
|
$this->add_attachment($attachment, $event['id']);
|
|
|
|
unset($attachment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove attachments
|
2021-02-01 08:30:34 +01:00
|
|
|
if ($success && !empty($event['deleted_attachments']) && is_array($event['deleted_attachments'])) {
|
2019-09-28 09:35:22 +02:00
|
|
|
foreach ($event['deleted_attachments'] as $attachment) {
|
|
|
|
$this->remove_attachment($attachment, $event['id']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($success) {
|
|
|
|
unset($this->cache[$event['id']]);
|
|
|
|
if ($update_recurring) {
|
|
|
|
$this->_update_recurring($event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $success;
|
2011-05-23 21:00:14 +02:00
|
|
|
}
|
2015-03-01 18:54:54 +01:00
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
/**
|
|
|
|
* Insert "fake" entries for recurring occurences of this event
|
|
|
|
*/
|
|
|
|
private function _update_recurring($event)
|
|
|
|
{
|
|
|
|
if (empty($this->calendars)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($event['recurrence'])) {
|
2024-01-24 11:24:41 +01:00
|
|
|
$exdata = [];
|
2019-09-28 09:35:22 +02:00
|
|
|
$exceptions = $this->_load_exceptions($event);
|
|
|
|
|
|
|
|
foreach ($exceptions as $exception) {
|
|
|
|
$exdate = substr($exception['_instance'], 0, 8);
|
|
|
|
$exdata[$exdate] = $exception;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear existing recurrence copies
|
|
|
|
$this->rc->db->query(
|
|
|
|
"DELETE FROM `{$this->db_events}`"
|
|
|
|
. " WHERE `recurrence_id` = ? AND `isexception` = 0 AND `calendar_id` IN ({$this->calendar_ids})",
|
2011-06-01 18:35:10 +02:00
|
|
|
$event['id']
|
2019-09-28 09:35:22 +02:00
|
|
|
);
|
2015-03-01 18:54:54 +01:00
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
// create new fake entries
|
|
|
|
if (!empty($event['recurrence'])) {
|
2022-12-02 14:51:33 +01:00
|
|
|
$recurrence = new libcalendaring_recurrence($this->cal->lib, $event);
|
2019-09-28 09:35:22 +02:00
|
|
|
$count = 0;
|
|
|
|
$event['allday'] = $event['all_day'];
|
|
|
|
$duration = $event['start']->diff($event['end']);
|
|
|
|
$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);
|
|
|
|
|
|
|
|
// skip exceptions
|
|
|
|
// TODO: merge updated data from master event
|
2021-02-01 08:30:34 +01:00
|
|
|
if (!empty($exdata[$datestr])) {
|
2019-09-28 09:35:22 +02:00
|
|
|
continue;
|
|
|
|
}
|
2012-12-04 20:10:04 +01:00
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
$next_start->setTimezone($this->server_timezone);
|
|
|
|
$next_end = clone $next_start;
|
|
|
|
$next_end->add($duration);
|
|
|
|
|
2024-01-24 11:24:41 +01:00
|
|
|
$notify_at = $this->_get_notification([
|
2021-02-01 08:30:34 +01:00
|
|
|
'alarms' => !empty($event['alarms']) ? $event['alarms'] : null,
|
2019-09-28 09:35:22 +02:00
|
|
|
'start' => $next_start,
|
|
|
|
'end' => $next_end,
|
2024-01-24 11:24:41 +01:00
|
|
|
'status' => $event['status'],
|
|
|
|
]);
|
2019-09-28 09:35:22 +02:00
|
|
|
|
|
|
|
$now = $this->rc->db->now();
|
|
|
|
$query = $this->rc->db->query(
|
|
|
|
"INSERT INTO `{$this->db_events}`"
|
|
|
|
. " (`calendar_id`, `recurrence_id`, `created`, `changed`, `uid`, `instance`, `start`, `end`,"
|
|
|
|
. " `all_day`, `sequence`, `recurrence`, `title`, `description`, `location`, `categories`,"
|
|
|
|
. " `url`, `free_busy`, `priority`, `sensitivity`, `status`, `alarms`, `attendees`, `notifyat`)"
|
|
|
|
. " SELECT `calendar_id`, ?, $now, $now, `uid`, ?, ?, ?,"
|
|
|
|
. " `all_day`, `sequence`, `recurrence`, `title`, `description`, `location`, `categories`,"
|
|
|
|
. " `url`, `free_busy`, `priority`, `sensitivity`, `status`, `alarms`, `attendees`, ?"
|
|
|
|
. " FROM `{$this->db_events}` WHERE `event_id` = ? AND `calendar_id` IN ({$this->calendar_ids})",
|
|
|
|
$event['id'],
|
|
|
|
$instance,
|
|
|
|
$next_start->format(self::DB_DATE_FORMAT),
|
|
|
|
$next_end->format(self::DB_DATE_FORMAT),
|
|
|
|
$notify_at,
|
|
|
|
$event['id']
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!$this->rc->db->affected_rows($query)) {
|
|
|
|
break;
|
|
|
|
}
|
2014-08-06 09:34:08 +02:00
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
// stop adding events for inifinite recurrence after 20 years
|
2021-02-01 08:30:34 +01:00
|
|
|
if (++$count > 999 || (empty($recurrence->recurEnd) && empty($recurrence->recurCount) && $next_start->format('Y') > date('Y') + 20)) {
|
2019-09-28 09:35:22 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove all exceptions after recurrence end
|
2021-02-01 08:30:34 +01:00
|
|
|
if (!empty($next_end) && !empty($exceptions)) {
|
2019-09-28 09:35:22 +02:00
|
|
|
$this->rc->db->query(
|
|
|
|
"DELETE FROM `{$this->db_events}`"
|
|
|
|
. " WHERE `recurrence_id` = ? AND `isexception` = 1 AND `start` > ?"
|
|
|
|
. " AND `calendar_id` IN ({$this->calendar_ids})",
|
|
|
|
$event['id'],
|
|
|
|
$next_end->format(self::DB_DATE_FORMAT)
|
|
|
|
);
|
|
|
|
}
|
2012-12-04 20:10:04 +01:00
|
|
|
}
|
|
|
|
}
|
2019-09-28 09:35:22 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private function _load_exceptions($event, $instance_id = null)
|
|
|
|
{
|
|
|
|
$sql_add_where = '';
|
|
|
|
if (!empty($instance_id)) {
|
|
|
|
$sql_add_where = " AND `instance` = ?";
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = $this->rc->db->query(
|
|
|
|
"SELECT * FROM `{$this->db_events}`"
|
|
|
|
. " WHERE `recurrence_id` = ? AND `isexception` = 1"
|
|
|
|
. " AND `calendar_id` IN ({$this->calendar_ids})" . $sql_add_where
|
|
|
|
. " ORDER BY `instance`, `start`",
|
|
|
|
$event['id'],
|
|
|
|
$instance_id
|
|
|
|
);
|
|
|
|
|
2024-01-24 11:24:41 +01:00
|
|
|
$exceptions = [];
|
2019-09-28 09:35:22 +02:00
|
|
|
while (($sql_arr = $this->rc->db->fetch_assoc($result)) && $sql_arr['event_id']) {
|
|
|
|
$exception = $this->_read_postprocess($sql_arr);
|
|
|
|
$instance = $exception['_instance'] ?: $exception['start']->format($exception['allday'] ? 'Ymd' : 'Ymd\THis');
|
|
|
|
$exceptions[$instance] = $exception;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $exceptions;
|
2012-12-04 20:10:04 +01:00
|
|
|
}
|
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
/**
|
|
|
|
* Move a single event
|
|
|
|
*
|
|
|
|
* @param array Hash array with event properties
|
|
|
|
* @see calendar_driver::move_event()
|
|
|
|
*/
|
|
|
|
public function move_event($event)
|
|
|
|
{
|
|
|
|
// let edit_event() do all the magic
|
|
|
|
return $this->edit_event($event + (array)$this->get_event($event));
|
2011-06-01 18:35:10 +02:00
|
|
|
}
|
2011-05-23 21:00:14 +02:00
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
/**
|
|
|
|
* Resize a single event
|
|
|
|
*
|
|
|
|
* @param array Hash array with event properties
|
|
|
|
* @see calendar_driver::resize_event()
|
|
|
|
*/
|
|
|
|
public function resize_event($event)
|
|
|
|
{
|
|
|
|
// let edit_event() do all the magic
|
|
|
|
return $this->edit_event($event + (array)$this->get_event($event));
|
2011-07-16 20:03:19 +02:00
|
|
|
}
|
2019-09-28 09:35:22 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a single event from the database
|
|
|
|
*
|
|
|
|
* @param array Hash array with event properties
|
|
|
|
* @param boolean Remove record irreversible (@TODO)
|
|
|
|
*
|
|
|
|
* @see calendar_driver::remove_event()
|
|
|
|
*/
|
|
|
|
public function remove_event($event, $force = true)
|
|
|
|
{
|
|
|
|
if (!empty($this->calendars)) {
|
|
|
|
$event += (array)$this->get_event($event);
|
|
|
|
$master = $event;
|
|
|
|
$update_master = false;
|
|
|
|
$savemode = 'all';
|
|
|
|
$ret = true;
|
|
|
|
|
|
|
|
// read master if deleting a recurring event
|
|
|
|
if ($event['recurrence'] || $event['recurrence_id']) {
|
2024-01-24 11:24:41 +01:00
|
|
|
$master = $event['recurrence_id'] ? $this->get_event(['id' => $event['recurrence_id']]) : $event;
|
2019-09-28 09:35:22 +02:00
|
|
|
$savemode = $event['_savemode'];
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($savemode) {
|
2024-01-24 11:24:41 +01:00
|
|
|
case 'current':
|
|
|
|
// add exception to master event
|
|
|
|
$master['recurrence']['EXDATE'][] = $event['start'];
|
|
|
|
$update_master = true;
|
2019-09-28 09:35:22 +02:00
|
|
|
|
2024-01-24 11:24:41 +01:00
|
|
|
// just delete this single occurence
|
|
|
|
$query = $this->rc->db->query(
|
|
|
|
"DELETE FROM `{$this->db_events}`"
|
|
|
|
. " WHERE `calendar_id` IN ({$this->calendar_ids}) AND `event_id` = ?",
|
|
|
|
$event['id']
|
|
|
|
);
|
|
|
|
break;
|
2019-09-28 09:35:22 +02:00
|
|
|
|
2024-01-24 11:24:41 +01:00
|
|
|
case 'future':
|
|
|
|
if ($master['id'] != $event['id']) {
|
|
|
|
// set until-date on master event
|
|
|
|
$master['recurrence']['UNTIL'] = clone $event['start'];
|
|
|
|
$master['recurrence']['UNTIL']->sub(new DateInterval('P1D'));
|
|
|
|
unset($master['recurrence']['COUNT']);
|
|
|
|
$update_master = true;
|
2019-09-28 09:35:22 +02:00
|
|
|
|
2024-01-24 11:24:41 +01:00
|
|
|
// delete this and all future instances
|
|
|
|
$fromdate = clone $event['start'];
|
|
|
|
$fromdate->setTimezone($this->server_timezone);
|
2019-09-28 09:35:22 +02:00
|
|
|
|
2024-01-24 11:24:41 +01:00
|
|
|
$query = $this->rc->db->query(
|
|
|
|
"DELETE FROM `{$this->db_events}`"
|
|
|
|
. " WHERE `calendar_id` IN ({$this->calendar_ids}) AND `start` >= ? AND `recurrence_id` = ?",
|
|
|
|
$fromdate->format(self::DB_DATE_FORMAT),
|
|
|
|
$master['id']
|
|
|
|
);
|
|
|
|
|
|
|
|
$ret = $master['id'];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// else: future == all if modifying the master event
|
|
|
|
|
|
|
|
// no break
|
|
|
|
default: // 'all' is default
|
2019-09-28 09:35:22 +02:00
|
|
|
$query = $this->rc->db->query(
|
|
|
|
"DELETE FROM `{$this->db_events}`"
|
2024-01-24 11:24:41 +01:00
|
|
|
. " WHERE (`event_id` = ? OR `recurrence_id` = ?) AND `calendar_id` IN ({$this->calendar_ids})",
|
|
|
|
$master['id'],
|
2019-09-28 09:35:22 +02:00
|
|
|
$master['id']
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$success = $this->rc->db->affected_rows($query);
|
|
|
|
|
|
|
|
if ($success && $update_master) {
|
|
|
|
$this->_update_event($master, true);
|
2015-03-01 18:54:54 +01:00
|
|
|
}
|
2019-09-28 09:35:22 +02:00
|
|
|
|
|
|
|
return $success ? $ret : false;
|
2015-03-01 18:54:54 +01:00
|
|
|
}
|
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
return false;
|
2011-05-20 19:04:25 +02:00
|
|
|
}
|
2014-01-27 19:12:29 +01:00
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
/**
|
|
|
|
* Return data of a specific event
|
|
|
|
*
|
|
|
|
* @param mixed Hash array with event properties or event UID
|
|
|
|
* @param integer Bitmask defining the scope to search events in
|
|
|
|
* @param boolean If true, recurrence exceptions shall be added
|
|
|
|
*
|
|
|
|
* @return array Hash array with event properties
|
|
|
|
*/
|
|
|
|
public function get_event($event, $scope = 0, $full = false)
|
|
|
|
{
|
2021-02-01 08:30:34 +01:00
|
|
|
$id = is_array($event) ? (!empty($event['id']) ? $event['id'] : $event['uid']) : $event;
|
|
|
|
$cal = is_array($event) && !empty($event['calendar']) ? $event['calendar'] : null;
|
2019-09-28 09:35:22 +02:00
|
|
|
$col = is_array($event) && is_numeric($id) ? 'event_id' : 'uid';
|
|
|
|
|
2021-02-01 08:30:34 +01:00
|
|
|
if (!empty($this->cache[$id])) {
|
2019-09-28 09:35:22 +02:00
|
|
|
return $this->cache[$id];
|
|
|
|
}
|
|
|
|
|
|
|
|
// get event from the address books birthday calendar
|
|
|
|
if ($cal == self::BIRTHDAY_CALENDAR_ID) {
|
|
|
|
return $this->get_birthday_event($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
$where_add = '';
|
2021-02-01 08:30:34 +01:00
|
|
|
if (is_array($event) && empty($event['id']) && !empty($event['_instance'])) {
|
2019-09-28 09:35:22 +02:00
|
|
|
$where_add = " AND e.instance = " . $this->rc->db->quote($event['_instance']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($scope & self::FILTER_ACTIVE) {
|
2021-02-01 08:30:34 +01:00
|
|
|
$calendars = [];
|
|
|
|
foreach ($this->calendars as $idx => $cal) {
|
|
|
|
if (!empty($cal['active'])) {
|
|
|
|
$calendars[] = $idx;
|
2019-09-28 09:35:22 +02:00
|
|
|
}
|
|
|
|
}
|
2024-01-24 11:24:41 +01:00
|
|
|
$cals = implode(',', $calendars);
|
|
|
|
} else {
|
2019-09-28 09:35:22 +02:00
|
|
|
$cals = $this->calendar_ids;
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = $this->rc->db->query(
|
|
|
|
"SELECT e.*, (SELECT COUNT(`attachment_id`) FROM `{$this->db_attachments}`"
|
|
|
|
. " WHERE `event_id` = e.event_id OR `event_id` = e.recurrence_id) AS _attachments"
|
|
|
|
. " FROM `{$this->db_events}` AS e"
|
|
|
|
. " WHERE e.calendar_id IN ($cals) AND e.$col = ?" . $where_add,
|
|
|
|
$id
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($result && ($sql_arr = $this->rc->db->fetch_assoc($result)) && $sql_arr['event_id']) {
|
|
|
|
$event = $this->_read_postprocess($sql_arr);
|
|
|
|
|
|
|
|
// also load recurrence exceptions
|
|
|
|
if (!empty($event['recurrence']) && $full) {
|
|
|
|
$event['recurrence']['EXCEPTIONS'] = array_values($this->_load_exceptions($event));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->cache[$id] = $event;
|
|
|
|
|
|
|
|
return $this->cache[$id];
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2014-01-27 19:12:29 +01:00
|
|
|
}
|
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
/**
|
|
|
|
* Get event data
|
|
|
|
*
|
|
|
|
* @see calendar_driver::load_events()
|
|
|
|
*/
|
2024-01-25 13:47:41 +01:00
|
|
|
public function load_events($start, $end, $query = null, $calendars = null, $virtual = true, $modifiedsince = null)
|
2019-09-28 09:35:22 +02:00
|
|
|
{
|
|
|
|
if (empty($calendars)) {
|
|
|
|
$calendars = array_keys($this->calendars);
|
2024-01-24 11:24:41 +01:00
|
|
|
} elseif (!is_array($calendars)) {
|
2019-09-28 09:35:22 +02:00
|
|
|
$calendars = explode(',', strval($calendars));
|
|
|
|
}
|
|
|
|
|
|
|
|
// only allow to select from calendars of this use
|
2024-01-24 11:24:41 +01:00
|
|
|
$calendar_ids = array_map([$this->rc->db, 'quote'], array_intersect($calendars, array_keys($this->calendars)));
|
2019-09-28 09:35:22 +02:00
|
|
|
|
|
|
|
// compose (slow) SQL query for searching
|
|
|
|
// FIXME: improve searching using a dedicated col and normalized values
|
2021-02-01 08:30:34 +01:00
|
|
|
$sql_add = '';
|
2019-09-28 09:35:22 +02:00
|
|
|
if ($query) {
|
2024-01-24 11:24:41 +01:00
|
|
|
foreach (['title','location','description','categories','attendees'] as $col) {
|
|
|
|
$sql_query[] = $this->rc->db->ilike($col, '%' . $query . '%');
|
2019-09-28 09:35:22 +02:00
|
|
|
}
|
2024-01-24 11:24:41 +01:00
|
|
|
$sql_add .= " AND (" . implode(' OR ', $sql_query) . ")";
|
2019-09-28 09:35:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$virtual) {
|
|
|
|
$sql_add .= " AND e.recurrence_id = 0";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($modifiedsince) {
|
|
|
|
$sql_add .= " AND e.changed >= " . $this->rc->db->quote(date('Y-m-d H:i:s', $modifiedsince));
|
|
|
|
}
|
|
|
|
|
2024-01-24 11:24:41 +01:00
|
|
|
$events = [];
|
2019-09-28 09:35:22 +02:00
|
|
|
if (!empty($calendar_ids)) {
|
|
|
|
$result = $this->rc->db->query(
|
|
|
|
"SELECT e.*, (SELECT COUNT(`attachment_id`) FROM `{$this->db_attachments}`"
|
|
|
|
. " WHERE `event_id` = e.event_id OR `event_id` = e.recurrence_id) AS _attachments"
|
|
|
|
. " FROM `{$this->db_events}` e"
|
2024-01-24 11:24:41 +01:00
|
|
|
. " WHERE e.calendar_id IN (" . implode(',', $calendar_ids) . ")"
|
2019-09-28 09:35:22 +02:00
|
|
|
. " AND e.start <= " . $this->rc->db->fromunixtime($end)
|
|
|
|
. " AND e.end >= " . $this->rc->db->fromunixtime($start)
|
2019-10-12 21:08:37 +02:00
|
|
|
. $sql_add
|
2019-09-28 09:35:22 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
while ($result && ($sql_arr = $this->rc->db->fetch_assoc($result))) {
|
|
|
|
$event = $this->_read_postprocess($sql_arr);
|
|
|
|
$add = true;
|
|
|
|
|
|
|
|
if (!empty($event['recurrence']) && !$event['recurrence_id']) {
|
|
|
|
// load recurrence exceptions (i.e. for export)
|
|
|
|
if (!$virtual) {
|
|
|
|
$event['recurrence']['EXCEPTIONS'] = $this->_load_exceptions($event);
|
|
|
|
}
|
|
|
|
// check for exception on first instance
|
|
|
|
else {
|
|
|
|
$instance = libcalendaring::recurrence_instance_identifier($event);
|
|
|
|
$exceptions = $this->_load_exceptions($event, $instance);
|
|
|
|
|
|
|
|
if ($exceptions && is_array($exceptions[$instance])) {
|
|
|
|
$event = $exceptions[$instance];
|
|
|
|
$add = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($add) {
|
|
|
|
$events[] = $event;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add events from the address books birthday calendar
|
|
|
|
if (in_array(self::BIRTHDAY_CALENDAR_ID, $calendars) && empty($query)) {
|
2021-02-01 08:30:34 +01:00
|
|
|
$events = array_merge($events, $this->load_birthday_events($start, $end, null, $modifiedsince));
|
2019-09-28 09:35:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $events;
|
2011-05-23 21:00:14 +02:00
|
|
|
}
|
2019-09-28 09:35:22 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get number of events in the given calendar
|
|
|
|
*
|
|
|
|
* @param mixed List of calendar IDs to count events (either as array or comma-separated string)
|
|
|
|
* @param integer Date range start (unix timestamp)
|
|
|
|
* @param integer Date range end (unix timestamp)
|
|
|
|
*
|
|
|
|
* @return array Hash array with counts grouped by calendar ID
|
|
|
|
*/
|
|
|
|
public function count_events($calendars, $start, $end = null)
|
|
|
|
{
|
|
|
|
// not implemented
|
2024-01-24 11:24:41 +01:00
|
|
|
return [];
|
2015-03-01 18:54:54 +01:00
|
|
|
}
|
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
/**
|
|
|
|
* Convert sql record into a rcube style event object
|
|
|
|
*/
|
|
|
|
private function _read_postprocess($event)
|
|
|
|
{
|
|
|
|
$free_busy_map = array_flip($this->free_busy_map);
|
|
|
|
$sensitivity_map = array_flip($this->sensitivity_map);
|
|
|
|
|
|
|
|
$event['id'] = $event['event_id'];
|
|
|
|
$event['start'] = new DateTime($event['start']);
|
|
|
|
$event['end'] = new DateTime($event['end']);
|
|
|
|
$event['allday'] = intval($event['all_day']);
|
|
|
|
$event['created'] = new DateTime($event['created']);
|
|
|
|
$event['changed'] = new DateTime($event['changed']);
|
|
|
|
$event['free_busy'] = $free_busy_map[$event['free_busy']];
|
|
|
|
$event['sensitivity'] = $sensitivity_map[$event['sensitivity']];
|
|
|
|
$event['calendar'] = $event['calendar_id'];
|
|
|
|
$event['recurrence_id'] = intval($event['recurrence_id']);
|
|
|
|
$event['isexception'] = intval($event['isexception']);
|
|
|
|
|
|
|
|
// parse recurrence rule
|
|
|
|
if ($event['recurrence'] && preg_match_all('/([A-Z]+)=([^;]+);?/', $event['recurrence'], $m, PREG_SET_ORDER)) {
|
2024-01-24 11:24:41 +01:00
|
|
|
$event['recurrence'] = [];
|
2019-09-28 09:35:22 +02:00
|
|
|
foreach ($m as $rr) {
|
|
|
|
if (is_numeric($rr[2])) {
|
|
|
|
$rr[2] = intval($rr[2]);
|
2024-01-24 11:24:41 +01:00
|
|
|
} elseif ($rr[1] == 'UNTIL') {
|
2019-09-28 09:35:22 +02:00
|
|
|
$rr[2] = date_create($rr[2]);
|
2024-01-24 11:24:41 +01:00
|
|
|
} elseif ($rr[1] == 'RDATE') {
|
2019-09-28 09:35:22 +02:00
|
|
|
$rr[2] = array_map('date_create', explode(',', $rr[2]));
|
2024-01-24 11:24:41 +01:00
|
|
|
} elseif ($rr[1] == 'EXDATE') {
|
2019-09-28 09:35:22 +02:00
|
|
|
$rr[2] = array_map('date_create', explode(',', $rr[2]));
|
|
|
|
}
|
|
|
|
|
|
|
|
$event['recurrence'][$rr[1]] = $rr[2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($event['recurrence_id']) {
|
|
|
|
libcalendaring::identify_recurrence_instance($event);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen($event['instance'])) {
|
|
|
|
$event['_instance'] = $event['instance'];
|
|
|
|
|
|
|
|
if (empty($event['recurrence_id'])) {
|
|
|
|
$event['recurrence_date'] = rcube_utils::anytodatetime($event['_instance'], $event['start']->getTimezone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-01 08:30:34 +01:00
|
|
|
if (!empty($event['_attachments'])) {
|
2019-09-28 09:35:22 +02:00
|
|
|
$event['attachments'] = (array)$this->list_attachments($event);
|
|
|
|
}
|
|
|
|
|
|
|
|
// decode serialized event attendees
|
|
|
|
if (strlen($event['attendees'])) {
|
|
|
|
$event['attendees'] = $this->unserialize_attendees($event['attendees']);
|
2024-01-24 11:24:41 +01:00
|
|
|
} else {
|
|
|
|
$event['attendees'] = [];
|
2019-09-28 09:35:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// decode serialized alarms
|
|
|
|
if ($event['alarms']) {
|
|
|
|
$event['valarms'] = $this->unserialize_alarms($event['alarms']);
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($event['event_id'], $event['calendar_id'], $event['notifyat'], $event['all_day'], $event['instance'], $event['_attachments']);
|
|
|
|
|
|
|
|
return $event;
|
2015-03-01 18:54:54 +01:00
|
|
|
}
|
2019-09-28 09:35:22 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of pending alarms to be displayed to the user
|
|
|
|
*
|
|
|
|
* @see calendar_driver::pending_alarms()
|
|
|
|
*/
|
|
|
|
public function pending_alarms($time, $calendars = null)
|
|
|
|
{
|
|
|
|
if (empty($calendars)) {
|
|
|
|
$calendars = array_keys($this->calendars);
|
2024-01-24 11:24:41 +01:00
|
|
|
} elseif (!is_array($calendars)) {
|
2019-09-28 09:35:22 +02:00
|
|
|
$calendars = explode(',', (array) $calendars);
|
|
|
|
}
|
|
|
|
|
|
|
|
// only allow to select from calendars with activated alarms
|
2024-01-24 11:24:41 +01:00
|
|
|
$calendar_ids = [];
|
2019-09-28 09:35:22 +02:00
|
|
|
foreach ($calendars as $cid) {
|
|
|
|
if ($this->calendars[$cid] && $this->calendars[$cid]['showalarms']) {
|
|
|
|
$calendar_ids[] = $cid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-24 11:24:41 +01:00
|
|
|
$calendar_ids = array_map([$this->rc->db, 'quote'], $calendar_ids);
|
|
|
|
$alarms = [];
|
2019-09-28 09:35:22 +02:00
|
|
|
|
|
|
|
if (!empty($calendar_ids)) {
|
|
|
|
$stime = $this->rc->db->fromunixtime($time);
|
|
|
|
$result = $this->rc->db->query(
|
|
|
|
"SELECT * FROM `{$this->db_events}`"
|
2024-01-24 11:24:41 +01:00
|
|
|
. " WHERE `calendar_id` IN (" . implode(',', $calendar_ids) . ")"
|
2019-10-12 21:08:37 +02:00
|
|
|
. " AND `notifyat` <= $stime AND `end` > $stime"
|
2019-09-28 09:35:22 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
while ($event = $this->rc->db->fetch_assoc($result)) {
|
|
|
|
$alarms[] = $this->_read_postprocess($event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $alarms;
|
2015-03-01 18:54:54 +01:00
|
|
|
}
|
2019-09-28 09:35:22 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Feedback after showing/sending an alarm notification
|
|
|
|
*
|
|
|
|
* @see calendar_driver::dismiss_alarm()
|
|
|
|
*/
|
|
|
|
public function dismiss_alarm($event_id, $snooze = 0)
|
|
|
|
{
|
|
|
|
// set new notifyat time or unset if not snoozed
|
|
|
|
$notify_at = $snooze > 0 ? date(self::DB_DATE_FORMAT, time() + $snooze) : null;
|
|
|
|
|
|
|
|
$query = $this->rc->db->query(
|
|
|
|
"UPDATE `{$this->db_events}`"
|
|
|
|
. " SET `changed` = " . $this->rc->db->now() . ", `notifyat` = ?"
|
|
|
|
. " WHERE `event_id` = ? AND `calendar_id` IN ({$this->calendar_ids})",
|
|
|
|
$notify_at,
|
|
|
|
$event_id
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->rc->db->affected_rows($query);
|
2011-07-26 08:38:13 +02:00
|
|
|
}
|
2019-09-28 09:35:22 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Save an attachment related to the given event
|
|
|
|
*/
|
|
|
|
private function add_attachment($attachment, $event_id)
|
|
|
|
{
|
2019-10-01 20:44:31 +02:00
|
|
|
if (isset($attachment['data'])) {
|
|
|
|
$data = $attachment['data'];
|
2024-01-24 11:24:41 +01:00
|
|
|
} elseif (!empty($attachment['path'])) {
|
2019-10-01 20:44:31 +02:00
|
|
|
$data = file_get_contents($attachment['path']);
|
2024-01-24 11:24:41 +01:00
|
|
|
} else {
|
2019-10-01 20:44:31 +02:00
|
|
|
return false;
|
|
|
|
}
|
2019-09-28 09:35:22 +02:00
|
|
|
|
|
|
|
$query = $this->rc->db->query(
|
|
|
|
"INSERT INTO `{$this->db_attachments}`"
|
|
|
|
. " (`event_id`, `filename`, `mimetype`, `size`, `data`)"
|
|
|
|
. " VALUES (?, ?, ?, ?, ?)",
|
|
|
|
$event_id,
|
|
|
|
$attachment['name'],
|
|
|
|
$attachment['mimetype'],
|
|
|
|
strlen($data),
|
|
|
|
base64_encode($data)
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->rc->db->affected_rows($query);
|
2013-11-20 12:36:17 +01:00
|
|
|
}
|
2019-09-28 09:35:22 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a specific attachment from the given event
|
|
|
|
*/
|
|
|
|
private function remove_attachment($attachment_id, $event_id)
|
|
|
|
{
|
|
|
|
$query = $this->rc->db->query(
|
|
|
|
"DELETE FROM `{$this->db_attachments}`"
|
|
|
|
. " WHERE `attachment_id` = ? AND `event_id` IN ("
|
|
|
|
. "SELECT `event_id` FROM `{$this->db_events}`"
|
|
|
|
. " WHERE `event_id` = ? AND `calendar_id` IN ({$this->calendar_ids}))",
|
|
|
|
$attachment_id,
|
|
|
|
$event_id
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->rc->db->affected_rows($query);
|
2014-04-17 17:49:00 +02:00
|
|
|
}
|
2019-09-28 09:35:22 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* List attachments of specified event
|
|
|
|
*/
|
|
|
|
public function list_attachments($event)
|
|
|
|
{
|
2024-01-24 11:24:41 +01:00
|
|
|
$attachments = [];
|
2019-09-28 09:35:22 +02:00
|
|
|
|
|
|
|
if (!empty($this->calendar_ids)) {
|
|
|
|
$result = $this->rc->db->query(
|
|
|
|
"SELECT `attachment_id` AS id, `filename` AS name, `mimetype`, `size`"
|
|
|
|
. " FROM `{$this->db_attachments}`"
|
|
|
|
. " WHERE `event_id` IN ("
|
|
|
|
. "SELECT `event_id` FROM `{$this->db_events}`"
|
|
|
|
. " WHERE `event_id` = ? AND `calendar_id` IN ({$this->calendar_ids}))"
|
|
|
|
. " ORDER BY `filename`",
|
|
|
|
$event['recurrence_id'] ? $event['recurrence_id'] : $event['event_id']
|
|
|
|
);
|
|
|
|
|
|
|
|
while ($arr = $this->rc->db->fetch_assoc($result)) {
|
|
|
|
$attachments[] = $arr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $attachments;
|
2011-07-31 14:40:52 +02:00
|
|
|
}
|
2019-09-28 09:35:22 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get attachment properties
|
|
|
|
*/
|
|
|
|
public function get_attachment($id, $event)
|
|
|
|
{
|
|
|
|
if (!empty($this->calendar_ids)) {
|
|
|
|
$result = $this->rc->db->query(
|
|
|
|
"SELECT `attachment_id` AS id, `filename` AS name, `mimetype`, `size` "
|
|
|
|
. " FROM `{$this->db_attachments}`"
|
|
|
|
. " WHERE `attachment_id` = ? AND `event_id` IN ("
|
|
|
|
. "SELECT `event_id` FROM `{$this->db_events}`"
|
|
|
|
. " WHERE `event_id` = ? AND `calendar_id` IN ({$this->calendar_ids}))",
|
|
|
|
$id,
|
2021-02-01 08:30:34 +01:00
|
|
|
!empty($event['recurrence_id']) ? $event['recurrence_id'] : $event['id']
|
2019-09-28 09:35:22 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
if ($result && ($arr = $this->rc->db->fetch_assoc($result))) {
|
|
|
|
return $arr;
|
|
|
|
}
|
|
|
|
}
|
2024-01-24 10:59:25 +01:00
|
|
|
|
|
|
|
return false;
|
2011-05-23 21:00:14 +02:00
|
|
|
}
|
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
/**
|
|
|
|
* Get attachment body
|
|
|
|
*/
|
|
|
|
public function get_attachment_body($id, $event)
|
|
|
|
{
|
|
|
|
if (!empty($this->calendar_ids)) {
|
|
|
|
$result = $this->rc->db->query(
|
|
|
|
"SELECT `data` FROM `{$this->db_attachments}`"
|
|
|
|
. " WHERE `attachment_id` = ? AND `event_id` IN ("
|
|
|
|
. "SELECT `event_id` FROM `{$this->db_events}`"
|
|
|
|
. " WHERE `event_id` = ? AND `calendar_id` IN ({$this->calendar_ids}))",
|
|
|
|
$id,
|
|
|
|
$event['id']
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($arr = $this->rc->db->fetch_assoc($result)) {
|
|
|
|
return base64_decode($arr['data']);
|
|
|
|
}
|
|
|
|
}
|
2024-01-24 10:59:25 +01:00
|
|
|
|
|
|
|
return '';
|
2011-07-01 21:08:12 +02:00
|
|
|
}
|
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
/**
|
|
|
|
* Remove the given category
|
|
|
|
*/
|
|
|
|
public function remove_category($name)
|
|
|
|
{
|
|
|
|
$query = $this->rc->db->query(
|
|
|
|
"UPDATE `{$this->db_events}` SET `categories` = ''"
|
|
|
|
. " WHERE `categories` = ? AND `calendar_id` IN ({$this->calendar_ids})",
|
|
|
|
$name
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->rc->db->affected_rows($query);
|
2011-07-01 21:08:12 +02:00
|
|
|
}
|
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
/**
|
|
|
|
* Update/replace a category
|
|
|
|
*/
|
|
|
|
public function replace_category($oldname, $name, $color)
|
|
|
|
{
|
|
|
|
$query = $this->rc->db->query(
|
|
|
|
"UPDATE `{$this->db_events}` SET `categories` = ?"
|
|
|
|
. " WHERE `categories` = ? AND `calendar_id` IN ({$this->calendar_ids})",
|
|
|
|
$name,
|
|
|
|
$oldname
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->rc->db->affected_rows($query);
|
2011-07-01 21:08:12 +02:00
|
|
|
}
|
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
/**
|
|
|
|
* Helper method to serialize the list of alarms into a string
|
|
|
|
*/
|
|
|
|
private function serialize_alarms($valarms)
|
|
|
|
{
|
|
|
|
foreach ((array)$valarms as $i => $alarm) {
|
2022-11-29 15:54:43 +01:00
|
|
|
if ($alarm['trigger'] instanceof DateTimeInterface) {
|
2019-09-28 09:35:22 +02:00
|
|
|
$valarms[$i]['trigger'] = '@' . $alarm['trigger']->format('c');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $valarms ? json_encode($valarms) : null;
|
2015-03-01 18:54:54 +01:00
|
|
|
}
|
2019-09-28 09:35:22 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method to decode a serialized list of alarms
|
|
|
|
*/
|
|
|
|
private function unserialize_alarms($alarms)
|
|
|
|
{
|
|
|
|
// decode json serialized alarms
|
|
|
|
if ($alarms && $alarms[0] == '[') {
|
|
|
|
$valarms = json_decode($alarms, true);
|
|
|
|
foreach ($valarms as $i => $alarm) {
|
|
|
|
if ($alarm['trigger'][0] == '@') {
|
|
|
|
try {
|
|
|
|
$valarms[$i]['trigger'] = new DateTime(substr($alarm['trigger'], 1));
|
2024-01-24 11:24:41 +01:00
|
|
|
} catch (Exception $e) {
|
2019-09-28 09:35:22 +02:00
|
|
|
unset($valarms[$i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// convert legacy alarms data
|
2024-01-24 11:24:41 +01:00
|
|
|
elseif (strlen($alarms)) {
|
|
|
|
[$trigger, $action] = explode(':', $alarms, 2);
|
2019-09-28 09:35:22 +02:00
|
|
|
if ($trigger = libcalendaring::parse_alarm_value($trigger)) {
|
2024-01-24 11:24:41 +01:00
|
|
|
$valarms = [['action' => $action, 'trigger' => $trigger[3] ?: $trigger[0]]];
|
2019-09-28 09:35:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-24 10:59:25 +01:00
|
|
|
return $valarms ?? [];
|
2015-03-01 18:54:54 +01:00
|
|
|
}
|
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
/**
|
|
|
|
* Helper method to decode the attendees list from string
|
|
|
|
*/
|
|
|
|
private function unserialize_attendees($s_attendees)
|
|
|
|
{
|
2024-01-24 11:24:41 +01:00
|
|
|
$attendees = [];
|
2014-04-23 20:44:46 +02:00
|
|
|
|
2019-09-28 09:35:22 +02:00
|
|
|
// decode json serialized string
|
|
|
|
if ($s_attendees[0] == '[') {
|
|
|
|
$attendees = json_decode($s_attendees, true);
|
|
|
|
}
|
|
|
|
// decode the old serialization format
|
|
|
|
else {
|
|
|
|
foreach (explode("\n", $s_attendees) as $line) {
|
2024-01-24 11:24:41 +01:00
|
|
|
$att = [];
|
2019-09-28 09:35:22 +02:00
|
|
|
foreach (rcube_utils::explode_quoted_string(';', $line) as $prop) {
|
2024-01-24 11:24:41 +01:00
|
|
|
[$key, $value] = explode("=", $prop);
|
2019-09-28 09:35:22 +02:00
|
|
|
$att[strtolower($key)] = stripslashes(trim($value, '""'));
|
|
|
|
}
|
|
|
|
$attendees[] = $att;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $attendees;
|
|
|
|
}
|
2011-05-20 19:04:25 +02:00
|
|
|
}
|