/usr/share/icingaweb2/modules/monitoring/library/Monitoring/Web/Widget/StateBadges.php is in icingaweb2-module-monitoring 2.1.0-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | <?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
namespace Icinga\Module\Monitoring\Web\Widget;
use Icinga\Web\Form;
use Icinga\Web\Navigation\Navigation;
use Icinga\Web\Navigation\NavigationItem;
use Icinga\Web\Url;
use Icinga\Web\Widget\AbstractWidget;
use Icinga\Data\Filter\Filter;
class StateBadges extends AbstractWidget
{
/**
* CSS class for the widget
*
* @var string
*/
const CSS_CLASS = 'state-badges';
/**
* State critical
*
* @var string
*/
const STATE_CRITICAL = 'state-critical';
/**
* State critical handled
*
* @var string
*/
const STATE_CRITICAL_HANDLED = 'state-critical handled';
/**
* State down
*
* @var string
*/
const STATE_DOWN = 'state-down';
/**
* State down handled
*
* @var string
*/
const STATE_DOWN_HANDLED = 'state-down handled';
/**
* State ok
*
* @var string
*/
const STATE_OK = 'state-ok';
/**
* State pending
*
* @var string
*/
const STATE_PENDING = 'state-pending';
/**
* State unknown
*
* @var string
*/
const STATE_UNKNOWN = 'state-unknown';
/**
* State unknown handled
*
* @var string
*/
const STATE_UNKNOWN_HANDLED = 'state-unknown handled';
/**
* State unreachable
*
* @var string
*/
const STATE_UNREACHABLE = 'state-unreachable';
/**
* State unreachable handled
*
* @var string
*/
const STATE_UNREACHABLE_HANDLED = 'state-unreachable handled';
/**
* State up
*
* @var string
*/
const STATE_UP = 'state-up';
/**
* State warning
*
* @var string
*/
const STATE_WARNING = 'state-warning';
/**
* State warning handled
*
* @var string
*/
const STATE_WARNING_HANDLED = 'state-warning handled';
/**
* State badges
*
* @var object[]
*/
protected $badges = array();
/**
* Internal counter for badge priorities
*
* @var int
*/
protected $priority = 1;
/**
* The base filter applied to any badge link
*
* @var Filter
*/
protected $baseFilter;
/**
* Base URL
*
* @var Url
*/
protected $url;
/**
* Get the base URL
*
* @return Url
*/
public function getUrl()
{
return $this->url;
}
/**
* Set the base URL
*
* @param Url|string $url
*
* @return $this
*/
public function setUrl($url)
{
if (! $url instanceof $url) {
$url = Url::fromPath($url);
}
$this->url = $url;
return $this;
}
/**
* Get the base filter
*
* @return Filter
*/
public function getBaseFilter()
{
return $this->baseFilter;
}
/**
* Set the base filter
*
* @param Filter $baseFilter
*
* @return $this
*/
public function setBaseFilter($baseFilter)
{
$this->baseFilter = $baseFilter;
return $this;
}
/**
* Add a state badge
*
* @param string $state
* @param int $count
* @param array $filter
* @param string $translateSingular
* @param string $translatePlural
* @param array $translateArgs
*
* @return $this
*/
public function add(
$state, $count, array $filter, $translateSingular, $translatePlural, array $translateArgs = array()
) {
$this->badges[$state] = (object) array(
'count' => (int) $count,
'filter' => $filter,
'translateArgs' => $translateArgs,
'translatePlural' => $translatePlural,
'translateSingular' => $translateSingular
);
return $this;
}
/**
* Create a badge
*
* @param string $state
* @param Navigation $badges
*
* @return $this
*/
public function createBadge($state, Navigation $badges)
{
if ($this->has($state)) {
$badge = $this->get($state);
$url = clone $this->url->setParams($badge->filter);
if (isset($this->baseFilter)) {
$url->addFilter($this->baseFilter);
}
$badges->addItem(new NavigationItem($state, array(
'attributes' => array('class' => 'badge ' . $state),
'label' => $badge->count,
'priority' => $this->priority++,
'title' => vsprintf(
mtp('monitoring', $badge->translateSingular, $badge->translatePlural, $badge->count),
$badge->translateArgs
),
'url' => $url
)));
}
return $this;
}
/**
* Create a badge group
*
* @param array $states
* @param Navigation $badges
*
* @return $this
*/
public function createBadgeGroup(array $states, Navigation $badges)
{
$group = array_intersect_key($this->badges, array_flip($states));
if (! empty($group)) {
$groupItem = new NavigationItem(
uniqid(),
array(
'label' => '',
'priority' => $this->priority++
)
);
$groupBadges = new Navigation();
$groupBadges->setLayout(Navigation::LAYOUT_TABS);
foreach (array_keys($group) as $state) {
$this->createBadge($state, $groupBadges);
}
$groupItem->setChildren($groupBadges);
$badges->addItem($groupItem);
}
return $this;
}
/**
* Get whether a badge for the given state has been added
*
* @param string $state
*
* @return bool
*/
public function has($state)
{
return isset($this->badges[$state]) && $this->badges[$state]->count;
}
/**
* Get the badge for the given state
*
* @param string $state
*
* @return object
*/
public function get($state)
{
return $this->badges[$state];
}
/**
* {@inheritdoc}
*/
public function render()
{
$badges = new Navigation();
$badges->setLayout(Navigation::LAYOUT_TABS);
$this
->createBadgeGroup(
array(static::STATE_CRITICAL, static::STATE_CRITICAL_HANDLED),
$badges
)
->createBadgeGroup(
array(static::STATE_DOWN, static::STATE_DOWN_HANDLED),
$badges
)
->createBadgeGroup(
array(static::STATE_WARNING, static::STATE_WARNING_HANDLED),
$badges
)
->createBadgeGroup(
array(static::STATE_UNREACHABLE, static::STATE_UNREACHABLE_HANDLED),
$badges
)
->createBadge(static::STATE_UNKNOWN, $badges)
->createBadge(static::STATE_OK, $badges)
->createBadge(static::STATE_UP, $badges)
->createBadge(static::STATE_PENDING, $badges);
return $badges
->getRenderer()
->setCssClass(static::CSS_CLASS)
->render();
}
}
|