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

/**
 * Module page class.
 */
class Module_admin_health_check
{
    /**
     * Find details of the module.
     *
     * @return ?array Map of module info (null: module is disabled)
     */
    public function info() : ?array
    {
        $info = [];
        $info['author'] = 'Chris Graham';
        $info['organisation'] = 'Composr';
        $info['hacked_by'] = null;
        $info['hack_version'] = null;
        $info['version'] = 1;
        $info['locked'] = false;
        $info['min_cms_version'] = 11.0;
        $info['addon'] = 'health_check';
        return $info;
    }

    /**
     * Find entry-points available within this module.
     *
     * @param  boolean $check_perms Whether to check permissions
     * @param  ?MEMBER $member_id The member to check permissions as (null: current user)
     * @param  boolean $support_crosslinks Whether to allow cross links to other modules (identifiable via a full-page-link rather than a screen-name)
     * @param  boolean $be_deferential Whether to avoid any entry-point (or even return null to disable the page in the Sitemap) if we know another module, or page_group, is going to link to that entry-point. Note that "!" and "browse" entry points are automatically merged with container page nodes (likely called by page-groupings) as appropriate
     * @return ?array A map of entry points (screen-name=>language-code/string or screen-name=>[language-code/string, icon-theme-image]) (null: disabled)
     */
    public function get_entry_points(bool $check_perms = true, ?int $member_id = null, bool $support_crosslinks = true, bool $be_deferential = false) : ?array
    {
        if (!addon_installed('health_check')) {
            return null;
        }

        return [
            'browse' => ['HEALTH_CHECK', 'menu/adminzone/tools/health_check'],
        ];
    }

    public $title;

    /**
     * Module pre-run function. Allows us to know metadata for <head> before we start streaming output.
     *
     * @return ?Tempcode Tempcode indicating some kind of exceptional output (null: none)
     */
    public function pre_run() : ?object
    {
        $error_msg = new Tempcode();
        if (!addon_installed__messaged('health_check', $error_msg)) {
            return $error_msg;
        }

        require_code('health_check');

        require_lang('menus');
        set_helper_panel_text(comcode_lang_string('DOC_HEALTH_CHECK'));

        $type = get_param_string('type', 'browse');

        if ($type == 'browse') {
            $this->title = get_screen_title('HEALTH_CHECK');
        }

        return null;
    }

    /**
     * Execute the module.
     *
     * @return Tempcode The result of execution
     */
    public function run() : object
    {
        $type = get_param_string('type', 'browse');

        if ($type == 'browse') {
            return $this->browse();
        }

        return new Tempcode();
    }

    /**
     * Health check UI / Run the health check.
     *
     * @return Tempcode The UI
     */
    public function browse() : object
    {
        if (post_param_integer('submitting', 0) == 1) {
            $sections_to_run = isset($_POST['sections_to_run']) ? $_POST['sections_to_run'] : [];

            $show_passes = (post_param_integer('show_passes', 0) == 1); // Cannot use any "pass*" names or password managers will stick buttons on top of the tick (check) thinking it's a login field.
            $show_skips = (post_param_integer('show_skips', 0) == 1);
            $show_manual_checks = (post_param_integer('show_manual_checks', 0) == 1);
        } else {
            $sections_to_run = (get_option('hc_cron_sections_to_run') == '') ? [] : explode(',', get_option('hc_cron_sections_to_run'));

            if ($GLOBALS['DEV_MODE']) {
                // So we can see issues more easily
                $show_passes = false;
                $show_skips = false;
                $show_manual_checks = false;
            } else {
                $show_passes = true;
                $show_skips = true;
                $show_manual_checks = true;
            }
        }

        $automatic_repair = false; // We don't want this in the UI, it's implemented for possible future use only

        $sections = create_selection_list_health_check_sections($sections_to_run);

        if (post_param_integer('submitting', 0) == 1) {
            set_value('last_run__health_check', strval(time()), true);

            $has_fails = false;
            $categories = run_health_check($has_fails, $sections_to_run, $show_passes, $show_skips, $show_manual_checks, $automatic_repair);

            $results = do_template('HEALTH_CHECK_RESULTS', ['_GUID' => 'f428b63eacc19bdd041d2b1a1d2f1155', 'CATEGORIES' => $categories]);

            log_it('HEALTH_CHECK');
        } else {
            $results = null;
        }

        return do_template('HEALTH_CHECK_SCREEN', [
            '_GUID' => 'cd2a0ec2477dcc6545cb0825b098cfc5',
            'TITLE' => $this->title,
            'SECTIONS' => $sections,
            'SHOW_PASSES' => $show_passes,
            'SHOW_SKIPS' => $show_skips,
            'SHOW_MANUAL_CHECKS' => $show_manual_checks,
            'RESULTS' => $results,
        ]);
    }
}
