<?php
//
// Basic dashlet wrapper for external functions
// Copyright (c) 2008-2020 Nagios Enterprises, LLC. All rights reserved.
//

require_once(dirname(__FILE__) . '/common.inc.php');
include_once(dirname(__FILE__) . '/components/xicore/ajaxhelpers-status.inc.php');

// Initialization stuff
pre_init();
init_session();
do_page();


function do_page() {
    $page_title = "Nagios XI";
    do_page_start(array("page_title" => $page_title), true);
?>

    <style>
        .auto-scale {
            transform-origin: top left;
        }
    </style>

    <script type="text/javascript">
        var autoscale = <?php echo json_encode($_GET["autoscale"]); ?> == "true";
        const unscalable = ["hypermap", "capacityplanning"]
        // Hypermap not compatible with auto-scale due to large page size
        if (unscalable.includes(<?php echo json_encode($_GET["dashlet"]); ?>))
        {
            autoscale = false;
        }

        function scaleContent() {
            if (autoscale)
            {
                var content = $('.child, .childpage').first();
                var scaleWidth = Math.min(1, window.innerWidth / content.width());
                var scaleHeight = Math.min(1, window.innerHeight / content.height());
                var scale = Math.max(Math.min(scaleWidth, scaleHeight), 0.1);
                content.addClass('auto-scale').css({
                    'transform': 'scale(' + scale + ')'
                });
            }
        }
        $(document).ready(function() {
            if (autoscale)
            {
                $('div').parents('.child, .childpage').css({
                    'overflow': 'hidden',
                    'width': '100%',
                    'padding': '0.5rem',
                    'margin': '0',
                    'min-width': 'fit-content'
                });
            }

            // Scale on window resize and load
            $(window).resize(scaleContent);
            setInterval(scaleContent, 300);
            
            <?php if (is_neptune()) { ?>
            $(document).on('focus mouseenter', 'a', function() {
                const excludedDashlets = '.getting_started_dashlet, .admin_task_dashlet';
                var href = $(this).attr('href');
                if ($(this).closest(excludedDashlets).length > 0) {
                    return;
                }
                if (href && !href.includes('?xiwindow=')) {
                    var parts = href.split('nagiosxi/');
                    var base = parts[0];
                    var secondary = parts[1];
                    $(this).attr('href', base + (secondary ? 'nagiosxi/dashboards?xiwindow=/nagiosxi/' + secondary : 'nagiosxi/dashboards'));
                }
            });
            <?php } ?>
        });
    </script>

    <?php
        $urlargs = $_GET["args"];
        $dargs = json_decode(base64_decode($urlargs), true);
        // Set explicit height for certain 'special case' dashlets. Generally, needed for 'dynamic' dashlets.
        // Width of zero represents a dashlet whose width does not need to be set explicitly
        $correct_size = [
            "graphexplorer" => [
                "width"  => 700,
                "height" => 350,
            ],
            "alert_history" => [
                "width"  => 1000,
                "height" => 350,
            ],
            "xicore_perfdata_chart" => [
                "width"  => 700,
                "height" => 350,
            ],
            "capacityplanning_map" => [
                "width"  => 700,
                "height" => 350,
            ],
            "highchart-dashlet" => [
                "width"  => 500,
                "height" => 350,
            ],
            "sansrisingports" => [
                "width" => 700,
                "height" => 350,
            ],
            "custom_text" => [
                "width" => 700,
                "height" => 350,
            ],
            "simple_digital" => [
                "width"  => 0,
                "height" => 350,
            ],
            "custom_dashlet" => [
                "width"  => 1600,
                "height" => 900,
            ],
            "availability" => [
                "width" => 350,
                "height" => 350,
            ],
        ];
        $dashlet = $_GET["dashlet"];
        if (is_neptune()) { ?>
            <base target="_top">
        <?php 
        } else { ?>
            <base target="maincontentframe">
        <?php }
        if (array_key_exists($dashlet, $correct_size)) {
            $height = $correct_size[$dashlet]["height"];
            $width = $correct_size[$dashlet]["width"];


            $style = "height:" . $height . "px;";

            if ($width > 0) {
                $style .= "width:" . $width . "px;";
            }
            
            echo "<div style=\"$style\">";
                    display_dashlet(
                    $_GET["dashlet"],
                    "",
                    $dargs,
                    DASHLET_MODE_INBOARD
                    );
                ?>
                </div>
            <?php
        }
        else {
        display_dashlet(
            $_GET["dashlet"],
            "",
            $dargs,
            DASHLET_MODE_INBOARD
        );
        }
    ?>
<?php
    do_page_end(true);
}
