annotate plugins/calendar/drivers/kolab/kolab_calendar.php @ 3:f6fe4b6ae66a

calendar plugin nearly as distributed
author Charlie Root
date Sat, 13 Jan 2018 08:56:12 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
1 <?php
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
2
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
3 /**
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
4 * Kolab calendar storage class
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
5 *
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
6 * @version @package_version@
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
7 * @author Thomas Bruederli <bruederli@kolabsys.com>
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
8 * @author Aleksander Machniak <machniak@kolabsys.com>
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
9 *
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
10 * Copyright (C) 2012-2015, Kolab Systems AG <contact@kolabsys.com>
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
11 *
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
12 * This program is free software: you can redistribute it and/or modify
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
13 * it under the terms of the GNU Affero General Public License as
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
14 * published by the Free Software Foundation, either version 3 of the
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
15 * License, or (at your option) any later version.
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
16 *
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
17 * This program is distributed in the hope that it will be useful,
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
20 * GNU Affero General Public License for more details.
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
21 *
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
22 * You should have received a copy of the GNU Affero General Public License
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
24 */
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
25
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
26
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
27 class kolab_calendar extends kolab_storage_folder_api
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
28 {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
29 public $ready = false;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
30 public $rights = 'lrs';
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
31 public $editable = false;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
32 public $attachments = true;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
33 public $alarms = false;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
34 public $history = false;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
35 public $subscriptions = true;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
36 public $categories = array();
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
37 public $storage;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
38
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
39 public $type = 'event';
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
40
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
41 protected $cal;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
42 protected $events = array();
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
43 protected $search_fields = array('title', 'description', 'location', 'attendees');
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
44
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
45 /**
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
46 * Factory method to instantiate a kolab_calendar object
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
47 *
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
48 * @param string Calendar ID (encoded IMAP folder name)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
49 * @param object calendar plugin object
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
50 * @return object kolab_calendar instance
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
51 */
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
52 public static function factory($id, $calendar)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
53 {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
54 $imap = $calendar->rc->get_storage();
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
55 $imap_folder = kolab_storage::id_decode($id);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
56 $info = $imap->folder_info($imap_folder, true);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
57 if (empty($info) || $info['noselect'] || strpos(kolab_storage::folder_type($imap_folder), 'event') !== 0) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
58 return new kolab_user_calendar($imap_folder, $calendar);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
59 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
60 else {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
61 return new kolab_calendar($imap_folder, $calendar);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
62 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
63 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
64
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
65 /**
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
66 * Default constructor
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
67 */
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
68 public function __construct($imap_folder, $calendar)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
69 {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
70 $this->cal = $calendar;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
71 $this->imap = $calendar->rc->get_storage();
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
72 $this->name = $imap_folder;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
73
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
74 // ID is derrived from folder name
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
75 $this->id = kolab_storage::folder_id($this->name, true);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
76 $old_id = kolab_storage::folder_id($this->name, false);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
77
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
78 // fetch objects from the given IMAP folder
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
79 $this->storage = kolab_storage::get_folder($this->name);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
80 $this->ready = $this->storage && $this->storage->valid;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
81
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
82 // Set writeable and alarms flags according to folder permissions
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
83 if ($this->ready) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
84 if ($this->storage->get_namespace() == 'personal') {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
85 $this->editable = true;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
86 $this->rights = 'lrswikxteav';
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
87 $this->alarms = true;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
88 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
89 else {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
90 $rights = $this->storage->get_myrights();
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
91 if ($rights && !PEAR::isError($rights)) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
92 $this->rights = $rights;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
93 if (strpos($rights, 't') !== false || strpos($rights, 'd') !== false)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
94 $this->editable = strpos($rights, 'i');;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
95 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
96 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
97
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
98 // user-specific alarms settings win
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
99 $prefs = $this->cal->rc->config->get('kolab_calendars', array());
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
100 if (isset($prefs[$this->id]['showalarms']))
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
101 $this->alarms = $prefs[$this->id]['showalarms'];
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
102 else if (isset($prefs[$old_id]['showalarms']))
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
103 $this->alarms = $prefs[$old_id]['showalarms'];
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
104 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
105
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
106 $this->default = $this->storage->default;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
107 $this->subtype = $this->storage->subtype;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
108 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
109
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
110
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
111 /**
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
112 * Getter for the IMAP folder name
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
113 *
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
114 * @return string Name of the IMAP folder
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
115 */
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
116 public function get_realname()
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
117 {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
118 return $this->name;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
119 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
120
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
121 /**
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
122 *
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
123 */
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
124 public function get_title()
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
125 {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
126 return null;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
127 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
128
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
129
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
130 /**
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
131 * Return color to display this calendar
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
132 */
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
133 public function get_color($default = null)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
134 {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
135 // color is defined in folder METADATA
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
136 if ($color = $this->storage->get_color()) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
137 return $color;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
138 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
139
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
140 // calendar color is stored in user prefs (temporary solution)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
141 $prefs = $this->cal->rc->config->get('kolab_calendars', array());
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
142
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
143 if (!empty($prefs[$this->id]) && !empty($prefs[$this->id]['color']))
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
144 return $prefs[$this->id]['color'];
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
145
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
146 return $default ?: 'cc0000';
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
147 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
148
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
149 /**
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
150 * Compose an URL for CalDAV access to this calendar (if configured)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
151 */
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
152 public function get_caldav_url()
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
153 {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
154 if ($template = $this->cal->rc->config->get('calendar_caldav_url', null)) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
155 return strtr($template, array(
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
156 '%h' => $_SERVER['HTTP_HOST'],
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
157 '%u' => urlencode($this->cal->rc->get_user_name()),
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
158 '%i' => urlencode($this->storage->get_uid()),
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
159 '%n' => urlencode($this->name),
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
160 ));
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
161 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
162
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
163 return false;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
164 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
165
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
166
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
167 /**
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
168 * Update properties of this calendar folder
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
169 *
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
170 * @see calendar_driver::edit_calendar()
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
171 */
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
172 public function update(&$prop)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
173 {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
174 $prop['oldname'] = $this->get_realname();
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
175 $newfolder = kolab_storage::folder_update($prop);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
176
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
177 if ($newfolder === false) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
178 $this->cal->last_error = $this->cal->gettext(kolab_storage::$last_error);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
179 return false;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
180 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
181
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
182 // create ID
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
183 return kolab_storage::folder_id($newfolder);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
184 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
185
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
186 /**
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
187 * Getter for a single event object
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
188 */
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
189 public function get_event($id)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
190 {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
191 // remove our occurrence identifier if it's there
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
192 $master_id = preg_replace('/-\d{8}(T\d{6})?$/', '', $id);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
193
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
194 // directly access storage object
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
195 if (!$this->events[$id] && $master_id == $id && ($record = $this->storage->get_object($id))) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
196 $this->events[$id] = $this->_to_driver_event($record, true);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
197 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
198
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
199 // maybe a recurring instance is requested
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
200 if (!$this->events[$id] && $master_id != $id) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
201 $instance_id = substr($id, strlen($master_id) + 1);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
202
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
203 if ($record = $this->storage->get_object($master_id)) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
204 $master = $this->_to_driver_event($record);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
205 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
206
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
207 if ($master) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
208 // check for match in top-level exceptions (aka loose single occurrences)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
209 if ($master['_formatobj'] && ($instance = $master['_formatobj']->get_instance($instance_id))) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
210 $this->events[$id] = $this->_to_driver_event($instance, false, true, $master);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
211 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
212 // check for match on the first instance already
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
213 else if ($master['_instance'] && $master['_instance'] == $instance_id) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
214 $this->events[$id] = $master;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
215 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
216 else if (is_array($master['recurrence'])) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
217 $this->get_recurring_events($record, $master['start'], null, $id);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
218 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
219 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
220 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
221
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
222 return $this->events[$id];
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
223 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
224
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
225 /**
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
226 * Get attachment body
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
227 * @see calendar_driver::get_attachment_body()
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
228 */
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
229 public function get_attachment_body($id, $event)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
230 {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
231 if (!$this->ready)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
232 return false;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
233
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
234 $data = $this->storage->get_attachment($event['id'], $id);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
235
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
236 if ($data == null) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
237 // try again with master UID
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
238 $uid = preg_replace('/-\d+(T\d{6})?$/', '', $event['id']);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
239 if ($uid != $event['id']) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
240 $data = $this->storage->get_attachment($uid, $id);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
241 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
242 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
243
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
244 return $data;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
245 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
246
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
247 /**
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
248 * @param integer Event's new start (unix timestamp)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
249 * @param integer Event's new end (unix timestamp)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
250 * @param string Search query (optional)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
251 * @param boolean Include virtual events (optional)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
252 * @param array Additional parameters to query storage
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
253 * @param array Additional query to filter events
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
254 * @return array A list of event records
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
255 */
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
256 public function list_events($start, $end, $search = null, $virtual = 1, $query = array(), $filter_query = null)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
257 {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
258 // convert to DateTime for comparisons
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
259 // #5190: make the range a little bit wider
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
260 // to workaround possible timezone differences
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
261 try {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
262 $start = new DateTime('@' . ($start - 12 * 3600));
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
263 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
264 catch (Exception $e) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
265 $start = new DateTime('@0');
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
266 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
267 try {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
268 $end = new DateTime('@' . ($end + 12 * 3600));
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
269 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
270 catch (Exception $e) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
271 $end = new DateTime('today +10 years');
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
272 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
273
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
274 // get email addresses of the current user
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
275 $user_emails = $this->cal->get_user_emails();
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
276
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
277 // query Kolab storage
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
278 $query[] = array('dtstart', '<=', $end);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
279 $query[] = array('dtend', '>=', $start);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
280
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
281 if (is_array($filter_query)) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
282 $query = array_merge($query, $filter_query);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
283 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
284
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
285 if (!empty($search)) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
286 $search = mb_strtolower($search);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
287 $words = rcube_utils::tokenize_string($search, 1);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
288 foreach (rcube_utils::normalize_string($search, true) as $word) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
289 $query[] = array('words', 'LIKE', $word);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
290 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
291 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
292 else {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
293 $words = array();
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
294 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
295
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
296 // set partstat filter to skip pending and declined invitations
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
297 if (empty($filter_query) && $this->cal->rc->config->get('kolab_invitation_calendars')
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
298 && $this->get_namespace() != 'other'
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
299 ) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
300 $partstat_exclude = array('NEEDS-ACTION','DECLINED');
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
301 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
302 else {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
303 $partstat_exclude = array();
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
304 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
305
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
306 $events = array();
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
307 foreach ($this->storage->select($query) as $record) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
308 $event = $this->_to_driver_event($record, !$virtual, false);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
309
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
310 // remember seen categories
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
311 if ($event['categories']) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
312 $cat = is_array($event['categories']) ? $event['categories'][0] : $event['categories'];
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
313 $this->categories[$cat]++;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
314 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
315
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
316 // list events in requested time window
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
317 if ($event['start'] <= $end && $event['end'] >= $start) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
318 unset($event['_attendees']);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
319 $add = true;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
320
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
321 // skip the first instance of a recurring event if listed in exdate
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
322 if ($virtual && !empty($event['recurrence']['EXDATE'])) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
323 $event_date = $event['start']->format('Ymd');
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
324 $exdates = (array)$event['recurrence']['EXDATE'];
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
325
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
326 foreach ($exdates as $exdate) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
327 if ($exdate->format('Ymd') == $event_date) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
328 $add = false;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
329 break;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
330 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
331 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
332 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
333
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
334 // find and merge exception for the first instance
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
335 if ($virtual && !empty($event['recurrence']) && is_array($event['recurrence']['EXCEPTIONS'])) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
336 foreach ($event['recurrence']['EXCEPTIONS'] as $exception) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
337 if ($event['_instance'] == $exception['_instance']) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
338 // clone date objects from main event before adjusting them with exception data
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
339 if (is_object($event['start'])) $event['start'] = clone $record['start'];
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
340 if (is_object($event['end'])) $event['end'] = clone $record['end'];
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
341 kolab_driver::merge_exception_data($event, $exception);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
342 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
343 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
344 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
345
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
346 if ($add)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
347 $events[] = $event;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
348 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
349
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
350 // resolve recurring events
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
351 if ($record['recurrence'] && $virtual == 1) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
352 $events = array_merge($events, $this->get_recurring_events($record, $start, $end));
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
353 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
354 // add top-level exceptions (aka loose single occurrences)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
355 else if (is_array($record['exceptions'])) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
356 foreach ($record['exceptions'] as $ex) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
357 $component = $this->_to_driver_event($ex, false, false, $record);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
358 if ($component['start'] <= $end && $component['end'] >= $start) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
359 $events[] = $component;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
360 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
361 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
362 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
363 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
364
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
365 // post-filter all events by fulltext search and partstat values
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
366 $me = $this;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
367 $events = array_filter($events, function($event) use ($words, $partstat_exclude, $user_emails, $me) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
368 // fulltext search
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
369 if (count($words)) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
370 $hits = 0;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
371 foreach ($words as $word) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
372 $hits += $me->fulltext_match($event, $word, false);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
373 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
374 if ($hits < count($words)) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
375 return false;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
376 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
377 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
378
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
379 // partstat filter
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
380 if (count($partstat_exclude) && is_array($event['attendees'])) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
381 foreach ($event['attendees'] as $attendee) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
382 if (in_array($attendee['email'], $user_emails) && in_array($attendee['status'], $partstat_exclude)) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
383 return false;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
384 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
385 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
386 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
387
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
388 return true;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
389 });
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
390
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
391 // Apply event-to-mail relations
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
392 $config = kolab_storage_config::get_instance();
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
393 $config->apply_links($events);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
394
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
395 // avoid session race conditions that will loose temporary subscriptions
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
396 $this->cal->rc->session->nowrite = true;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
397
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
398 return $events;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
399 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
400
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
401 /**
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
402 *
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
403 * @param integer Date range start (unix timestamp)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
404 * @param integer Date range end (unix timestamp)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
405 * @param array Additional query to filter events
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
406 * @return integer Count
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
407 */
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
408 public function count_events($start, $end = null, $filter_query = null)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
409 {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
410 // convert to DateTime for comparisons
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
411 try {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
412 $start = new DateTime('@'.$start);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
413 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
414 catch (Exception $e) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
415 $start = new DateTime('@0');
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
416 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
417 if ($end) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
418 try {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
419 $end = new DateTime('@'.$end);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
420 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
421 catch (Exception $e) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
422 $end = null;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
423 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
424 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
425
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
426 // query Kolab storage
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
427 $query[] = array('dtend', '>=', $start);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
428
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
429 if ($end)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
430 $query[] = array('dtstart', '<=', $end);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
431
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
432 // add query to exclude pending/declined invitations
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
433 if (empty($filter_query)) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
434 foreach ($this->cal->get_user_emails() as $email) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
435 $query[] = array('tags', '!=', 'x-partstat:' . $email . ':needs-action');
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
436 $query[] = array('tags', '!=', 'x-partstat:' . $email . ':declined');
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
437 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
438 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
439 else if (is_array($filter_query)) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
440 $query = array_merge($query, $filter_query);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
441 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
442
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
443 // we rely the Kolab storage query (no post-filtering)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
444 return $this->storage->count($query);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
445 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
446
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
447 /**
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
448 * Create a new event record
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
449 *
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
450 * @see calendar_driver::new_event()
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
451 *
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
452 * @return mixed The created record ID on success, False on error
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
453 */
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
454 public function insert_event($event)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
455 {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
456 if (!is_array($event))
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
457 return false;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
458
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
459 // email links are stored separately
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
460 $links = $event['links'];
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
461 unset($event['links']);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
462
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
463 //generate new event from RC input
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
464 $object = $this->_from_driver_event($event);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
465 $saved = $this->storage->save($object, 'event');
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
466
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
467 if (!$saved) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
468 rcube::raise_error(array(
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
469 'code' => 600, 'type' => 'php',
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
470 'file' => __FILE__, 'line' => __LINE__,
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
471 'message' => "Error saving event object to Kolab server"),
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
472 true, false);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
473 $saved = false;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
474 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
475 else {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
476 // save links in configuration.relation object
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
477 if ($this->save_links($event['uid'], $links)) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
478 $object['links'] = $links;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
479 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
480
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
481 $this->events = array($event['uid'] => $this->_to_driver_event($object, true));
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
482 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
483
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
484 return $saved;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
485 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
486
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
487 /**
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
488 * Update a specific event record
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
489 *
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
490 * @see calendar_driver::new_event()
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
491 * @return boolean True on success, False on error
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
492 */
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
493
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
494 public function update_event($event, $exception_id = null)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
495 {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
496 $updated = false;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
497 $old = $this->storage->get_object($event['uid'] ?: $event['id']);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
498 if (!$old || PEAR::isError($old))
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
499 return false;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
500
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
501 // email links are stored separately
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
502 $links = $event['links'];
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
503 unset($event['links']);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
504
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
505 $object = $this->_from_driver_event($event, $old);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
506 $saved = $this->storage->save($object, 'event', $old['uid']);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
507
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
508 if (!$saved) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
509 rcube::raise_error(array(
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
510 'code' => 600, 'type' => 'php',
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
511 'file' => __FILE__, 'line' => __LINE__,
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
512 'message' => "Error saving event object to Kolab server"),
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
513 true, false);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
514 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
515 else {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
516 // save links in configuration.relation object
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
517 if ($this->save_links($event['uid'], $links)) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
518 $object['links'] = $links;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
519 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
520
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
521 $updated = true;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
522 $this->events = array($event['uid'] => $this->_to_driver_event($object, true));
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
523
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
524 // refresh local cache with recurring instances
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
525 if ($exception_id) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
526 $this->get_recurring_events($object, $event['start'], $event['end'], $exception_id);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
527 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
528 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
529
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
530 return $updated;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
531 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
532
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
533 /**
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
534 * Delete an event record
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
535 *
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
536 * @see calendar_driver::remove_event()
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
537 * @return boolean True on success, False on error
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
538 */
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
539 public function delete_event($event, $force = true)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
540 {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
541 $deleted = $this->storage->delete($event['uid'] ?: $event['id'], $force);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
542
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
543 if (!$deleted) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
544 rcube::raise_error(array(
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
545 'code' => 600, 'type' => 'php',
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
546 'file' => __FILE__, 'line' => __LINE__,
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
547 'message' => sprintf("Error deleting event object '%s' from Kolab server", $event['id'])),
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
548 true, false);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
549 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
550
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
551 return $deleted;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
552 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
553
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
554 /**
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
555 * Restore deleted event record
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
556 *
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
557 * @see calendar_driver::undelete_event()
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
558 * @return boolean True on success, False on error
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
559 */
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
560 public function restore_event($event)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
561 {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
562 if ($this->storage->undelete($event['id'])) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
563 return true;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
564 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
565 else {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
566 rcube::raise_error(array(
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
567 'code' => 600, 'type' => 'php',
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
568 'file' => __FILE__, 'line' => __LINE__,
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
569 'message' => "Error undeleting the event object $event[id] from the Kolab server"),
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
570 true, false);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
571 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
572
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
573 return false;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
574 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
575
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
576 /**
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
577 * Find messages linked with an event
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
578 */
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
579 protected function get_links($uid)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
580 {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
581 $storage = kolab_storage_config::get_instance();
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
582 return $storage->get_object_links($uid);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
583 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
584
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
585 /**
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
586 *
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
587 */
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
588 protected function save_links($uid, $links)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
589 {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
590 $storage = kolab_storage_config::get_instance();
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
591 return $storage->save_object_links($uid, (array) $links);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
592 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
593
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
594 /**
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
595 * Create instances of a recurring event
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
596 *
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
597 * @param array Hash array with event properties
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
598 * @param object DateTime Start date of the recurrence window
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
599 * @param object DateTime End date of the recurrence window
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
600 * @param string ID of a specific recurring event instance
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
601 * @return array List of recurring event instances
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
602 */
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
603 public function get_recurring_events($event, $start, $end = null, $event_id = null)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
604 {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
605 $object = $event['_formatobj'];
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
606 if (!$object) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
607 $rec = $this->storage->get_object($event['id']);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
608 $object = $rec['_formatobj'];
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
609 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
610 if (!is_object($object))
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
611 return array();
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
612
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
613 // determine a reasonable end date if none given
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
614 if (!$end) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
615 $end = clone $event['start'];
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
616 $end->add(new DateInterval('P100Y'));
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
617 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
618
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
619 // copy the recurrence rule from the master event (to be used in the UI)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
620 $recurrence_rule = $event['recurrence'];
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
621 unset($recurrence_rule['EXCEPTIONS'], $recurrence_rule['EXDATE']);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
622
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
623 // read recurrence exceptions first
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
624 $events = array();
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
625 $exdata = array();
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
626 $futuredata = array();
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
627 $recurrence_id_format = libcalendaring::recurrence_id_format($event);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
628
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
629 if (is_array($event['recurrence']['EXCEPTIONS'])) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
630 foreach ($event['recurrence']['EXCEPTIONS'] as $exception) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
631 if (!$exception['_instance'])
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
632 $exception['_instance'] = libcalendaring::recurrence_instance_identifier($exception, $event['allday']);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
633
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
634 $rec_event = $this->_to_driver_event($exception, false, false, $event);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
635 $rec_event['id'] = $event['uid'] . '-' . $exception['_instance'];
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
636 $rec_event['isexception'] = 1;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
637
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
638 // found the specifically requested instance: register exception (single occurrence wins)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
639 if ($rec_event['id'] == $event_id && (!$this->events[$event_id] || $this->events[$event_id]['thisandfuture'])) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
640 $rec_event['recurrence'] = $recurrence_rule;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
641 $rec_event['recurrence_id'] = $event['uid'];
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
642 $this->events[$rec_event['id']] = $rec_event;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
643 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
644
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
645 // remember this exception's date
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
646 $exdate = substr($exception['_instance'], 0, 8);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
647 if (!$exdata[$exdate] || $exdata[$exdate]['thisandfuture']) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
648 $exdata[$exdate] = $rec_event;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
649 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
650 if ($rec_event['thisandfuture']) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
651 $futuredata[$exdate] = $rec_event;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
652 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
653 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
654 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
655
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
656 // found the specifically requested instance, exiting...
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
657 if ($event_id && !empty($this->events[$event_id])) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
658 return array($this->events[$event_id]);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
659 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
660
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
661 // use libkolab to compute recurring events
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
662 if (class_exists('kolabcalendaring')) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
663 $recurrence = new kolab_date_recurrence($object);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
664 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
665 else {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
666 // fallback to local recurrence implementation
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
667 require_once($this->cal->home . '/lib/calendar_recurrence.php');
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
668 $recurrence = new calendar_recurrence($this->cal, $event);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
669 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
670
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
671 $i = 0;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
672 while ($next_event = $recurrence->next_instance()) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
673 $datestr = $next_event['start']->format('Ymd');
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
674 $instance_id = $next_event['start']->format($recurrence_id_format);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
675
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
676 // use this event data for future recurring instances
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
677 if ($futuredata[$datestr])
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
678 $overlay_data = $futuredata[$datestr];
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
679
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
680 $rec_id = $event['uid'] . '-' . $instance_id;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
681 $exception = $exdata[$datestr] ?: $overlay_data;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
682 $event_start = $next_event['start'];
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
683 $event_end = $next_event['end'];
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
684
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
685 // copy some event from exception to get proper start/end dates
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
686 if ($exception) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
687 $event_copy = $next_event;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
688 kolab_driver::merge_exception_dates($event_copy, $exception);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
689 $event_start = $event_copy['start'];
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
690 $event_end = $event_copy['end'];
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
691 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
692
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
693 // add to output if in range
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
694 if (($event_start <= $end && $event_end >= $start) || ($event_id && $rec_id == $event_id)) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
695 $rec_event = $this->_to_driver_event($next_event, false, false, $event);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
696 $rec_event['_instance'] = $instance_id;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
697 $rec_event['_count'] = $i + 1;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
698
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
699 if ($exception) // copy data from exception
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
700 kolab_driver::merge_exception_data($rec_event, $exception);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
701
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
702 $rec_event['id'] = $rec_id;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
703 $rec_event['recurrence_id'] = $event['uid'];
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
704 $rec_event['recurrence'] = $recurrence_rule;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
705 unset($rec_event['_attendees']);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
706 $events[] = $rec_event;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
707
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
708 if ($rec_id == $event_id) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
709 $this->events[$rec_id] = $rec_event;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
710 break;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
711 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
712 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
713 else if ($next_event['start'] > $end) // stop loop if out of range
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
714 break;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
715
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
716 // avoid endless recursion loops
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
717 if (++$i > 100000)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
718 break;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
719 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
720
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
721 return $events;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
722 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
723
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
724 /**
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
725 * Convert from Kolab_Format to internal representation
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
726 */
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
727 private function _to_driver_event($record, $noinst = false, $links = true, $master_event = null)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
728 {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
729 $record['calendar'] = $this->id;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
730
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
731 if ($links && !array_key_exists('links', $record)) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
732 $record['links'] = $this->get_links($record['uid']);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
733 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
734
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
735 if ($this->get_namespace() == 'other') {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
736 $record['className'] = 'fc-event-ns-other';
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
737 $record = kolab_driver::add_partstat_class($record, array('NEEDS-ACTION','DECLINED'), $this->get_owner());
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
738 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
739
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
740 // add instance identifier to first occurrence (master event)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
741 $recurrence_id_format = libcalendaring::recurrence_id_format($master_event ? $master_event : $record);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
742 if (!$noinst && $record['recurrence'] && !$record['recurrence_id'] && !$record['_instance']) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
743 $record['_instance'] = $record['start']->format($recurrence_id_format);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
744 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
745 else if (is_a($record['recurrence_date'], 'DateTime')) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
746 $record['_instance'] = $record['recurrence_date']->format($recurrence_id_format);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
747 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
748
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
749 // clean up exception data
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
750 if ($record['recurrence'] && is_array($record['recurrence']['EXCEPTIONS'])) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
751 array_walk($record['recurrence']['EXCEPTIONS'], function(&$exception) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
752 unset($exception['_mailbox'], $exception['_msguid'], $exception['_formatobj'], $exception['_attachments']);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
753 });
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
754 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
755
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
756 return $record;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
757 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
758
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
759 /**
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
760 * Convert the given event record into a data structure that can be passed to Kolab_Storage backend for saving
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
761 * (opposite of self::_to_driver_event())
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
762 */
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
763 private function _from_driver_event($event, $old = array())
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
764 {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
765 // set current user as ORGANIZER
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
766 if ($identity = $this->cal->rc->user->list_emails(true)) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
767 $event['attendees'] = (array) $event['attendees'];
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
768 $found = false;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
769
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
770 // there can be only resources on attendees list (T1484)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
771 // let's check the existence of an organizer
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
772 foreach ($event['attendees'] as $attendee) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
773 if ($attendee['role'] == 'ORGANIZER') {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
774 $found = true;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
775 break;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
776 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
777 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
778
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
779 if (!$found) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
780 $event['attendees'][] = array('role' => 'ORGANIZER', 'name' => $identity['name'], 'email' => $identity['email']);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
781 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
782
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
783 $event['_owner'] = $identity['email'];
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
784 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
785
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
786 // remove EXDATE values if RDATE is given
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
787 if (!empty($event['recurrence']['RDATE'])) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
788 $event['recurrence']['EXDATE'] = array();
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
789 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
790
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
791 // remove recurrence information (e.g. EXDATES and EXCEPTIONS) entirely
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
792 if ($event['recurrence'] && empty($event['recurrence']['FREQ']) && empty($event['recurrence']['RDATE'])) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
793 $event['recurrence'] = array();
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
794 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
795
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
796 // keep 'comment' from initial itip invitation
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
797 if (!empty($old['comment'])) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
798 $event['comment'] = $old['comment'];
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
799 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
800
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
801 // clean up exception data
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
802 if (is_array($event['exceptions'])) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
803 array_walk($event['exceptions'], function(&$exception) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
804 unset($exception['_mailbox'], $exception['_msguid'], $exception['_formatobj'], $exception['_attachments'],
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
805 $event['attachments'], $event['deleted_attachments'], $event['recurrence_id']);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
806 });
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
807 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
808
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
809 // remove some internal properties which should not be saved
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
810 unset($event['_savemode'], $event['_fromcalendar'], $event['_identity'], $event['_folder_id'],
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
811 $event['recurrence_id'], $event['attachments'], $event['deleted_attachments'], $event['className']);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
812
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
813 // copy meta data (starting with _) from old object
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
814 foreach ((array)$old as $key => $val) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
815 if (!isset($event[$key]) && $key[0] == '_')
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
816 $event[$key] = $val;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
817 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
818
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
819 return $event;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
820 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
821
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
822 /**
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
823 * Match the given word in the event contents
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
824 */
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
825 public function fulltext_match($event, $word, $recursive = true)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
826 {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
827 $hits = 0;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
828 foreach ($this->search_fields as $col) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
829 $sval = is_array($event[$col]) ? self::_complex2string($event[$col]) : $event[$col];
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
830 if (empty($sval))
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
831 continue;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
832
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
833 // do a simple substring matching (to be improved)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
834 $val = mb_strtolower($sval);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
835 if (strpos($val, $word) !== false) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
836 $hits++;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
837 break;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
838 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
839 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
840
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
841 return $hits;
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
842 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
843
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
844 /**
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
845 * Convert a complex event attribute to a string value
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
846 */
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
847 private static function _complex2string($prop)
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
848 {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
849 static $ignorekeys = array('role','status','rsvp');
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
850
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
851 $out = '';
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
852 if (is_array($prop)) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
853 foreach ($prop as $key => $val) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
854 if (is_numeric($key)) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
855 $out .= self::_complex2string($val);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
856 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
857 else if (!in_array($key, $ignorekeys)) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
858 $out .= $val . ' ';
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
859 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
860 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
861 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
862 else if (is_string($prop) || is_numeric($prop)) {
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
863 $out .= $prop . ' ';
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
864 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
865
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
866 return rtrim($out);
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
867 }
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
868
f6fe4b6ae66a calendar plugin nearly as distributed
Charlie Root
parents:
diff changeset
869 }