<?php

/**
 * Handles topic muting for users.
 *
 * @package TopicMute
 * @author snow flurry https://f9.hell-labs.net
 * @copyright 2024 Hell Labs
 * @license BSD-2-Clause
 *
 * @version 1.0
 */

if (!defined('SMF'))
    die('No direct access...');

/**
 * Tells if the topic has been muted by the user.
 *
 * @param int $topic The topic ID to check
 * @param int $user The user ID to check settings for, or -1 to get the current user
 * @return boolean True if muted, False otherwise.
 */
function topic_is_muted($topic, $user = -1)
{
    global $context, $smcFunc, $user_info;
    if ($user_info['is_guest']) {
        return false;
    }

    if ($user == -1) {
        $user = $user_info['id'];
    }
    
    $request = $smcFunc['db_query']('', '
        SELECT COUNT(*)
        FROM {db_prefix}log_mute_topic
        WHERE
            id_topic = {int:topic}
            AND id_member = {int:user}',
        array(
            'topic' => $topic,
            'user' => $user,
        )
    );
    $row = $smcFunc['db_fetch_row']($request);
    $smcFunc['db_free_result']($request);
    return ($row[0] > 0);
}

/**
 * Integration hook to hide muted topics on board index.
 */
function hide_muted_topics(&$selects, &$tables, &$params, &$wheres, &$topic_ids, &$topic_wheres)
{
    global $user_info, $options;

    // Guests can't mute threads
    if (!$user_info['is_guest']) {
        $tables[] = 'LEFT JOIN {db_prefix}log_mute_topic AS lmt '
            . 'ON (lmt.id_topic = t.id_topic AND lmt.id_member = {int:current_member})';
        if (empty($options['topic_mute_roadblock'])) {
            $selects[] = '(lmt.id_topic IS NOT NULL) AS topic_muted';
        } else {
            $wheres[] = 'lmt.id_topic IS NULL';
        }
    }
}

/**
 * Integration hook to add the (un)mute button to topic pages.
 */
function add_mute_button(&$buttons) {
    global $scripturl, $context, $user_info;

    loadLanguage('TopicMute');

    if (!$user_info['is_guest']) {
        $btn_text = topic_is_muted($context['current_topic']) ? 'unmute_topic' : 'mute_topic';
        $buttons['mute_topic'] = array(
            'text' => $btn_text,
            'url' => $scripturl . '?action=topicmute;topic=' . $context['current_topic'] . ';'
                . $context['session_var'] . '=' . $context['session_id'],
        );
    }
}

/**
 * Integration hook to add the 'topicmute' action.
 */
function add_topicmute_action(&$actions)
{
    $actions['topicmute'] = array(
        'TopicMute.php',
        'TopicMute',
    );
}

/**
 * Integration hook to show the "How to present muted threads" option in the theme.
 */
function mute_theme_option()
{
    global $context, $txt;

    loadLanguage('TopicMute');

    $posting_idx = 0;
    foreach ($context['theme_options'] as $key) {
        $posting_idx++;
        if ($key == $txt['theme_opt_posting']) {
            break;
        }
    }

    $option = array(
        array(
            'id' => 'topic_mute_roadblock',
            'label' => $txt['topic_mute_roadblock'],
            'default' => false,
        ),
    );

    $context['theme_options'] = array_merge(
        array_slice($context['theme_options'], 0, $posting_idx, true),
        $option,
        array_slice($context['theme_options'], $posting_idx, null, true),
    );
}

function add_topic_mute_js() {
    loadJavaScriptFile('topic-mute.js', array('minimize' => true), 'topic_mute_ajax');
}

?>
