<?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_block_ui_renderers_leader_board
{
    /**
     * See if a particular block parameter's UI input can be handled as a hidden field.
     *
     * @param  ID_TEXT $block The block
     * @param  ID_TEXT $parameter The parameter of the block
     * @param  boolean $has_default Whether there is a default value for the field, due to this being an edit
     * @param  string $default Default value for field
     * @return ?Tempcode Rendered field (null: not handled)
     */
    public function render_block_ui_parameter_hidden(string $block, string $parameter, bool $has_default, string $default) : ?object
    {
        if (!addon_installed('leader_board')) {
            return null;
        }

        return null;
    }

    /**
     * See if a particular block parameter's UI input can be rendered by this.
     *
     * @param  ID_TEXT $block The block
     * @param  ID_TEXT $parameter The parameter of the block
     * @param  boolean $has_default Whether there is a default value for the field, due to this being an edit
     * @param  string $default Default value for field
     * @param  string $description Field description
     * @return ?Tempcode Rendered field (null: not handled)
     */
    public function render_block_ui_parameter(string $block, string $parameter, bool $has_default, string $default, string $description) : ?object
    {
        if (!addon_installed('leader_board')) {
            return null;
        }

        if (($parameter == 'param') && (in_array($block, ['main_leader_board']))) {
            $list = new Tempcode();
            $rows = $GLOBALS['SITE_DB']->query_select('leader_boards', ['id', 'lb_title'], [], 'ORDER BY lb_title');
            foreach ($rows as $row) {
                $list->attach(form_input_list_entry(strval($row['id']), $has_default && strval($row['id']) == $default, $row['lb_title']));
            }
            return form_input_list(do_lang_tempcode('BLOCK_main_leader_board_PARAM_param_TITLE'), do_lang_tempcode('BLOCK_main_leader_board_PARAM_param'), $parameter, $list, null, false, false);
        }
        return null;
    }
}
