From 7713b8e5047c35f794e8c15080be330fff3e28ae Mon Sep 17 00:00:00 2001 From: "Michael J. Rubinsky" Date: Tue, 18 Aug 2009 11:10:52 -0400 Subject: [PATCH] Allow updating of existing resources, some error checking etc... --- kronolith/lib/Driver/Sql.php | 34 +++++++++++++++++++++++++++++----- kronolith/lib/Resource.php | 2 +- kronolith/lib/Resource/Base.php | 21 +++++++++++++++++++-- kronolith/lib/Resource/Single.php | 9 +++++++++ kronolith/resources.php | 17 +++++++++++++++++ 5 files changed, 75 insertions(+), 8 deletions(-) create mode 100644 kronolith/resources.php diff --git a/kronolith/lib/Driver/Sql.php b/kronolith/lib/Driver/Sql.php index 82346249c..4492ce7a9 100644 --- a/kronolith/lib/Driver/Sql.php +++ b/kronolith/lib/Driver/Sql.php @@ -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; } /** diff --git a/kronolith/lib/Resource.php b/kronolith/lib/Resource.php index 66e8dbd2f..fb2db7a0f 100644 --- a/kronolith/lib/Resource.php +++ b/kronolith/lib/Resource.php @@ -27,7 +27,7 @@ class Kronolith_Resource $resource->calendar_id = $calendar; $driver = Kronolith::getDriver('Sql'); - $driver->saveResource($resource); + return $driver->saveResource($resource); } /** diff --git a/kronolith/lib/Resource/Base.php b/kronolith/lib/Resource/Base.php index 590013e5a..59b283fd5 100644 --- a/kronolith/lib/Resource/Base.php +++ b/kronolith/lib/Resource/Base.php @@ -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 diff --git a/kronolith/lib/Resource/Single.php b/kronolith/lib/Resource/Single.php index 09dafc0fc..513e3990c 100644 --- a/kronolith/lib/Resource/Single.php +++ b/kronolith/lib/Resource/Single.php @@ -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 index 000000000..4350f8c35 --- /dev/null +++ b/kronolith/resources.php @@ -0,0 +1,17 @@ + _("N329SP"), + 'category' => 'test'); + +$resource = new Kronolith_Resource_Single($new); +$results = Kronolith_Resource::addResource($resource); +var_dump($results); \ No newline at end of file -- 2.11.0