Need to move the acceptance/denial of requests to the resource object
authorMichael J. Rubinsky <mrubinsk@horde.org>
Tue, 18 Aug 2009 20:05:19 +0000 (16:05 -0400)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Tue, 29 Sep 2009 20:53:48 +0000 (16:53 -0400)
itself...otherwise, the internal properties array won't be serialized
before saving.

kronolith/lib/Driver/Sql.php
kronolith/lib/Event.php
kronolith/lib/Event/Sql.php
kronolith/lib/Resource.php
kronolith/lib/Resource/Base.php
kronolith/lib/Resource/Single.php
kronolith/resources.php

index 25a3999..65bea9a 100644 (file)
@@ -534,16 +534,8 @@ class Kronolith_Driver_Sql extends Kronolith_Driver
      */
     public function saveEvent($event)
     {
-        /* Deal with Kronolith_Resources before anything else is done */
-        foreach ($event->getResources() as $resource) {
-            if ($resource->isFree($this)) {
-                $resource->attachToEvent($this);
-                $event->addResource($resource, Kronolith::RESPONSE_ACCEPTED);
-            } else {
-                $event->addResource($resource, Kronolith::RESPONSE_DECLINED);
-            }
-        }
-
+        /* Check for acceptence/denial of resources */
+        $event->checkResources();
         if ($event->isStored() || $event->exists()) {
             $values = array();
 
@@ -808,7 +800,6 @@ class Kronolith_Driver_Sql extends Kronolith_Driver
             throw new Horde_Exception('Resource not found');
         }
         $return = array();
-        var_dump($results);
         foreach ($results as $field => $value) {
             $return[str_replace('resource_', '', $field)] = $this->convertFromDriver($value);
         }
index 98cc900..06d6c21 100644 (file)
@@ -1552,7 +1552,7 @@ abstract class Kronolith_Event
      */
     public function addResource($resource, $response)
     {
-        $this->_resources[$resource->uid] = array(
+        $this->_resources[$resource->id] = array(
             'attendance' => Kronolith::PART_REQUIRED,
             'response' => $response,
             'name' => $resource->name
index 11b10de..915241b 100644 (file)
@@ -207,4 +207,26 @@ class Kronolith_Event_Sql extends Kronolith_Event
         return $this->_properties;
     }
 
+    /**
+     * Function to check availability and auto accept/decline for each resource
+     * attached to this event. Needed here instead of in Kronolith_Driver::saveEvent
+     * since the _properties array is already built at that point.
+     *
+     * @return unknown_type
+     */
+    public function checkResources()
+    {
+        foreach ($this->_resources as $id => $resource) {
+            $r = Kronolith_Resource::getResource($id);
+            if ($r->isFree($this)) {
+                $r->addEvent($this);
+                $this->addResource($r, Kronolith::RESPONSE_ACCEPTED);
+            } else {
+                $this->addResource($r, Kronolith::RESPONSE_DECLINED);
+            }
+        }
+        $driver = $this->getDriver();
+        $this->_properties['event_resources'] = serialize($driver->convertToDriver($this->_resources));
+    }
+
 }
index 093df48..15bc6a7 100644 (file)
@@ -1,6 +1,20 @@
 <?php
 /**
+ * Base class for dealing with Kronolith_Resource objects. Handles basic
+ * creation/deletion/listing by delegating to the underlying Kronolith_Driver
+ * object.
  *
+ * For now, assume SQL driver only. Could probably easily extend this to use
+ * different backend drivers if/when support is added to those drivers for
+ * resources.
+ *
+ * Copyright 2009 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (GPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
+ *
+ * @author Michael J. Rubinsky <mrubinsk@horde.org>
+ * @package Kronolith
  */
 class Kronolith_Resource
 {
@@ -45,7 +59,6 @@ class Kronolith_Resource
     static public function getResource($id)
     {
         $driver = Kronolith::getDriver('Sql');
-
         return new Kronolith_Resource_Single($driver->getResource($id));
     }
 
index f0b7e4f..1c874da 100644 (file)
@@ -22,7 +22,7 @@ abstract class Kronolith_Resource_Base
      *
      *  Properties:
      * name        - Display name of resource.
-     * calendar_id - The calendar associated with this resource.
+     * calendar    - The calendar associated with this resource.
      * category    - The category of this resource...an arbitrary label used
      *               to group multiple resources for the resource_group implementation
      * properties  - any other properties this resource may have?
@@ -51,7 +51,7 @@ abstract class Kronolith_Resource_Base
      * @param $endTime
      * @return unknown_type
      */
-    abstract public function isFree($startTime, $endTime);
+    abstract public function isFree($event);
 
     /**
      * Adds $event to this resource's calendar - thus blocking the time
@@ -60,7 +60,7 @@ abstract class Kronolith_Resource_Base
      * @param $event
      * @return unknown_type
      */
-    abstract public function attachToEvent($event);
+    abstract public function addEvent($event);
 
     /**
      * Remove this event from resource's calendar
@@ -68,7 +68,7 @@ abstract class Kronolith_Resource_Base
      * @param $event
      * @return unknown_type
      */
-    abstract public function detachFromEvent($event);
+    abstract public function removeEvent($event);
 
     /**
      * Obtain the freebusy information for this resource.  Takes into account
index ad034df..1181ef6 100644 (file)
@@ -2,6 +2,13 @@
 /**
  * Kronolith_Resource implementation to represent a single resource.
  *
+ * Copyright 2009 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (GPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
+ *
+ * @author Michael J. Rubinsky <mrubinsk@horde.org>
+ * @package Kronolith
  */
 class Kronolith_Resource_Single extends Kronolith_Resource_Base
 {
@@ -12,36 +19,36 @@ class Kronolith_Resource_Single extends Kronolith_Resource_Base
      * @param $endTime
      * @return unknown_type
      */
-    public function isFree($startTime, $endTime)
+    public function isFree($event)
     {
-
+        return true;
     }
 
     /**
-     * Adds $event to this resource's calendar - thus blocking the time
-     * for any other event.
+     * Adds $event to this resource's calendar or updates the current entry
+     * of the event in the calendar.
      *
      * @param $event
      *
-     * @return unknown_type
-     * @throws Horde_Exception
+     * @return void
      */
-    public function attachToEvent($event)
+    public function addEvent($event)
     {
         /* Get a driver for this resource's calendar */
-        $driver = Kronolith::getDriver(null, $this->calendar_id);
+        $driver = Kronolith::getDriver(null, $this->calendar);
         /* Make sure it's not already attached. */
         $uid = $event->getUID();
-        $existing = $driver->getByUID($uid, array($this->calendar_id));
+        $existing = $driver->getByUID($uid, array($this->calendar));
         if (!($existing instanceof PEAR_Error)) {
-            throw new Horde_Exception(_("Already Exists"));
+            $existing->fromiCalendar($event->toiCalendar(new Horde_iCalendar('2.0')));
+            $existing->save();
+        } else {
+            /* Create a new event */
+            $e = $driver->getEvent();
+            $e->setCalendar($this->calendar);
+            $e->fromiCalendar($event->toiCalendar(new Horde_iCalendar('2.0')));
+            $e->save();
         }
-
-        /* Create a new event */
-        $e = $driver->getEvent();
-        $e->setCalendar($this->calendar_id);
-        $e->fromiCalendar($event->toiCalendar($iCal = new Horde_iCalendar('2.0')));
-        $e->save();
     }
 
     /**
@@ -50,7 +57,7 @@ class Kronolith_Resource_Single extends Kronolith_Resource_Base
      * @param $event
      * @return unknown_type
      */
-    public function detachFromEvent($event)
+    public function removeEvent($event)
     {
 
     }
index e15ba9a..251c6a3 100644 (file)
@@ -8,7 +8,7 @@ require_once dirname(__FILE__) . '/lib/base.php';
 $title = _("Edit resources");
 require KRONOLITH_TEMPLATES . '/common-header.inc';
 
-/* Test some resource crap */
+/* Test creating a new resource */
 $new = array('name' => _("N329SP"),
              'category' => 'test');
 
@@ -17,4 +17,8 @@ $new = array('name' => _("N329SP"),
 //var_dump($results);
 
 /* Test adding resource to event */
-var_dump(Kronolith_Resource::getResource(6));
\ No newline at end of file
+$resource = Kronolith_Resource::getResource(6);
+$driver = Kronolith::getDriver('Sql');
+$event = $driver->getByUID('20090610181329.12687chinwtntsg8@localhost');
+$event->addResource($resource, Kronolith::RESPONSE_NONE);
+$event->save();