Allow updating of existing resources, some error checking etc...
authorMichael J. Rubinsky <mrubinsk@horde.org>
Tue, 18 Aug 2009 15:10:52 +0000 (11:10 -0400)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Tue, 29 Sep 2009 20:53:48 +0000 (16:53 -0400)
kronolith/lib/Driver/Sql.php
kronolith/lib/Resource.php
kronolith/lib/Resource/Base.php
kronolith/lib/Resource/Single.php
kronolith/resources.php [new file with mode: 0644]

index 8234624..4492ce7 100644 (file)
@@ -763,13 +763,37 @@ class Kronolith_Driver_Sql extends Kronolith_Driver
         return true;
     }
 
+    /**
+     * Save or update a Kronolith_Resource
+     *
+     * @param $resource
+     *
+     * @return Kronolith_Resource object
+     */
     public function saveResource($resource)
     {
-        $query = 'INSERT INTO kronolith_resources (resource_uid, resource_name, resource_calendar, resource_category)';
-        $cols_values = ' VALUES (?, ?, ?, ?, ?)';
-        $id = $this->_db->nextId('kronolity_resources');
-        $values = array($id, $resource->name, $resource->calendar_id, $resource->category);
-        $result = $this->_write_db->query($query, $values);
+        if (!empty($resource->uid)) {
+            $query = 'UPDATE kronolith_resources SET resource_name = ?, resource_calendar = ?, resource_category = ? WHERE resource_uid = ?';
+            $values = array($resource->name, $resource->calendar_id, $resource->category, $resource->uid);
+            $result = $this->_write_db->query($query, $values);
+            if (!($result instanceof PEAR_Error)) {
+                throw new Horde_Exception($result->getMessage());
+            }
+        } else {
+            $query = 'INSERT INTO kronolith_resources (resource_uid, resource_name, resource_calendar, resource_category)';
+            $cols_values = ' VALUES (?, ?, ?, ?)';
+            $id = $this->_db->nextId('kronolity_resources');
+            $values = array($id, $resource->name, $resource->calendar_id, $resource->category);
+            $result = $this->_write_db->query($query . $cols_values, $values);
+            if (!($result instanceof PEAR_Error)) {
+                return true;
+            } else {
+                throw new Horde_Exception($result->getMessage());
+            }
+            $resource->setUid($id);
+        }
+
+        return $resource;
     }
 
     /**
index 66e8dbd..fb2db7a 100644 (file)
@@ -27,7 +27,7 @@ class Kronolith_Resource
         $resource->calendar_id = $calendar;
 
         $driver = Kronolith::getDriver('Sql');
-        $driver->saveResource($resource);
+        return $driver->saveResource($resource);
     }
 
     /**
index 590013e..59b283f 100644 (file)
@@ -3,7 +3,7 @@
  * Kronolith resources
  *
  */
-class Kronolith_Resource_Base
+abstract class Kronolith_Resource_Base
 {
     protected $_params = array();
     protected $_uid = '';
@@ -33,7 +33,15 @@ class Kronolith_Resource_Base
      */
     public function __get($property)
     {
-        return $this->_params[$property];
+        if ($property == 'uid') {
+            return $this->_uid;
+        }
+
+        if (isset($this->_params[$property])) {
+            return $this->_params[$property];
+        } else {
+            throw new Horde_Exception(sprintf(_("Invalid property, %s, requested in Kronolith_Resource"), $property));
+        }
     }
 
     /**
@@ -70,4 +78,13 @@ class Kronolith_Resource_Base
      */
     abstract public function getFreeBusy();
 
+    /**
+     * Sets the current resource's id. Must not be an existing resource.
+     *
+     * @param int $id  The id for this resource
+     *
+     * @return unknown_type
+     */
+    abstract public function setUid($id);
+
 }
\ No newline at end of file
index 09dafc0..513e399 100644 (file)
@@ -66,4 +66,13 @@ class Kronolith_Resource_Single extends Kronolith_Resource_Base
 
     }
 
+    public function setUid($id)
+    {
+        if (!empty($this->_uid)) {
+            $this->_uid = $id;
+        } else {
+            throw new Horde_Exception(_("Resource already exists. Cannot change the id."));
+        }
+    }
+
 }
\ No newline at end of file
diff --git a/kronolith/resources.php b/kronolith/resources.php
new file mode 100644 (file)
index 0000000..4350f8c
--- /dev/null
@@ -0,0 +1,17 @@
+<?php
+/**
+ * See the enclosed file COPYING for license information (GPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
+ */
+require_once dirname(__FILE__) . '/lib/base.php';
+
+$title = _("Edit resources");
+require KRONOLITH_TEMPLATES . '/common-header.inc';
+
+/* Test some resource crap */
+$new = array('name' => _("N329SP"),
+             'category' => 'test');
+
+$resource = new Kronolith_Resource_Single($new);
+$results = Kronolith_Resource::addResource($resource);
+var_dump($results);
\ No newline at end of file