1: <?php
2: /**
3: * The Kronolith_Driver_Horde class implements the Kronolith_Driver API for
4: * time objects retrieved from other Horde applications.
5: *
6: * Possible driver parameters:
7: *
8: * Copyright 2009-2012 Horde LLC (http://www.horde.org/)
9: *
10: * See the enclosed file COPYING for license information (GPL). If you
11: * did not receive this file, see http://www.horde.org/licenses/gpl.
12: *
13: * @author Jan Schneider <jan@horde.org>
14: * @package Kronolith
15: */
16: class Kronolith_Driver_Horde extends Kronolith_Driver
17: {
18: /**
19: * The API (application) of the current calendar.
20: *
21: * @var string
22: */
23: public $api;
24:
25: public function open($calendar)
26: {
27: parent::open($calendar);
28: list($this->api,) = explode('/', $this->calendar, 2);
29: }
30:
31: public function listAlarms($date, $fullevent = false)
32: {
33: return array();
34: }
35:
36: /**
37: * Lists all events in the time range, optionally restricting results to
38: * only events with alarms.
39: *
40: * @param Horde_Date $startDate Start of range date object.
41: * @param Horde_Date $endDate End of range data object.
42: * @param boolean $showRecurrence Return every instance of a recurring
43: * event? If false, will only return
44: * recurring events once inside the
45: * $startDate - $endDate range.
46: * @param boolean $hasAlarm Only return events with alarms?
47: * @param boolean $json Store the results of the events'
48: * toJson() method?
49: * @param boolean $coverDates Whether to add the events to all days
50: * that they cover.
51: *
52: * @return array Events in the given time range.
53: * @throws Kronolith_Exception
54: */
55: public function listEvents($startDate = null, $endDate = null,
56: $showRecurrence = false, $hasAlarm = false,
57: $json = false, $coverDates = true)
58: {
59: list($this->api, $category) = explode('/', $this->calendar, 2);
60: if (!$this->_params['registry']->hasMethod($this->api . '/listTimeObjects')) {
61: return array();
62: }
63:
64: if (is_null($startDate)) {
65: $startDate = new Horde_Date(array('mday' => 1,
66: 'month' => 1,
67: 'year' => 0000));
68: }
69: if (is_null($endDate)) {
70: $endDate = new Horde_Date(array('mday' => 31,
71: 'month' => 12,
72: 'year' => 9999));
73: }
74:
75: $startDate = clone $startDate;
76: $startDate->hour = $startDate->min = $startDate->sec = 0;
77: $endDate = clone $endDate;
78: $endDate->hour = 23;
79: $endDate->min = $endDate->sec = 59;
80:
81: try {
82: $eventsList = $this->_params['registry']->call($this->api . '/listTimeObjects', array(array($category), $startDate, $endDate));
83: } catch (Horde_Exception $e) {
84: throw new Kronolith_Exception($e);
85: }
86:
87: $results = array();
88: foreach ($eventsList as $eventsListItem) {
89: $event = new Kronolith_Event_Horde($this, $eventsListItem);
90:
91: /* Ignore events out of the period. */
92: if (
93: /* Starts after the period. */
94: $event->start->compareDateTime($endDate) > 0 ||
95: /* End before the period and doesn't recur. */
96: (!$event->recurs() &&
97: $event->end->compareDateTime($startDate) < 0) ||
98: /* Recurs and ... */
99: ($event->recurs() &&
100: /* ... has a recurrence end before the period. */
101: ($event->recurrence->hasRecurEnd() &&
102: $event->recurrence->recurEnd->compareDateTime($startDate) < 0))) {
103: continue;
104: }
105:
106: Kronolith::addEvents($results, $event, $startDate,
107: $endDate, $showRecurrence, $json, $coverDates);
108: }
109:
110: return $results;
111: }
112:
113: /**
114: * Updates an existing event in the backend.
115: *
116: * @param Kronolith_Event_Horde $event The event to save.
117: *
118: * @return string The event id.
119: * @throws Kronolith_Exception
120: */
121: protected function _updateEvent(Kronolith_Event $event)
122: {
123: if (!isset($this->api)) {
124: list($this->api, $category) = explode('/', $this->calendar, 2);
125: }
126: try {
127: $this->_params['registry']->call($this->api . '/saveTimeObject', array($event->toTimeobject()));
128: } catch (Horde_Exception $e) {
129: throw new Kronolith_Exception($e);
130: }
131:
132: return $event->timeobject['id'];
133: }
134:
135: /**
136: * @todo: implement getTimeObject in timeobjects API.
137: * @throws Kronolith_Exception
138: * @throws Horde_Exception_NotFound
139: */
140: public function getEvent($eventId = null, $start = null)
141: {
142: $end = null;
143: if ($start) {
144: $start = new Horde_Date($start);
145: $end = clone $start;
146: $end->mday++;
147: }
148:
149: $events = $this->listEvents($start, $end, (bool)$start);
150: foreach ($events as $day) {
151: if (isset($day[$eventId])) {
152: return $day[$eventId];
153: }
154: }
155:
156: throw new Horde_Exception_NotFound(_("Event not found"));
157: }
158:
159: }
160: