function delete_forum($forum_id, $action_posts = 'delete', $action_subforums = 'delete', $posts_to_id = 0, $subforums_to_id = 0)
{
    global $_CLASS;
    $row = get_forum_info($forum_id);
    extract($row);
    $errors = array();
    $log_action_posts = $log_action_forums = '';
    if ($action_posts == 'delete') {
        $_CLASS['core_db']->query('UPDATE ' . FORUMS_FORUMS_TABLE . ' SET forum_status = ' . ITEM_DELETING . ' WHERE forum_id = ' . $forum_id);
        $log_action_posts = 'POSTS';
        if ($delete_error = delete_forum_content($forum_id)) {
            $errors[] = $delete_error;
        }
    } elseif ($action_posts == 'move') {
        if (!$posts_to_id) {
            $errors[] = $_CLASS['core_user']->lang['NO_DESTINATION_FORUM'];
        } else {
            $log_action_posts = 'MOVE_POSTS';
            $sql = 'SELECT forum_name 
				FROM ' . FORUMS_FORUMS_TABLE . '
				WHERE forum_id = ' . $posts_to_id;
            $result = $_CLASS['core_db']->query($sql);
            if (!($row = $_CLASS['core_db']->fetch_row_assoc($result))) {
                $errors[] = $_CLASS['core_user']->lang['NO_FORUM'];
            } else {
                $posts_to_name = $row['forum_name'];
                unset($row);
                $errors[] = move_forum_content($forum_id, $subforums_to_id);
            }
        }
    }
    if (count($errors)) {
        return $errors;
    }
    if ($action_subforums == 'delete') {
        $log_action_forums = 'FORUMS';
        $forum_ids = array($forum_id);
        $rows = get_forum_branch($forum_id, 'children', 'descending', FALSE);
        // Maybe add feild to the get_forum_branch
        foreach ($rows as $row) {
            $forum_ids[] = $row['forum_id'];
        }
        unset($rows);
        $_CLASS['core_db']->query('UPDATE ' . FORUMS_FORUMS_TABLE . '  SET forum_status = ' . ITEM_DELETING . ' WHERE forum_id  IN (' . implode(', ', $forum_ids) . ')');
        foreach ($forum_ids as $forum_id) {
            if ($delete_error = delete_forum_content($forum_id)) {
                $errors[] = $delete_error;
            }
        }
        if (count($errors)) {
            return $errors;
        }
        $diff = count($forum_ids) * 2;
        $sql = 'DELETE FROM ' . FORUMS_FORUMS_TABLE . '
			WHERE forum_id IN (' . implode(', ', $forum_ids) . ')';
        $_CLASS['core_db']->query($sql);
    } elseif ($action_subforums == 'move') {
        if (!$subforums_to_id) {
            $errors[] = $_CLASS['core_user']->lang['NO_DESTINATION_FORUM'];
        } else {
            $log_action_forums = 'MOVE_FORUMS';
            $sql = 'SELECT forum_name 
				FROM ' . FORUMS_FORUMS_TABLE . '
				WHERE forum_id = ' . $subforums_to_id;
            $result = $_CLASS['core_db']->query($sql);
            if (!($row = $_CLASS['core_db']->fetch_row_assoc($result))) {
                $errors[] = $_CLASS['core_user']->lang['NO_FORUM'];
            } else {
                $subforums_to_name = $row['forum_name'];
                unset($row);
                $sql = 'SELECT forum_id
					FROM ' . FORUMS_FORUMS_TABLE . "\n\t\t\t\t\tWHERE parent_id = {$forum_id}";
                $result = $_CLASS['core_db']->query($sql);
                while ($row = $_CLASS['core_db']->fetch_row_assoc($result)) {
                    move_forum($row['forum_id'], intval($_POST['subforums_to_id']));
                }
                $_CLASS['core_db']->free_result($result);
                $sql = 'UPDATE ' . FORUMS_FORUMS_TABLE . "\n\t\t\t\t\tSET parent_id = {$subforums_to_id}\n\t\t\t\t\tWHERE parent_id = {$forum_id}";
                $_CLASS['core_db']->query($sql);
                $diff = 2;
                $sql = 'DELETE FROM ' . FORUMS_FORUMS_TABLE . "\n\t\t\t\t\tWHERE forum_id = {$forum_id}";
                $_CLASS['core_db']->query($sql);
            }
        }
        if (count($errors)) {
            return $errors;
        }
    } else {
        $diff = 2;
        $sql = 'DELETE FROM ' . FORUMS_FORUMS_TABLE . "\n\t\t\tWHERE forum_id = {$forum_id}";
        $_CLASS['core_db']->query($sql);
    }
    // Resync tree
    $sql = 'UPDATE ' . FORUMS_FORUMS_TABLE . "\n\t\tSET right_id = right_id - {$diff}\n\t\tWHERE left_id < {$right_id} AND right_id > {$right_id}";
    $_CLASS['core_db']->query($sql);
    $sql = 'UPDATE ' . FORUMS_FORUMS_TABLE . "\n\t\tSET left_id = left_id - {$diff}, right_id = right_id - {$diff}\n\t\tWHERE left_id > {$right_id}";
    $_CLASS['core_db']->query($sql);
    if (!is_array($forum_ids)) {
        $forum_ids = array($forum_id);
    }
    // Delete forum ids from extension groups table
    $sql = 'SELECT group_id, allowed_forums 
		FROM ' . EXTENSION_GROUPS_TABLE . "\n\t\tWHERE allowed_forums <> ''";
    $result = $_CLASS['core_db']->query($sql);
    while ($row = $_CLASS['core_db']->fetch_row_assoc($result)) {
        $allowed_forums = unserialize(trim($row['allowed_forums']));
        $allowed_forums = array_diff($allowed_forums, $forum_ids);
        $sql = 'UPDATE ' . EXTENSION_GROUPS_TABLE . " \n\t\t\tSET allowed_forums = '" . (sizeof($allowed_forums) ? serialize($allowed_forums) : '') . "'\n\t\t\tWHERE group_id = {$row['group_id']}";
        $_CLASS['core_db']->query($sql);
    }
    $_CLASS['core_cache']->destroy('extensions');
    $log_action = implode('_', array($log_action_posts, $log_action_forums));
    switch ($log_action) {
        case 'MOVE_POSTS_MOVE_FORUMS':
            add_log('admin', 'LOG_FORUM_DEL_MOVE_POSTS_MOVE_FORUMS', $posts_to_name, $subforums_to_name, $forum_name);
            break;
        case 'MOVE_POSTS_FORUMS':
            add_log('admin', 'LOG_FORUM_DEL_MOVE_POSTS_FORUMS', $posts_to_name, $forum_name);
            break;
        case 'POSTS_MOVE_FORUMS':
            add_log('admin', 'LOG_FORUM_DEL_POSTS_MOVE_FORUMS', $subforums_to_name, $forum_name);
            break;
        case '_MOVE_FORUMS':
            add_log('admin', 'LOG_FORUM_DEL_MOVE_FORUMS', $subforums_to_name, $forum_name);
            break;
        case 'MOVE_POSTS_':
            add_log('admin', 'LOG_FORUM_DEL_MOVE_POSTS', $posts_to_name, $forum_name);
            break;
        case 'POSTS_FORUMS':
            add_log('admin', 'LOG_FORUM_DEL_POSTS_FORUMS', $forum_name);
            break;
        case '_FORUMS':
            add_log('admin', 'LOG_FORUM_DEL_FORUMS', $forum_name);
            break;
        case 'POSTS_':
            add_log('admin', 'LOG_FORUM_DEL_POSTS', $forum_name);
            break;
    }
    return $errors;
}
/**
* Remove complete forum
*/
function delete_forum($forum_id, $action_posts = 'delete', $action_subforums = 'delete', $posts_to_id = 0, $subforums_to_id = 0)
{
    global $_CLASS;
    $forum_data = get_forum_info($forum_id);
    $errors = array();
    $log_action_posts = $log_action_forums = $posts_to_name = $subforums_to_name = '';
    $forum_ids = array($forum_id);
    if ($action_posts == 'delete') {
        $log_action_posts = 'POSTS';
        $errors = array_merge($errors, delete_forum_content($forum_id));
    } else {
        if ($action_posts == 'move') {
            if (!$posts_to_id) {
                $errors[] = $_CLASS['core_user']->lang['NO_DESTINATION_FORUM'];
            } else {
                $log_action_posts = 'MOVE_POSTS';
                $sql = 'SELECT forum_name 
				FROM ' . FORUMS_FORUMS_TABLE . '
				WHERE forum_id = ' . $posts_to_id;
                $result = $_CLASS['core_db']->query($sql);
                $row = $_CLASS['core_db']->fetch_row_assoc($result);
                $_CLASS['core_db']->free_result($result);
                if (!$row) {
                    $errors[] = $_CLASS['core_user']->lang['NO_FORUM'];
                } else {
                    $posts_to_name = $row['forum_name'];
                    $errors = array_merge($errors, move_forum_content($forum_id, $posts_to_id));
                }
            }
        }
    }
    if (sizeof($errors)) {
        return $errors;
    }
    if ($action_subforums === 'delete') {
        $log_action_forums = 'FORUMS';
        $rows = get_forum_branch($forum_id, 'children', 'descending', false);
        foreach ($rows as $row) {
            $forum_ids[] = $row['forum_id'];
            $errors = array_merge($errors, delete_forum_content($row['forum_id']));
        }
        if (sizeof($errors)) {
            return $errors;
        }
        $diff = sizeof($forum_ids) * 2;
        $sql = 'DELETE FROM ' . FORUMS_FORUMS_TABLE . '
			WHERE forum_id IN (' . implode(', ', $forum_ids) . ')';
        $_CLASS['core_db']->query($sql);
        $sql = 'DELETE FROM ' . FORUMS_ACL_TABLE . '
			WHERE  forum_id IN (' . implode(', ', $forum_ids) . ')';
        $_CLASS['core_db']->query($sql);
    } else {
        if ($action_subforums === 'move') {
            if (!$subforums_to_id) {
                $errors[] = $_CLASS['core_user']->lang['NO_DESTINATION_FORUM'];
            } else {
                $log_action_forums = 'MOVE_FORUMS';
                $sql = 'SELECT forum_name 
				FROM ' . FORUMS_FORUMS_TABLE . '
				WHERE forum_id = ' . $subforums_to_id;
                $result = $_CLASS['core_db']->query($sql);
                $row = $_CLASS['core_db']->fetch_row_assoc($result);
                $_CLASS['core_db']->free_result($result);
                if (!$row) {
                    $errors[] = $_CLASS['core_user']->lang['NO_FORUM'];
                } else {
                    $subforums_to_name = $row['forum_name'];
                    $sql = 'SELECT forum_id
					FROM ' . FORUMS_FORUMS_TABLE . "\r\n\t\t\t\t\tWHERE parent_id = {$forum_id}";
                    $result = $_CLASS['core_db']->query($sql);
                    while ($row = $_CLASS['core_db']->fetch_row_assoc($result)) {
                        move_forum($row['forum_id'], $subforums_to_id);
                    }
                    $_CLASS['core_db']->free_result($result);
                    $sql = 'UPDATE ' . FORUMS_FORUMS_TABLE . "\r\n\t\t\t\t\tSET parent_id = {$subforums_to_id}\r\n\t\t\t\t\tWHERE parent_id = {$forum_id}";
                    $_CLASS['core_db']->query($sql);
                    $diff = 2;
                    $sql = 'DELETE FROM ' . FORUMS_FORUMS_TABLE . "\r\n\t\t\t\t\tWHERE forum_id = {$forum_id}";
                    $_CLASS['core_db']->query($sql);
                    $sql = 'DELETE FROM ' . FORUMS_ACL_TABLE . '
					WHERE forum_id = ' . $forum_id;
                    $_CLASS['core_db']->query($sql);
                }
            }
            if (sizeof($errors)) {
                return $errors;
            }
        } else {
            $diff = 2;
            $sql = 'DELETE FROM ' . FORUMS_FORUMS_TABLE . "\r\n\t\t\tWHERE forum_id = {$forum_id}";
            $_CLASS['core_db']->query($sql);
            $sql = 'DELETE FROM ' . FORUMS_ACL_TABLE . '
			WHERE forum_id = ' . $forum_id;
            $_CLASS['core_db']->query($sql);
        }
    }
    // Resync tree
    $sql = 'UPDATE ' . FORUMS_FORUMS_TABLE . "\r\n\t\tSET right_id = right_id - {$diff}\r\n\t\tWHERE left_id < {$forum_data['right_id']} AND right_id > {$forum_data['right_id']}";
    $_CLASS['core_db']->query($sql);
    $sql = 'UPDATE ' . FORUMS_FORUMS_TABLE . "\r\n\t\tSET left_id = left_id - {$diff}, right_id = right_id - {$diff}\r\n\t\tWHERE left_id > {$forum_data['right_id']}";
    $_CLASS['core_db']->query($sql);
    // Delete forum ids from extension groups table
    $sql = 'SELECT group_id, allowed_forums 
		FROM ' . FORUMS_EXTENSION_GROUPS_TABLE;
    $result = $_CLASS['core_db']->query($sql);
    while ($row = $_CLASS['core_db']->fetch_row_assoc($result)) {
        if (!$row['allowed_forums']) {
            continue;
        }
        $allowed_forums = unserialize(trim($row['allowed_forums']));
        $allowed_forums = array_diff($allowed_forums, $forum_ids);
        $sql = 'UPDATE ' . FORUMS_EXTENSION_GROUPS_TABLE . " \r\n\t\t\tSET allowed_forums = '" . (sizeof($allowed_forums) ? serialize($allowed_forums) : '') . "'\r\n\t\t\tWHERE group_id = {$row['group_id']}";
        $_CLASS['core_db']->query($sql);
    }
    $_CLASS['core_db']->free_result($result);
    $_CLASS['core_cache']->destroy('_extensions');
    $log_action = implode('_', array($log_action_posts, $log_action_forums));
    switch ($log_action) {
        case 'MOVE_POSTS_MOVE_FORUMS':
            add_log('admin', 'LOG_FORUM_DEL_MOVE_POSTS_MOVE_FORUMS', $posts_to_name, $subforums_to_name, $forum_data['forum_name']);
            break;
        case 'MOVE_POSTS_FORUMS':
            add_log('admin', 'LOG_FORUM_DEL_MOVE_POSTS_FORUMS', $posts_to_name, $forum_data['forum_name']);
            break;
        case 'POSTS_MOVE_FORUMS':
            add_log('admin', 'LOG_FORUM_DEL_POSTS_MOVE_FORUMS', $subforums_to_name, $forum_data['forum_name']);
            break;
        case '_MOVE_FORUMS':
            add_log('admin', 'LOG_FORUM_DEL_MOVE_FORUMS', $subforums_to_name, $forum_data['forum_name']);
            break;
        case 'MOVE_POSTS_':
            add_log('admin', 'LOG_FORUM_DEL_MOVE_POSTS', $posts_to_name, $forum_data['forum_name']);
            break;
        case 'POSTS_FORUMS':
            add_log('admin', 'LOG_FORUM_DEL_POSTS_FORUMS', $forum_data['forum_name']);
            break;
        case '_FORUMS':
            add_log('admin', 'LOG_FORUM_DEL_FORUMS', $forum_data['forum_name']);
            break;
        case 'POSTS_':
            add_log('admin', 'LOG_FORUM_DEL_POSTS', $forum_data['forum_name']);
            break;
        default:
            add_log('admin', 'LOG_FORUM_DEL_FORUM', $forum_data['forum_name']);
            break;
    }
    return $errors;
}