From: Jan Schneider Date: Wed, 11 Aug 2010 13:18:32 +0000 (+0200) Subject: Split up saveEvent() into _addEvent() and _updateEvent(). X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=37b8712b17f767c622cdf4ddd876ec90bd78e477;p=horde.git Split up saveEvent() into _addEvent() and _updateEvent(). --- diff --git a/kronolith/lib/Driver.php b/kronolith/lib/Driver.php index 28ebbdebc..e65fcfca8 100644 --- a/kronolith/lib/Driver.php +++ b/kronolith/lib/Driver.php @@ -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); } diff --git a/kronolith/lib/Driver/Horde.php b/kronolith/lib/Driver/Horde.php index 0f5d5869d..51e26f920 100644 --- a/kronolith/lib/Driver/Horde.php +++ b/kronolith/lib/Driver/Horde.php @@ -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']; } /** diff --git a/kronolith/lib/Driver/Kolab.php b/kronolith/lib/Driver/Kolab.php index 3df74aef6..7441fda52 100644 --- a/kronolith/lib/Driver/Kolab.php +++ b/kronolith/lib/Driver/Kolab.php @@ -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'); diff --git a/kronolith/lib/Driver/Sql.php b/kronolith/lib/Driver/Sql.php index 4a7c48683..031cb45b7 100644 --- a/kronolith/lib/Driver/Sql.php +++ b/kronolith/lib/Driver/Sql.php @@ -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; } /**