function mergePosts($msgs = array(), $from_topic, $target_topic = 0, $from_board = 0, $target_board = 0)
{
global $context, $db_prefix, $smfFunc, $modSettings;
// Is it an array?
if (!is_array($msgs))
$msgs = array($msgs);
// Lets make sure they are int.
foreach ($msgs as $key => $msg)
$msgs[$key] = (int) $msg;
// Time to move them.
$smfFunc['db_query']('', "
UPDATE {$db_prefix}messages
SET
id_topic = $target_topic,
id_board = $target_board,
icon = 'xx'
WHERE id_msg IN(" . implode(',', $msgs) . ")", __FILE__, __LINE__);
// Fix the id_first_msg and id_last_msg for the target topic.
$request = $smfFunc['db_query']('', "
SELECT MIN(id_msg) AS id_first_msg, MAX(id_msg) AS id_last_msg, COUNT(*) -1 AS num_replies
FROM {$db_prefix}messages
WHERE id_topic = $target_topic
AND approved = 1
GROUP BY id_topic", __FILE__, __LINE__);
list ($id_first_msg, $id_last_msg, $num_replies) = $smfFunc['db_fetch_row']($request);
$smfFunc['db_free_result']($request);
// We need to see how many of the new posts are unapproved.
$request = $smfFunc['db_query']('', "
SELECT COUNT(*)
FROM {$db_prefix}messages
WHERE approved = 0
AND id_msg IN(" . implode(',', $msgs) . ")", __FILE__, __LINE__);
list ($new_unapproved_posts) = $smfFunc['db_fetch_row']($request);
$smfFunc['db_free_result']($request);
// Update the topic details.
$smfFunc['db_query']('', "
UPDATE {$db_prefix}topics
SET
id_first_msg = $id_first_msg,
id_last_msg = $id_last_msg,
num_replies = $num_replies,
unapproved_posts = unapproved_posts + $new_unapproved_posts
WHERE id_topic = $target_topic", __FILE__, __LINE__);
// New approved posts.
//$new_approved_posts = count($msgs) - $new_unapproved_posts;
// We have a new post count for the board.
$smfFunc['db_query']('', "
UPDATE {$db_prefix}boards
SET
num_posts = num_posts + $num_replies,
unapproved_posts = unapproved_posts + $new_unapproved_posts
WHERE id_board = $target_board", __FILE__, __LINE__);
// In some cases we merged the only post in a topic so the topic data is left behind in the topic table.
$request = $smfFunc['db_query']('', "
SELECT id_topic
FROM {$db_prefix}messages
WHERE id_topic = $from_topic", __FILE__, __LINE__);
$topic_exists = true;
if ($smfFunc['db_num_rows']($request) == 0)
{
removeTopics($from_topic, false, true);
$topic_exists = false;
}
$smfFunc['db_free_result']($request);
// Recycled topic.
if ($topic_exists == true)
{
// Fix the id_first_msg and id_last_msg for the target topic.
$request = $smfFunc['db_query']('', "
SELECT MIN(id_msg) AS id_first_msg, MAX(id_msg) AS id_last_msg, COUNT(*) -1 AS num_replies
FROM {$db_prefix}messages
WHERE id_topic = $from_topic
AND approved = 1
GROUP BY id_topic", __FILE__, __LINE__);
list ($id_first_msg, $id_last_msg, $num_replies) = $smfFunc['db_fetch_row']($request);
$smfFunc['db_free_result']($request);
// We need to see how many of the new posts are unapproved.
$request = $smfFunc['db_query']('', "
SELECT COUNT(*)
FROM {$db_prefix}messages
WHERE approved = 0
AND id_topic = $from_topic", __FILE__, __LINE__);
list ($new_unapproved_posts) = $smfFunc['db_fetch_row']($request);
$smfFunc['db_free_result']($request);
// Update the topic details.
$smfFunc['db_query']('', "
UPDATE {$db_prefix}topics
SET
id_first_msg = $id_first_msg,
id_last_msg = $id_last_msg,
num_replies = $num_replies,
unapproved_posts = unapproved_posts + $new_unapproved_posts
WHERE id_topic = $from_topic", __FILE__, __LINE__);
// We have a new post count for the board.
$smfFunc['db_query']('', "
UPDATE {$db_prefix}boards
SET
num_posts = num_posts + $num_replies,
unapproved_posts = unapproved_posts + $new_unapproved_posts
WHERE id_board = $from_board", __FILE__, __LINE__);
}
}