From 18db984057b83ca4962c89b6b79bdce6a660b58f Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 14:42:35 +0200 Subject: Adding upstream version 2.4.0. Signed-off-by: Daniel Baumann --- application/forms/SimulationForm.php | 136 +++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 application/forms/SimulationForm.php (limited to 'application/forms/SimulationForm.php') diff --git a/application/forms/SimulationForm.php b/application/forms/SimulationForm.php new file mode 100644 index 0000000..263976b --- /dev/null +++ b/application/forms/SimulationForm.php @@ -0,0 +1,136 @@ +enumStateNames(); + + // TODO: Fetch state from object + if ($this->simulatedNode) { + $simulatedState = $this->simulatedNode->getState(); + $states[$simulatedState] = sprintf( + '%s (%s)', + $this->node->getStateName($simulatedState), + $this->translate('Current simulation') + ); + $node = $this->simulatedNode; + $hasSimulation = true; + } else { + $hasSimulation = false; + $node = $this->node; + } + + $view = $this->getView(); + if ($hasSimulation) { + $title = $this->translate('Modify simulation for %s'); + } else { + $title = $this->translate('Add simulation for %s'); + } + $this->addHtml( + '

' + . $view->escape(sprintf($title, $node->getAlias())) + . '

' + ); + + $this->addElement('select', 'state', array( + 'label' => $this->translate('State'), + 'multiOptions' => $states, + 'class' => 'autosubmit', + 'value' => $this->simulatedNode ? $node->getState() : null, + )); + + $sentState = $this->getSentValue('state'); + if (in_array($sentState, array('0', '99'))) { + return; + } + + if ($hasSimulation || ($sentState !== null && ctype_digit($sentState))) { + $this->addElement('checkbox', 'acknowledged', array( + 'label' => $this->translate('Acknowledged'), + 'value' => $node->isAcknowledged(), + )); + + $this->addElement('checkbox', 'in_downtime', array( + 'label' => $this->translate('In downtime'), + 'value' => $node->isInDowntime(), + )); + } + + $this->setSubmitLabel($this->translate('Apply')); + } + + public function setNode($node) + { + $this->node = $node; + return $this; + } + + public function setSimulation(Simulation $simulation) + { + $this->simulation = $simulation; + + $name = $this->node->getName(); + if ($simulation->hasNode($name)) { + $this->simulatedNode = clone($this->node); + $s = $simulation->getNode($name); + $this->simulatedNode->setState($s->state) + ->setAck($s->acknowledged) + ->setDowntime($s->in_downtime) + ->setMissing(false); + } + + return $this; + } + + public function onSuccess() + { + $nodeName = $this->node->getName(); + $state = $this->getValue('state'); + + if ($state !== null && ctype_digit($state)) { + $this->notifySuccess($this->translate('Simulation has been set')); + $this->simulation->set($nodeName, (object) array( + 'state' => $this->getValue('state'), + 'acknowledged' => $this->getValue('acknowledged'), + 'in_downtime' => $this->getValue('in_downtime'), + )); + } else { + if ($this->simulation->remove($nodeName)) { + $this->notifySuccess($this->translate('Simulation has been removed')); + } + } + + parent::onSuccess(); + } + + /** + * @return array + */ + protected function enumStateNames() + { + $states = array( + null => sprintf( + $this->translate('Use current state (%s)'), + $this->translate($this->node->getStateName()) + ) + ) + $this->node->enumStateNames(); + + return $states; + } +} -- cgit v1.2.3