<?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    leader_board
 */

/**
 * Hook class.
 */
class Hook_addon_registry_leader_board
{
    /**
     * Get a list of file permissions to set.
     *
     * @param  boolean $runtime Whether to include wildcards represented runtime-created chmoddable files
     * @return array File permissions to set
     */
    public function get_chmod_array(bool $runtime = false) : array
    {
        return [];
    }

    /**
     * Get the version of the software this addon is for (used in generating the TAR filename).
     *
     * @return float Version number
     */
    public function get_version() : float
    {
        return cms_version_number();
    }

    /**
     * Get the minimum required version of the website software needed to use this addon.
     *
     * @return float Minimum required website software version
     */
    public function get_min_cms_version() : float
    {
        return 11.0;
    }

    /**
     * Get the maximum compatible version of the website software to use this addon.
     *
     * @return ?float Maximum compatible website software version (null: no maximum version currently)
     */
    public function get_max_cms_version() : ?float
    {
        return 11.9;
    }

    /**
     * Get the addon category.
     *
     * @return string The category
     */
    public function get_category() : string
    {
        return 'Fun and Games';
    }

    /**
     * Get the description of the addon.
     *
     * @return string Description of the addon
     */
    public function get_description() : string
    {
        return 'Allow website administrators to display leader-boards of members earning the most points.';
    }

    /**
     * Get a list of tutorials that apply to this addon.
     *
     * @return array List of tutorials
     */
    public function get_applicable_tutorials() : array
    {
        return [
            'tut_points',
        ];
    }

    /**
     * Get a mapping of dependency types.
     *
     * @return array A structure specifying dependency information
     */
    public function get_dependencies() : array
    {
        return [
            'requires' => ['points', 'System scheduler'],
            'recommends' => [
                'cns_forum', // for voting power display via cns_polls
            ],
            'conflicts_with' => [],
        ];
    }

    /**
     * Explicitly say which icon should be used.
     *
     * @return URLPATH Icon
     */
    public function get_default_icon() : string
    {
        return 'themes/default/images/icons/menu/social/leader_board.svg';
    }

    /**
     * Get a list of files that belong to this addon.
     *
     * @return array List of files
     */
    public function get_file_list() : array
    {
        return [
            'adminzone/pages/modules/admin_leader_board.php',
            'lang/EN/leader_board.ini',
            'site/pages/modules/leader_board.php',
            'sources/blocks/main_leader_board.php',
            'sources/hooks/modules/admin_import_types/leader_board.php',
            'sources/hooks/modules/admin_setupwizard/leader_board.php',
            'sources/hooks/systems/actionlog/leader_board.php',
            'sources/hooks/systems/addon_registry/leader_board.php',
            'sources/hooks/systems/block_ui_renderers/leader_board.php',
            'sources/hooks/systems/commandr_fs/leader_board.php',
            'sources/hooks/systems/cron/leader_board.php',
            'sources/hooks/systems/page_groupings/leader_board.php',
            'sources/hooks/systems/privacy/leader_board.php',
            'sources/hooks/systems/resource_meta_aware/leader_board.php',
            'sources/leader_board.php',
            'sources/leader_board2.php',
            'themes/default/css/leader_board.css',
            'themes/default/images/icons/menu/social/leader_board.svg',
            'themes/default/images/icons_monochrome/menu/social/leader_board.svg',
            'themes/default/templates/POINTS_LEADER_BOARD_ROW.tpl',
            'themes/default/templates/POINTS_LEADER_BOARD_SCREEN.tpl',
            'themes/default/templates/POINTS_LEADER_BOARD_SET.tpl',
        ];
    }

    /**
     * Get mapping between template names and the method of this class that can render a preview of them.
     *
     * @return array The mapping
     */
    public function tpl_previews() : array
    {
        return [
            'templates/POINTS_LEADER_BOARD_ROW.tpl' => 'points_leader_board_screen',
            'templates/POINTS_LEADER_BOARD_SET.tpl' => 'points_leader_board_screen',
            'templates/POINTS_LEADER_BOARD_SCREEN.tpl' => 'points_leader_board_screen',
        ];
    }

    /**
     * Find available predefined content, and what is installed.
     *
     * @return array A map of available predefined content codenames, and details (if installed, and title)
     */
    public function enumerate_predefined_content() : array
    {
        require_lang('leader_board');

        $map = [
            'lb_title' => do_lang('DEFAULT'),
            'lb_type' => 'holders',
            'lb_member_count' => 10,
            'lb_timeframe' => 'week',
            'lb_rolling' => 1,
            'lb_include_staff' => 0,
        ];
        $installed_holders = ($GLOBALS['SITE_DB']->query_select_value_if_there('leader_boards', 'id', $map) !== null);

        $map['lb_type'] = 'earners';
        $installed_earners = ($GLOBALS['SITE_DB']->query_select_value_if_there('leader_boards', 'id', $map) !== null);

        return [
            'weekly_10_holders' => [
                'title' => do_lang_tempcode('LEADER_BOARD_PREREFINED_WEEKLY_10_HOLDERS'),
                'description' => do_lang_tempcode('DESCRIPTION_LEADER_BOARD_PREREFINED_WEEKLY_10_HOLDERS'),
                'installed' => $installed_holders,
            ],
            'weekly_10_earners' => [
                'title' => do_lang_tempcode('LEADER_BOARD_PREREFINED_WEEKLY_10_EARNERS'),
                'description' => do_lang_tempcode('DESCRIPTION_LEADER_BOARD_PREREFINED_WEEKLY_10_EARNERS'),
                'installed' => $installed_earners,
            ],
        ];
    }

     /**
     * Install predefined content.
     *
     * @param  ?array $content A list of predefined content labels to install (null: all)
     */
    public function install_predefined_content(?array $content = null)
    {
        require_code('lorem');

        if ((($content === null) || (in_array('weekly_10_holders', $content))) && (!has_predefined_content('leader_board', 'weekly_10_holders'))) {
            require_code('leader_board2');
            add_leader_board(lorem_phrase() . '-holders', 'holders', 10, 'week', 1, 0, [], 1);
        }
        if ((($content === null) || (in_array('weekly_10_earners', $content))) && (!has_predefined_content('leader_board', 'weekly_10_earners'))) {
            require_code('leader_board2');
            add_leader_board(lorem_phrase() . '-earners', 'earners', 10, 'week', 1, 0, [], 1);
        }
    }

    /**
     * Uninstall predefined content.
     *
     * @param  ?array $content A list of predefined content labels to uninstall (null: all)
     */
    public function uninstall_predefined_content(?array $content = null)
    {
        require_code('lorem');

        if ((($content === null) || (in_array('weekly_10_holders', $content))) && (has_predefined_content('leader_board', 'weekly_10_holders'))) {
            $map = [
                'lb_title' => lorem_phrase() . '-holders',
                'lb_type' => 'holders',
                'lb_member_count' => 10,
                'lb_timeframe' => 'week',
                'lb_rolling' => 1,
                'lb_include_staff' => 0,
            ];
            require_code('leader_board2');
            $id = $GLOBALS['SITE_DB']->query_select_value('leader_boards', 'id', $map);
            delete_leader_board($id);
        }
        if ((($content === null) || (in_array('weekly_10_earners', $content))) && (has_predefined_content('leader_board', 'weekly_10_earners'))) {
            $map = [
                'lb_title' => lorem_phrase() . '-earners',
                'lb_type' => 'earners',
                'lb_member_count' => 10,
                'lb_timeframe' => 'week',
                'lb_rolling' => 1,
                'lb_include_staff' => 0,
            ];
            require_code('leader_board2');
            $id = $GLOBALS['SITE_DB']->query_select_value('leader_boards', 'id', $map);
            delete_leader_board($id);
        }
    }

    /**
     * Get a preview(s) of a (group of) template(s), as a full standalone piece of HTML in Tempcode format.
     * Uses sources/lorem.php functions to place appropriate stock-text. Should not hard-code things, as the code is intended to be declarative.
     * Assumptions: You can assume all Lang/CSS/JavaScript files in this addon have been pre-required.
     *
     * @return Tempcode Preview
     */
    public function tpl_preview__points_leader_board_screen() : object
    {
        require_lang('cns_polls');
        require_lang('leader_board');

        $out = new Tempcode();
        foreach (placeholder_array() as $k => $v) {
            $set_tpl = new Tempcode();
            foreach (placeholder_array() as $_k => $_v) {
                $set_tpl->attach(do_lorem_template('POINTS_LEADER_BOARD_ROW', [
                    'ID' => placeholder_numeric_id(),
                    'POINTS_URL' => placeholder_url(),
                    'PROFILE_URL' => placeholder_url(),
                    '_POINTS' => placeholder_number(),
                    'POINTS' => placeholder_number(),
                    '_VOTING_POWER' => placeholder_number(),
                    'VOTING_POWER' => placeholder_number(),
                    '_VOTING_CONTROL' => placeholder_number(),
                    'VOTING_CONTROL' => placeholder_number(),
                    'USERNAME' => lorem_phrase(),
                    'HAS_RANK_IMAGES' => true,
                ]));
            }

            $about = do_lang_tempcode('LEADER_BOARD_ABOUT_earners', escape_html(placeholder_number()), escape_html(placeholder_date()), escape_html(placeholder_date()));

            $out->attach(do_lorem_template('POINTS_LEADER_BOARD_SET', [
                '_SET_NUMBER' => placeholder_number(),
                '_TYPE' => 'holders',
                '_COUNT' => placeholder_number(),
                '_DATE' => placeholder_date_raw(),
                '_START_DATE' => placeholder_date_raw(),
                'URL' => placeholder_url(),
                'TITLE' => lorem_phrase(),
                'ABOUT' => $about,
                'ROWS' => $set_tpl,
                'IS_BLOCK' => true
            ]));
        }

        return lorem_globalise(do_lorem_template('POINTS_LEADER_BOARD_SCREEN', [
            '_ID' => placeholder_numeric_id(),
            '_TYPE' => lorem_phrase(),
            '_COUNT' => placeholder_number(),
            'TITLE' => lorem_phrase(),
            'LEADER_BOARD_TYPE' => do_lang_tempcode('LEADER_BOARD_TYPE_holders'),
            'LEADER_BOARD_TITLE' => lorem_phrase(),
            'SETS' => $out
        ]), null, '', true);
    }
}
