Split up saveEvent() into _addEvent() and _updateEvent().
authorJan Schneider <jan@horde.org>
Wed, 11 Aug 2010 13:18:32 +0000 (15:18 +0200)
committerJan Schneider <jan@horde.org>
Wed, 11 Aug 2010 15:34:55 +0000 (17:34 +0200)
kronolith/lib/Driver.php
kronolith/lib/Driver/Horde.php
kronolith/lib/Driver/Kolab.php
kronolith/lib/Driver/Sql.php

index 28ebbde..e65fcfc 100644 (file)
@@ -293,11 +293,47 @@ class Kronolith_Driver
     }
 
     /**
+     * Saves an event in the backend.
+     *
+     * If it is a new event, it is added, otherwise the event is updated.
+     *
+     * @param Kronolith_Event $event  The event to save.
+     *
+     * @return string  The event id.
+     * @throws Horde_Mime_Exception
+     * @throws Kronolith_Exception
+     */
+    public function saveEvent($event)
+    {
+        if ($event->stored || $event->exists()) {
+            return $this->_updateEvent($event);
+        }
+
+        if (!$event->id) {
+            $event->id = (string)new Horde_Support_Randomid;
+        }
+        if (!$event->uid) {
+            $event->uid = (string)new Horde_Support_Guid;
+        }
+        return $this->_addEvent($event);
+    }
+
+    /**
+     * Stub to be overridden in the child class.
+     *
+     * @throws Kronolith_Exception
+     */
+    protected function _addEvent()
+    {
+        throw new Kronolith_Exception($this->_errormsg);
+    }
+
+    /**
      * Stub to be overridden in the child class.
      *
      * @throws Kronolith_Exception
      */
-    public function saveEvent()
+    protected function _updateEvent()
     {
         throw new Kronolith_Exception($this->_errormsg);
     }
index 0f5d586..51e26f9 100644 (file)
@@ -107,19 +107,24 @@ class Kronolith_Driver_Horde extends Kronolith_Driver
     }
 
     /**
-     * Saves an (existing) event in the backend.
+     * Updates an existing event in the backend.
      *
      * @param Kronolith_Event_Horde $event  The event to save.
      *
-     * @return integer  The event id.
+     * @return string  The event id.
      * @throws Horde_Exception
+     * @throws Kronolith_Exception
      */
-    public function saveEvent($event)
+    protected function _updateEvent($event)
     {
         if (!isset($this->api)) {
             list($this->api, $category) = explode('/', $this->calendar, 2);
         }
-        $this->_params['registry']->call($this->api . '/saveTimeObject', array($event->timeobject));
+        $result = $this->_params['registry']->call($this->api . '/saveTimeObject', array($event->timeobject));
+        if ($result instanceof PEAR_Error) {
+            throw new Kronolith_Exception($result);
+        }
+        return $event->timeobject['id'];
     }
 
     /**
index 3df74ae..7441fda 100644 (file)
@@ -328,39 +328,51 @@ class Kronolith_Driver_Kolab extends Kronolith_Driver
     }
 
     /**
-     * Saves an event in the backend.
-     * If it is a new event, it is added, otherwise the event is updated.
+     * Updates an existing event in the backend.
      *
      * @param Kronolith_Event $event  The event to save.
      *
-     * @return integer  The event id.
+     * @return string  The event id.
      * @throws Horde_Mime_Exception
      */
-    public function saveEvent($event)
+    protected function _updateEvent($event)
     {
-        $result = $this->synchronize();
+        return $this->_saveEvent($event, true);
+    }
 
-        $uid = $event->uid;
-        if ($uid == null) {
-            $event->uid = $this->_store->generateUID();
-        }
+    /**
+     * Adds an event to the backend.
+     *
+     * @param Kronolith_Event $event  The event to save.
+     *
+     * @return string  The event id.
+     * @throws Horde_Mime_Exception
+     */
+    protected function _addEvent($event)
+    {
+        return $this->_saveEvent($event, false);
+    }
 
-        $attributes = $event->toDriver();
+    /**
+     * Saves an event in the backend.
+     *
+     * @param Kronolith_Event $event  The event to save.
+     *
+     * @return string  The event id.
+     * @throws Horde_Mime_Exception
+     */
+    protected function _saveEvent($event, $edit)
+    {
+        $this->synchronize();
 
-        $edit = false;
-        $stored_uid = null;
-        if ($event->stored || $event->exists()) {
-            $stored_uid = $attributes['uid'];
-            $action = array('action' => 'modify');
-            $edit = true;
-        } else {
-            $action = array('action' => 'add');
-        }
+        $action = $edit
+            ? array('action' => 'modify')
+            : array('action' => 'add');
 
-        $result = $this->_store->save($attributes, $stored_uid);
+        $this->_store->save($event->toDriver(), $edit ? $event->uid : null);
 
         /* Deal with tags */
-        if (!empty($edit)) {
+        if ($edit) {
             Kronolith::getTagger()->replaceTags($event->uid, $event->tags, $event->creator, 'event');
         } else {
             Kronolith::getTagger()->tag($event->uid, $event->tags, $event->creator, 'event');
index 4a7c486..031cb45 100644 (file)
@@ -536,99 +536,88 @@ class Kronolith_Driver_Sql extends Kronolith_Driver
     }
 
     /**
-     * Saves an event in the backend.
-     *
-     * If it is a new event, it is added, otherwise the event is updated.
+     * Updates an existing event in the backend.
      *
      * @param Kronolith_Event $event  The event to save.
      *
-     * @return integer  The event id.
+     * @return string  The event id.
      * @throws Horde_Mime_Exception
      * @throws Kronolith_Exception
      */
-    public function saveEvent($event)
+    protected function _updateEvent($event)
     {
-        if ($event->stored || $event->exists()) {
-            $values = array();
-
-            $query = 'UPDATE ' . $this->_params['table'] . ' SET ';
-
-            foreach ($event->getProperties() as $key => $val) {
-                $query .= " $key = ?,";
-                $values[] = $val;
-            }
-            $query = substr($query, 0, -1);
-            $query .= ' WHERE event_id = ?';
-            $values[] = $event->id;
+        $values = array();
+        $query = 'UPDATE ' . $this->_params['table'] . ' SET ';
+        foreach ($event->getProperties() as $key => $val) {
+            $query .= " $key = ?,";
+            $values[] = $val;
+        }
+        $query = substr($query, 0, -1);
+        $query .= ' WHERE event_id = ?';
+        $values[] = $event->id;
 
-            /* Log the query at a DEBUG log level. */
-            Horde::logMessage(sprintf('Kronolith_Driver_Sql::saveEvent(): user = "%s"; query = "%s"; values = "%s"',
-                                      $GLOBALS['registry']->getAuth(), $query, implode(',', $values)), 'DEBUG');
+        /* Log the query at a DEBUG log level. */
+        Horde::logMessage(sprintf('Kronolith_Driver_Sql::saveEvent(): user = "%s"; query = "%s"; values = "%s"',
+                                  $GLOBALS['registry']->getAuth(), $query, implode(',', $values)), 'DEBUG');
 
-            $result = $this->_write_db->query($query, $values);
-            $this->handleError($result);
+        $result = $this->_write_db->query($query, $values);
+        $this->handleError($result);
 
-            /* Log the modification of this item in the history log. */
-            if ($event->uid) {
-                try {
-                    $GLOBALS['injector']->getInstance('Horde_History')->log('kronolith:' . $this->calendar . ':' . $event->uid, array('action' => 'modify'), true);
-                } catch (Exception $e) {
-                    Horde::logMessage($e, 'ERR');
-                }
+        /* Log the modification of this item in the history log. */
+        if ($event->uid) {
+            try {
+                $GLOBALS['injector']->getInstance('Horde_History')->log('kronolith:' . $this->calendar . ':' . $event->uid, array('action' => 'modify'), true);
+            } catch (Exception $e) {
+                Horde::logMessage($e, 'ERR');
             }
+        }
 
-            /* If this event is an exception, we need to modify the base event's
-             * history log also, or some synch clients will never pick up the
-             *  change*/
-             if ($event->baseid) {
-                try {
-                    $GLOBALS['injector']->getInstance('Horde_History')->log('kronolith:' . $this->calendar . ':' . $event->baseid, array('action' => 'modify'), true);
-                } catch (Exception $e) {
-                    Horde::logMessage($e, 'ERR');
-                }
-             }
-            $this->_updateTags($event);
-
-            /* Update Geolocation */
-            if ($gDriver = Kronolith::getGeoDriver()) {
-                $gDriver->setLocation($event->id, $event->geoLocation);
+        /* If this event is an exception, we need to modify the base event's
+         * history log also, or some sync clients will never pick up the
+         * change. */
+        if ($event->baseid) {
+            try {
+                $GLOBALS['injector']->getInstance('Horde_History')->log('kronolith:' . $this->calendar . ':' . $event->baseid, array('action' => 'modify'), true);
+            } catch (Exception $e) {
+                Horde::logMessage($e, 'ERR');
             }
-
-            /* Notify users about the changed event. */
-            Kronolith::sendNotification($event, 'edit');
-
-            return $event->id;
         }
+        $this->_updateTags($event);
 
-        if ($event->id) {
-            $id = $event->id;
-        } else {
-            $id = strval(new Horde_Support_Randomid);
-            $event->id = $id;
+        /* Update Geolocation */
+        if ($gDriver = Kronolith::getGeoDriver()) {
+            $gDriver->setLocation($event->id, $event->geoLocation);
         }
 
-        if ($event->uid) {
-            $uid = $event->uid;
-        } else {
-            $uid = (string)new Horde_Support_Guid;
-            $event->uid = $uid;
-        }
+        /* Notify users about the changed event. */
+        Kronolith::sendNotification($event, 'edit');
 
+        return $event->id;
+    }
+
+    /**
+     * Adds an event to the backend.
+     *
+     * @param Kronolith_Event $event  The event to save.
+     *
+     * @return string  The event id.
+     * @throws Horde_Mime_Exception
+     * @throws Kronolith_Exception
+     */
+    protected function _addEvent($event)
+    {
         $query = 'INSERT INTO ' . $this->_params['table'];
         $cols_name = ' (event_id, event_uid,';
         $cols_values = ' VALUES (?, ?,';
-        $values = array($id, $uid);
-
+        $values = array($event->id, $event->uid);
         foreach ($event->getProperties() as $key => $val) {
             $cols_name .= " $key,";
             $cols_values .= ' ?,';
             $values[] = $val;
         }
-
         $cols_name .= ' calendar_id)';
         $cols_values .= ' ?)';
         $values[] = $this->calendar;
-
         $query .= $cols_name . $cols_values;
 
         /* Log the query at a DEBUG log level. */
@@ -640,7 +629,7 @@ class Kronolith_Driver_Sql extends Kronolith_Driver
 
         /* Log the creation of this item in the history log. */
         try {
-            $GLOBALS['injector']->getInstance('Horde_History')->log('kronolith:' . $this->calendar . ':' . $uid, array('action' => 'add'), true);
+            $GLOBALS['injector']->getInstance('Horde_History')->log('kronolith:' . $this->calendar . ':' . $event->uid, array('action' => 'add'), true);
         } catch (Exception $e) {
             Horde::logMessage($e, 'ERR');
         }
@@ -655,7 +644,7 @@ class Kronolith_Driver_Sql extends Kronolith_Driver
         /* Notify users about the new event. */
         Kronolith::sendNotification($event, 'add');
 
-        return $id;
+        return $event->id;
     }
 
     /**