From: Michael J. Rubinsky Date: Tue, 18 Aug 2009 16:13:20 +0000 (-0400) Subject: Better field name, add method for retrieving resources, supporting code... X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=0c8e2076fb8ad654405a10c9aecaf7f222fb40f8;p=horde.git Better field name, add method for retrieving resources, supporting code... --- diff --git a/kronolith/lib/Driver/Sql.php b/kronolith/lib/Driver/Sql.php index 4492ce7a9..000d141c4 100644 --- a/kronolith/lib/Driver/Sql.php +++ b/kronolith/lib/Driver/Sql.php @@ -773,14 +773,14 @@ class Kronolith_Driver_Sql extends Kronolith_Driver public function saveResource($resource) { if (!empty($resource->uid)) { - $query = 'UPDATE kronolith_resources SET resource_name = ?, resource_calendar = ?, resource_category = ? WHERE resource_uid = ?'; + $query = 'UPDATE kronolith_resources SET resource_name = ?, resource_calendar = ?, resource_category = ? WHERE resource_id = ?'; $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)'; + $query = 'INSERT INTO kronolith_resources (resource_id, 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); @@ -796,6 +796,19 @@ class Kronolith_Driver_Sql extends Kronolith_Driver return $resource; } + public function getResource($id) + { + $query = 'SELECT resource_id, resource_name, resource_calendar, resource_category FROM kronolith_resources WHERE resource_uid = ?'; + + $results = $this->_db->getRow($query, array($id), DB_FETCHMODE_ASSOC); + $return = array(); + foreach ($results as $field => $value) { + $return[str_replace('resource_', '', $field)] = $this->convertFromDriver($value); + } + + return $return; + } + /** * Attempts to open a connection to the SQL server. * diff --git a/kronolith/lib/Resource.php b/kronolith/lib/Resource.php index fb2db7a0f..093df485e 100644 --- a/kronolith/lib/Resource.php +++ b/kronolith/lib/Resource.php @@ -42,4 +42,11 @@ class Kronolith_Resource } + static public function getResource($id) + { + $driver = Kronolith::getDriver('Sql'); + + return new Kronolith_Resource_Single($driver->getResource($id)); + } + } \ No newline at end of file diff --git a/kronolith/lib/Resource/Base.php b/kronolith/lib/Resource/Base.php index 59b283fd5..f0b7e4f0c 100644 --- a/kronolith/lib/Resource/Base.php +++ b/kronolith/lib/Resource/Base.php @@ -6,13 +6,13 @@ abstract class Kronolith_Resource_Base { protected $_params = array(); - protected $_uid = ''; + protected $_id = ''; public function __construct($params = array()) { if (!empty($params['id'])) { // Existing resource - $this->_uid = $params['id']; + $this->_id = $params['id']; } $this->_params = $params; @@ -33,8 +33,8 @@ abstract class Kronolith_Resource_Base */ public function __get($property) { - if ($property == 'uid') { - return $this->_uid; + if ($property == 'id') { + return $this->_id; } if (isset($this->_params[$property])) { @@ -85,6 +85,6 @@ abstract class Kronolith_Resource_Base * * @return unknown_type */ - abstract public function setUid($id); + abstract public function setId($id); } \ No newline at end of file diff --git a/kronolith/lib/Resource/Single.php b/kronolith/lib/Resource/Single.php index 513e3990c..ad034df90 100644 --- a/kronolith/lib/Resource/Single.php +++ b/kronolith/lib/Resource/Single.php @@ -66,10 +66,10 @@ class Kronolith_Resource_Single extends Kronolith_Resource_Base } - public function setUid($id) + public function setId($id) { - if (!empty($this->_uid)) { - $this->_uid = $id; + if (!empty($this->_id)) { + $this->_id = $id; } else { throw new Horde_Exception(_("Resource already exists. Cannot change the id.")); } diff --git a/kronolith/resources.php b/kronolith/resources.php index 4350f8c35..be3c901fc 100644 --- a/kronolith/resources.php +++ b/kronolith/resources.php @@ -12,6 +12,9 @@ require KRONOLITH_TEMPLATES . '/common-header.inc'; $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 +//$resource = new Kronolith_Resource_Single($new); +//$results = Kronolith_Resource::addResource($resource); +//var_dump($results); + +/* Test adding resource to event */ +var_dump(Kronolith_Resource::getResource(5)); \ No newline at end of file diff --git a/kronolith/scripts/sql/kronolith.sql b/kronolith/scripts/sql/kronolith.sql index 37b2f180a..507054bfe 100644 --- a/kronolith/scripts/sql/kronolith.sql +++ b/kronolith/scripts/sql/kronolith.sql @@ -81,7 +81,7 @@ CREATE INDEX kronolith_shares_users_user_uid_idx ON kronolith_shares_users (user CREATE INDEX kronolith_shares_users_perm_idx ON kronolith_shares_users (perm); CREATE TABLE kronolith_resources { - resource_uid INT NOT NULL, + resource_id INT NOT NULL, resource_name VARCHAR(255), resource_calendar VARCHAR(255), resource_category VARCHAR(255), diff --git a/kronolith/scripts/upgrades/2009-08-17_add_resource_field.sql b/kronolith/scripts/upgrades/2009-08-17_add_resource_field.sql index 655fcaacd..125de933f 100644 --- a/kronolith/scripts/upgrades/2009-08-17_add_resource_field.sql +++ b/kronolith/scripts/upgrades/2009-08-17_add_resource_field.sql @@ -1,10 +1,10 @@ ALTER TABLE kronolith_events ADD event_resources TEXT; CREATE TABLE kronolith_resources ( - resource_uid INT NOT NULL, + resource_id INT NOT NULL, resource_name VARCHAR(255), resource_calendar VARCHAR(255), resource_category VARCHAR(255), - PRIMARY KEY (resource_uid) + PRIMARY KEY (resource_id) );