}
/**
+ * 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);
}
}
/**
- * 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');
}
/**
- * 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. */
/* 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');
}
/* Notify users about the new event. */
Kronolith::sendNotification($event, 'add');
- return $id;
+ return $event->id;
}
/**