<?php /*

 Composr
 Copyright (c) Christopher Graham, 2004-2024

 See docs/LICENSE.md for full licensing information.


 NOTE TO PROGRAMMERS:
   Do not edit this file. If you need to make changes, save your changed file to the appropriate *_custom folder
   **** If you ignore this advice, then your website upgrades (e.g. for bug fixes) will likely kill your changes ****

*/

/**
 * @license    http://opensource.org/licenses/cpal_1.0 Common Public Attribution License
 * @copyright  Christopher Graham
 * @package    cns_forum
 */

/**
 * Block class.
 */
class Block_main_cns_involved_topics
{
    /**
     * Find details of the block.
     *
     * @return ?array Map of block info (null: block is disabled)
     */
    public function info() : ?array
    {
        if (get_forum_type() != 'cns') {
            return null;
        }

        $info = [];
        $info['author'] = 'Chris Graham';
        $info['organisation'] = 'Composr';
        $info['hacked_by'] = null;
        $info['hack_version'] = null;
        $info['version'] = 2;
        $info['locked'] = false;
        $info['min_cms_version'] = 11.0;
        $info['addon'] = 'cns_forum';
        $info['parameters'] = ['member_id', 'max', 'start', 'private_topics', 'check'];
        return $info;
    }

    /**
     * Execute the block.
     *
     * @param  array $map A map of parameters
     * @return Tempcode The result of execution
     */
    public function run(array $map) : object
    {
        $error_msg = new Tempcode();
        if (!addon_installed__messaged('cns_forum', $error_msg)) {
            return $error_msg;
        }

        if (get_forum_type() != 'cns') {
            return do_template('RED_ALERT', ['_GUID' => '3wdm0cx063l7qs0mtk5qhq1kwczp8q5e', 'TEXT' => do_lang_tempcode('NO_CNS')]);
        }

        $block_id = get_block_id($map);

        $check_perms = !array_key_exists('check', $map) || ($map['check'] == '1');
        $private_topics = array_key_exists('private_topics', $map) && ($map['private_topics'] == '1');
        $member_id_of = array_key_exists('member_id', $map) ? intval($map['member_id']) : get_member();
        $max = get_param_integer($block_id . '_max', array_key_exists('max', $map) ? intval($map['max']) : 10);
        $start = get_param_integer($block_id . '_start', array_key_exists('start', $map) ? intval($map['start']) : 0);

        require_code('cns_topics');
        require_code('cns_general');
        require_lang('cns');
        require_code('cns_forumview');

        $topics = new Tempcode();
        $where_more = '';

        /*
        Actually including this just slows down the COUNT part of the query due to lack of indexability
        if ($forum1 !== null) {
            $where_more .= ' AND p_cache_forum_id<>' . strval($forum1);
        }
        if ($forum2 !== null) {
            $where_more .= ' AND p_cache_forum_id<>' . strval($forum2);
        }

        $forum1 = null;//$GLOBALS['FORUM_DRIVER']->forum_id_from_name(get_option('comments_forum_name'));
        $forum2 = null;
        if (addon_installed('tickets')) {
            $tf = get_option('ticket_forum_name', true);
            if ($tf !== null) {
                $forum2 = $GLOBALS['FORUM_DRIVER']->forum_id_from_name($tf);
            }
        }
        */

        $sql = 'SELECT DISTINCT p_topic_id,p_time FROM ' . $GLOBALS['FORUM_DB']->get_table_prefix() . 'f_posts WHERE p_posting_member=' . strval($member_id_of) . $where_more;
        if (!$private_topics) {
            $sql .= ' AND p_cache_forum_id IS NOT NULL';
        }
        $sql .= ' ORDER BY p_time DESC';
        $rows = $GLOBALS['FORUM_DB']->query($sql, $max, $start, false, true);
        if (!empty($rows)) {
            if (get_bot_type() === null) {
                $sql_max_rows = 'SELECT COUNT(DISTINCT p_topic_id) FROM ' . $GLOBALS['FORUM_DB']->get_table_prefix() . 'f_posts WHERE p_posting_member=' . strval($member_id_of) . $where_more;
                if (!$private_topics) {
                    $sql_max_rows .= ' AND p_cache_forum_id IS NOT NULL';
                }
                $max_rows = $GLOBALS['FORUM_DB']->query_value_if_there($sql_max_rows, false, true);
            } else {
                $max_rows = count($rows); // We don't want bots hogging resources on somewhere they don't need to dig into
            }

            $moderator_actions = ''; // XHTMLXHTML
            $has_topic_marking = has_delete_permission('mid', get_member(), $member_id_of, 'topics');
            if ($has_topic_marking) {
                $moderator_actions .= '<option value="delete_topics_and_posts">' . do_lang('DELETE_TOPICS_AND_POSTS') . '</option>';
            }

            $extra_join_sql = '';
            $where_sup = '';
            if ((!$GLOBALS['FORUM_DRIVER']->is_super_admin(get_member())) && ($check_perms)) {
                $where_sup .= get_category_permission_where_clause('forums', 't_forum_id', get_member(), get_permission_where_clause_groups(get_member()), 't');
            }

            $where = '';
            foreach ($rows as $row) {
                if ($where != '') {
                    $where .= ' OR ';
                }
                $where .= 't.id=' . strval($row['p_topic_id']);
            }
            $query = 'SELECT t.*,l_time';
            if (multi_lang_content()) {
                $query .= ',t_cache_first_post AS p_post';
            } else {
                $query .= ',p_post,p_post__text_parsed,p_post__source_user';
            }
            $query .= ' FROM ' . $GLOBALS['FORUM_DB']->get_table_prefix() . 'f_topics t LEFT JOIN ' . $GLOBALS['FORUM_DB']->get_table_prefix() . 'f_read_logs l ON t.id=l.l_topic_id AND l.l_member_id=' . strval(get_member());
            if (!multi_lang_content()) {
                $query .= ' LEFT JOIN ' . $GLOBALS['FORUM_DB']->get_table_prefix() . 'f_posts p2 ON p2.id=t.t_cache_first_post_id';
            }
            $query .= $extra_join_sql;
            $query .= ' WHERE ' . $where . $where_sup;
            if (multi_lang_content()) {
                $topic_rows = $GLOBALS['FORUM_DB']->query($query, null, 0, false, true, ['t_cache_first_post' => 'LONG_TRANS__COMCODE']);
            } else {
                $topic_rows = $GLOBALS['FORUM_DB']->query($query, null, 0, false, true);
            }
            $topic_rows_map = [];
            foreach ($topic_rows as $topic_row) {
                if (cns_may_access_topic($topic_row['id'], get_member(), $topic_row)) {
                    $topic_rows_map[$topic_row['id']] = $topic_row;
                }
            }
            $hot_topic_definition = intval(get_option('hot_topic_definition'));
            foreach ($rows as $row) {
                if (array_key_exists($row['p_topic_id'], $topic_rows_map)) {
                    $topics->attach(cns_render_topic(cns_get_topic_array($topic_rows_map[$row['p_topic_id']], get_member(), $hot_topic_definition, true), $has_topic_marking));
                }
            }
            if (!$topics->is_empty()) {
                $action_url = build_url(['page' => 'topics'], get_module_zone('topics'), [], false, true);

                $forum_name = do_lang_tempcode('TOPICS_PARTICIPATED_IN', escape_html(integer_format($start + 1)) . '-' . integer_format($start + $max));
                $breadcrumbs = new Tempcode();
                require_code('templates_pagination');
                $pagination = pagination(do_lang_tempcode('FORUM_TOPICS'), $start, $block_id . '_start', $max, $block_id . '_max', $max_rows, false, null, null);
                $topics = do_template('CNS_FORUM_TOPIC_WRAPPER', [
                    '_GUID' => '8723270b128b4eea47ab3c756b342e14',
                    'TYPE' => 'main_cns_involved_topics',
                    'SORT' => '',
                    'MAX' => '15',
                    'MAY_CHANGE_MAX' => false,
                    'BREADCRUMBS' => $breadcrumbs,
                    'ACTION_URL' => $action_url,
                    'BUTTONS' => '',
                    'STARTER_TITLE' => '',
                    'FORUM_NAME' => $forum_name,
                    'TOPICS' => $topics,
                    'PAGINATION' => $pagination,
                    'MODERATOR_ACTIONS' => $moderator_actions,
                    'ID' => null,
                ]);
            }
        }

        return do_template('BLOCK_MAIN_CNS_INVOLVED_TOPICS', [
            '_GUID' => '3f1025f5d3391d43afbdfa292721aa09',
            'BLOCK_ID' => $block_id,
            'BLOCK_PARAMS' => comma_list_arr_to_str(['block_id' => $block_id] + $map),
            'TOPICS' => $topics,

            'START' => strval($start),
            'MAX' => strval($max),
            'START_PARAM' => $block_id . '_start',
            'MAX_PARAM' => $block_id . '_max',
            'EXTRA_GET_PARAMS' => (get_param_integer($block_id . '_max', null) === null) ? null : ('&' . $block_id . '_max=' . urlencode(strval($max))),
        ]);
    }
}
