1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16:
17: class Kronolith_Driver_Resource extends Kronolith_Driver_Sql
18: {
19: 20: 21: 22: 23:
24: protected $_eventClass = 'Kronolith_Event_Resource';
25:
26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36:
37: public function deleteEvent($event, $silent = false)
38: {
39: $delete_event = $this->getEvent($event);
40:
41: $uid = $delete_event->uid;
42: $driver = Kronolith::getDriver();
43: $events = $driver->getByUID($uid, null, true);
44: foreach ($events as $e) {
45: $resources = $e->getResources();
46: if (count($resources)) {
47:
48: $r = $this->getResource($this->getResourceIdByCalendar($delete_event->calendar));
49: $e->removeResource($r);
50: $e->save();
51: }
52: }
53: $this->open($delete_event->calendar);
54: parent::deleteEvent($event, $silent);
55: }
56:
57: 58: 59: 60: 61: 62: 63: 64:
65: public function save($resource)
66: {
67: if ($resource->getId()) {
68: $query = 'UPDATE kronolith_resources SET resource_name = ?, '
69: . 'resource_calendar = ? , resource_description = ?, '
70: . 'resource_response_type = ?, resource_type = ?, '
71: . 'resource_members = ?, resource_email = ? WHERE resource_id = ?';
72:
73: $values = array($this->convertToDriver($resource->get('name')),
74: $resource->get('calendar'),
75: $this->convertToDriver($resource->get('description')),
76: $resource->get('response_type'),
77: $resource->get('type'),
78: serialize($resource->get('members')),
79: $resource->get('email'),
80: $resource->getId());
81:
82: try {
83: $this->_db->update($query, $values);
84: } catch (Horde_Db_Exception $e) {
85: throw new Kronolith_Exception($e);
86: }
87: } else {
88: $query = 'INSERT INTO kronolith_resources '
89: . '(resource_name, resource_calendar, '
90: . 'resource_description, resource_response_type, '
91: . ' resource_type, resource_members, resource_email)'
92: . ' VALUES (?, ?, ?, ?, ?, ?, ?)';
93: $values = array($this->convertToDriver($resource->get('name')),
94: $resource->get('calendar'),
95: $this->convertToDriver($resource->get('description')),
96: $resource->get('response_type'),
97: $resource->get('type'),
98: serialize($resource->get('members')),
99: $resource->get('email'));
100: try {
101: $id = $this->_db->insert($query, $values);
102: } catch (Horde_Db_Exception $e) {
103: throw new Kronolith_Exception($e);
104: }
105: $resource->setId($id);
106: }
107:
108: return $resource;
109: }
110:
111: 112: 113: 114: 115: 116: 117: 118:
119: public function delete($resource)
120: {
121: if (!($resource instanceof Kronolith_Resource_Base) || !$resource->getId()) {
122: throw new Kronolith_Exception(_("Resource not valid."));
123: }
124:
125: $query = 'DELETE FROM ' . $this->_params['table'] . ' WHERE calendar_id = ?';
126: try {
127: $this->_db->delete($query, array($resource->get('calendar')));
128: $query = 'DELETE FROM kronolith_resources WHERE resource_id = ?';
129: $this->_db->delete($query, array($resource->getId()));
130: } catch (Horde_Db_Exception $e) {
131: throw new Kronolith_Exception($e);
132: }
133: }
134:
135: 136: 137: 138: 139: 140: 141: 142:
143: public function getResource($id)
144: {
145: $query = 'SELECT resource_id, resource_name, resource_calendar, '
146: . 'resource_description, resource_response_type, resource_type, '
147: . 'resource_members, resource_email FROM kronolith_resources '
148: . 'WHERE resource_id = ?';
149:
150: try {
151: $results = $this->_db->selectOne($query, array($id));
152: } catch (Horde_Db_Exception $e) {
153: throw new Kronolith_Exception($e);
154: }
155: if (!count($results)) {
156: throw new Kronolith_Exception('Resource not found');
157: }
158:
159: $class = 'Kronolith_Resource_' . $results['resource_type'];
160: if (!class_exists($class)) {
161: throw new Kronolith_Exception('Could not load the class definition for ' . $class);
162: }
163:
164: return new $class($this->_fromDriver($results));
165: }
166:
167: 168: 169: 170: 171: 172: 173: 174:
175: public function getResourceIdByCalendar($calendar)
176: {
177: $query = 'SELECT resource_id FROM kronolith_resources WHERE resource_calendar = ?';
178: try {
179: $result = $this->_db->selectValue($query, array($calendar));
180: } catch (Horde_Db_Exception $e) {
181: throw new Kronolith_Exception($e);
182: }
183: if (empty($result)) {
184: throw new Kronolith_Exception('Resource not found');
185: }
186:
187: return $result;
188: }
189:
190: 191: 192: 193: 194: 195: 196:
197: public function isResourceCalendar($calendar)
198: {
199: $query = 'SELECT count(*) FROM kronolith_resources WHERE resource_calendar = ?';
200: try {
201: return $this->_db->selectValue($query, array($calendar)) > 0;
202: } catch (Horde_Db_Exception $e) {
203: throw new Kronolith_Exception($e);
204: }
205: }
206:
207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219:
220: public function listResources($perms = Horde_Perms::READ, $filter = array(), $orderby = null)
221: {
222: if (($perms & (Horde_Perms::EDIT | Horde_Perms::DELETE)) &&
223: !$GLOBALS['registry']->isAdmin()) {
224: return array();
225: }
226:
227: $query = 'SELECT resource_id, resource_name, resource_calendar, resource_description, resource_response_type, resource_type, resource_members, resource_email FROM kronolith_resources';
228: if (count($filter)) {
229: $clause = ' WHERE ';
230: $i = 0;
231: $c = count($filter);
232: foreach ($filter as $field => $value) {
233: $clause .= 'resource_' . $field . ' = ?' . (($i++ < ($c - 1)) ? ' AND ' : '');
234: }
235: $query .= $clause;
236: }
237:
238: if (!empty($orderby)) {
239: $query .= ' ORDER BY resource_' . $orderby;
240: }
241:
242: try {
243: $results = $this->_db->selectAll($query, $filter);
244: } catch (Horde_Db_Exception $e) {
245: throw new Kronolith_Exception($e);
246: }
247: $return = array();
248: foreach ($results as $row) {
249: $class = 'Kronolith_Resource_' . $row['resource_type'];
250: $return[$row['resource_id']] = new $class($this->_fromDriver(array_merge(array('resource_id' => $row['resource_id']), $row)));
251: }
252:
253: return $return;
254: }
255:
256: 257: 258: 259: 260: 261: 262: 263:
264: public function getGroupMemberships($resource_id)
265: {
266: $groups = $this->listResources(Horde_Perms::READ, array('type' => Kronolith_Resource::TYPE_GROUP));
267: $in = array();
268: foreach ($groups as $group) {
269: $members = $group->get('members');
270: if (array_search($resource_id, $members) !== false) {
271: $in[] = $group->getId();
272: }
273: }
274:
275: return $in;
276: }
277:
278: 279: 280: 281: 282: 283: 284:
285: protected function _fromDriver($params)
286: {
287: $return = array();
288: foreach ($params as $field => $value) {
289: if ($field == 'resource_name' || $field == 'resource_description') {
290: $value = $this->convertFromDriver($value);
291: } elseif ($field == 'resource_members') {
292: $value = @unserialize($value);
293: }
294:
295: $return[str_replace('resource_', '', $field)] = $value;
296: }
297:
298: return $return;
299: }
300:
301: 302: 303: 304: 305:
306: protected function _updateTags($event)
307: {
308:
309: }
310:
311: 312: 313: 314: 315:
316: protected function _addTags($event)
317: {
318:
319: }
320:
321: protected function _handleNotifications($event, $action)
322: {
323:
324: }
325:
326: }
327: