icingaweb2-module-businessp.../application/forms/SimulationForm.php

98 lines
2.8 KiB
PHP
Raw Normal View History

2014-11-30 06:22:11 -05:00
<?php
2014-11-30 09:56:58 -05:00
namespace Icinga\Module\Businessprocess\Forms;
2014-11-30 06:22:11 -05:00
2014-11-30 09:56:58 -05:00
use Icinga\Module\Businessprocess\BpNode;
2015-03-16 04:08:00 -04:00
use Icinga\Module\Businessprocess\Form;
use Icinga\Module\Businessprocess\Simulation;
use Icinga\Web\Notification;
use Icinga\Web\Request;
2014-11-30 06:22:11 -05:00
class SimulationForm extends Form
{
protected $node;
2015-03-16 04:08:00 -04:00
protected $simulation;
2014-11-30 06:22:11 -05:00
public function __construct($options = null)
{
parent::__construct($options);
$this->setup();
}
public function setup()
{
$this->addElement('select', 'state', array(
'label' => $this->translate('State'),
'multiOptions' => array(
'' => $this->translate('Use current state'),
'0' => $this->translate('OK'),
'1' => $this->translate('WARNING'),
'2' => $this->translate('CRITICAL'),
'3' => $this->translate('UNKNOWN'),
2015-03-16 04:08:00 -04:00
'99' => $this->translate('PENDING'),
2014-11-30 06:22:11 -05:00
)
));
$this->addElement('text', 'output', array(
'label' => $this->translate('Plugin output'),
));
$this->addElement('checkbox', 'acknowledged', array(
'label' => $this->translate('Acknowledged'),
));
$this->addElement('checkbox', 'in_downtime', array(
'label' => $this->translate('In downtime'),
));
$this->addElement('submit', $this->translate('Apply'));
}
public function setNode($node)
{
$this->node = $node;
$this->setDefaults(array(
// TODO: extend descr 'state' => (string) $node->getState(),
'acknowledged' => $node->isAcknowledged(),
'in_downtime' => $node->isInDowntime(),
));
2015-03-16 04:08:00 -04:00
return $this->checkDefaults();
2014-11-30 06:22:11 -05:00
}
2015-03-16 04:08:00 -04:00
public function setSimulation($simulation)
2014-11-30 06:22:11 -05:00
{
2015-03-16 04:08:00 -04:00
$this->simulation = $simulation;
return $this->checkDefaults();
2014-11-30 06:22:11 -05:00
}
2015-03-16 04:08:00 -04:00
protected function checkDefaults()
2014-11-30 06:22:11 -05:00
{
2015-03-16 04:08:00 -04:00
if ($this->node !== null
&& $this->simulation !== null
&& $this->simulation->hasNode((string) $this->node)
) {
$this->setDefaults((array) $this->simulation->getNode((string) $this->node));
2014-11-30 06:22:11 -05:00
}
return $this;
}
public function onSuccess()
{
$node = (string) $this->node;
if ($this->getValue('state') === '') {
2015-03-16 04:08:00 -04:00
if ($this->simulation->remove($node)) {
2014-11-30 06:22:11 -05:00
Notification::success($this->translate('Simulation has been removed'));
}
} else {
2015-03-16 04:08:00 -04:00
Notification::success($this->translate('Simulation has been set'));
$this->simulation->set($node, (object) array(
2014-11-30 06:22:11 -05:00
'state' => $this->getValue('state'),
'acknowledged' => $this->getValue('acknowledged'),
2015-03-16 04:08:00 -04:00
'in_downtime' => $this->getValue('in_downtime'),
));
2014-11-30 06:22:11 -05:00
}
}
}