function move_module($from_module_id, $to_parent_id)
{
global $db;
$moved_modules = $this->get_module_branch($from_module_id, 'children', 'descending');
$from_data = $moved_modules[0];
$diff = sizeof($moved_modules) * 2;
$moved_ids = array();
for ($i = 0; $i < sizeof($moved_modules); ++$i) {
$moved_ids[] = $moved_modules[$i]['module_id'];
}
// Resync parents
$sql = 'UPDATE ' . MODULES_TABLE . "\n\t\t\tSET right_id = right_id - {$diff}\n\t\t\tWHERE module_class = '" . $db->sql_escape($this->module_class) . "'\n\t\t\t\tAND left_id < " . (int) $from_data['right_id'] . '
AND right_id > ' . (int) $from_data['right_id'];
$db->sql_query($sql);
// Resync righthand side of tree
$sql = 'UPDATE ' . MODULES_TABLE . "\n\t\t\tSET left_id = left_id - {$diff}, right_id = right_id - {$diff}\n\t\t\tWHERE module_class = '" . $db->sql_escape($this->module_class) . "'\n\t\t\t\tAND left_id > " . (int) $from_data['right_id'];
$db->sql_query($sql);
if ($to_parent_id > 0) {
$to_data = $this->get_module_row($to_parent_id);
// Resync new parents
$sql = 'UPDATE ' . MODULES_TABLE . "\n\t\t\t\tSET right_id = right_id + {$diff}\n\t\t\t\tWHERE module_class = '" . $db->sql_escape($this->module_class) . "'\n\t\t\t\t\tAND " . (int) $to_data['right_id'] . ' BETWEEN left_id AND right_id
AND ' . $db->sql_in_set('module_id', $moved_ids, true);
$db->sql_query($sql);
// Resync the righthand side of the tree
$sql = 'UPDATE ' . MODULES_TABLE . "\n\t\t\t\tSET left_id = left_id + {$diff}, right_id = right_id + {$diff}\n\t\t\t\tWHERE module_class = '" . $db->sql_escape($this->module_class) . "'\n\t\t\t\t\tAND left_id > " . (int) $to_data['right_id'] . '
AND ' . $db->sql_in_set('module_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 ' . MODULES_TABLE . "\n\t\t\t\tWHERE module_class = '" . $db->sql_escape($this->module_class) . "'\n\t\t\t\t\tAND " . $db->sql_in_set('module_id', $moved_ids, true);
$result = $db->sql_query($sql);
$row = $db->sql_fetchrow($result);
$db->sql_freeresult($result);
$diff = '+ ' . (int) ($row['right_id'] - $from_data['left_id'] + 1);
}
$sql = 'UPDATE ' . MODULES_TABLE . "\n\t\t\tSET left_id = left_id {$diff}, right_id = right_id {$diff}\n\t\t\tWHERE module_class = '" . $db->sql_escape($this->module_class) . "'\n\t\t\t\tAND " . $db->sql_in_set('module_id', $moved_ids);
$db->sql_query($sql);
}