Overview

Packages

  • Kronolith
  • None

Classes

  • Kronolith
  • Kronolith_Ajax_Application
  • Kronolith_Ajax_Imple_ContactAutoCompleter
  • Kronolith_Ajax_Imple_Embed
  • Kronolith_Ajax_Imple_TagActions
  • Kronolith_Ajax_Imple_TagAutoCompleter
  • Kronolith_Api
  • Kronolith_Calendar
  • Kronolith_Calendar_External
  • Kronolith_Calendar_External_Tasks
  • Kronolith_Calendar_Holiday
  • Kronolith_Calendar_Internal
  • Kronolith_Calendar_Remote
  • Kronolith_Calendar_Resource
  • Kronolith_Calendars_Base
  • Kronolith_Calendars_Default
  • Kronolith_Calendars_Kolab
  • Kronolith_Day
  • Kronolith_Driver
  • Kronolith_Driver_Holidays
  • Kronolith_Driver_Horde
  • Kronolith_Driver_Ical
  • Kronolith_Driver_Kolab
  • Kronolith_Driver_Mock
  • Kronolith_Driver_Resource
  • Kronolith_Driver_Sql
  • Kronolith_Event
  • Kronolith_Event_Holidays
  • Kronolith_Event_Horde
  • Kronolith_Event_Ical
  • Kronolith_Event_Kolab
  • Kronolith_Event_Resource
  • Kronolith_Event_Sql
  • Kronolith_Exception
  • Kronolith_Factory_Calendars
  • Kronolith_Factory_Geo
  • Kronolith_Form_CreateCalendar
  • Kronolith_Form_CreateResource
  • Kronolith_Form_CreateResourceGroup
  • Kronolith_Form_DeleteCalendar
  • Kronolith_Form_DeleteResource
  • Kronolith_Form_DeleteResourceGroup
  • Kronolith_Form_EditCalendar
  • Kronolith_Form_EditRemoteCalendar
  • Kronolith_Form_EditResource
  • Kronolith_Form_EditResourceGroup
  • Kronolith_Form_SubscribeRemoteCalendar
  • Kronolith_Form_UnsubscribeRemoteCalendar
  • Kronolith_FreeBusy
  • Kronolith_FreeBusy_View
  • Kronolith_FreeBusy_View_Day
  • Kronolith_FreeBusy_View_Month
  • Kronolith_FreeBusy_View_Week
  • Kronolith_FreeBusy_View_Workweek
  • Kronolith_Geo_Base
  • Kronolith_Geo_Mysql
  • Kronolith_Geo_Sql
  • Kronolith_LoginTasks_SystemTask_Upgrade
  • Kronolith_LoginTasks_Task_PurgeEvents
  • Kronolith_Notification_Listener_AjaxStatus
  • Kronolith_Resource
  • Kronolith_Resource_Base
  • Kronolith_Resource_Group
  • Kronolith_Resource_Single
  • Kronolith_Storage
  • Kronolith_Storage_Kolab
  • Kronolith_Storage_Sql
  • Kronolith_Tagger
  • Kronolith_Test
  • Kronolith_View_Day
  • Kronolith_View_DeleteEvent
  • Kronolith_View_EditEvent
  • Kronolith_View_Event
  • Kronolith_View_ExportEvent
  • Kronolith_View_Month
  • Kronolith_View_Week
  • Kronolith_View_WorkWeek
  • Kronolith_View_Year
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * The Kronolith_Driver_Resource class implements the Kronolith_Driver API for
  4:  * storing resource calendars in a SQL backend.
  5:  *
  6:  * Copyright 1999-2012 Horde LLC (http://www.horde.org/)
  7:  *
  8:  * See the enclosed file COPYING for license information (GPL). If you
  9:  * did not receive this file, see http://www.horde.org/licenses/gpl.
 10:  *
 11:  * @author  Luc Saillard <luc.saillard@fr.alcove.com>
 12:  * @author  Chuck Hagenbuch <chuck@horde.org>
 13:  * @author  Jan Schneider <jan@horde.org>
 14:  * @author  Michael J Rubinsky <mrubinsk@horde.org>
 15:  * @package Kronolith
 16:  */
 17: class Kronolith_Driver_Resource extends Kronolith_Driver_Sql
 18: {
 19:     /**
 20:      * The class name of the event object to instantiate.
 21:      *
 22:      * @var string
 23:      */
 24:     protected $_eventClass = 'Kronolith_Event_Resource';
 25: 
 26:     /**
 27:      * Delete an event.
 28:      *
 29:      * Since this is the Kronolith_Resource's version of the event, if we
 30:      * delete it, we must also make sure to remove it from the event that
 31:      * it is attached to. Not sure if there is a better way to do this...
 32:      *
 33:      * @see lib/Driver/Kronolith_Driver_Sql#deleteEvent($eventId, $silent)
 34:      * @throws Kronolith_Exception
 35:      * @throws Horde_Exception_NotFound
 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:                 // found the right entry
 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:      * Save or update a Kronolith_Resource
 59:      *
 60:      * @param Kronolith_Resource $resource
 61:      *
 62:      * @return Kronolith_Resource object
 63:      * @throws Kronolith_Exception
 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:      * Removes a resource from storage, along with any events in the resource's
113:      * calendar.
114:      *
115:      * @param Kronolith_Resource $resource  The kronolith resource to remove
116:      *
117:      * @throws Kronolith_Exception
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:      * Obtain a Kronolith_Resource by the resource's id
137:      *
138:      * @param int $id  The key for the Kronolith_Resource
139:      *
140:      * @return Kronolith_Resource_Base
141:      * @throws Kronolith_Exception
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:      * Obtain the resource id associated with the given calendar uid.
169:      *
170:      * @param string $calendar  The calendar's uid.
171:      *
172:      * @return integer  The Kronolith_Resource id.
173:      * @throws Kronolith_Exception
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:      * Determine if the provided calendar id represents a resource's calendar.
192:      *
193:      * @param string $calendar  The calendar identifier to check.
194:      *
195:      * @return boolean
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:      * Return a list of Kronolith_Resources
209:      *
210:      * Right now, all users have Horde_Perms::READ, but only system admins have
211:      * Horde_Perms::EDIT | Horde_Perms::DELETE
212:      *
213:      * @param integer $perms   A Horde_Perms::* constant.
214:      * @param array $filter    A hash of field/values to filter on.
215:      * @param string $orderby  Field to order results by. Null for no ordering.
216:      *
217:      * @return an array of Kronolith_Resource objects.
218:      * @throws Kronolith_Exception
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:      * Obtain the group id for each group the speciied resource is a member of.
258:      *
259:      * @param integer $resource_id  The resource id to check for.
260:      *
261:      * @return array of group ids.
262:      * @throws Kronolith_Exception
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:      * Convert from driver keys and charset to Kronolith keys and charset.
280:      *
281:      * @param array $params  The key/values to convert.
282:      *
283:      * @return An array of converted values.
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:      * Helper function to update an existing event's tags to tagger storage.
303:      *
304:      * @param Kronolith_Event $event  The event to update
305:      */
306:     protected function _updateTags($event)
307:     {
308:         // noop
309:     }
310: 
311:     /**
312:      * Helper function to add tags from a newly creted event to the tagger.
313:      *
314:      * @param Kronolith_Event $event  The event to save tags to storage for.
315:      */
316:     protected function _addTags($event)
317:     {
318:         // noop
319:     }
320: 
321:     protected function _handleNotifications($event, $action)
322:     {
323:         // noop
324:     }
325: 
326: }
327: 
API documentation generated by ApiGen