File: IndexController.php

package info (click to toggle)
icingaweb2-module-map 2.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 9,028 kB
  • sloc: javascript: 11,939; php: 1,207; sh: 54; makefile: 15
file content (102 lines) | stat: -rw-r--r-- 3,659 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php

namespace Icinga\Module\Map\Controllers;

use Icinga\Application\Modules\Module;
use Icinga\Data\Filter\FilterException;
use Icinga\Module\Map\ProvidedHook\Icingadb\IcingadbSupport;
use Icinga\Web\Controller\ModuleActionController;

class IndexController extends ModuleActionController
{
    public function indexAction()
    {
        $config = $this->Config();
        $map = null;
        $mapConfig = null;

        // try to load stored map
        if ($this->params->has("load")) {
            $map = $this->params->get("load");

            if (!preg_match("/^[\w]+$/", $map)) {
                throw new FilterException("Invalid character in map name. Allow characters: a-zA-Z0-9_");
            }

            $mapConfig = $this->Config("maps");
            if (!$mapConfig->hasSection($map)) {
                throw new FilterException("Could not find stored map with name = " . $map);
            }
        }

        $this->view->id = uniqid();
        $this->view->host = $this->params->get("showHost");
        $this->view->expand = $this->params->get("expand");
        $this->view->fullscreen = ($this->params->get("showFullscreen") == 1);
        $this->view->isUsingIcingadb = false;

        if (Module::exists('icingadb') && IcingadbSupport::useIcingaDbAsBackend()) {
            $this->view->isUsingIcingadb = true;
        }

        $parameterDefaults = array(
            "default_zoom" => "4",
            "default_long" => '13.377485',
            "default_lat" => '52.515855',
            "min_zoom" => "2",
            "max_zoom" => "19",
            "max_native_zoom" => "19",
            "disable_cluster_at_zoom" => null, // should be by default: max_zoom - 1
            "cluster_problem_count" => 0,
            "popup_mouseover" => 0,
            "tile_url" => "//{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
            "opencage_apikey" => "",
        );

        /*
         * 1. url
         * 2. stored map
         * 3. user config
         * 4. config
         */
        $userPreferences = $this->Auth()->getUser()->getPreferences();
        if ($userPreferences->has("map")) {
            $config->getSection("map")->merge($userPreferences->get("map"));
        }

        foreach ($parameterDefaults as $parameter => $default) {
            if ($this->params->has($parameter)) {
                $this->view->$parameter = $this->params->get($parameter);
            } elseif (isset($map) && $mapConfig->getSection($map)->offsetExists($parameter)) {
                $this->view->$parameter = $mapConfig->get($map, $parameter);
            } else {
                $this->view->$parameter = $config->get("map", $parameter, $default);
            }
        }

        if (!$this->view->disable_cluster_at_zoom) {
            $this->view->disable_cluster_at_zoom = $this->view->max_zoom - 1;
        }

        $pattern = "/^([_]{0,1}(host|service))|\(|(object|state)Type/";
        $urlParameters = $this->filterArray($this->params->toArray(false), $pattern);

        if (isset($map)) {
            $mapParameters = $this->filterArray($mapConfig->getSection($map)->toArray(), $pattern);
            $urlParameters = array_merge($mapParameters, $urlParameters);
        }

        array_walk($urlParameters, function (&$a, $b) {
            $a = $b . "=" . $a;
        });
        $this->view->url_parameters = join('&', $urlParameters);

        $this->view->dashletHeight = $config->get('map', 'dashlet_height', '300');
    }

    function filterArray($array, $pattern)
    {
        $matches = preg_grep($pattern, array_keys($array));
        return array_intersect_key($array, array_flip($matches));
    }
}