From b1a2a58ace0f4c602592cc6f7f64e98c54ab901b Mon Sep 17 00:00:00 2001 From: "Michael J. Rubinsky" Date: Sat, 12 Sep 2009 11:38:48 -0400 Subject: [PATCH] Move static resource related methods to Kronolith_Resource:: --- kronolith/attendees.php | 2 +- kronolith/delete.php | 2 +- kronolith/lib/Event.php | 2 +- kronolith/lib/Forms/CreateResource.php | 2 +- kronolith/lib/FreeBusy.php | 2 +- kronolith/lib/Kronolith.php | 90 ++--------------------------- kronolith/lib/Resource.php | 101 ++++++++++++++++++++++++++++++++- kronolith/resources/index.php | 4 +- 8 files changed, 111 insertions(+), 94 deletions(-) diff --git a/kronolith/attendees.php b/kronolith/attendees.php index 5f01e532b..42e351925 100644 --- a/kronolith/attendees.php +++ b/kronolith/attendees.php @@ -21,7 +21,7 @@ $resources = (isset($_SESSION['kronolith']['resources']) && is_array($_SESSION['kronolith']['resources'])) ? $_SESSION['kronolith']['resources'] : array(); -$allResources = Kronolith::listResources(); +$allResources = Kronolith_Resource::listResources(); // Get the action ID and value. This specifies what action the user initiated. $actionID = Horde_Util::getFormData('actionID'); diff --git a/kronolith/delete.php b/kronolith/delete.php index 37ffe6bd2..2f9407283 100644 --- a/kronolith/delete.php +++ b/kronolith/delete.php @@ -11,7 +11,7 @@ require_once dirname(__FILE__) . '/lib/base.php'; -if (Kronolith::isResourceCalendar($c = Horde_Util::getFormData('calendar'))) { +if (Kronolith_Resource::isResourceCalendar($c = Horde_Util::getFormData('calendar'))) { $driver = 'Resource'; } else { $driver = null; diff --git a/kronolith/lib/Event.php b/kronolith/lib/Event.php index 1d003c31f..8a683e24a 100644 --- a/kronolith/lib/Event.php +++ b/kronolith/lib/Event.php @@ -317,7 +317,7 @@ abstract class Kronolith_Event } // TODO: Should this go here? - Kronolith::checkResources($this); + Kronolith_Resource::checkResources($this); $this->toDriver(); $result = $this->getDriver()->saveEvent($this); if (!is_a($result, 'PEAR_Error') && diff --git a/kronolith/lib/Forms/CreateResource.php b/kronolith/lib/Forms/CreateResource.php index d0c89fa07..af13c386c 100644 --- a/kronolith/lib/Forms/CreateResource.php +++ b/kronolith/lib/Forms/CreateResource.php @@ -50,7 +50,7 @@ class Kronolith_CreateResourceForm extends Horde_Form { 'max_reservations' => $this->_vars->get('max_reservations')); $resource = new Kronolith_Resource_Single($new); - return $results = Kronolith::addResource($resource); + return $results = Kronolith_Resource::addResource($resource); } } diff --git a/kronolith/lib/FreeBusy.php b/kronolith/lib/FreeBusy.php index a931e4c54..a5d1a676a 100644 --- a/kronolith/lib/FreeBusy.php +++ b/kronolith/lib/FreeBusy.php @@ -35,7 +35,7 @@ class Kronolith_FreeBusy { if (is_a($share, 'PEAR_Error')) { // Might be a Kronolith_Resource try { - $resource = Kronolith::isResourceCalendar($calendar[0]); + $resource = Kronolith_Resource::isResourceCalendar($calendar[0]); $owner = $calendar[0]; } catch (Horde_Exception $e) { return $returnObj ? $share : ''; diff --git a/kronolith/lib/Kronolith.php b/kronolith/lib/Kronolith.php index 16978448b..3d87b89ea 100644 --- a/kronolith/lib/Kronolith.php +++ b/kronolith/lib/Kronolith.php @@ -2034,95 +2034,13 @@ class Kronolith } /** - * Adds a new resource to storage + * Obtain an internal calendar. Use this where we don't know if we will + * have a Horde_Share or a Kronolith_Resource based calendar. * - * @param Kronolith_Resource $resource + * @param string $target The calendar id to retrieve. * - * @return unknown_type + * @return mixed Kronolith_Resource or Horde_Share_Object */ - static public function addResource($resource) - { - // Create a new calendar id. - $calendar = 'resource_' . hash('md5', microtime()); - $resource->set('calendar', $calendar); - $driver = Kronolith::getDriver('Resource'); - - return $driver->save($resource); - } - - /** - * Return a list of resources that the current user has access to at the - * specified permission level. Right now, all users have PERMS_READ, but - * only system admins have PERMS_EDIT | PERMS_DELETE - * - * @return array of Kronolith_Resource objects - */ - static public function listResources($perms = PERMS_READ, $params = array()) - { - if (($perms & (PERMS_EDIT | PERMS_DELETE)) && !Horde_Auth::isAdmin()) { - return array(); - } - - // Query kronolith_resource table for all(?) available resources? - // maybe by 'type' or 'name'? type would be arbitrary? - $driver = Kronolith::getDriver('Resource'); - return $driver->listResources($params); - } - - static public function isResourceCalendar($calendar) - { - if (strncmp($calendar, 'resource_', 9) === 0) { - return true; - } - - return false; - } - - /** - * Function to check availability and auto accept/decline for each resource - * attached to the event. - * - * @return unknown_type - */ - static public function checkResources($event) - { - foreach ($event->getResources() as $id => $resource) { - - /* Get the resource */ - $r = Kronolith::getDriver('Resource')->getResource($id); - - /* Determine if we have to calculate, or just auto-reply */ - $type = $r->getResponseType(); - switch($type) { - case Kronolith_Resource::RESPONSETYPE_ALWAYS_ACCEPT: - $r->addEvent($event); - $event->addResource($r, Kronolith::RESPONSE_ACCEPTED); - break; - case Kronolith_Resource::RESPONSETYPE_AUTO: - if ($r->isFree($event)) { - $r->addEvent($event); - $event->addResource($r, Kronolith::RESPONSE_ACCEPTED); - } else { - $event->addResource($r, Kronolith::RESPONSE_DECLINED); - } - break; - - case Kronolith_Resource::RESPONSETYPE_ALWAYS_DECLINE: - $event->addResource($r, Kronolith::RESPONSE_DECLINED); - break; - - case Kronolith_Resource::RESPONSETYPE_NONE: - $event->addResource($r, Kronolith::RESPONSE_NONE); - break; - - case Kronolith_Resource::RESPONSETYPE_MANUAL: - //??? - break; - } - - } - } - static public function getInternalCalendar($target) { if (self::isResourceCalendar($target)) { diff --git a/kronolith/lib/Resource.php b/kronolith/lib/Resource.php index 356964e89..72b6c3988 100644 --- a/kronolith/lib/Resource.php +++ b/kronolith/lib/Resource.php @@ -14,9 +14,108 @@ class Kronolith_Resource /** * */ - public function factory($driver, $params) + static public function factory($driver, $params) { } + /** + * Adds a new resource to storage + * + * @param Kronolith_Resource $resource + * + * @return unknown_type + */ + static public function addResource($resource) + { + // Create a new calendar id. + $calendar = 'resource_' . hash('md5', microtime()); + $resource->set('calendar', $calendar); + $driver = Kronolith::getDriver('Resource'); + + return $driver->save($resource); + } + + /** + * Return a list of resources that the current user has access to at the + * specified permission level. Right now, all users have PERMS_READ, but + * only system admins have PERMS_EDIT | PERMS_DELETE + * + * @return array of Kronolith_Resource objects + */ + static public function listResources($perms = PERMS_READ, $params = array()) + { + if (($perms & (PERMS_EDIT | PERMS_DELETE)) && !Horde_Auth::isAdmin()) { + return array(); + } + + // Query kronolith_resource table for all(?) available resources? + // maybe by 'type' or 'name'? type would be arbitrary? + $driver = Kronolith::getDriver('Resource'); + return $driver->listResources($params); + } + + /** + * Determine if the provided calendar id represents a resource's calendar. + * + * @param $calendar + * @return unknown_type + */ + static public function isResourceCalendar($calendar) + { + if (strncmp($calendar, 'resource_', 9) === 0) { + return true; + } + + return false; + } + + /** + * Function to check availability and set response status for each resource + * attached to the event. + * + * @return void + */ + static public function checkResources($event) + { + foreach ($event->getResources() as $id => $resource) { + + /* Get the resource */ + $r = Kronolith::getDriver('Resource')->getResource($id); + + /* Determine if we have to calculate, or just auto-reply */ + $type = $r->getResponseType(); + switch($type) { + case Kronolith_Resource::RESPONSETYPE_ALWAYS_ACCEPT: + $r->addEvent($event); + $event->addResource($r, Kronolith::RESPONSE_ACCEPTED); + break; + case Kronolith_Resource::RESPONSETYPE_AUTO: + if ($r->isFree($event)) { + $r->addEvent($event); + $event->addResource($r, Kronolith::RESPONSE_ACCEPTED); + } else { + $event->addResource($r, Kronolith::RESPONSE_DECLINED); + } + break; + + case Kronolith_Resource::RESPONSETYPE_ALWAYS_DECLINE: + $event->addResource($r, Kronolith::RESPONSE_DECLINED); + break; + + case Kronolith_Resource::RESPONSETYPE_NONE: + $event->addResource($r, Kronolith::RESPONSE_NONE); + break; + + case Kronolith_Resource::RESPONSETYPE_MANUAL: + // Would be nice to be able to utilize iTips, but + // no idea how that would work right now...resources are not + // user accounts etc...for now, just set as NONE + $event->addResource($r, Kronolith::RESONSE_NONE); + break; + } + + } + } + } \ No newline at end of file diff --git a/kronolith/resources/index.php b/kronolith/resources/index.php index cf2339842..7082606d6 100644 --- a/kronolith/resources/index.php +++ b/kronolith/resources/index.php @@ -17,7 +17,7 @@ if (!Horde_Auth::getAuth()) { } $edit_url_base = Horde::applicationUrl('resources/edit.php'); $edit_img = Horde::img('edit.png', _("Edit"), null, $registry->getImageDir('horde')); -$resources = Kronolith::listResources(); +$resources = Kronolith_Resource::listResources(); $display_url_base = Horde::applicationUrl('month.php', true, -1); $delete_url_base = Horde::applicationUrl('resources/delete.php'); $delete_img = Horde::img('delete.png', _("Delete"), null, $registry->getImageDir('horde')); @@ -89,7 +89,7 @@ function shorten_url($url, $separator = '...', $first_chunk_length = 35, $last_c // 'category' => 'conference rooms'); // //$resource = new Kronolith_Resource_Single($new); -//$results = Kronolith::addResource($resource); +//$results = Kronolith_Resource::addResource($resource); //var_dump($results); ?> \ No newline at end of file -- 2.11.0