1<?php
2
3/**
4 * Resources directory interface definition
5 *
6 * @author Thomas Bruederli <bruederli@kolabsys.com>
7 *
8 * Copyright (C) 2014, Kolab Systems AG <contact@kolabsys.com>
9 *
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as
12 * published by the Free Software Foundation, either version 3 of the
13 * License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
19 *
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24/**
25 * Interface definition for a resources directory driver classe
26 */
27abstract class resources_driver
28{
29  protected $cal;
30
31  /**
32   * Default constructor
33   */
34  function __construct($cal)
35  {
36      $this->cal = $cal;
37  }
38
39  /**
40   * Fetch resource objects to be displayed for booking
41   *
42   * @param  string  Search query (optional)
43   * @return array  List of resource records available for booking
44   */
45  abstract public function load_resources($query = null);
46
47  /**
48   * Return properties of a single resource
49   *
50   * @param string  Unique resource identifier
51   * @return array  Resource object as hash array
52   */
53  abstract public function get_resource($id);
54
55  /**
56   * Return properties of a resource owner
57   *
58   * @param string  Owner identifier
59   * @return array  Resource object as hash array
60   */
61  public function get_resource_owner($id)
62  {
63      return null;
64  }
65
66  /**
67   * Get event data to display a resource's calendar
68   *
69   * The default implementation extracts the resource's email address
70   * and fetches free-busy data using the calendar backend driver.
71   *
72   * @param  integer Event's new start (unix timestamp)
73   * @param  integer Event's new end (unix timestamp)
74   * @return array A list of event objects (see calendar_driver specification)
75   */
76  public function get_resource_calendar($id, $start, $end)
77  {
78      $events = array();
79      $rec = $this->get_resource($id);
80      if ($rec && !empty($rec['email']) && $this->cal->driver) {
81          $fbtypemap = array(
82              calendar::FREEBUSY_BUSY => 'busy',
83              calendar::FREEBUSY_TENTATIVE => 'tentative',
84              calendar::FREEBUSY_OOF => 'outofoffice',
85          );
86
87          // if the backend has free-busy information
88          $fblist = $this->cal->driver->get_freebusy_list($rec['email'], $start, $end);
89          if (is_array($fblist)) {
90              foreach ($fblist as $slot) {
91                  list($from, $to, $type) = $slot;
92                  if ($type == calendar::FREEBUSY_FREE || $type == calendar::FREEBUSY_UNKNOWN) {
93                      continue;
94                  }
95                  if ($from < $end && $to > $start) {
96                      $event = array(
97                          'id'     => sha1($id . $from . $to),
98                          'title'  => $rec['name'],
99                          'start'  => new DateTime('@' . $from),
100                          'end'    => new DateTime('@' . $to),
101                          'status' => $fbtypemap[$type],
102                          'calendar' => '_resource',
103                      );
104                      $events[] = $event;
105                  }
106              }
107          }
108      }
109
110      return $events;
111  }
112}
113