summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Chart/Inline/Inline.php
blob: 3acbd73e0f2b9d9dd28a54b9adab001edd7b3836 (plain)
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
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

namespace Icinga\Chart\Inline;

/**
 * Class to render and inline chart directly from the request params.
 *
 * When rendering huge amounts of inline charts it is too expensive
 * to bootstrap the complete application for ever single chart and
 * we need to be able render Charts in a compact environment without
 * the other Icinga classes.
 *
 * Class Inline
 * @package Icinga\Chart\Inline
 */
class Inline
{

    /**
     * The data displayed in this chart
     *
     * @var array
     */
    protected $data;

    /**
     * The colors used to display this chart
     *
     * @var array
     */
    protected $colors = array(
        '#00FF00', // OK
        '#FFFF00', // Warning
        '#FF0000', // Critical
        '#E066FF'  // Unreachable
    );

    /**
     * The labels displayed on this chart
     *
     * @var array
     */
    protected $labels = array();

    /**
     * The height in percent
     *
     * @var int
     */
    protected $height = 100;

    /**
     * The width in percent
     *
     * @var int
     */
    protected $width = 100;

    protected function sanitizeStringArray(array $arr)
    {
        $sanitized = array();
        foreach ($arr as $key => $value) {
            $sanitized[$key] = htmlspecialchars($value);
        }
        return $sanitized;
    }

    /**
     * Populate the properties from the current request.
     */
    public function initFromRequest()
    {
        $this->data = explode(',', $_GET['data']);
        foreach ($this->data as $key => $value) {
            $this->data[$key] = (int)$value;
        }
        for ($i = 0; $i < count($this->data); $i++) {
            $this->labels[] = '';
        }

        if (array_key_exists('colors', $_GET)) {
            $this->colors = $this->sanitizeStringArray(explode(',', $_GET['colors']));
        }
        while (count($this->colors) < count($this->data)) {
            $this->colors[] = '#FEFEFE';
        }

        if (array_key_exists('width', $_GET)) {
            $this->width = (int)$_GET['width'];
        }
        if (array_key_exists('height', $_GET)) {
            $this->height = (int)$_GET['height'];
        }
    }
}