More work on resource groups. Most of the backend support for resource groups is...
authorMichael J. Rubinsky <mrubinsk@horde.org>
Tue, 22 Sep 2009 00:23:01 +0000 (20:23 -0400)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Tue, 29 Sep 2009 20:53:57 +0000 (16:53 -0400)
but there is no GUI support yet.

kronolith/attendees.php
kronolith/lib/Driver/Resource.php
kronolith/lib/Resource/Base.php
kronolith/lib/Resource/Group.php

index b03de1a..a917039 100644 (file)
@@ -112,7 +112,7 @@ case 'add':
         $end = new Horde_Date(Horde_Util::getFormData('enddate'));
         $response = $resource->getResponse(array('start' => $date, 'end' => $end));
 
-        $resources[$newResource] = array(
+        $resources[$resource->getId()] = array(
             'attendance' => Kronolith::PART_REQUIRED,
             'response'   => $response,
             'name'       => $resource->get('name'),
index a7b643a..2bdf22a 100644 (file)
@@ -361,8 +361,8 @@ class Kronolith_Driver_Resource extends Kronolith_Driver_Sql
     public function save($resource)
     {
         if ($resource->getId()) {
-            $query = 'UPDATE kronolith_resources SET resource_name = ?, resource_calendar = ?, resource_category = ? , resource_description = ?, resource_response_type = ? WHERE resource_id = ?';
-            $values = array($resource->get('name'), $resource->get('calendar'), $resource->get('category'), $resource->get('description'), $resource->get('response_type'), $resource->getId());
+            $query = 'UPDATE kronolith_resources SET resource_name = ?, resource_calendar = ?, resource_category = ? , resource_description = ?, resource_response_type = ?, resource_type = ?, resource_members = ? WHERE resource_id = ?';
+            $values = array($resource->get('name'), $resource->get('calendar'), $resource->get('category'), $resource->get('description'), $resource->get('response_type'), $resource->get('type'), $resource->get('members'), $resource->getId());
             $result = $this->_write_db->query($query, $values);
             if ($result instanceof PEAR_Error) {
                 throw new Horde_Exception($result->getMessage());
@@ -447,12 +447,12 @@ class Kronolith_Driver_Resource extends Kronolith_Driver_Sql
      *
      * @param int $id  The key for the Kronolith_Resource
      *
-     * @return array  A hash of resource object properties
+     * @return Kronolith_Resource_Single || Kronolith_Resource_Group
      * @throws Horde_Exception
      */
     public function getResource($id)
     {
-        $query = 'SELECT resource_id, resource_name, resource_calendar, resource_category, resource_description, resource_response_type FROM kronolith_resources WHERE resource_id = ?';
+        $query = 'SELECT resource_id, resource_name, resource_calendar, resource_category, resource_description, resource_response_type, resource_type, resource_members FROM kronolith_resources WHERE resource_id = ?';
 
         $results = $this->_db->getRow($query, array($id), DB_FETCHMODE_ASSOC);
         if ($results instanceof PEAR_Error) {
@@ -462,7 +462,12 @@ class Kronolith_Driver_Resource extends Kronolith_Driver_Sql
             throw new Horde_Exception('Resource not found');
         }
 
-        return new Kronolith_Resource_Single($this->_fromDriver($results));
+        $class = 'Kronolith_Resource_' . $results['resource_type'];
+        if (!class_exists($class)) {
+            throw new Horde_Exception(sprintf(_("Could not load the class definition for %s"), $class));
+        }
+
+        return new $class($this->_fromDriver($results));
     }
 
     /**
@@ -497,7 +502,7 @@ class Kronolith_Driver_Resource extends Kronolith_Driver_Sql
      */
     public function listResources($filter = array())
     {
-        $query = 'SELECT resource_id, resource_name, resource_calendar, resource_category, resource_description, resource_response_type FROM kronolith_resources';
+        $query = 'SELECT resource_id, resource_name, resource_calendar, resource_category, resource_description, resource_response_type, resource_type, resource_members FROM kronolith_resources';
         if (count($filter)) {
             $clause = ' WHERE ';
             $i = 0;
@@ -515,7 +520,8 @@ class Kronolith_Driver_Resource extends Kronolith_Driver_Sql
 
         $return = array();
         foreach ($results as $key => $result) {
-            $return[$key] = new Kronolith_Resource_Single($this->_fromDriver(array_merge(array('resource_id' => $key), $result)));
+            $class = 'Kronolith_Resource_' . $result['resource_type'];
+            $return[$key] = new $class($this->_fromDriver(array_merge(array('resource_id' => $key), $result)));
         }
 
         return $return;
index 405eace..20ad896 100644 (file)
@@ -69,9 +69,7 @@ abstract class Kronolith_Resource_Base
      */
     public function set($property, $value)
     {
-        //if (in_array($property, array('name', 'category', 'calendar', 'description'))) {
-            $this->_params[$property] = $value;
-        //}
+        $this->_params[$property] = $value;
     }
 
     /**
@@ -99,8 +97,12 @@ abstract class Kronolith_Resource_Base
      */
     public function get($property)
     {
-       $property = str_replace('resource_', '', $property);
-       return !empty($this->_params[$property]) ? $this->_params[$property] : false;
+        $property = str_replace('resource_', '', $property);
+        if ($property == 'type' && empty($this->_params['type'])) {
+            return (self instanceof Kronolith_Resource_Single) ? 'Single' : 'Group';
+        }
+
+        return !empty($this->_params[$property]) ? $this->_params[$property] : false;
     }
 
     /**
index 0a72b60..457814e 100644 (file)
@@ -56,7 +56,16 @@ class Kronolith_Resource_Group extends Kronolith_Resource_Base
         if (empty($this->_selectedResource)) {
             return parent::get($property);
         } else {
-            return $this->_selectedResoruce->get($property);
+            return $this->_selectedResource->get($property);
+        }
+    }
+
+    public function getId()
+    {
+        if (!empty($this->_selectedResource)) {
+            return $this->_selectedResource->getId();
+        } else {
+            return parent::getId();
         }
     }
 
@@ -84,8 +93,8 @@ class Kronolith_Resource_Group extends Kronolith_Resource_Base
         $resources = unserialize($this->get('members'));
 
         /* Iterate over all resources until one with no conflicts is found */
-        $conflict = false;
         foreach ($resources as $resource_id) {
+            $conflict = false;
             $resource = $this->_driver->getResource($resource_id);
             $busy = Kronolith::listEvents($start, $end, array($resource->get('calendar')));
             if ($busy instanceof PEAR_Error) {
@@ -112,8 +121,8 @@ class Kronolith_Resource_Group extends Kronolith_Resource_Base
                           $e->hasStatus(Kronolith::STATUS_FREE)) &&
                          $e->getUID() !== $uid) {
 
-                         if (!($e->start->compareDateTime($end) >= 1 ||
-                             $e->end->compareDateTime($start) <= -1)) {
+                         if (!($e->start->compareDateTime($end) >= 0) &&
+                             !($e->end->compareDateTime($start) <= 0)) {
 
                             // Not free, continue to the next resource
                             $conflict = true;
@@ -143,24 +152,7 @@ class Kronolith_Resource_Group extends Kronolith_Resource_Base
      */
     public function addEvent($event)
     {
-        if (empty($this->_selectedResource)) {
-            $this->isFree($event);
-        }
-//        /* Make sure it's not already attached. */
-//        $uid = $event->getUID();
-//        $existing = $driver->getByUID($uid, array($this->get('calendar')));
-//        if (!($existing instanceof PEAR_Error)) {
-//            /* Already attached, just update */
-//            $existing->fromiCalendar($event->toiCalendar(new Horde_iCalendar('2.0')));
-//            $existing->status = $event->status;
-//            $existing->save();
-//        } else {
-//            /* Create a new event */
-//            $e = $driver->getEvent();
-//            $e->setCalendar($this->get('calendar'));
-//            $e->fromiCalendar($event->toiCalendar(new Horde_iCalendar('2.0')));
-//            $e->save();
-//        }
+        throw new Horde_Exception(_("Events should be added to the Single resource object, not directly to the Group object."));
     }
 
     /**