/**
 * Delete a topic.
 *
 * @param  AUTO_LINK  The ID of the topic to delete.
 * @param  LONG_TEXT  The reason for this action .
 * @param  ?AUTO_LINK Where topic to move posts in this topic to (NULL: delete the posts).
 * @return AUTO_LINK  The forum ID the topic is in (could be found without calling the function, but as we've looked it up, it is worth keeping).
 */
function ocf_delete_topic($topic_id, $reason = '', $post_target_topic_id = NULL)
{
    // Info about source
    $info = $GLOBALS['FORUM_DB']->query_select('f_topics', array('t_pt_to', 't_pt_from', 't_cache_first_title', 't_cache_first_member_id', 't_poll_id', 't_forum_id', 't_cache_num_posts', 't_validated'), array('id' => $topic_id));
    if (!array_key_exists(0, $info)) {
        warn_exit(do_lang_tempcode('MISSING_RESOURCE'));
    }
    $name = $info[0]['t_cache_first_title'];
    $poll_id = $info[0]['t_poll_id'];
    $forum_id = $info[0]['t_forum_id'];
    $num_posts = $info[0]['t_cache_num_posts'];
    $validated = $info[0]['t_validated'];
    if (!ocf_may_delete_topics_by($forum_id, get_member(), $info[0]['t_cache_first_member_id']) || !is_null($info[0]['t_pt_from']) && $info[0]['t_pt_from'] != get_member() && (!is_null($info[0]['t_pt_to']) && $info[0]['t_pt_to'] != get_member()) && !ocf_has_special_pt_access($topic_id) && !has_specific_permission(get_member(), 'view_other_pt') && is_null($forum_id)) {
        access_denied('I_ERROR');
    }
    if (!is_null($post_target_topic_id)) {
        $to = $GLOBALS['FORUM_DB']->query_value_null_ok('f_topics', 't_forum_id', array('id' => $post_target_topic_id));
        if (is_null($to)) {
            warn_exit(do_lang_tempcode('MISSING_RESOURCE'));
        }
    }
    if (!is_null($forum_id)) {
        // Update member post counts if we've switched between post-count countable forums
        $post_count_info = $GLOBALS['FORUM_DB']->query('SELECT id,f_post_count_increment FROM ' . $GLOBALS['FORUM_DB']->get_table_prefix() . 'f_forums WHERE id=' . strval((int) $forum_id) . (!is_null($post_target_topic_id) ? ' OR id=' . strval((int) $to) : ''), 2);
        if ($post_count_info[0]['id'] == $forum_id) {
            $from_cnt = $post_count_info[0]['f_post_count_increment'];
            $to_cnt = array_key_exists(1, $post_count_info) ? $post_count_info[1]['f_post_count_increment'] : 0;
        } else {
            $from_cnt = $post_count_info[1]['f_post_count_increment'];
            $to_cnt = $post_count_info[0]['f_post_count_increment'];
        }
        require_code('ocf_posts_action');
        if ($from_cnt != $to_cnt) {
            $sql = 'SELECT p_poster FROM ' . $GLOBALS['FORUM_DB']->get_table_prefix() . 'f_posts WHERE p_topic_id=' . strval((int) $topic_id);
            if (addon_installed('unvalidated')) {
                $sql .= ' AND p_validated=1';
            }
            $_member_post_counts = collapse_1d_complexity('p_poster', $GLOBALS['FORUM_DB']->query($sql));
            $member_post_counts = array_count_values($_member_post_counts);
            foreach ($member_post_counts as $member_id => $member_post_count) {
                if ($to_cnt == 0) {
                    $member_post_count = -$member_post_count;
                }
                ocf_force_update_member_post_count($member_id, $member_post_count);
            }
        }
    }
    // What to do with our posts
    if (!is_null($post_target_topic_id)) {
        $GLOBALS['FORUM_DB']->query_update('f_posts', array('p_cache_forum_id' => $to, 'p_topic_id' => $post_target_topic_id), array('p_topic_id' => $topic_id));
        require_code('ocf_posts_action2');
        ocf_force_update_topic_cacheing($post_target_topic_id);
        if (!is_null($forum_id)) {
            ocf_force_update_forum_cacheing($forum_id, $to, 1, $num_posts);
        }
    } else {
        $_postdetails = $GLOBALS['FORUM_DB']->query('SELECT p_post FROM ' . $GLOBALS['FORUM_DB']->get_table_prefix() . 'f_posts WHERE p_topic_id=' . strval((int) $topic_id));
        foreach ($_postdetails as $post) {
            delete_lang($post['p_post'], $GLOBALS['FORUM_DB']);
        }
        $GLOBALS['FORUM_DB']->query_delete('f_posts', array('p_topic_id' => $topic_id));
    }
    // Delete stuff
    if (!is_null($poll_id)) {
        require_code('ocf_polls_action');
        require_code('ocf_polls_action2');
        ocf_delete_poll($poll_id, '');
    }
    $GLOBALS['FORUM_DB']->query_delete('f_topics', array('id' => $topic_id), '', 1);
    $GLOBALS['FORUM_DB']->query_delete('f_read_logs', array('l_topic_id' => $topic_id));
    require_code('notifications');
    delete_all_notifications_on('ocf_topics', strval($topic_id));
    // Delete the ticket row if it's a ticket
    if (addon_installed('tickets')) {
        require_code('tickets');
        if (!is_null($forum_id) && is_ticket_forum($forum_id)) {
            require_code('tickets2');
            delete_ticket_by_topic_id($topic_id);
        }
    }
    // Update forum view cacheing
    if (!is_null($forum_id)) {
        require_code('ocf_posts_action2');
        ocf_force_update_forum_cacheing($forum_id, $validated == 0 ? 0 : -1, -$num_posts);
    }
    require_code('ocf_general_action2');
    ocf_mod_log_it('DELETE_TOPIC', strval($topic_id), $name, $reason);
    if (!is_null($forum_id)) {
        require_code('ocf_posts_action');
        ocf_decache_ocp_blocks($forum_id);
    } else {
        decache('side_ocf_personal_topics');
        decache('_new_pp');
    }
    return $forum_id;
}
/**
 * Delete a forum.
 *
 * @param  AUTO_LINK		The ID of the forum we are deleting.
 * @param  AUTO_LINK		The ID of the forum that topics will be moved to.
 * @param  BINARY			Whether to delete topics instead of moving them to the target forum.
 */
function ocf_delete_forum($forum_id, $target_forum_id, $delete_topics = 0)
{
    if ($forum_id == db_get_first_id()) {
        warn_exit(do_lang_tempcode('CANNOT_DELETE_ROOT_FORUM'));
    }
    require_code('ocf_topics_action');
    require_code('ocf_topics_action2');
    if ($delete_topics == 0) {
        ocf_move_topics($forum_id, $target_forum_id);
    } else {
        $rows = $GLOBALS['FORUM_DB']->query_select('f_topics', array('id'), array('t_forum_id' => $forum_id));
        foreach ($rows as $row) {
            ocf_delete_topic($row['id'], '');
        }
    }
    $forum_info = $GLOBALS['FORUM_DB']->query_select('f_forums', array('*'), array('id' => $forum_id), '', 1);
    if (!array_key_exists(0, $forum_info)) {
        warn_exit(do_lang_tempcode('MISSING_RESOURCE'));
    }
    delete_lang($forum_info[0]['f_description'], $GLOBALS['FORUM_DB']);
    delete_lang($forum_info[0]['f_intro_question'], $GLOBALS['FORUM_DB']);
    $name = $GLOBALS['FORUM_DB']->query_value('f_forums', 'f_name', array('id' => $forum_id));
    $GLOBALS['FORUM_DB']->query_update('f_multi_moderations', array('mm_move_to' => NULL), array('mm_move_to' => $forum_id));
    $GLOBALS['FORUM_DB']->query_update('f_forums', array('f_parent_forum' => db_get_first_id()), array('f_parent_forum' => $forum_id));
    $GLOBALS['FORUM_DB']->query_delete('f_forums', array('id' => $forum_id), '', 1);
    $GLOBALS['FORUM_DB']->query_delete('group_category_access', array('module_the_name' => 'forums', 'category_name' => strval($forum_id)));
    $GLOBALS['FORUM_DB']->query_delete('gsp', array('module_the_name' => 'forums', 'category_name' => strval($forum_id)));
    require_code('notifications');
    delete_all_notifications_on('ocf_topic', 'forum:' . strval($forum_id));
    $GLOBALS['FORUM_DB']->query_delete('f_forum_intro_member', array('i_forum_id' => $forum_id));
    $GLOBALS['FORUM_DB']->query_delete('f_forum_intro_ip', array('i_forum_id' => $forum_id));
    log_it('DELETE_FORUM', strval($forum_id), $name);
}