Refactor a bit.
authorMichael J. Rubinsky <mrubinsk@horde.org>
Fri, 18 Sep 2009 04:35:11 +0000 (00:35 -0400)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Tue, 29 Sep 2009 20:53:56 +0000 (16:53 -0400)
Move Kronolith_Resource::checkResources to Kronolith_Resource_*::getResponse()
and remove the responsibilty of adding the event.

kronolith/attendees.php
kronolith/lib/Event.php
kronolith/lib/Resource.php
kronolith/lib/Resource/Single.php

index 0e2fd1e..b03de1a 100644 (file)
@@ -101,26 +101,16 @@ case 'add':
 
     // Any new resources?
     if (!empty($newResource)) {
+
+        /* Get the requested resource */
         $resource = Kronolith::getDriver('Resource')->getResource($newResource);
 
-        /* If we know we always accept/deny mark it that way now. */
-        $type = $resource->getResponseType();
-        if ($type == Kronolith_Resource::RESPONSETYPE_ALWAYS_ACCEPT) {
-            $response = Kronolith::RESPONSE_ACCEPTED;
-        } elseif ($type == Kronolith_Resource::RESPONSETYPE_ALWAYS_DECLINE) {
-            $response = Kronolith::RESPONSE_DECLINED;
-        } elseif ($type == Kronolith_Resource::RESPONSETYPE_AUTO) {
-            // Try to figure out the expected response
-            $date = new Horde_Date(Horde_Util::getFormData('date'));
-            $end = new Horde_Date(Horde_Util::getFormData('enddate'));
-            if ($resource->isFree(array('start' => $date, 'end' => $end))) {
-                $response = Kronolith::RESPONSE_ACCEPTED;
-            } else {
-                $response = Kronolith::RESPONSE_DECLINED;
-            }
-        } else {
-            $response = Kronolith::RESPONSE_NONE;
-        }
+        /* Do our best to see what the response will be. Note that this response
+         * is only guarenteed once the event is saved.
+         */
+        $date = new Horde_Date(Horde_Util::getFormData('date'));
+        $end = new Horde_Date(Horde_Util::getFormData('enddate'));
+        $response = $resource->getResponse(array('start' => $date, 'end' => $end));
 
         $resources[$newResource] = array(
             'attendance' => Kronolith::PART_REQUIRED,
index 8a683e2..70d3fab 100644 (file)
@@ -316,8 +316,21 @@ abstract class Kronolith_Event
             return PEAR::raiseError('Event not yet initialized');
         }
 
-        // TODO: Should this go here?
-        Kronolith_Resource::checkResources($this);
+        /* Check for acceptance/denial of this event's resources.
+         *
+         * @TODO: Need to look at how to lock the resource to avoid two events
+         * inadvertantly getting accepted. (Two simultaneous requests, both
+         * return RESPONSE_ACCEPTED from getResponse()) Maybe Horde_Lock?
+         */
+        foreach ($this->getResources() as $id => $resourceData) {
+            $resource = Kronolith::getDriver('Resource')->getResource($id);
+            $response = $resource->getResponse($this);
+            if ($response == Kronolith::RESPONSE_ACCEPTED) {
+                $resource->addEvent($this);
+            }
+            $this->addResource($resource, $response);
+        }
+
         $this->toDriver();
         $result = $this->getDriver()->saveEvent($this);
         if (!is_a($result, 'PEAR_Error') &&
index 586d828..e2971f3 100644 (file)
@@ -72,54 +72,4 @@ class Kronolith_Resource
         return false;
     }
 
-    /**
-     * Function to check availability and set response status for each resource
-     * attached to the event.
-     *
-     * @param Kronolith_Event $event  The event object to check the resources of
-     *
-     * @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
index ac85625..4784c30 100644 (file)
@@ -117,6 +117,32 @@ class Kronolith_Resource_Single extends Kronolith_Resource_Base
     }
 
     /**
+     * Check avilability and return an appropriate Kronolith response code.
+     *
+     * @param Kronolith_Event $event  The event to check on
+     *
+     * @return integer Kronolith::RESPONSE* constant
+     */
+    function getResponse($event)
+    {
+        switch($this->getResponseType()) {
+        case Kronolith_Resource::RESPONSETYPE_ALWAYS_ACCEPT:
+            return Kronolith::RESPONSE_ACCEPTED;
+        case Kronolith_Resource::RESPONSETYPE_AUTO:
+            if ($this->isFree($event)) {
+                return Kronolith::RESPONSE_ACCEPTED;
+            } else {
+                return Kronolith::RESPONSE_DECLINED;
+            }
+        case Kronolith_Resource::RESPONSETYPE_ALWAYS_DECLINE:
+            return Kronolith::RESPONSE_DECLINED;
+        case Kronolith_Resource::RESPONSETYPE_NONE:
+        case Kronolith_Resource::RESPONSETYPE_MANUAL:
+            return Kronolith::RESPONSE_NONE;
+        }
+    }
+
+    /**
      * Obtain the freebusy information for this resource.
      *
      * @return unknown_type