Skip to content

Commit

Permalink
Restore StateBadge and StateBadges (#811)
Browse files Browse the repository at this point in the history
Removing and transferring them to ipl-web was a mistake and not thought
through. ipl-icinga could be an alternative location, but that's only
theory right now.

ref #474
  • Loading branch information
nilmerg authored Jul 20, 2023
2 parents 25d1dda + 4a0583f commit a8dac55
Show file tree
Hide file tree
Showing 8 changed files with 399 additions and 7 deletions.
189 changes: 187 additions & 2 deletions library/Icingadb/Common/StateBadges.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,192 @@

namespace Icinga\Module\Icingadb\Common;

/** @deprecated Use {@see \ipl\Web\Common\StateBadges} instead */
abstract class StateBadges extends \ipl\Web\Common\StateBadges
use Icinga\Module\Icingadb\Widget\StateBadge;
use ipl\Html\BaseHtmlElement;
use ipl\Html\Html;
use ipl\Stdlib\BaseFilter;
use ipl\Stdlib\Filter;
use ipl\Web\Filter\QueryString;
use ipl\Web\Url;
use ipl\Web\Widget\Link;

abstract class StateBadges extends BaseHtmlElement
{
use BaseFilter;

/** @var object $item */
protected $item;

/** @var string */
protected $type;

/** @var string Prefix */
protected $prefix;

/** @var Url Badge link */
protected $url;

protected $tag = 'ul';

protected $defaultAttributes = ['class' => 'state-badges'];

/**
* Create a new widget for state badges
*
* @param object $item
*/
public function __construct($item)
{
$this->item = $item;
$this->type = $this->getType();
$this->prefix = $this->getPrefix();
$this->url = $this->getBaseUrl();
}

/**
* Get the badge base URL
*
* @return Url
*/
abstract protected function getBaseUrl(): Url;

/**
* Get the type of the items
*
* @return string
*/
abstract protected function getType(): string;

/**
* Get the prefix for accessing state information
*
* @return string
*/
abstract protected function getPrefix(): string;

/**
* Get the integer of the given state text
*
* @param string $state
*
* @return int
*/
abstract protected function getStateInt(string $state): int;

/**
* Get the badge URL
*
* @return Url
*/
public function getUrl(): Url
{
return $this->url;
}

/**
* Set the badge URL
*
* @param Url $url
*
* @return $this
*/
public function setUrl(Url $url): self
{
$this->url = $url;

return $this;
}

/**
* Create a badge link
*
* @param mixed $content
* @param ?array $filter
*
* @return Link
*/
public function createLink($content, array $filter = null): Link
{
$url = clone $this->getUrl();

$urlFilter = Filter::all();
if (! empty($filter)) {
foreach ($filter as $column => $value) {
$urlFilter->add(Filter::equal($column, $value));
}
}

if ($this->hasBaseFilter()) {
$urlFilter->add($this->getBaseFilter());
}

if (! $urlFilter->isEmpty()) {
$urlParams = $url->getParams()->toArray(false);
$url->setQueryString(QueryString::render($urlFilter))
->addParams($urlParams);
}

return new Link($content, $url);
}

/**
* Create a state bade
*
* @param string $state
*
* @return ?BaseHtmlElement
*/
protected function createBadge(string $state)
{
$key = $this->prefix . "_{$state}";

if (isset($this->item->$key) && $this->item->$key) {
return Html::tag('li', $this->createLink(
new StateBadge($this->item->$key, $state),
[$this->type . '.state.soft_state' => $this->getStateInt($state)]
));
}

return null;
}

/**
* Create a state group
*
* @param string $state
*
* @return ?BaseHtmlElement
*/
protected function createGroup(string $state)
{
$content = [];
$handledKey = $this->prefix . "_{$state}_handled";
$unhandledKey = $this->prefix . "_{$state}_unhandled";

if (isset($this->item->$unhandledKey) && $this->item->$unhandledKey) {
$content[] = Html::tag('li', $this->createLink(
new StateBadge($this->item->$unhandledKey, $state),
[
$this->type . '.state.soft_state' => $this->getStateInt($state),
$this->type . '.state.is_handled' => 'n'
]
));
}

if (isset($this->item->$handledKey) && $this->item->$handledKey) {
$content[] = Html::tag('li', $this->createLink(
new StateBadge($this->item->$handledKey, $state, true),
[
$this->type . '.state.soft_state' => $this->getStateInt($state),
$this->type . '.state.is_handled' => 'y'
]
));
}

if (empty($content)) {
return null;
}

return Html::tag('li', Html::tag('ul', $content));
}
}
2 changes: 1 addition & 1 deletion library/Icingadb/Web/Navigation/Renderer/ProblemsBadge.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
use Exception;
use Icinga\Application\Logger;
use Icinga\Module\Icingadb\Common\Database;
use Icinga\Module\Icingadb\Widget\StateBadge;
use Icinga\Web\Navigation\NavigationItem;
use Icinga\Web\Navigation\Renderer\NavigationItemRenderer;
use ipl\Html\HtmlDocument;
use ipl\Html\HtmlString;
use ipl\Web\Widget\Link;
use ipl\Web\Widget\StateBadge;

abstract class ProblemsBadge extends NavigationItemRenderer
{
Expand Down
2 changes: 1 addition & 1 deletion library/Icingadb/Widget/HostStateBadges.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Icinga\Module\Icingadb\Common\HostStates;
use Icinga\Module\Icingadb\Common\Links;
use ipl\Web\Common\StateBadges;
use Icinga\Module\Icingadb\Common\StateBadges;
use ipl\Web\Url;

class HostStateBadges extends StateBadges
Expand Down
2 changes: 1 addition & 1 deletion library/Icingadb/Widget/ServiceStateBadges.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Icinga\Module\Icingadb\Common\Links;
use Icinga\Module\Icingadb\Common\ServiceStates;
use ipl\Web\Common\StateBadges;
use Icinga\Module\Icingadb\Common\StateBadges;
use ipl\Web\Url;

class ServiceStateBadges extends StateBadges
Expand Down
43 changes: 41 additions & 2 deletions library/Icingadb/Widget/StateBadge.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,46 @@

namespace Icinga\Module\Icingadb\Widget;

/** @deprecated Use {@see \ipl\Web\Widget\StateBadge} instead */
class StateBadge extends \ipl\Web\Widget\StateBadge
use ipl\Html\BaseHtmlElement;

class StateBadge extends BaseHtmlElement
{
protected $defaultAttributes = ['class' => 'state-badge'];

/** @var mixed Badge content */
protected $content;

/** @var bool Whether the state is handled */
protected $isHandled;

/** @var string Textual representation of a state */
protected $state;

/**
* Create a new state badge
*
* @param mixed $content Content of the badge
* @param string $state Textual representation of a state
* @param bool $isHandled True if state is handled
*/
public function __construct($content, string $state, bool $isHandled = false)
{
$this->content = $content;
$this->isHandled = $isHandled;
$this->state = $state;
}

protected function assemble()
{
$this->setTag('span');

$class = "state-{$this->state}";
if ($this->isHandled) {
$class .= ' handled';
}

$this->addAttributes(['class' => $class]);

$this->add($this->content);
}
}
31 changes: 31 additions & 0 deletions public/css/mixin/state-badges.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
.state-badges() {
&.state-badges {
padding: 0;

ul {
padding: 0;
}

li {
display: inline-block;
}

li > ul > li:first-child:not(:last-child) .state-badge {
border-bottom-right-radius: 0;
border-top-right-radius: 0;
}

li > ul > li:last-child:not(:first-child) .state-badge {
border-bottom-left-radius: 0;
border-top-left-radius: 0;
}

> li:not(:last-child) {
margin-right: .25em;
}

li > ul > li + li {
margin-left: 1px;
}
}
}
52 changes: 52 additions & 0 deletions public/css/widget/state-badge.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
.state-badge {
.rounded-corners();
color: @text-color-on-icinga-blue;
display: inline-block;
font-size: 1em;
min-width: 2em;
padding: .25em;
text-align: center;

&.handled {
opacity: .8;
}

&.state-critical {
background-color: @color-critical;
}

&.state-down {
background-color: @color-down;
}

&.state-ok {
background-color: @color-ok;
}

&.state-pending {
background-color: @color-pending;
}

&.state-unknown {
background-color: @color-unknown;
}

&.state-up {
background-color: @color-up;
}

&.state-warning {
background-color: @color-warning;
}

&.state-none {
background-color: @state-none;
color: @text-color-light;
}
}

a .state-badge {
&:not(.disabled):hover {
filter: brightness(80%);
}
}
Loading

0 comments on commit a8dac55

Please sign in to comment.