]>
git.datanom.net - caldav_driver.git/blob - database_driver.php
4 * Database driver for the Calendar plugin
6 * @version @package_version@
7 * @author Lazlo Westerhof <hello@lazlo.me>
8 * @author Thomas Bruederli <bruederli@kolabsys.com>
10 * Copyright (C) 2010, Lazlo Westerhof <hello@lazlo.me>
11 * Copyright (C) 2012, Kolab Systems AG <contact@kolabsys.com>
13 * This program is free software: you can redistribute it and/or modify
14 * it under the terms of the GNU Affero General Public License as
15 * published by the Free Software Foundation, either version 3 of the
16 * License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Affero General Public License for more details.
23 * You should have received a copy of the GNU Affero General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 class database_driver
extends calendar_driver
30 const DB_DATE_FORMAT
= 'Y-m-d H:i:s';
32 // features this backend supports
33 public $alarms = true;
34 public $attendees = true;
35 public $freebusy = false;
36 public $attachments = true;
37 public $alarm_types = array('DISPLAY');
41 private $cache = array();
42 private $calendars = array();
43 private $calendar_ids = '';
44 private $free_busy_map = array('free' => 0, 'busy' => 1, 'out-of-office' => 2, 'outofoffice' => 2, 'tentative' => 3);
45 private $sensitivity_map = array('public' => 0, 'private' => 1, 'confidential' => 2);
46 private $server_timezone;
48 private $db_events = 'events';
49 private $db_calendars = 'calendars';
50 private $db_attachments = 'attachments';
56 public function __construct($cal)
60 $this->server_timezone
= new DateTimeZone(date_default_timezone_get());
62 // load library classes
63 require_once($this->cal
->home
. '/lib/Horde_Date_Recurrence.php');
65 // read database config
66 $db = $this->rc
->get_dbh();
67 $this->db_events
= $this->rc
->config
->get('db_table_events', $db->table_name($this->db_events
));
68 $this->db_calendars
= $this->rc
->config
->get('db_table_calendars', $db->table_name($this->db_calendars
));
69 $this->db_attachments
= $this->rc
->config
->get('db_table_attachments', $db->table_name($this->db_attachments
));
71 $this->_read_calendars();
75 * Read available calendars for the current user and store them internally
77 private function _read_calendars()
79 $hidden = array_filter(explode(',', $this->rc
->config
->get('hidden_calendars', '')));
81 if (!empty($this->rc
->user
->ID
)) {
82 $calendar_ids = array();
83 $result = $this->rc
->db
->query(
84 "SELECT *, calendar_id AS id FROM " . $this->db_calendars
. "
89 while ($result && ($arr = $this->rc
->db
->fetch_assoc($result))) {
90 $arr['showalarms'] = intval($arr['showalarms']);
91 $arr['active'] = !in_array($arr['id'], $hidden);
92 $arr['name'] = html
::quote($arr['name']);
93 $arr['listname'] = html
::quote($arr['name']);
94 $this->calendars
[$arr['calendar_id']] = $arr;
95 $calendar_ids[] = $this->rc
->db
->quote($arr['calendar_id']);
97 $this->calendar_ids
= join(',', $calendar_ids);
102 * Get a list of available calendars from this source
104 * @param bool $active Return only active calendars
105 * @param bool $personal Return only personal calendars
107 * @return array List of calendars
109 public function list_calendars($active = false, $personal = false)
111 // attempt to create a default calendar for this user
112 if (empty($this->calendars
)) {
113 if ($this->create_calendar(array('name' => 'Default', 'color' => 'cc0000')))
114 $this->_read_calendars();
117 $calendars = $this->calendars
;
119 // filter active calendars
121 foreach ($calendars as $idx => $cal) {
122 if (!$cal['active']) {
123 unset($calendars[$idx]);
128 // 'personal' is unsupported in this driver
134 * Create a new calendar assigned to the current user
136 * @param array Hash array with calendar properties
137 * name: Calendar name
138 * color: The color of the calendar
139 * @return mixed ID of the calendar on success, False on error
141 public function create_calendar($prop)
143 $result = $this->rc
->db
->query(
144 "INSERT INTO " . $this->db_calendars
. "
145 (user_id, name, color, showalarms)
146 VALUES (?, ?, ?, ?)",
150 $prop['showalarms']?
1:0
154 return $this->rc
->db
->insert_id($this->db_calendars
);
160 * Update properties of an existing calendar
162 * @see calendar_driver::edit_calendar()
164 public function edit_calendar($prop)
166 $query = $this->rc
->db
->query(
167 "UPDATE " . $this->db_calendars
. "
168 SET name=?, color=?, showalarms=?
173 $prop['showalarms']?
1:0,
178 return $this->rc
->db
->affected_rows($query);
182 * Set active/subscribed state of a calendar
183 * Save a list of hidden calendars in user prefs
185 * @see calendar_driver::subscribe_calendar()
187 public function subscribe_calendar($prop)
189 $hidden = array_flip(explode(',', $this->rc
->config
->get('hidden_calendars', '')));
192 unset($hidden[$prop['id']]);
194 $hidden[$prop['id']] = 1;
196 return $this->rc
->user
->save_prefs(array('hidden_calendars' => join(',', array_keys($hidden))));
200 * Delete the given calendar with all its contents
202 * @see calendar_driver::remove_calendar()
204 public function remove_calendar($prop)
206 if (!$this->calendars
[$prop['id']])
209 // events and attachments will be deleted by foreign key cascade
211 $query = $this->rc
->db
->query(
212 "DELETE FROM " . $this->db_calendars
. "
213 WHERE calendar_id=?",
217 return $this->rc
->db
->affected_rows($query);
221 * Add a single event to the database
223 * @param array Hash array with event properties
224 * @see calendar_driver::new_event()
226 public function new_event($event)
228 if (!$this->validate($event))
231 if (!empty($this->calendars
)) {
232 if ($event['calendar'] && !$this->calendars
[$event['calendar']])
234 if (!$event['calendar'])
235 $event['calendar'] = reset(array_keys($this->calendars
));
237 $event = $this->_save_preprocess($event);
239 $this->rc
->db
->query(sprintf(
240 "INSERT INTO " . $this->db_events
. "
241 (calendar_id, created, changed, uid, %s, %s, all_day, recurrence, title, description, location, categories, url, free_busy, priority, sensitivity, attendees, alarms, notifyat)
242 VALUES (?, %s, %s, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
243 $this->rc
->db
->quote_identifier('start'),
244 $this->rc
->db
->quote_identifier('end'),
245 $this->rc
->db
->now(),
249 strval($event['uid']),
250 $event['start']->format(self
::DB_DATE_FORMAT
),
251 $event['end']->format(self
::DB_DATE_FORMAT
),
252 intval($event['all_day']),
253 $event['_recurrence'],
254 strval($event['title']),
255 strval($event['description']),
256 strval($event['location']),
257 strval($event['categories']),
258 strval($event['url']),
259 intval($event['free_busy']),
260 intval($event['priority']),
261 intval($event['sensitivity']),
267 $event_id = $this->rc
->db
->insert_id($this->db_events
);
270 $event['id'] = $event_id;
273 if (!empty($event['attachments'])) {
274 foreach ($event['attachments'] as $attachment) {
275 $this->add_attachment($attachment, $event_id);
280 $this->_update_recurring($event);
290 * Update an event entry with the given data
292 * @param array Hash array with event properties
293 * @see calendar_driver::edit_event()
295 public function edit_event($event)
297 if (!empty($this->calendars
)) {
298 $update_master = false;
299 $update_recurring = true;
300 $old = $this->get_event($event);
302 // increment sequence number
303 if ($old['sequence'] && empty($event['sequence']))
304 $event['sequence'] = max($event['sequence'], $old['sequence']+
1);
306 // modify a recurring event, check submitted savemode to do the right things
307 if ($old['recurrence'] ||
$old['recurrence_id']) {
308 $master = $old['recurrence_id'] ?
$this->get_event(array('id' => $old['recurrence_id'])) : $old;
310 // keep saved exceptions (not submitted by the client)
311 if ($old['recurrence']['EXDATE'])
312 $event['recurrence']['EXDATE'] = $old['recurrence']['EXDATE'];
314 switch ($event['_savemode']) {
316 $event['uid'] = $this->cal
->generate_uid();
317 return $this->new_event($event);
320 // add exception to master event
321 $master['recurrence']['EXDATE'][] = $old['start'];
322 $update_master = true;
324 // just update this occurence (decouple from master)
325 $update_recurring = false;
326 $event['recurrence_id'] = 0;
327 $event['recurrence'] = array();
331 if ($master['id'] != $event['id']) {
332 // set until-date on master event, then save this instance as new recurring event
333 $master['recurrence']['UNTIL'] = clone $event['start'];
334 $master['recurrence']['UNTIL']->sub(new DateInterval('P1D'));
335 unset($master['recurrence']['COUNT']);
336 $update_master = true;
338 // if recurrence COUNT, update value to the correct number of future occurences
339 if ($event['recurrence']['COUNT']) {
340 $fromdate = clone $event['start'];
341 $fromdate->setTimezone($this->server_timezone
);
342 $sqlresult = $this->rc
->db
->query(sprintf(
343 "SELECT event_id FROM " . $this->db_events
. "
344 WHERE calendar_id IN (%s)
346 AND recurrence_id=?",
348 $this->rc
->db
->quote_identifier('start')
350 $fromdate->format(self
::DB_DATE_FORMAT
),
352 if ($count = $this->rc
->db
->num_rows($sqlresult))
353 $event['recurrence']['COUNT'] = $count;
356 $update_recurring = true;
357 $event['recurrence_id'] = 0;
360 // else: 'future' == 'all' if modifying the master event
362 default: // 'all' is default
363 $event['id'] = $master['id'];
364 $event['recurrence_id'] = 0;
366 // use start date from master but try to be smart on time or duration changes
367 $old_start_date = $old['start']->format('Y-m-d');
368 $old_start_time = $old['allday'] ?
'' : $old['start']->format('H:i');
369 $old_duration = $old['end']->format('U') - $old['start']->format('U');
371 $new_start_date = $event['start']->format('Y-m-d');
372 $new_start_time = $event['allday'] ?
'' : $event['start']->format('H:i');
373 $new_duration = $event['end']->format('U') - $event['start']->format('U');
375 $diff = $old_start_date != $new_start_date ||
$old_start_time != $new_start_time ||
$old_duration != $new_duration;
377 // shifted or resized
378 if ($diff && ($old_start_date == $new_start_date ||
$old_duration == $new_duration)) {
379 $event['start'] = $master['start']->add($old['start']->diff($event['start']));
380 $event['end'] = clone $event['start'];
381 $event['end']->add(new DateInterval('PT'.$new_duration.'S'));
387 $success = $this->_update_event($event, $update_recurring);
388 if ($success && $update_master)
389 $this->_update_event($master, true);
398 * Convert save data to be used in SQL statements
400 private function _save_preprocess($event)
402 // shift dates to server's timezone
403 $event['start'] = clone $event['start'];
404 $event['start']->setTimezone($this->server_timezone
);
405 $event['end'] = clone $event['end'];
406 $event['end']->setTimezone($this->server_timezone
);
408 // compose vcalendar-style recurrencue rule from structured data
409 $rrule = $event['recurrence'] ? libcalendaring
::to_rrule($event['recurrence']) : '';
410 $event['_recurrence'] = rtrim($rrule, ';');
411 $event['free_busy'] = intval($this->free_busy_map
[strtolower($event['free_busy'])]);
412 $event['sensitivity'] = intval($this->sensitivity_map
[strtolower($event['sensitivity'])]);
414 if (isset($event['allday'])) {
415 $event['all_day'] = $event['allday'] ?
1 : 0;
418 // compute absolute time to notify the user
419 $event['notifyat'] = $this->_get_notification($event);
421 // process event attendees
423 foreach ((array)$event['attendees'] as $attendee) {
424 if (!$attendee['name'] && !$attendee['email'])
426 $_attendees .= 'NAME="'.addcslashes($attendee['name'], '"') . '"' .
427 ';STATUS=' . $attendee['status'].
428 ';ROLE=' . $attendee['role'] .
429 ';EMAIL=' . $attendee['email'] .
432 $event['attendees'] = rtrim($_attendees);
438 * Compute absolute time to notify the user
440 private function _get_notification($event)
442 if ($event['alarms'] && $event['start'] > new DateTime()) {
443 $alarm = libcalendaring
::get_next_alarm($event);
445 if ($alarm['time'] && $alarm['action'] == 'DISPLAY')
446 return date('Y-m-d H:i:s', $alarm['time']);
453 * Save the given event record to database
455 * @param array Event data, already passed through self::_save_preprocess()
456 * @param boolean True if recurring events instances should be updated, too
458 private function _update_event($event, $update_recurring = true)
460 $event = $this->_save_preprocess($event);
462 $set_cols = array('start', 'end', 'all_day', 'recurrence_id', 'sequence', 'title', 'description', 'location', 'categories', 'url', 'free_busy', 'priority', 'sensitivity', 'attendees', 'alarms', 'notifyat');
463 foreach ($set_cols as $col) {
464 if (is_object($event[$col]) && is_a($event[$col], 'DateTime'))
465 $sql_set[] = $this->rc
->db
->quote_identifier($col) . '=' . $this->rc
->db
->quote($event[$col]->format(self
::DB_DATE_FORMAT
));
466 else if (isset($event[$col]))
467 $sql_set[] = $this->rc
->db
->quote_identifier($col) . '=' . $this->rc
->db
->quote($event[$col]);
470 if ($event['_recurrence'])
471 $sql_set[] = $this->rc
->db
->quote_identifier('recurrence') . '=' . $this->rc
->db
->quote($event['_recurrence']);
473 if ($event['_fromcalendar'] && $event['_fromcalendar'] != $event['calendar'])
474 $sql_set[] = 'calendar_id=' . $this->rc
->db
->quote($event['calendar']);
476 $query = $this->rc
->db
->query(sprintf(
477 "UPDATE " . $this->db_events
. "
480 AND calendar_id IN (" . $this->calendar_ids
. ")",
481 $this->rc
->db
->now(),
482 ($sql_set ?
', ' . join(', ', $sql_set) : '')
487 $success = $this->rc
->db
->affected_rows($query);
490 if ($success && !empty($event['attachments'])) {
491 foreach ($event['attachments'] as $attachment) {
492 $this->add_attachment($attachment, $event['id']);
497 // remove attachments
498 if ($success && !empty($event['deleted_attachments'])) {
499 foreach ($event['deleted_attachments'] as $attachment) {
500 $this->remove_attachment($attachment, $event['id']);
505 unset($this->cache
[$event['id']]);
506 if ($update_recurring)
507 $this->_update_recurring($event);
514 * Insert "fake" entries for recurring occurences of this event
516 private function _update_recurring($event)
518 if (empty($this->calendars
))
521 // clear existing recurrence copies
522 $this->rc
->db
->query(
523 "DELETE FROM " . $this->db_events
. "
524 WHERE recurrence_id=?
525 AND calendar_id IN (" . $this->calendar_ids
. ")",
529 // create new fake entries
530 if ($event['recurrence']) {
531 // include library class
532 require_once($this->cal
->home
. '/lib/calendar_recurrence.php');
534 $recurrence = new calendar_recurrence($this->cal
, $event);
537 $duration = $event['start']->diff($event['end']);
538 while ($next_start = $recurrence->next_start()) {
539 $next_start->setTimezone($this->server_timezone
);
540 $next_end = clone $next_start;
541 $next_end->add($duration);
542 $notify_at = $this->_get_notification(array('alarms' => $event['alarms'], 'start' => $next_start, 'end' => $next_end));
543 $query = $this->rc
->db
->query(sprintf(
544 "INSERT INTO " . $this->db_events
. "
545 (calendar_id, recurrence_id, created, changed, uid, %s, %s, all_day, recurrence, title, description, location, categories, url, free_busy, priority, sensitivity, alarms, notifyat)
546 SELECT calendar_id, ?, %s, %s, uid, ?, ?, all_day, recurrence, title, description, location, categories, url, free_busy, priority, sensitivity, alarms, ?
547 FROM " . $this->db_events
. " WHERE event_id=? AND calendar_id IN (" . $this->calendar_ids
. ")",
548 $this->rc
->db
->quote_identifier('start'),
549 $this->rc
->db
->quote_identifier('end'),
550 $this->rc
->db
->now(),
554 $next_start->format(self
::DB_DATE_FORMAT
),
555 $next_end->format(self
::DB_DATE_FORMAT
),
560 if (!$this->rc
->db
->affected_rows($query))
563 // stop adding events for inifinite recurrence after 20 years
564 if (++
$count > 999 ||
(!$recurrence->recurEnd
&& !$recurrence->recurCount
&& $next_start->format('Y') > date('Y') +
20))
571 * Move a single event
573 * @param array Hash array with event properties
574 * @see calendar_driver::move_event()
576 public function move_event($event)
578 // let edit_event() do all the magic
579 return $this->edit_event($event +
(array)$this->get_event($event));
583 * Resize a single event
585 * @param array Hash array with event properties
586 * @see calendar_driver::resize_event()
588 public function resize_event($event)
590 // let edit_event() do all the magic
591 return $this->edit_event($event +
(array)$this->get_event($event));
595 * Remove a single event from the database
597 * @param array Hash array with event properties
598 * @param boolean Remove record irreversible (@TODO)
600 * @see calendar_driver::remove_event()
602 public function remove_event($event, $force = true)
604 if (!empty($this->calendars
)) {
605 $event +
= (array)$this->get_event($event);
607 $update_master = false;
610 // read master if deleting a recurring event
611 if ($event['recurrence'] ||
$event['recurrence_id']) {
612 $master = $event['recurrence_id'] ?
$this->get_event(array('id' => $event['recurrence_id'])) : $event;
613 $savemode = $event['_savemode'];
618 // add exception to master event
619 $master['recurrence']['EXDATE'][] = $event['start'];
620 $update_master = true;
622 // just delete this single occurence
623 $query = $this->rc
->db
->query(
624 "DELETE FROM " . $this->db_events
. "
625 WHERE calendar_id IN (" . $this->calendar_ids
. ")
632 if ($master['id'] != $event['id']) {
633 // set until-date on master event
634 $master['recurrence']['UNTIL'] = clone $event['start'];
635 $master['recurrence']['UNTIL']->sub(new DateInterval('P1D'));
636 unset($master['recurrence']['COUNT']);
637 $update_master = true;
639 // delete this and all future instances
640 $fromdate = clone $event['start'];
641 $fromdate->setTimezone($this->server_timezone
);
642 $query = $this->rc
->db
->query(
643 "DELETE FROM " . $this->db_events
. "
644 WHERE calendar_id IN (" . $this->calendar_ids
. ")
645 AND " . $this->rc
->db
->quote_identifier('start') . " >= ?
646 AND recurrence_id=?",
647 $fromdate->format(self
::DB_DATE_FORMAT
),
652 // else: future == all if modifying the master event
654 default: // 'all' is default
655 $query = $this->rc
->db
->query(
656 "DELETE FROM " . $this->db_events
. "
657 WHERE (event_id=? OR recurrence_id=?)
658 AND calendar_id IN (" . $this->calendar_ids
. ")",
665 $success = $this->rc
->db
->affected_rows($query);
666 if ($success && $update_master)
667 $this->_update_event($master, true);
676 * Return data of a specific event
677 * @param mixed Hash array with event properties or event UID
678 * @param boolean Only search in writeable calendars (ignored)
679 * @param boolean Only search in active calendars
680 * @param boolean Only search in personal calendars (ignored)
681 * @return array Hash array with event properties
683 public function get_event($event, $writeable = false, $active = false, $personal = false)
685 $id = is_array($event) ?
($event['id'] ?
$event['id'] : $event['uid']) : $event;
686 $col = is_array($event) && is_numeric($id) ?
'event_id' : 'uid';
688 if ($this->cache
[$id])
689 return $this->cache
[$id];
692 $calendars = $this->calendars
;
693 foreach ($calendars as $idx => $cal) {
694 if (!$cal['active']) {
695 unset($calendars[$idx]);
698 $cals = join(',', $calendars);
701 $cals = $this->calendar_ids
;
704 $result = $this->rc
->db
->query(sprintf(
705 "SELECT e.*, (SELECT COUNT(attachment_id) FROM " . $this->db_attachments
. "
706 WHERE event_id = e.event_id OR event_id = e.recurrence_id) AS _attachments
707 FROM " . $this->db_events
. " AS e
708 WHERE e.calendar_id IN (%s)
714 if ($result && ($event = $this->rc
->db
->fetch_assoc($result)) && $event['event_id']) {
715 $this->cache
[$id] = $this->_read_postprocess($event);
716 return $this->cache
[$id];
725 * @see calendar_driver::load_events()
727 public function load_events($start, $end, $query = null, $calendars = null, $virtual = 1, $modifiedsince = null)
729 if (empty($calendars))
730 $calendars = array_keys($this->calendars
);
731 else if (is_string($calendars))
732 $calendars = explode(',', $calendars);
734 // only allow to select from calendars of this use
735 $calendar_ids = array_map(array($this->rc
->db
, 'quote'), array_intersect($calendars, array_keys($this->calendars
)));
737 // compose (slow) SQL query for searching
738 // FIXME: improve searching using a dedicated col and normalized values
740 foreach (array('title','location','description','categories','attendees') as $col)
741 $sql_query[] = $this->rc
->db
->ilike($col, '%'.$query.'%');
742 $sql_add = 'AND (' . join(' OR ', $sql_query) . ')';
746 $sql_arr .= ' AND e.recurrence_id = 0';
749 $sql_add .= ' AND e.changed >= ' . $this->rc
->db
->quote(date('Y-m-d H:i:s', $modifiedsince));
752 if (!empty($calendar_ids)) {
753 $result = $this->rc
->db
->query(sprintf(
754 "SELECT e.*, (SELECT COUNT(attachment_id) FROM " . $this->db_attachments
. "
755 WHERE event_id = e.event_id OR event_id = e.recurrence_id) AS _attachments
756 FROM " . $this->db_events
. " AS e
757 WHERE e.calendar_id IN (%s)
758 AND e.start <= %s AND e.end >= %s
760 GROUP BY e.event_id",
761 join(',', $calendar_ids),
762 $this->rc
->db
->fromunixtime($end),
763 $this->rc
->db
->fromunixtime($start),
767 while ($result && ($event = $this->rc
->db
->fetch_assoc($result))) {
768 $events[] = $this->_read_postprocess($event);
776 * Convert sql record into a rcube style event object
778 private function _read_postprocess($event)
780 $free_busy_map = array_flip($this->free_busy_map
);
781 $sensitivity_map = array_flip($this->sensitivity_map
);
783 $event['id'] = $event['event_id'];
784 $event['start'] = new DateTime($event['start']);
785 $event['end'] = new DateTime($event['end']);
786 $event['allday'] = intval($event['all_day']);
787 $event['created'] = new DateTime($event['created']);
788 $event['changed'] = new DateTime($event['changed']);
789 $event['free_busy'] = $free_busy_map[$event['free_busy']];
790 $event['sensitivity'] = $sensitivity_map[$event['sensitivity']];
791 $event['calendar'] = $event['calendar_id'];
792 $event['recurrence_id'] = intval($event['recurrence_id']);
794 // parse recurrence rule
795 if ($event['recurrence'] && preg_match_all('/([A-Z]+)=([^;]+);?/', $event['recurrence'], $m, PREG_SET_ORDER
)) {
796 $event['recurrence'] = array();
797 foreach ($m as $rr) {
798 if (is_numeric($rr[2]))
799 $rr[2] = intval($rr[2]);
800 else if ($rr[1] == 'UNTIL')
801 $rr[2] = date_create($rr[2]);
802 else if ($rr[1] == 'EXDATE')
803 $rr[2] = array_map('date_create', explode(',', $rr[2]));
804 $event['recurrence'][$rr[1]] = $rr[2];
808 if ($event['_attachments'] > 0)
809 $event['attachments'] = (array)$this->list_attachments($event);
811 // decode serialized event attendees
812 if ($event['attendees']) {
813 $attendees = array();
814 foreach (explode("\n", $event['attendees']) as $line) {
816 foreach (rcube_utils
::explode_quoted_string(';', $line) as $prop) {
817 list($key, $value) = explode("=", $prop);
818 $att[strtolower($key)] = stripslashes(trim($value, '""'));
822 $event['attendees'] = $attendees;
825 unset($event['event_id'], $event['calendar_id'], $event['notifyat'], $event['all_day'], $event['_attachments']);
830 * Get a list of pending alarms to be displayed to the user
832 * @see calendar_driver::pending_alarms()
834 public function pending_alarms($time, $calendars = null)
836 if (empty($calendars))
837 $calendars = array_keys($this->calendars
);
838 else if (is_string($calendars))
839 $calendars = explode(',', $calendars);
841 // only allow to select from calendars with activated alarms
842 $calendar_ids = array();
843 foreach ($calendars as $cid) {
844 if ($this->calendars
[$cid] && $this->calendars
[$cid]['showalarms'])
845 $calendar_ids[] = $cid;
847 $calendar_ids = array_map(array($this->rc
->db
, 'quote'), $calendar_ids);
850 if (!empty($calendar_ids)) {
851 $result = $this->rc
->db
->query(sprintf(
852 "SELECT * FROM " . $this->db_events
. "
853 WHERE calendar_id IN (%s)
854 AND notifyat <= %s AND %s > %s",
855 join(',', $calendar_ids),
856 $this->rc
->db
->fromunixtime($time),
857 $this->rc
->db
->quote_identifier('end'),
858 $this->rc
->db
->fromunixtime($time)
861 while ($result && ($event = $this->rc
->db
->fetch_assoc($result)))
862 $alarms[] = $this->_read_postprocess($event);
869 * Feedback after showing/sending an alarm notification
871 * @see calendar_driver::dismiss_alarm()
873 public function dismiss_alarm($event_id, $snooze = 0)
875 // set new notifyat time or unset if not snoozed
876 $notify_at = $snooze > 0 ?
date(self
::DB_DATE_FORMAT
, time() +
$snooze) : null;
878 $query = $this->rc
->db
->query(sprintf(
879 "UPDATE " . $this->db_events
. "
880 SET changed=%s, notifyat=?
882 AND calendar_id IN (" . $this->calendar_ids
. ")",
883 $this->rc
->db
->now()),
888 return $this->rc
->db
->affected_rows($query);
892 * Save an attachment related to the given event
894 private function add_attachment($attachment, $event_id)
896 $data = $attachment['data'] ?
$attachment['data'] : file_get_contents($attachment['path']);
898 $query = $this->rc
->db
->query(
899 "INSERT INTO " . $this->db_attachments
.
900 " (event_id, filename, mimetype, size, data)" .
901 " VALUES (?, ?, ?, ?, ?)",
904 $attachment['mimetype'],
909 return $this->rc
->db
->affected_rows($query);
913 * Remove a specific attachment from the given event
915 private function remove_attachment($attachment_id, $event_id)
917 $query = $this->rc
->db
->query(
918 "DELETE FROM " . $this->db_attachments
.
919 " WHERE attachment_id = ?" .
920 " AND event_id IN (SELECT event_id FROM " . $this->db_events
.
921 " WHERE event_id = ?" .
922 " AND calendar_id IN (" . $this->calendar_ids
. "))",
927 return $this->rc
->db
->affected_rows($query);
931 * List attachments of specified event
933 public function list_attachments($event)
935 $attachments = array();
937 if (!empty($this->calendar_ids
)) {
938 $result = $this->rc
->db
->query(
939 "SELECT attachment_id AS id, filename AS name, mimetype, size " .
940 " FROM " . $this->db_attachments
.
941 " WHERE event_id IN (SELECT event_id FROM " . $this->db_events
.
942 " WHERE event_id=?" .
943 " AND calendar_id IN (" . $this->calendar_ids
. "))".
944 " ORDER BY filename",
945 $event['recurrence_id'] ?
$event['recurrence_id'] : $event['event_id']
948 while ($result && ($arr = $this->rc
->db
->fetch_assoc($result))) {
949 $attachments[] = $arr;
957 * Get attachment properties
959 public function get_attachment($id, $event)
961 if (!empty($this->calendar_ids
)) {
962 $result = $this->rc
->db
->query(
963 "SELECT attachment_id AS id, filename AS name, mimetype, size " .
964 " FROM " . $this->db_attachments
.
965 " WHERE attachment_id=?".
968 $event['recurrence_id'] ?
$event['recurrence_id'] : $event['id']
971 if ($result && ($arr = $this->rc
->db
->fetch_assoc($result))) {
980 * Get attachment body
982 public function get_attachment_body($id, $event)
984 if (!empty($this->calendar_ids
)) {
985 $result = $this->rc
->db
->query(
987 " FROM " . $this->db_attachments
.
988 " WHERE attachment_id=?".
994 if ($result && ($arr = $this->rc
->db
->fetch_assoc($result))) {
995 return base64_decode($arr['data']);
1003 * Remove the given category
1005 public function remove_category($name)
1007 $query = $this->rc
->db
->query(
1008 "UPDATE " . $this->db_events
. "
1011 AND calendar_id IN (" . $this->calendar_ids
. ")",
1015 return $this->rc
->db
->affected_rows($query);
1019 * Update/replace a category
1021 public function replace_category($oldname, $name, $color)
1023 $query = $this->rc
->db
->query(
1024 "UPDATE " . $this->db_events
. "
1027 AND calendar_id IN (" . $this->calendar_ids
. ")",
1032 return $this->rc
->db
->affected_rows($query);
This page took 0.197622 seconds and 6 git commands to generate.