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
|
|
|
|
2016-11-27 20:11:21 -05:00
|
|
|
use Icinga\Module\Businessprocess\MonitoredNode;
|
2015-03-16 04:08:00 -04:00
|
|
|
use Icinga\Module\Businessprocess\Simulation;
|
2016-11-27 20:11:21 -05:00
|
|
|
use Icinga\Module\Businessprocess\Web\Form\QuickForm;
|
2014-11-30 06:22:11 -05:00
|
|
|
|
2015-10-07 11:10:49 -04:00
|
|
|
class SimulationForm extends QuickForm
|
2014-11-30 06:22:11 -05:00
|
|
|
{
|
2016-11-27 20:11:21 -05:00
|
|
|
/** @var MonitoredNode */
|
2014-11-30 06:22:11 -05:00
|
|
|
protected $node;
|
|
|
|
|
|
2016-11-27 20:11:21 -05:00
|
|
|
/** @var MonitoredNode */
|
2015-10-07 11:10:49 -04:00
|
|
|
protected $simulatedNode;
|
2014-11-30 06:22:11 -05:00
|
|
|
|
2016-11-27 20:11:21 -05:00
|
|
|
/** @var Simulation */
|
2015-10-07 11:10:49 -04:00
|
|
|
protected $simulation;
|
2014-11-30 06:22:11 -05:00
|
|
|
|
|
|
|
|
public function setup()
|
|
|
|
|
{
|
2016-11-27 20:11:21 -05:00
|
|
|
$states = array_merge(
|
|
|
|
|
array(
|
|
|
|
|
null => sprintf(
|
|
|
|
|
$this->translate('Use current state (%s)'),
|
|
|
|
|
$this->translate($this->node->getStateName())
|
|
|
|
|
)
|
2015-10-07 11:10:49 -04:00
|
|
|
),
|
2016-11-27 20:11:21 -05:00
|
|
|
$this->node->enumStateNames()
|
2015-10-07 11:10:49 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// TODO: Fetch state from object
|
|
|
|
|
if ($this->simulatedNode) {
|
2016-11-27 20:11:21 -05:00
|
|
|
$simulatedState = $this->simulatedNode->getState();
|
|
|
|
|
$states[$simulatedState] = sprintf(
|
|
|
|
|
'%s (%s)',
|
|
|
|
|
$simulatedState,
|
|
|
|
|
$this->translate('Current simulation')
|
|
|
|
|
);
|
2015-10-07 11:10:49 -04:00
|
|
|
$node = $this->simulatedNode;
|
|
|
|
|
} else {
|
|
|
|
|
$node = $this->node;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-27 20:11:21 -05:00
|
|
|
$this->addHtml('<h2>Configure this simulation</h2>');
|
|
|
|
|
|
2014-11-30 06:22:11 -05:00
|
|
|
$this->addElement('select', 'state', array(
|
|
|
|
|
'label' => $this->translate('State'),
|
2015-10-07 11:10:49 -04:00
|
|
|
'multiOptions' => $states,
|
2016-11-27 20:11:21 -05:00
|
|
|
'class' => 'autosubmit',
|
2015-10-07 11:10:49 -04:00
|
|
|
'value' => $this->simulatedNode ? $node->getState() : null,
|
2014-11-30 06:22:11 -05:00
|
|
|
));
|
2016-11-27 20:11:21 -05:00
|
|
|
if (ctype_digit($this->getSentValue('state'))) {
|
|
|
|
|
$this->addElement('checkbox', 'acknowledged', array(
|
|
|
|
|
'label' => $this->translate('Acknowledged'),
|
|
|
|
|
'value' => $node->isAcknowledged(),
|
|
|
|
|
));
|
2014-11-30 06:22:11 -05:00
|
|
|
|
2016-11-27 20:11:21 -05:00
|
|
|
$this->addElement('checkbox', 'in_downtime', array(
|
|
|
|
|
'label' => $this->translate('In downtime'),
|
|
|
|
|
'value' => $node->isInDowntime(),
|
|
|
|
|
));
|
|
|
|
|
}
|
2014-11-30 06:22:11 -05:00
|
|
|
|
2015-10-07 11:10:49 -04:00
|
|
|
$this->setSubmitLabel($this->translate('Apply'));
|
2014-11-30 06:22:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function setNode($node)
|
|
|
|
|
{
|
|
|
|
|
$this->node = $node;
|
2015-10-07 11:10:49 -04:00
|
|
|
return $this;
|
2014-11-30 06:22:11 -05:00
|
|
|
}
|
|
|
|
|
|
2016-11-27 20:11:21 -05:00
|
|
|
public function setSimulation(Simulation $simulation)
|
2014-11-30 06:22:11 -05:00
|
|
|
{
|
2015-03-16 04:08:00 -04:00
|
|
|
$this->simulation = $simulation;
|
2014-11-30 06:22:11 -05:00
|
|
|
|
2015-10-07 11:10:49 -04:00
|
|
|
$nodeName = (string) $this->node;
|
|
|
|
|
if ($simulation->hasNode($nodeName)) {
|
|
|
|
|
$this->simulatedNode = clone($this->node);
|
|
|
|
|
$s = $simulation->getNode($nodeName);
|
|
|
|
|
$this->simulatedNode->setState($s->state)
|
|
|
|
|
->setAck($s->acknowledged)
|
|
|
|
|
->setDowntime($s->in_downtime)
|
|
|
|
|
->setMissing(false);
|
2014-11-30 06:22:11 -05:00
|
|
|
}
|
2015-10-07 11:10:49 -04:00
|
|
|
|
2014-11-30 06:22:11 -05:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function onSuccess()
|
|
|
|
|
{
|
2015-10-07 11:10:49 -04:00
|
|
|
$nodeName = (string) $this->node;
|
2014-11-30 06:22:11 -05:00
|
|
|
|
2015-10-07 11:10:49 -04:00
|
|
|
if (ctype_digit($this->getValue('state'))) {
|
|
|
|
|
$this->notifySuccess($this->translate('Simulation has been set'));
|
|
|
|
|
$this->simulation->set($nodeName, (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'),
|
|
|
|
|
));
|
2015-10-07 11:10:49 -04:00
|
|
|
} else {
|
|
|
|
|
if ($this->simulation->remove($nodeName)) {
|
|
|
|
|
$this->notifySuccess($this->translate('Simulation has been removed'));
|
|
|
|
|
}
|
2014-11-30 06:22:11 -05:00
|
|
|
}
|
2016-11-27 20:11:21 -05:00
|
|
|
$this->redirectOnSuccess();
|
2014-11-30 06:22:11 -05:00
|
|
|
}
|
|
|
|
|
}
|