diff --git a/library/Icingadb/Model/State.php b/library/Icingadb/Model/State.php index e28ecfdfb..143420d97 100644 --- a/library/Icingadb/Model/State.php +++ b/library/Icingadb/Model/State.php @@ -4,11 +4,13 @@ namespace Icinga\Module\Icingadb\Model; +use Icinga\Module\Icingadb\Common\Icons; use Icinga\Module\Icingadb\Model\Behavior\BoolCast; use ipl\Orm\Behavior\Binary; use ipl\Orm\Behavior\MillisecondTimestamp; use ipl\Orm\Behaviors; use ipl\Orm\Model; +use ipl\Web\Widget\Icon; /** * Base class for the {@link HostState} and {@link ServiceState} models providing common columns. @@ -78,4 +80,38 @@ public function createBehaviors(Behaviors $behaviors) 'last_comment_id' ])); } + + /** + * Get the state icon + * + * @return Icon|null + */ + public function getIcon(): ?Icon + { + $icon = null; + switch (true) { + case $this->is_acknowledged: + $icon = new Icon(Icons::IS_ACKNOWLEDGED); + break; + case $this->in_downtime: + $icon = new Icon( + Icons::IN_DOWNTIME, + ['title' => sprintf( + '%s (%s)', + strtoupper($this->getStateTextTranslated()), + $this->is_handled ? t('handled by Downtime') : t('in Downtime') + )] + ); + + break; + case $this->is_flapping: + $icon = new Icon(Icons::IS_FLAPPING); + break; + case $this->is_handled: + $icon = new Icon(Icons::HOST_DOWN); + break; + } + + return $icon; + } } diff --git a/library/Icingadb/Widget/ItemList/HostDetailHeader.php b/library/Icingadb/Widget/ItemList/HostDetailHeader.php index 0c90fea4b..3d10ad3ff 100644 --- a/library/Icingadb/Widget/ItemList/HostDetailHeader.php +++ b/library/Icingadb/Widget/ItemList/HostDetailHeader.php @@ -5,10 +5,8 @@ namespace Icinga\Module\Icingadb\Widget\ItemList; use Icinga\Module\Icingadb\Common\HostStates; -use Icinga\Module\Icingadb\Widget\CheckAttempt; use Icinga\Module\Icingadb\Widget\StateChange; use ipl\Html\BaseHtmlElement; -use ipl\Web\Widget\Icon; use ipl\Web\Widget\StateBall; class HostDetailHeader extends HostListItemMinimal @@ -51,11 +49,8 @@ protected function assembleVisual(BaseHtmlElement $visual) } } - if ($this->state->is_handled) { - $currentStateBall = $stateChange->ensureAssembled()->getContent()[1]; - $currentStateBall->addHtml(new Icon($this->getHandledIcon())); - $currentStateBall->getAttributes()->add('class', 'handled'); - } + $stateChange->setIcon($this->state->getIcon()); + $stateChange->setHandled($this->state->is_handled); $visual->addHtml($stateChange); } diff --git a/library/Icingadb/Widget/ItemList/ServiceDetailHeader.php b/library/Icingadb/Widget/ItemList/ServiceDetailHeader.php index 6036929be..a9ad7bbee 100644 --- a/library/Icingadb/Widget/ItemList/ServiceDetailHeader.php +++ b/library/Icingadb/Widget/ItemList/ServiceDetailHeader.php @@ -5,10 +5,8 @@ namespace Icinga\Module\Icingadb\Widget\ItemList; use Icinga\Module\Icingadb\Common\ServiceStates; -use Icinga\Module\Icingadb\Widget\CheckAttempt; use Icinga\Module\Icingadb\Widget\StateChange; use ipl\Html\BaseHtmlElement; -use ipl\Web\Widget\Icon; use ipl\Web\Widget\StateBall; class ServiceDetailHeader extends ServiceListItemMinimal @@ -51,11 +49,8 @@ protected function assembleVisual(BaseHtmlElement $visual) } } - if ($this->state->is_handled) { - $currentStateBall = $stateChange->ensureAssembled()->getContent()[1]; - $currentStateBall->addHtml(new Icon($this->getHandledIcon())); - $currentStateBall->getAttributes()->add('class', 'handled'); - } + $stateChange->setIcon($this->state->getIcon()); + $stateChange->setHandled($this->state->is_handled); $visual->addHtml($stateChange); } diff --git a/library/Icingadb/Widget/ItemList/StateListItem.php b/library/Icingadb/Widget/ItemList/StateListItem.php index f4d43f182..7d50b8a9a 100644 --- a/library/Icingadb/Widget/ItemList/StateListItem.php +++ b/library/Icingadb/Widget/ItemList/StateListItem.php @@ -76,12 +76,9 @@ protected function assembleTitle(BaseHtmlElement $title) protected function assembleVisual(BaseHtmlElement $visual) { $stateBall = new StateBall($this->state->getStateText(), $this->getStateBallSize()); - + $stateBall->add($this->state->getIcon()); if ($this->state->is_handled) { - $stateBall->addHtml(new Icon($this->getHandledIcon())); $stateBall->getAttributes()->add('class', 'handled'); - } elseif ($this->state->getStateText() === 'pending' && $this->state->in_downtime) { - $stateBall->addHtml(new Icon($this->getHandledIcon())); } $visual->addHtml($stateBall); @@ -104,20 +101,6 @@ protected function createTimestamp() } } - protected function getHandledIcon(): string - { - switch (true) { - case $this->state->is_acknowledged: - return Icons::IS_ACKNOWLEDGED; - case $this->state->in_downtime: - return Icons::IN_DOWNTIME; - case $this->state->is_flapping: - return Icons::IS_FLAPPING; - default: - return Icons::HOST_DOWN; - } - } - protected function assemble() { if ($this->state->is_overdue) { diff --git a/library/Icingadb/Widget/ItemTable/StateRowItem.php b/library/Icingadb/Widget/ItemTable/StateRowItem.php index 326322190..2c5fd1c95 100644 --- a/library/Icingadb/Widget/ItemTable/StateRowItem.php +++ b/library/Icingadb/Widget/ItemTable/StateRowItem.php @@ -29,26 +29,12 @@ abstract class StateRowItem extends BaseRowItem /** @var StateItemTable */ protected $list; - protected function getHandledIcon(): string - { - switch (true) { - case $this->item->state->in_downtime: - return Icons::IN_DOWNTIME; - case $this->item->state->is_acknowledged: - return Icons::IS_ACKNOWLEDGED; - case $this->item->state->is_flapping: - return Icons::IS_FLAPPING; - default: - return Icons::HOST_DOWN; - } - } - protected function assembleVisual(BaseHtmlElement $visual) { $stateBall = new StateBall($this->item->state->getStateText(), StateBall::SIZE_LARGE); + $stateBall->add($this->item->state->getIcon()); if ($this->item->state->is_handled) { - $stateBall->addHtml(new Icon($this->getHandledIcon())); $stateBall->getAttributes()->add('class', 'handled'); } diff --git a/library/Icingadb/Widget/StateChange.php b/library/Icingadb/Widget/StateChange.php index 0bf4fa30b..a9987be72 100644 --- a/library/Icingadb/Widget/StateChange.php +++ b/library/Icingadb/Widget/StateChange.php @@ -5,6 +5,7 @@ namespace Icinga\Module\Icingadb\Widget; use ipl\Html\BaseHtmlElement; +use ipl\Web\Widget\Icon; use ipl\Web\Widget\StateBall; class StateChange extends BaseHtmlElement @@ -21,6 +22,12 @@ class StateChange extends BaseHtmlElement protected $tag = 'div'; + /** @var ?Icon Current state ball icon */ + protected $icon; + + /** @var bool Whether the state is handled */ + protected $isHandled = false; + public function __construct(string $state, string $previousState) { $this->previousState = $previousState; @@ -55,20 +62,48 @@ public function setCurrentStateBallSize(string $size): self return $this; } + /** + * Set the current state ball icon + * + * @param $icon + * + * @return $this + */ + public function setIcon($icon): self + { + $this->icon = $icon; + + return $this; + } + + /** + * Set whether the current state is handled + * + * @return $this + */ + public function setHandled($isHandled = true): self + { + $this->isHandled = $isHandled; + + return $this; + } + protected function assemble() { + $currentStateBall = (new StateBall($this->state, $this->currentStateBallSize)) + ->add($this->icon); + + if ($this->isHandled) { + $currentStateBall->getAttributes()->add('class', 'handled'); + } + + $previousStateBall = new StateBall($this->previousState, $this->previousStateBallSize); if ($this->isRightBiggerThanLeft()) { $this->getAttributes()->add('class', 'reversed-state-balls'); - $this->addHtml( - new StateBall($this->state, $this->currentStateBallSize), - new StateBall($this->previousState, $this->previousStateBallSize) - ); + $this->addHtml($currentStateBall, $previousStateBall); } else { - $this->addHtml( - new StateBall($this->previousState, $this->previousStateBallSize), - new StateBall($this->state, $this->currentStateBallSize) - ); + $this->addHtml($previousStateBall, $currentStateBall); } }