summaryrefslogtreecommitdiff
path: root/plugins/calendar/drivers/kolab/kolab_invitation_calendar.php
blob: a2e9cf2ed267fdc1688ba96d902d12b389d4ac51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<?php

/**
 * Kolab calendar storage class simulating a virtual calendar listing pedning/declined invitations
 *
 * @author Thomas Bruederli <bruederli@kolabsys.com>
 *
 * Copyright (C) 2014, Kolab Systems AG <contact@kolabsys.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

class kolab_invitation_calendar
{
  public $id = '__invitation__';
  public $ready = true;
  public $alarms = false;
  public $readonly = true;
  public $attachments = false;
  public $subscriptions = false;
  public $partstats = array('unknown');
  public $categories = array();
  public $name = 'Invitations';

  /**
   * Default constructor
   */
  public function __construct($id, $calendar)
  {
    $this->cal = $calendar;
    $this->id = $id;

    switch ($this->id) {
      case kolab_driver::INVITATIONS_CALENDAR_PENDING:
        $this->partstats = array('NEEDS-ACTION');
        $this->name = $this->cal->gettext('invitationspending');
        if (!empty($_REQUEST['_quickview']))
          $this->partstats[] = 'TENTATIVE';
        break;

      case kolab_driver::INVITATIONS_CALENDAR_DECLINED:
        $this->partstats = array('DECLINED');
        $this->name = $this->cal->gettext('invitationsdeclined');
        break;
    }

    // user-specific alarms settings win
    $prefs = $this->cal->rc->config->get('kolab_calendars', array());
    if (isset($prefs[$this->id]['showalarms']))
      $this->alarms = $prefs[$this->id]['showalarms'];
  }


  /**
   * Getter for a nice and human readable name for this calendar
   *
   * @return string Name of this calendar
   */
  public function get_name()
  {
    return $this->name;
  }


  /**
   * Getter for the IMAP folder owner
   *
   * @return string Name of the folder owner
   */
  public function get_owner()
  {
    return $this->cal->rc->get_user_name();
  }


  /**
   *
   */
  public function get_title()
  {
    return $this->get_name();
  }


  /**
   * Getter for the name of the namespace to which the IMAP folder belongs
   *
   * @return string Name of the namespace (personal, other, shared)
   */
  public function get_namespace()
  {
    return 'x-special';
  }


  /**
   * Getter for the top-end calendar folder name (not the entire path)
   *
   * @return string Name of this calendar
   */
  public function get_foldername()
  {
    return $this->get_name();
  }

  /**
   * Getter for the Cyrus mailbox identifier corresponding to this folder
   *
   * @return string Mailbox ID
   */
  public function get_mailbox_id()
  {
    // this is a virtual collection and has no concrete mailbox ID
    return null;
  }

  /**
   * Return color to display this calendar
   */
  public function get_color()
  {
    // calendar color is stored in local user prefs
    $prefs = $this->cal->rc->config->get('kolab_calendars', array());

    if (!empty($prefs[$this->id]) && !empty($prefs[$this->id]['color']))
      return $prefs[$this->id]['color'];

    return 'ffffff';
  }

  /**
   * Compose an URL for CalDAV access to this calendar (if configured)
   */
  public function get_caldav_url()
  {
    return false;
  }

  /**
   * Check activation status of this folder
   *
   * @return boolean True if enabled, false if not
   */
  public function is_active()
  {
    $prefs = $this->cal->rc->config->get('kolab_calendars', array());  // read local prefs
    return (bool)$prefs[$this->id]['active'];
  }

  /**
   * Update properties of this calendar folder
   *
   * @see calendar_driver::edit_calendar()
   */
  public function update(&$prop)
  {
    // don't change anything.
    // let kolab_driver save props in local prefs
    return $prop['id'];
  }


  /**
   * Getter for a single event object
   */
  public function get_event($id)
  {
    // redirect call to kolab_driver::get_event()
    $event = $this->cal->driver->get_event($id, true);

    if (is_array($event)) {
      // add pointer to original calendar folder
      $event['_folder_id'] = $event['calendar'];
      $event = $this->_mod_event($event);
    }

    return $event;
  }


  /**
   * @param  integer Event's new start (unix timestamp)
   * @param  integer Event's new end (unix timestamp)
   * @param  string  Search query (optional)
   * @param  boolean Include virtual events (optional)
   * @param  array   Additional parameters to query storage
   * @return array A list of event records
   */
  public function list_events($start, $end, $search = null, $virtual = 1, $query = array())
  {
    // get email addresses of the current user
    $user_emails = $this->cal->get_user_emails();
    $subquery = array();
    foreach ($user_emails as $email) {
      foreach ($this->partstats as $partstat) {
        $subquery[] = array('tags', '=', 'x-partstat:' . $email . ':' . strtolower($partstat));
      }
    }

    // aggregate events from all calendar folders
    $events = array();
    foreach (kolab_storage::list_folders('', '*', 'event', null) as $foldername) {
      $cal = new kolab_calendar($foldername, $this->cal);
      if ($cal->get_namespace() == 'other')
        continue;

      foreach ($cal->list_events($start, $end, $search, 1, $query, array(array($subquery, 'OR'))) as $event) {
        $match = false;

        // post-filter events to match out partstats
        if (is_array($event['attendees'])) {
          foreach ($event['attendees'] as $attendee) {
            if (in_array($attendee['email'], $user_emails) && in_array($attendee['status'], $this->partstats)) {
              $match = true;
              break;
            }
          }
        }

        if ($match) {
          $events[$event['id']] = $this->_mod_event($event);
        }
      }

      // merge list of event categories (really?)
      $this->categories += $cal->categories;
    }

    return $events;
  }

  /**
   *
   * @param  integer Date range start (unix timestamp)
   * @param  integer Date range end (unix timestamp)
   * @return integer Count
   */
  public function count_events($start, $end = null)
  {
    // get email addresses of the current user
    $user_emails = $this->cal->get_user_emails();
    $subquery = array();
    foreach ($user_emails as $email) {
      foreach ($this->partstats as $partstat) {
        $subquery[] = array('tags', '=', 'x-partstat:' . $email . ':' . strtolower($partstat));
      }
    }

    // aggregate counts from all calendar folders
    $count = 0;
    foreach (kolab_storage::list_folders('', '*', 'event', null) as $foldername) {
      $cal = new kolab_calendar($foldername, $this->cal);
      if ($cal->get_namespace() == 'other')
        continue;

      $count += $cal->count_events($start, $end, array(array($subquery, 'OR')));
    }

    return $count;
  }

  /**
   * Helper method to modify some event properties
   */
  private function _mod_event($event)
  {
    // set classes according to PARTSTAT
    if (is_array($event['attendees'])) {
      $user_emails = $this->cal->get_user_emails();
      $partstat = 'UNKNOWN';
      foreach ($event['attendees'] as $attendee) {
        if (in_array($attendee['email'], $user_emails)) {
          $partstat = $attendee['status'];
          break;
        }
      }

      if (in_array($partstat, $this->partstats)) {
        $event['className'] = 'fc-invitation-' . strtolower($partstat);
        $event['calendar'] = $this->id;
      }
    }

    return $event;
  }


  /**
   * Create a new event record
   *
   * @see calendar_driver::new_event()
   * 
   * @return mixed The created record ID on success, False on error
   */
  public function insert_event($event)
  {
    return false;
  }

  /**
   * Update a specific event record
   *
   * @see calendar_driver::new_event()
   * @return boolean True on success, False on error
   */

  public function update_event($event, $exception_id = null)
  {
    // forward call to the actual storage folder
    if ($event['_folder_id']) {
      $cal = $this->cal->driver->get_calendar($event['_folder_id']);
      if ($cal && $cal->ready) {
        return $cal->update_event($event, $exception_id);
      }
    }

    return false;
  }

  /**
   * Delete an event record
   *
   * @see calendar_driver::remove_event()
   * @return boolean True on success, False on error
   */
  public function delete_event($event, $force = true)
  {
    return false;
  }

  /**
   * Restore deleted event record
   *
   * @see calendar_driver::undelete_event()
   * @return boolean True on success, False on error
   */
  public function restore_event($event)
  {
    return false;
  }


}