<?php

/**
 * Toggle the mute status of a topic. Must be called with a topic specified in the URL.
 *
 * Accessed via ?action=topicmute.
 */
function TopicMute()
{
    global $smcFunc, $user_info, $topic, $scripturl, $context;

    is_not_guest();
    checkSession('get');

    if (empty($topic))
        fatal_lang_error('not_a_topic', false);


    if (topic_is_muted($topic)) {
        // Topic is muted, unmute it
        $smcFunc['db_query']('', '
            DELETE FROM {db_prefix}log_mute_topic
            WHERE id_member = {int:current_user}
                AND id_topic = {int:current_topic}',
            array(
                'current_user' => $user_info['id'],
                'current_topic' => $topic,
            )
        );
    } else {
        // Topic isn't muted, so mute it
        $smcFunc['db_insert']('ignore',
            '{db_prefix}log_mute_topic',
            array('id_member' => 'int', 'id_topic' => 'int'),
            array(
                $user_info['id'],
                $topic,
            ),
            array('id_member')
        );
    }

    // Handle AJAX requests
    if (isset($_GET['xml']))
    {
        loadTemplate('Xml');
        $context['xml_data']['errors'] = array(
            'identifier' => 'error',
            'children' => array(
                array(
                    'value' => 0,
                ),
            ),
        );
        $context['sub_template'] = 'generic_xml';
        // TODO: is this the best way to do it?
        obExit(false, false);
    } else {
        redirectexit('topic=' . $topic . '.' . $_REQUEST['start']);
    }
}

?>
