summaryrefslogtreecommitdiffstats
path: root/application/views/helpers/Badges.php
blob: 6066a7250a97180423bf86cfdb4415edec92aa8f (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
<?php
/* Icinga Web 2 Top Level View | (c) 2017 Icinga Development Team | GPLv2+ */

use Icinga\Module\Toplevelview\Tree\TLVStatus;
use Icinga\Web\Url;

class Zend_View_Helper_Badges extends Zend_View_Helper_Abstract
{
    /** @var \Icinga\Web\View */
    public $view;

    protected function prettyTitle($identifier)
    {
        $s = '';
        foreach (preg_split('/[\.\-_\s]+/', $identifier) as $p) {
            $s .= ' ' . ucfirst($p);
        }
        return trim($s);
    }

    public function badges(TLVStatus $status, $problemsOnly = true, $showTotal = false)
    {
        $htm = '';

        $values = false;
        $htm .= '<div class="badges">';
        foreach ($status->getProperties() as $key => $value) {
            if ($problemsOnly === true && ($key === 'ok' || $key === 'downtime_active')
                || ($key === 'total' && $showTotal !== true)
            ) {
                continue;
            }
            if ($value !== null && $value > 0) {
                $values = true;
                $title = $value . ' ' . $this->prettyTitle($key);
                $class = 'tlv-status-tile ' . str_replace('_', ' ', $key);
                $htm .= sprintf(
                    '<div class="badge status-badge %s" title="%s">%s</div>',
                    $class,
                    $title,
                    $value
                );
            }
        }
        $htm .= '</div>';

        return $values ? $htm : '';
    }
}