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

/**
 * Hook class.
 */
class Hook_addon_registry_banners
{
    /**
     * 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
    {
        $chmod = [];
        if ($runtime) {
            $chmod = array_merge(
                $chmod,
                [
                    'uploads/banners/*',
                ]
            );
        }
        $chmod = array_merge(
            $chmod,
            [
                'uploads/banners',
            ]
        );
        return $chmod;
    }

    /**
     * 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 'eCommerce';
    }

    /**
     * Get the description of the addon.
     *
     * @return string Description of the addon
     */
    public function get_description() : string
    {
        return 'An advanced banner system, with support for multiple banner rotations, commercial banner campaigns, and webring-style systems. Support for graphical, text, and HTML banners. Hotword activation support.';
    }

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

    /**
     * Get a mapping of dependency types.
     *
     * @return array A structure specifying dependency information
     */
    public function get_dependencies() : array
    {
        return [
            'requires' => [],
            'recommends' => [],
            'conflicts_with' => [],
        ];
    }

    /**
     * Explicitly say which icon should be used.
     *
     * @return URLPATH Icon
     */
    public function get_default_icon() : string
    {
        return 'themes/default/images/icons/menu/cms/banners.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_banners.php',
            'banner.php',
            'cms/pages/modules/cms_banners.php',
            'data/images/advertise_here.png',
            'data/images/donate.png',
            'data/images/placeholder_leaderboard.jpg',
            'lang/EN/banners.ini',
            'site/pages/comcode/EN/top_sites.txt',
            'site/pages/modules/banners.php',
            'sources/banners.php',
            'sources/banners2.php',
            'sources/blocks/main_banner_wave.php',
            'sources/blocks/main_top_sites.php',
            'sources/hooks/modules/admin_import_types/banners.php',
            'sources/hooks/modules/admin_setupwizard/banners.php',
            'sources/hooks/modules/admin_stats/banners.php',
            'sources/hooks/systems/actionlog/banners.php',
            'sources/hooks/systems/addon_registry/banners.php',
            'sources/hooks/systems/block_ui_renderers/banners.php',
            'sources/hooks/systems/commandr_fs/banners.php',
            'sources/hooks/systems/config/admin_banners.php',
            'sources/hooks/systems/config/banner_autosize.php',
            'sources/hooks/systems/config/enable_edit_banner_buttons.php',
            'sources/hooks/systems/config/points_ADD_BANNER.php',
            'sources/hooks/systems/config/use_banner_permissions.php',
            'sources/hooks/systems/content_meta_aware/banner.php',
            'sources/hooks/systems/content_meta_aware/banner_type.php',
            'sources/hooks/systems/ecommerce/banners.php',
            'sources/hooks/systems/page_groupings/banners.php',
            'sources/hooks/systems/points/banner__add.php',
            'sources/hooks/systems/preview/banner.php',
            'sources/hooks/systems/privacy/banners.php',
            'sources/hooks/systems/realtime_rain/banners.php',
            'sources/hooks/systems/reorganise_uploads/banners.php',
            'sources/hooks/systems/sitemap/banner.php',
            'sources/hooks/systems/snippets/exists_banner.php',
            'sources/hooks/systems/snippets/exists_banner_type.php',
            'themes/default/css/banners.css',
            'themes/default/images/icons/menu/cms/banners.svg',
            'themes/default/images/icons_monochrome/menu/cms/banners.svg',
            'themes/default/javascript/banners.js',
            'themes/default/templates/BANNERS_NONE.tpl',
            'themes/default/templates/BANNER_ADDED_SCREEN.tpl',
            'themes/default/templates/BANNER_IFRAME.tpl',
            'themes/default/templates/BANNER_IMAGE.tpl',
            'themes/default/templates/BANNER_PREVIEW.tpl',
            'themes/default/templates/BANNER_SHOW_CODE.tpl',
            'themes/default/templates/BANNER_TEXT.tpl',
            'themes/default/templates/BANNER_VIEW_SCREEN.tpl',
            'themes/default/templates/BLOCK_MAIN_BANNER_WAVE.tpl',
            'themes/default/templates/BLOCK_MAIN_BANNER_WAVE_BWRAP.tpl',
            'themes/default/templates/BLOCK_MAIN_TOP_SITES.tpl',
            'uploads/banners/.htaccess',
            'uploads/banners/index.html',
        ];
    }

    /**
     * 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/BANNER_PREVIEW.tpl' => 'banner_preview',
            'templates/BANNER_SHOW_CODE.tpl' => 'banner_show_code',
            'templates/BANNER_ADDED_SCREEN.tpl' => 'administrative__banner_added_screen',
            'templates/BLOCK_MAIN_TOP_SITES.tpl' => 'block_main_top_sites',
            'templates/BLOCK_MAIN_BANNER_WAVE_BWRAP.tpl' => 'block_main_banner_wave',
            'templates/BLOCK_MAIN_BANNER_WAVE.tpl' => 'block_main_banner_wave',
            'templates/BANNERS_NONE.tpl' => 'banners_none',
            'templates/BANNER_IMAGE.tpl' => 'banner_image',
            'templates/BANNER_IFRAME.tpl' => 'banner_iframe',
            'templates/BANNER_TEXT.tpl' => 'banner_text',
            'templates/BANNER_VIEW_SCREEN.tpl' => 'administrative__banner_view_screen',
        ];
    }

    /**
     * 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__banner_preview() : object
    {
        return lorem_globalise(do_lorem_template('BANNER_PREVIEW', [
            'PREVIEW' => lorem_phrase(),
        ]), null, '', true);
    }

    /**
     * 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__banner_show_code() : object
    {
        return lorem_globalise(do_lorem_template('BANNER_SHOW_CODE', [
            'NAME' => placeholder_codename(),
            'WIDTH' => placeholder_number(),
            'HEIGHT' => placeholder_number(),
            'TYPE' => lorem_word(),
        ]), null, '', true);
    }

    /**
     * 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__administrative__banner_added_screen() : object
    {
        return lorem_globalise(do_lorem_template('BANNER_ADDED_SCREEN', [
            'TITLE' => lorem_screen_title(),
            'TEXT' => lorem_sentence_html(),
            'BANNER_CODE' => lorem_phrase(),
            'STATS_URL' => placeholder_url(),
            'DO_NEXT' => lorem_phrase(),
        ]), null, '', true);
    }

    /**
     * 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__block_main_top_sites() : object
    {
        $banners = [];
        foreach (placeholder_array() as $k => $v) {
            $banners[] = [
                'DESCRIPTION' => lorem_word(),
                'BANNER' => $v,
                'HITS_FROM' => placeholder_number(),
                'HITS_TO' => placeholder_number(),
            ];
        }

        return lorem_globalise(do_lorem_template('BLOCK_MAIN_TOP_SITES', [
            'BLOCK_ID' => lorem_word(),
            'TYPE' => lorem_phrase(),
            'BANNERS' => $banners,
            'SUBMIT_URL' => placeholder_url(),
        ]), null, '', true);
    }

    /**
     * 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__block_main_banner_wave() : object
    {
        $banners = new Tempcode();
        $banners->attach(do_lorem_template('BANNER_IMAGE', [
            'URL' => placeholder_url(),
            'B_TYPE' => lorem_phrase(),
            'WIDTH' => placeholder_number(),
            'HEIGHT' => placeholder_number(),
            'SOURCE' => lorem_phrase(),
            'DEST' => lorem_phrase(),
            'CAPTION' => lorem_phrase(),
            'IMG' => placeholder_image_url(),
            'LOCAL' => false,
        ]));
        $banners->attach(do_lorem_template('BANNER_IFRAME', [
            'B_TYPE' => lorem_phrase(),
            'IMG' => placeholder_image_url(),
            'WIDTH' => placeholder_number(),
            'HEIGHT' => placeholder_number(),
        ]));

        $assemble = do_lorem_template('BLOCK_MAIN_BANNER_WAVE_BWRAP', [
            'TYPE' => lorem_phrase(),
            'BANNER' => $banners,
            'MAX' => placeholder_number(),
            'REGION' => '',
        ]);

        return lorem_globalise(do_lorem_template('BLOCK_MAIN_BANNER_WAVE', [
            'BLOCK_ID' => lorem_word(),
            'TYPE' => lorem_phrase(),
            'ASSEMBLE' => $assemble,
            'MAX' => placeholder_number(),
            'REGION' => '',
        ]), null, '', true);
    }

    /**
     * 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__banners_none() : object
    {
        return lorem_globalise(do_lorem_template('BANNERS_NONE', [
            'ADD_BANNER_URL' => placeholder_url(),
        ]), null, '', true);
    }

    /**
     * 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__banner_image() : object
    {
        return lorem_globalise(do_lorem_template('BANNER_IMAGE', [
            'URL' => placeholder_url(),
            'B_TYPE' => lorem_phrase(),
            'WIDTH' => placeholder_number(),
            'HEIGHT' => placeholder_number(),
            'SOURCE' => lorem_phrase(),
            'DEST' => lorem_phrase(),
            'CAPTION' => lorem_phrase(),
            'IMG' => placeholder_image_url(),
            'LOCAL' => false,
        ]), null, '', true);
    }

    /**
     * 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__banner_iframe() : object
    {
        return lorem_globalise(do_lorem_template('BANNER_IFRAME', [
            'B_TYPE' => lorem_phrase(),
            'IMG' => placeholder_image_url(),
            'WIDTH' => placeholder_number(),
            'HEIGHT' => placeholder_number(),
        ]), null, '', true);
    }

    /**
     * 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__banner_text() : object
    {
        return lorem_globalise(do_lorem_template('BANNER_TEXT', [
            'B_TYPE' => lorem_phrase(),
            'TITLE_TEXT' => lorem_phrase(),
            'CAPTION' => lorem_phrase(),
            'SOURCE' => lorem_phrase(),
            'DEST' => lorem_phrase(),
            'URL' => placeholder_url(),
            'FILTERED_URL' => placeholder_url(),
        ]), null, '', true);
    }

    /**
     * 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__administrative__banner_view_screen() : object
    {
        return lorem_globalise(do_lorem_template('BANNER_VIEW_SCREEN', [
            'TITLE' => lorem_screen_title(),
            'EDIT_URL' => placeholder_url(),
            'MAP_TABLE' => placeholder_table(),
            'BANNER' => lorem_phrase(),
            'NAME' => placeholder_codename(),
            'RESULTS_TABLE' => placeholder_table(),
            'RESET_URL' => placeholder_url(),
        ]), null, '', true);
    }

    /**
     * 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('banners');

        $test_have_default_banners_donation = $GLOBALS['SITE_DB']->query_select_value_if_there('banners', 'name', ['name' => 'donate']);
        $test_have_default_banners_advertising = $GLOBALS['SITE_DB']->query_select_value_if_there('banners', 'name', ['name' => 'advertise_here']);

        return [
            'have_default_banners_donation' => [
                'title' => do_lang_tempcode('HAVE_DEFAULT_BANNERS_DONATION'),
                'description' => do_lang_tempcode('DESCRIPTION_HAVE_DEFAULT_BANNERS_DONATION'),
                'installed' => ($test_have_default_banners_donation !== null),
            ],
            'have_default_banners_advertising' => [
                'title' => do_lang_tempcode('HAVE_DEFAULT_BANNERS_ADVERTISING'),
                'description' => do_lang_tempcode('DESCRIPTION_HAVE_DEFAULT_BANNERS_ADVERTISING'),
                'installed' => ($test_have_default_banners_advertising !== null),
            ],
        ];
    }

    /**
     * Install predefined content.
     *
     * @param  ?array $content A list of predefined content labels to install (null: all)
     */
    public function install_predefined_content(?array $content = null)
    {
        if ((($content === null) || (in_array('have_default_banners_donation', $content))) && (!has_predefined_content('banners', 'have_default_banners_donation'))) {
            require_lang('banners');
            require_code('banners');
            require_code('lang3');

            $map = [
                'name' => 'donate',
                'title_text' => '',
                'direct_code' => '',
                'deployment_agreement' => BANNER_PERMANENT,
                'img_url' => 'data/images/donate.png',
                'campaign_remaining' => 0,
                'site_url' => get_base_url() . '/index.php?page=donate',
                'hits_from' => 0,
                'views_from' => 0,
                'hits_to' => 0,
                'views_to' => 0,
                'display_likelihood' => 30,
                'notes' => do_lang('STOCK_DEFAULT_BANNER'),
                'validated' => 1,
                'add_date' => time(),
                'submitter' => $GLOBALS['FORUM_DRIVER']->get_guest_id(),
                'b_type' => '',
                'expiry_date' => null,
                'edit_date' => null,
            ];
            $map += lang_code_to_default_content('caption', 'DONATION', true, 1);
            $GLOBALS['SITE_DB']->query_insert('banners', $map);

            require_code('permissions2');
            set_global_category_access('banners', 'donate');
        }

        if ((($content === null) || (in_array('have_default_banners_advertising', $content))) && (!has_predefined_content('banners', 'have_default_banners_advertising'))) {
            require_lang('banners');
            require_code('lang3');

            $map = [
                'name' => 'advertise_here',
                'title_text' => '',
                'direct_code' => '',
                'deployment_agreement' => BANNER_FALLBACK,
                'img_url' => 'data/images/advertise_here.png',
                'campaign_remaining' => 0,
                'site_url' => get_base_url() . '/index.php?page=advertise',
                'hits_from' => 0,
                'views_from' => 0,
                'hits_to' => 0,
                'views_to' => 0,
                'display_likelihood' => 10,
                'notes' => do_lang('STOCK_DEFAULT_BANNER_FALLBACK'),
                'validated' => 1,
                'add_date' => time(),
                'submitter' => $GLOBALS['FORUM_DRIVER']->get_guest_id(),
                'b_type' => '',
                'expiry_date' => null,
                'edit_date' => null,
            ];
            $map += lang_code_to_default_content('caption', 'ADVERTISE_HERE', true, 1);
            $GLOBALS['SITE_DB']->query_insert('banners', $map);

            require_code('permissions2');
            set_global_category_access('banners', 'advertise_here');
        }
    }

    /**
     * Uninstall predefined content.
     *
     * @param  ?array $content A list of predefined content labels to uninstall (null: all)
     */
    public function uninstall_predefined_content(?array $content = null)
    {
        if ((($content === null) || (in_array('have_default_banners_donation', $content))) && (has_predefined_content('banners', 'have_default_banners_donation'))) {
            require_code('banners2');
            delete_banner('donate');

            //require_code('zones3');
            //delete_cms_page('site', 'donate', null, true);    Page no longer bundled, can be made via page templates
        }

        if ((($content === null) || (in_array('have_default_banners_advertising', $content))) && (has_predefined_content('banners', 'have_default_banners_advertising'))) {
            require_code('banners2');
            delete_banner('advertise_here');

            //require_code('zones3');
            //delete_cms_page('site', 'advertise', null, true);    Page no longer bundled, can be made via page templates
        }
    }
}
