function move_forum($from_id, $to_id)
{
global $db, $user, $phpbb_dispatcher;
$errors = array();
// Check if we want to move to a parent with link type
if ($to_id > 0) {
$to_data = $this->get_forum_info($to_id);
if ($to_data['forum_type'] == FORUM_LINK) {
$errors[] = $user->lang['PARENT_IS_LINK_FORUM'];
}
}
/**
* Event when we move all children of one forum to another
*
* This event may be triggered, when a forum is deleted
*
* @event core.acp_manage_forums_move_children
* @var int from_id If of the current parent forum
* @var int to_id If of the new parent forum
* @var array errors Array of errors, should be strings and not
* language key.
* @since 3.1.0-a1
*/
$vars = array('from_id', 'to_id', 'errors');
extract($phpbb_dispatcher->trigger_event('core.acp_manage_forums_move_children', compact($vars)));
// Return if there were errors
if (!empty($errors)) {
return $errors;
}
$moved_forums = get_forum_branch($from_id, 'children', 'descending');
$from_data = $moved_forums[0];
$diff = sizeof($moved_forums) * 2;
$moved_ids = array();
for ($i = 0, $size = sizeof($moved_forums); $i < $size; ++$i) {
$moved_ids[] = $moved_forums[$i]['forum_id'];
}
// Resync parents
$sql = 'UPDATE ' . FORUMS_TABLE . "\n\t\t\tSET right_id = right_id - {$diff}, forum_parents = ''\n\t\t\tWHERE left_id < " . $from_data['right_id'] . "\n\t\t\t\tAND right_id > " . $from_data['right_id'];
$db->sql_query($sql);
// Resync righthand side of tree
$sql = 'UPDATE ' . FORUMS_TABLE . "\n\t\t\tSET left_id = left_id - {$diff}, right_id = right_id - {$diff}, forum_parents = ''\n\t\t\tWHERE left_id > " . $from_data['right_id'];
$db->sql_query($sql);
if ($to_id > 0) {
// Retrieve $to_data again, it may have been changed...
$to_data = $this->get_forum_info($to_id);
// Resync new parents
$sql = 'UPDATE ' . FORUMS_TABLE . "\n\t\t\t\tSET right_id = right_id + {$diff}, forum_parents = ''\n\t\t\t\tWHERE " . $to_data['right_id'] . ' BETWEEN left_id AND right_id
AND ' . $db->sql_in_set('forum_id', $moved_ids, true);
$db->sql_query($sql);
// Resync the righthand side of the tree
$sql = 'UPDATE ' . FORUMS_TABLE . "\n\t\t\t\tSET left_id = left_id + {$diff}, right_id = right_id + {$diff}, forum_parents = ''\n\t\t\t\tWHERE left_id > " . $to_data['right_id'] . '
AND ' . $db->sql_in_set('forum_id', $moved_ids, true);
$db->sql_query($sql);
// Resync moved branch
$to_data['right_id'] += $diff;
if ($to_data['right_id'] > $from_data['right_id']) {
$diff = '+ ' . ($to_data['right_id'] - $from_data['right_id'] - 1);
} else {
$diff = '- ' . abs($to_data['right_id'] - $from_data['right_id'] - 1);
}
} else {
$sql = 'SELECT MAX(right_id) AS right_id
FROM ' . FORUMS_TABLE . '
WHERE ' . $db->sql_in_set('forum_id', $moved_ids, true);
$result = $db->sql_query($sql);
$row = $db->sql_fetchrow($result);
$db->sql_freeresult($result);
$diff = '+ ' . ($row['right_id'] - $from_data['left_id'] + 1);
}
$sql = 'UPDATE ' . FORUMS_TABLE . "\n\t\t\tSET left_id = left_id {$diff}, right_id = right_id {$diff}, forum_parents = ''\n\t\t\tWHERE " . $db->sql_in_set('forum_id', $moved_ids);
$db->sql_query($sql);
return $errors;
}