1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13: class Kronolith_Resource_Single extends Kronolith_Resource_Base
14: {
15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26: public function isFree($event)
27: {
28: if (is_array($event)) {
29: $start = $event['start'];
30: $end = $event['end'];
31: } else {
32: $start = $event->start;
33: $end = $event->end;
34: }
35:
36:
37: $busy = Kronolith::getDriver('Resource', $this->get('calendar'))
38: ->listEvents($start, $end, true);
39:
40:
41: if (!count($busy)) {
42: return true;
43: }
44:
45: 46:
47: if (!is_array($event)) {
48: $uid = $event->uid;
49: } else {
50: $uid = 0;
51: }
52: foreach ($busy as $events) {
53: foreach ($events as $e) {
54: if (!($e->status == Kronolith::STATUS_CANCELLED ||
55: $e->status == Kronolith::STATUS_FREE) &&
56: $e->uid !== $uid) {
57:
58:
59:
60: if (!($e->start->compareDateTime($end) >= 0) &&
61: !($e->end->compareDateTime($start) <= 0)) {
62:
63: return false;
64: }
65: }
66: }
67: }
68:
69: return true;
70: }
71:
72: 73: 74: 75: 76: 77: 78: 79:
80: public function addEvent($event)
81: {
82:
83: $driver = $this->getDriver();
84:
85:
86: $uid = $event->uid;
87: try {
88: $existing = $driver->getByUID($uid, array($this->get('calendar')));
89:
90: $this->_copyEvent($event, $existing);
91: $result = $existing->save();
92: } catch (Horde_Exception_NotFound $ex) {
93:
94: $e = $driver->getEvent();
95: $this->_copyEvent($event, $e);
96: $result = $e->save();
97: }
98: }
99:
100: 101: 102: 103: 104: 105: 106: 107:
108: public function removeEvent($event)
109: {
110: $driver = Kronolith::getDriver('Resource', $this->get('calendar'));
111: $re = $driver->getByUID($event->uid, array($this->get('calendar')));
112: $driver->deleteEvent($re->id);
113: }
114:
115: 116: 117: 118: 119:
120: public function getFreeBusy($startstamp = null, $endstamp = null, $asObject = false)
121: {
122: $vfb = Kronolith_Freebusy::generate($this->get('calendar'), $startstamp, $endstamp, $asObject);
123: $vfb->removeAttribute('ORGANIZER');
124: $vfb->setAttribute('ORGANIZER', $this->get('name'));
125:
126: return $vfb;
127: }
128:
129: public function setId($id)
130: {
131: if (empty($this->_id)) {
132: $this->_id = $id;
133: } else {
134: throw new Horde_Exception('Resource already exists. Cannot change the id.');
135: }
136: }
137:
138: public function getResponseType()
139: {
140: return $this->get('response_type');
141: }
142:
143: 144: 145: 146: 147: 148: 149: 150: 151:
152: private function _copyEvent($from, &$to)
153: {
154: $to->uid = $from->uid;
155: $to->title = $from->title;
156: $to->location = $from->location;
157: $to->status = $from->status;
158: $to->description = $from->description;
159: $to->url = $from->url;
160: $to->tags = $from->tags;
161: $to->geoLocation = $from->geoLocation;
162: $to->first = $from ->first;
163: $to->last = $from->last;
164: $to->start = $from->start;
165: $to->end = $from->end;
166: $to->durMin = $from->durMin;
167: $to->allday = $from->allday;
168: $to->recurrence = $from->recurrence;
169: $to->initialized = true;
170: }
171:
172: }