Better field name, add method for retrieving resources, supporting code...
authorMichael J. Rubinsky <mrubinsk@horde.org>
Tue, 18 Aug 2009 16:13:20 +0000 (12:13 -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
kronolith/scripts/sql/kronolith.sql
kronolith/scripts/upgrades/2009-08-17_add_resource_field.sql

index 4492ce7..000d141 100644 (file)
@@ -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.
      *
index fb2db7a..093df48 100644 (file)
@@ -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
index 59b283f..f0b7e4f 100644 (file)
@@ -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
index 513e399..ad034df 100644 (file)
@@ -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."));
         }
index 4350f8c..be3c901 100644 (file)
@@ -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
index 37b2f18..507054b 100644 (file)
@@ -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),
index 655fcaa..125de93 100644 (file)
@@ -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)
 );