public function mergeThread($thread_from, $message_id)
{
$sql = 'SELECT message_thread, parents FROM ' . $this->_threads_table . ' WHERE message_id = ?';
try {
$destination = $this->_db->selectOne($sql, array($message_id));
} catch (Horde_Db_Exception $e) {
throw new Agora_Exception($e->getMessage());
}
/* Merge to the top level */
if ($destination['message_thread'] == 0) {
$destination['message_thread'] = $message_id;
}
$sql = 'SELECT message_thread, parents, message_id FROM ' . $this->_threads_table . ' WHERE message_id = ? OR message_thread = ?';
try {
$children = $this->_db->select($sql, array($thread_from, $thread_from));
} catch (Horde_Db_Exception $e) {
throw new Agora_Exception($e->getMessage());
}
/* TODO: merging more than one message breaks parent/child relations,
* also merging to deeper level than thread root doesn't work. */
if (!empty($children)) {
$sql = 'UPDATE ' . $this->_threads_table . ' SET message_thread = ?, parents = ? WHERE message_id = ?';
foreach ($children as $i => $message) {
$children[$i]['message_thread'] = $destination['message_thread'];
if (!empty($destination['parents'])) {
$children[$i]['parents'] = $destination['parents'] . $message['parents'];
} else {
$children[$i]['parents'] = ':' . $message_id;
}
try {
$this->_db->execute($sql, $children[$i]);
} catch (Horde_Db_Exception $e) {
throw new Agora_Exception($e->getMessage());
}
}
}
$count = $this->countThreads($destination['message_thread']);
$sql = 'UPDATE ' . $this->_threads_table . ' SET message_seq = ? WHERE message_id = ?';
try {
$this->_db->execute($sql, array($count, $destination['message_thread']));
} catch (Horde_Db_Exception $e) {
throw new Agora_Exception($e->getMessage());
}
/* Update last message */
$this->_lastInForum($this->_forum_id);
$this->_lastInThread($destination['message_thread']);
$this->_forumSequence($this->_forum_id, 'thread', '-');
/* Update cache */
$this->_updateCacheState($destination['message_thread']);
}