summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Web/Navigation/Renderer/BadgeNavigationItemRenderer.php
blob: 8510f70701076d3c731f072d0ee0e57863e59d79 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/* Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */

namespace Icinga\Web\Navigation\Renderer;

use Icinga\Web\Navigation\NavigationItem;

/**
 * Abstract base class for a NavigationItem with a status badge
 */
abstract class BadgeNavigationItemRenderer extends NavigationItemRenderer
{
    const STATE_OK = 'ok';
    const STATE_CRITICAL = 'critical';
    const STATE_WARNING = 'warning';
    const STATE_PENDING = 'pending';
    const STATE_UNKNOWN = 'unknown';

    /**
     * The tooltip text for the badge
     *
     * @var string
     */
    protected $title;

    /**
     * The state identifier being used
     *
     * The state identifier defines the background color of the badge.
     *
     * @var string
     */
    protected $state;

    /**
     * Set the tooltip text for the badge
     *
     * @param   string  $title
     *
     * @return  $this
     */
    public function setTitle($title)
    {
        $this->title = $title;
        return $this;
    }

    /**
     * Return the tooltip text for the badge
     *
     * @return  string
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * Set the state identifier to use
     *
     * @param   string  $state
     *
     * @return  $this
     */
    public function setState($state)
    {
        $this->state = $state;
        return $this;
    }

    /**
     * Return the state identifier to use
     *
     * @return  string
     */
    public function getState()
    {
        return $this->state;
    }

    /**
     * Return the amount of items represented by the badge
     *
     * @return  int
     */
    abstract public function getCount();

    /**
     * Render the given navigation item as HTML anchor with a badge
     *
     * @param   NavigationItem  $item
     *
     * @return  string
     */
    public function render(NavigationItem $item = null)
    {
        if ($item === null) {
            $item = $this->getItem();
        }

        $cssClass = '';
        if ($item->getCssClass() !== null) {
            $cssClass = ' ' . $item->getCssClass();
        }

        $item->setCssClass('badge-nav-item' . $cssClass);
        $this->setEscapeLabel(false);
        $label = $this->view()->escape($item->getLabel());
        $item->setLabel($this->renderBadge() . $label);
        $html = parent::render($item);
        return $html;
    }

    /**
     * Render the badge
     *
     * @return  string
     */
    protected function renderBadge()
    {
        if ($count = $this->getCount()) {
            if ($count > 1000000) {
                $count = round($count, -6) / 1000000 . 'M';
            } elseif ($count > 1000) {
                $count = round($count, -3) / 1000 . 'k';
            }

            $view = $this->view();
            return sprintf(
                '<span title="%s" class="badge state-%s">%s</span>',
                $view->escape($this->getTitle()),
                $view->escape($this->getState()),
                $count
            );
        }

        return '';
    }
}