summaryrefslogtreecommitdiffstats
path: root/application/forms
diff options
context:
space:
mode:
Diffstat (limited to 'application/forms')
-rw-r--r--application/forms/AddNodeForm.php412
-rw-r--r--application/forms/BpConfigForm.php236
-rw-r--r--application/forms/BpUploadForm.php207
-rw-r--r--application/forms/CleanupNodeForm.php61
-rw-r--r--application/forms/DeleteNodeForm.php125
-rw-r--r--application/forms/EditNodeForm.php315
-rw-r--r--application/forms/MoveNodeForm.php172
-rw-r--r--application/forms/ProcessForm.php158
-rw-r--r--application/forms/SimulationForm.php138
9 files changed, 1824 insertions, 0 deletions
diff --git a/application/forms/AddNodeForm.php b/application/forms/AddNodeForm.php
new file mode 100644
index 0000000..3840d8a
--- /dev/null
+++ b/application/forms/AddNodeForm.php
@@ -0,0 +1,412 @@
+<?php
+
+namespace Icinga\Module\Businessprocess\Forms;
+
+use Exception;
+use Icinga\Module\Businessprocess\BpConfig;
+use Icinga\Module\Businessprocess\BpNode;
+use Icinga\Module\Businessprocess\Common\Sort;
+use Icinga\Module\Businessprocess\Modification\ProcessChanges;
+use Icinga\Module\Businessprocess\Node;
+use Icinga\Module\Businessprocess\Storage\Storage;
+use Icinga\Module\Businessprocess\Web\Form\Element\IplStateOverrides;
+use Icinga\Module\Businessprocess\Web\Form\Validator\HostServiceTermValidator;
+use Icinga\Module\Monitoring\Backend\MonitoringBackend;
+use Icinga\Web\Session\SessionNamespace;
+use ipl\Html\HtmlElement;
+use ipl\Html\Text;
+use ipl\I18n\Translation;
+use ipl\Stdlib\Str;
+use ipl\Web\Compat\CompatForm;
+use ipl\Web\FormElement\TermInput;
+use ipl\Web\Url;
+
+class AddNodeForm extends CompatForm
+{
+ use Sort;
+ use Translation;
+
+ /** @var Storage */
+ protected $storage;
+
+ /** @var ?BpConfig */
+ protected $bp;
+
+ /** @var ?BpNode */
+ protected $parent;
+
+ /** @var SessionNamespace */
+ protected $session;
+
+ /**
+ * Set the storage to use
+ *
+ * @param Storage $storage
+ *
+ * @return $this
+ */
+ public function setStorage(Storage $storage): self
+ {
+ $this->storage = $storage;
+
+ return $this;
+ }
+
+ /**
+ * Set the affected configuration
+ *
+ * @param BpConfig $bp
+ *
+ * @return $this
+ */
+ public function setProcess(BpConfig $bp): self
+ {
+ $this->bp = $bp;
+
+ return $this;
+ }
+
+ /**
+ * Set the affected sub-process
+ *
+ * @param ?BpNode $node
+ *
+ * @return $this
+ */
+ public function setParentNode(BpNode $node = null): self
+ {
+ $this->parent = $node;
+
+ return $this;
+ }
+
+ /**
+ * Set the user's session
+ *
+ * @param SessionNamespace $session
+ *
+ * @return $this
+ */
+ public function setSession(SessionNamespace $session): self
+ {
+ $this->session = $session;
+
+ return $this;
+ }
+
+ protected function assemble()
+ {
+ if ($this->parent !== null) {
+ $title = sprintf($this->translate('Add a node to %s'), $this->parent->getAlias());
+ $nodeTypes = [
+ 'host' => $this->translate('Host'),
+ 'service' => $this->translate('Service'),
+ 'process' => $this->translate('Existing Process'),
+ 'new-process' => $this->translate('New Process')
+ ];
+ } else {
+ $title = $this->translate('Add a new root node');
+ if (! $this->bp->isEmpty()) {
+ $nodeTypes = [
+ 'process' => $this->translate('Existing Process'),
+ 'new-process' => $this->translate('New Process')
+ ];
+ } else {
+ $nodeTypes = [];
+ }
+ }
+
+ $this->addHtml(new HtmlElement('h2', null, Text::create($title)));
+
+ if (! empty($nodeTypes)) {
+ $this->addElement('select', 'node_type', [
+ 'label' => $this->translate('Node type'),
+ 'options' => array_merge(
+ ['' => ' - ' . $this->translate('Please choose') . ' - '],
+ $nodeTypes
+ ),
+ 'disabledOptions' => [''],
+ 'class' => 'autosubmit',
+ 'required' => true,
+ 'ignore' => true
+ ]);
+
+ $nodeType = $this->getPopulatedValue('node_type');
+ } else {
+ $nodeType = 'new-process';
+ }
+
+ if ($nodeType === 'new-process') {
+ $this->assembleNewProcessElements();
+ } elseif ($nodeType === 'process') {
+ $this->assembleExistingProcessElements();
+ } elseif ($nodeType === 'host') {
+ $this->assembleHostElements();
+ } elseif ($nodeType === 'service') {
+ $this->assembleServiceElements();
+ }
+
+ $this->addElement('submit', 'submit', [
+ 'label' => $this->translate('Add Process')
+ ]);
+ }
+
+ protected function assembleNewProcessElements(): void
+ {
+ $this->addElement('text', 'name', [
+ 'required' => true,
+ 'ignore' => true,
+ 'label' => $this->translate('ID'),
+ 'description' => $this->translate('This is the unique identifier of this process'),
+ 'validators' => [
+ 'callback' => function ($value, $validator) {
+ if ($this->parent !== null ? $this->parent->hasChild($value) : $this->bp->hasRootNode($value)) {
+ $validator->addMessage(
+ sprintf($this->translate('%s is already defined in this process'), $value)
+ );
+
+ return false;
+ }
+
+ return true;
+ }
+ ]
+ ]);
+
+ $this->addElement('text', 'alias', [
+ 'label' => $this->translate('Display Name'),
+ 'description' => $this->translate(
+ 'Usually this name will be shown for this node. Equals ID if not given'
+ ),
+ ]);
+
+ $this->addElement('select', 'operator', [
+ 'required' => true,
+ 'label' => $this->translate('Operator'),
+ 'multiOptions' => Node::getOperators()
+ ]);
+
+ $display = 1;
+ if (! $this->bp->isEmpty() && $this->bp->getMetadata()->isManuallyOrdered()) {
+ $rootNodes = self::applyManualSorting($this->bp->getRootNodes());
+ $display = end($rootNodes)->getDisplay() + 1;
+ }
+ $this->addElement('select', 'display', [
+ 'required' => true,
+ 'label' => $this->translate('Visualization'),
+ 'description' => $this->translate('Where to show this process'),
+ 'value' => $this->parent !== null ? '0' : "$display",
+ 'multiOptions' => [
+ "$display" => $this->translate('Toplevel Process'),
+ '0' => $this->translate('Subprocess only'),
+ ]
+ ]);
+
+ $this->addElement('text', 'infoUrl', [
+ 'label' => $this->translate('Info URL'),
+ 'description' => $this->translate('URL pointing to more information about this node')
+ ]);
+ }
+
+ protected function assembleExistingProcessElements(): void
+ {
+ $termValidator = function (array $terms) {
+ foreach ($terms as $term) {
+ /** @var TermInput\ValidatedTerm $term */
+ $nodeName = $term->getSearchValue();
+ if ($nodeName[0] === '@') {
+ if ($this->parent === null) {
+ $term->setMessage($this->translate('Imported nodes cannot be used as root nodes'));
+ } elseif (strpos($nodeName, ':') === false) {
+ $term->setMessage($this->translate('Missing node name'));
+ } else {
+ [$config, $nodeName] = Str::trimSplit(substr($nodeName, 1), ':', 2);
+ if (! $this->storage->hasProcess($config)) {
+ $term->setMessage($this->translate('Config does not exist or access has been denied'));
+ } else {
+ try {
+ $bp = $this->storage->loadProcess($config);
+ } catch (Exception $e) {
+ $term->setMessage(
+ sprintf($this->translate('Cannot load config: %s'), $e->getMessage())
+ );
+ }
+
+ if (isset($bp)) {
+ if (! $bp->hasNode($nodeName)) {
+ $term->setMessage($this->translate('No node with this name found in config'));
+ } else {
+ $term->setLabel($bp->getNode($nodeName)->getAlias());
+ }
+ }
+ }
+ }
+ } elseif (! $this->bp->hasNode($nodeName)) {
+ $term->setMessage($this->translate('No node with this name found in config'));
+ } else {
+ $term->setLabel($this->bp->getNode($nodeName)->getAlias());
+ }
+
+ if ($this->parent !== null && $this->parent->hasChild($term->getSearchValue())) {
+ $term->setMessage($this->translate('Already defined in this process'));
+ }
+
+ if ($this->parent !== null && $term->getSearchValue() === $this->parent->getName()) {
+ $term->setMessage($this->translate('Results in a parent/child loop'));
+ }
+ }
+ };
+
+ $this->addElement(
+ (new TermInput('children'))
+ ->setRequired()
+ ->setVerticalTermDirection()
+ ->setLabel($this->translate('Process Nodes'))
+ ->setSuggestionUrl(Url::fromPath('businessprocess/suggestions/process', [
+ 'node' => isset($this->parent) ? $this->parent->getName() : null,
+ 'config' => $this->bp->getName(),
+ 'showCompact' => true,
+ '_disableLayout' => true
+ ]))
+ ->on(TermInput::ON_ENRICH, $termValidator)
+ ->on(TermInput::ON_ADD, $termValidator)
+ ->on(TermInput::ON_PASTE, $termValidator)
+ ->on(TermInput::ON_SAVE, $termValidator)
+ );
+ }
+
+ protected function assembleHostElements(): void
+ {
+ if ($this->bp->getBackend() instanceof MonitoringBackend) {
+ $suggestionsPath = 'businessprocess/suggestions/monitoring-host';
+ } else {
+ $suggestionsPath = 'businessprocess/suggestions/icingadb-host';
+ }
+
+ $this->addElement($this->createChildrenElementForObjects(
+ $this->translate('Hosts'),
+ $suggestionsPath
+ ));
+
+ $this->addElement('checkbox', 'host_override', [
+ 'ignore' => true,
+ 'class' => 'autosubmit',
+ 'label' => $this->translate('Override Host State')
+ ]);
+ if ($this->getPopulatedValue('host_override') === 'y') {
+ $this->addElement(new IplStateOverrides('stateOverrides', [
+ 'label' => $this->translate('State Overrides'),
+ 'options' => [
+ 0 => $this->translate('UP'),
+ 1 => $this->translate('DOWN'),
+ 99 => $this->translate('PENDING')
+ ]
+ ]));
+ }
+ }
+
+ protected function assembleServiceElements(): void
+ {
+ if ($this->bp->getBackend() instanceof MonitoringBackend) {
+ $suggestionsPath = 'businessprocess/suggestions/monitoring-service';
+ } else {
+ $suggestionsPath = 'businessprocess/suggestions/icingadb-service';
+ }
+
+ $this->addElement($this->createChildrenElementForObjects(
+ $this->translate('Services'),
+ $suggestionsPath
+ ));
+
+ $this->addElement('checkbox', 'service_override', [
+ 'ignore' => true,
+ 'class' => 'autosubmit',
+ 'label' => $this->translate('Override Service State')
+ ]);
+ if ($this->getPopulatedValue('service_override') === 'y') {
+ $this->addElement(new IplStateOverrides('stateOverrides', [
+ 'label' => $this->translate('State Overrides'),
+ 'options' => [
+ 0 => $this->translate('OK'),
+ 1 => $this->translate('WARNING'),
+ 2 => $this->translate('CRITICAL'),
+ 3 => $this->translate('UNKNOWN'),
+ 99 => $this->translate('PENDING'),
+ ]
+ ]));
+ }
+ }
+
+ protected function createChildrenElementForObjects(string $label, string $suggestionsPath): TermInput
+ {
+ $termValidator = function (array $terms) {
+ (new HostServiceTermValidator())
+ ->setParent($this->parent)
+ ->isValid($terms);
+ };
+
+ return (new TermInput('children'))
+ ->setRequired()
+ ->setLabel($label)
+ ->setVerticalTermDirection()
+ ->setSuggestionUrl(Url::fromPath($suggestionsPath, [
+ 'node' => isset($this->parent) ? $this->parent->getName() : null,
+ 'config' => $this->bp->getName(),
+ 'showCompact' => true,
+ '_disableLayout' => true
+ ]))
+ ->on(TermInput::ON_ENRICH, $termValidator)
+ ->on(TermInput::ON_ADD, $termValidator)
+ ->on(TermInput::ON_PASTE, $termValidator)
+ ->on(TermInput::ON_SAVE, $termValidator);
+ }
+
+ protected function onSuccess()
+ {
+ $changes = ProcessChanges::construct($this->bp, $this->session);
+
+ $nodeType = $this->getValue('node_type');
+ if (! $nodeType || $nodeType === 'new-process') {
+ $properties = $this->getValues();
+ if (! $properties['alias']) {
+ unset($properties['alias']);
+ }
+
+ if ($this->parent !== null) {
+ $properties['parentName'] = $this->parent->getName();
+ }
+
+ $changes->createNode(BpConfig::escapeName($this->getValue('name')), $properties);
+ } else {
+ /** @var TermInput $term */
+ $term = $this->getElement('children');
+ $children = array_unique(array_map(function ($term) {
+ return $term->getSearchValue();
+ }, $term->getTerms()));
+
+ if ($nodeType === 'host' || $nodeType === 'service') {
+ $stateOverrides = $this->getValue('stateOverrides');
+ if (! empty($stateOverrides)) {
+ $childOverrides = [];
+ foreach ($children as $nodeName) {
+ $childOverrides[$nodeName] = $stateOverrides;
+ }
+
+ $changes->modifyNode($this->parent, [
+ 'stateOverrides' => array_merge($this->parent->getStateOverrides(), $childOverrides)
+ ]);
+ }
+ }
+
+ if ($this->parent !== null) {
+ $changes->addChildrenToNode($children, $this->parent);
+ } else {
+ foreach ($children as $nodeName) {
+ $changes->copyNode($nodeName);
+ }
+ }
+ }
+
+ unset($changes);
+ }
+}
diff --git a/application/forms/BpConfigForm.php b/application/forms/BpConfigForm.php
new file mode 100644
index 0000000..8a0bc95
--- /dev/null
+++ b/application/forms/BpConfigForm.php
@@ -0,0 +1,236 @@
+<?php
+
+namespace Icinga\Module\Businessprocess\Forms;
+
+use Icinga\Authentication\Auth;
+use Icinga\Module\Businessprocess\BpConfig;
+use Icinga\Module\Businessprocess\Web\Form\BpConfigBaseForm;
+
+class BpConfigForm extends BpConfigBaseForm
+{
+ protected $deleteButtonName;
+
+ public function setup()
+ {
+ $this->addElement('text', 'name', array(
+ 'label' => $this->translate('ID'),
+ 'required' => true,
+ 'validators' => array(
+ array(
+ 'validator' => 'StringLength',
+ 'options' => array(
+ 'min' => 2,
+ 'max' => 40
+ )
+ ),
+ [
+ 'validator' => 'Regex',
+ 'options' => [
+ 'pattern' => '/^[a-zA-Z0-9](?:[\w\h._-]*)?\w$/',
+ 'messages' => [
+ 'regexNotMatch' => $this->translate(
+ 'Id must only consist of alphanumeric characters.'
+ . ' Underscore at the beginning and space, dot and hyphen at the beginning'
+ . ' and end are not allowed.'
+ )
+ ]
+ ]
+ ]
+ ),
+ 'description' => $this->translate(
+ 'This is the unique identifier of this process'
+ ),
+ ));
+
+ $this->addElement('text', 'Title', array(
+ 'label' => $this->translate('Display Name'),
+ 'description' => $this->translate(
+ 'Usually this name will be shown for this process. Equals ID'
+ . ' if not given'
+ ),
+ ));
+
+ $this->addElement('textarea', 'Description', array(
+ 'label' => $this->translate('Description'),
+ 'description' => $this->translate(
+ 'A slightly more detailed description for this process, about 100-150 characters long'
+ ),
+ 'rows' => 4,
+ ));
+
+ if (! empty($this->listAvailableBackends())) {
+ $this->addElement('select', 'Backend', array(
+ 'label' => $this->translate('Backend'),
+ 'description' => $this->translate(
+ 'Icinga Web Monitoring Backend where current object states for'
+ . ' this process should be retrieved from'
+ ),
+ 'multiOptions' => array(
+ null => $this->translate('Use the configured default backend'),
+ ) + $this->listAvailableBackends()
+ ));
+ }
+
+ $this->addElement('select', 'Statetype', array(
+ 'label' => $this->translate('State Type'),
+ 'required' => true,
+ 'description' => $this->translate(
+ 'Whether this process should be based on Icinga hard or soft states'
+ ),
+ 'multiOptions' => array(
+ 'soft' => $this->translate('Use SOFT states'),
+ 'hard' => $this->translate('Use HARD states'),
+ )
+ ));
+
+ $this->addElement('select', 'AddToMenu', array(
+ 'label' => $this->translate('Add to menu'),
+ 'required' => true,
+ 'description' => $this->translate(
+ 'Whether this process should be linked in the main Icinga Web 2 menu'
+ ),
+ 'multiOptions' => array(
+ 'yes' => $this->translate('Yes'),
+ 'no' => $this->translate('No'),
+ )
+ ));
+
+ $this->addElement('text', 'AllowedUsers', array(
+ 'label' => $this->translate('Allowed Users'),
+ 'description' => $this->translate(
+ 'Allowed Users (comma-separated)'
+ ),
+ ));
+
+ $this->addElement('text', 'AllowedGroups', array(
+ 'label' => $this->translate('Allowed Groups'),
+ 'description' => $this->translate(
+ 'Allowed Groups (comma-separated)'
+ ),
+ ));
+
+ $this->addElement('text', 'AllowedRoles', array(
+ 'label' => $this->translate('Allowed Roles'),
+ 'description' => $this->translate(
+ 'Allowed Roles (comma-separated)'
+ ),
+ ));
+
+ if ($this->bp === null) {
+ $this->setSubmitLabel(
+ $this->translate('Add')
+ );
+ } else {
+ $config = $this->bp;
+
+ $meta = $config->getMetadata();
+ foreach ($meta->getProperties() as $k => $v) {
+ if ($el = $this->getElement($k)) {
+ $el->setValue($v);
+ }
+ }
+ $this->getElement('name')
+ ->setValue($config->getName())
+ ->setAttrib('readonly', true);
+
+ $this->setSubmitLabel(
+ $this->translate('Store')
+ );
+
+ $label = $this->translate('Delete');
+ $el = $this->createElement('submit', $label, array(
+ 'data-base-target' => '_main'
+ ))->setLabel($label)->setDecorators(array('ViewHelper'));
+ $this->deleteButtonName = $el->getName();
+ $this->addElement($el);
+ }
+ }
+
+ protected function onSetup()
+ {
+ $this->getElement($this->getSubmitLabel())->setAttrib('data-base-target', '_main');
+ }
+
+ protected function onRequest()
+ {
+ $name = $this->getValue('name');
+
+ if ($this->shouldBeDeleted()) {
+ if ($this->bp->isReferenced()) {
+ $this->addError(sprintf(
+ $this->translate('Process "%s" cannot be deleted as it has been referenced in other processes'),
+ $name
+ ));
+ } else {
+ $this->bp->clearAppliedChanges();
+ $this->storage->deleteProcess($name);
+ $this->setSuccessUrl('businessprocess');
+ $this->redirectOnSuccess(sprintf('Process %s has been deleted', $name));
+ }
+ }
+ }
+
+ public function onSuccess()
+ {
+ $name = $this->getValue('name');
+
+ if ($this->bp === null) {
+ if ($this->storage->hasProcess($name)) {
+ $this->addError(sprintf(
+ $this->translate('A process named "%s" already exists'),
+ $name
+ ));
+
+ return;
+ }
+
+ // New config
+ $config = new BpConfig();
+ $config->setName($name);
+
+ if (! $this->prepareMetadata($config)) {
+ return;
+ }
+
+ $this->setSuccessUrl(
+ $this->getSuccessUrl()->setParams(
+ array('config' => $name, 'unlocked' => true)
+ )
+ );
+ $this->setSuccessMessage(sprintf('Process %s has been created', $name));
+ } else {
+ $config = $this->bp;
+ $this->setSuccessMessage(sprintf('Process %s has been stored', $name));
+ }
+ $meta = $config->getMetadata();
+ foreach ($this->getValues() as $key => $value) {
+ if (! in_array($key, ['Title', 'Description', 'Backend'], true)
+ && ($value === null || $value === '')) {
+ continue;
+ }
+
+ if ($meta->hasKey($key)) {
+ $meta->set($key, $value);
+ }
+ }
+
+ $this->storage->storeProcess($config);
+ $config->clearAppliedChanges();
+ parent::onSuccess();
+ }
+
+ public function hasDeleteButton()
+ {
+ return $this->deleteButtonName !== null;
+ }
+
+ public function shouldBeDeleted()
+ {
+ if (! $this->hasDeleteButton()) {
+ return false;
+ }
+
+ $name = $this->deleteButtonName;
+ return $this->getSentValue($name) === $this->getElement($name)->getLabel();
+ }
+}
diff --git a/application/forms/BpUploadForm.php b/application/forms/BpUploadForm.php
new file mode 100644
index 0000000..a746740
--- /dev/null
+++ b/application/forms/BpUploadForm.php
@@ -0,0 +1,207 @@
+<?php
+
+namespace Icinga\Module\Businessprocess\Forms;
+
+use Exception;
+use Icinga\Module\Businessprocess\BpConfig;
+use Icinga\Module\Businessprocess\Storage\LegacyConfigParser;
+use Icinga\Module\Businessprocess\Web\Form\BpConfigBaseForm;
+use Icinga\Web\Notification;
+
+class BpUploadForm extends BpConfigBaseForm
+{
+ protected $node;
+
+ protected $objectList = array();
+
+ protected $processList = array();
+
+ protected $deleteButtonName;
+
+ private $sourceCode;
+
+ /** @var BpConfig */
+ private $uploadedConfig;
+
+ public function setup()
+ {
+ $this->showUpload();
+ if ($this->hasSource()) {
+ $this->showDetails();
+ }
+ }
+
+ protected function showDetails()
+ {
+ $this->addElement('text', 'name', array(
+ 'label' => $this->translate('Name'),
+ 'required' => true,
+ 'description' => $this->translate(
+ 'This is the unique identifier of this process'
+ ),
+ 'validators' => array(
+ array(
+ 'validator' => 'StringLength',
+ 'options' => array(
+ 'min' => 2,
+ 'max' => 40
+ )
+ ),
+ [
+ 'validator' => 'Regex',
+ 'options' => [
+ 'pattern' => '/^[a-zA-Z0-9](?:[\w\h._-]*)?\w$/',
+ 'messages' => [
+ 'regexNotMatch' => $this->translate(
+ 'Id must only consist of alphanumeric characters.'
+ . ' Underscore at the beginning and space, dot and hyphen at the beginning'
+ . ' and end are not allowed.'
+ )
+ ]
+ ]
+ ]
+ ),
+ ));
+
+ $this->addElement('textarea', 'source', array(
+ 'label' => $this->translate('Source'),
+ 'description' => $this->translate(
+ 'Business process source code'
+ ),
+ 'value' => $this->sourceCode,
+ 'class' => 'preformatted smaller',
+ 'rows' => 7,
+ ));
+
+ $this->getUploadedConfig();
+
+ $this->setSubmitLabel(
+ $this->translate('Store')
+ );
+ }
+
+ public function getUploadedConfig()
+ {
+ if ($this->uploadedConfig === null) {
+ $this->uploadedConfig = $this->parseSubmittedSourceCode();
+ }
+
+ return $this->uploadedConfig;
+ }
+
+ protected function parseSubmittedSourceCode()
+ {
+ $code = $this->getSentValue('source');
+ $name = $this->getSentValue('name', '<new config>');
+ if (empty($code)) {
+ $code = $this->sourceCode;
+ }
+
+ try {
+ $config = LegacyConfigParser::parseString($name, $code);
+
+ if ($config->hasErrors()) {
+ foreach ($config->getErrors() as $error) {
+ $this->addError($error);
+ }
+ }
+ } catch (Exception $e) {
+ $this->addError($e->getMessage());
+ return null;
+ }
+
+ return $config;
+ }
+
+ protected function hasSource()
+ {
+ if ($this->hasBeenSent() && $source = $this->getSentValue('source')) {
+ $this->sourceCode = $source;
+ } else {
+ $this->processUploadedSource();
+ }
+
+ if (empty($this->sourceCode)) {
+ return false;
+ } else {
+ $this->removeElement('uploaded_file');
+ return true;
+ }
+ }
+
+ protected function showUpload()
+ {
+ $this->setAttrib('enctype', 'multipart/form-data');
+
+ $this->addElement('file', 'uploaded_file', array(
+ 'label' => $this->translate('File'),
+ 'destination' => $this->getTempDir(),
+ 'required' => true,
+ ));
+
+ /** @var \Zend_Form_Element_File $el */
+ $el = $this->getElement('uploaded_file');
+ $el->setValueDisabled(true);
+
+ $this->setSubmitLabel(
+ $this->translate('Next')
+ );
+ }
+
+ protected function getTempDir()
+ {
+ return sys_get_temp_dir();
+ }
+
+ protected function processUploadedSource()
+ {
+ /** @var ?\Zend_Form_Element_File $el */
+ $el = $this->getElement('uploaded_file');
+
+ if ($el && $this->hasBeenSent()) {
+ $tmpdir = $this->getTempDir();
+ $tmpfile = tempnam($tmpdir, 'bpupload_');
+
+ // TODO: race condition, try to do this without unlinking here
+ unlink($tmpfile);
+
+ $el->addFilter('Rename', $tmpfile);
+ if ($el->receive()) {
+ $this->sourceCode = file_get_contents($tmpfile);
+ unlink($tmpfile);
+ } else {
+ foreach ($el->file->getMessages() as $error) {
+ $this->addError($error);
+ }
+ }
+ }
+
+ return $this;
+ }
+
+ public function onSuccess()
+ {
+ $config = $this->getUploadedConfig();
+ $name = $config->getName();
+
+ if ($this->storage->hasProcess($name)) {
+ $this->addError(sprintf(
+ $this->translate('A process named "%s" already exists'),
+ $name
+ ));
+
+ return;
+ }
+
+ if (! $this->prepareMetadata($config)) {
+ return;
+ }
+
+ $this->storage->storeProcess($config);
+ Notification::success(sprintf('Process %s has been stored', $name));
+
+ $this->getSuccessUrl()->setParam('config', $name);
+
+ parent::onSuccess();
+ }
+}
diff --git a/application/forms/CleanupNodeForm.php b/application/forms/CleanupNodeForm.php
new file mode 100644
index 0000000..c6e5398
--- /dev/null
+++ b/application/forms/CleanupNodeForm.php
@@ -0,0 +1,61 @@
+<?php
+
+namespace Icinga\Module\Businessprocess\Forms;
+
+use Icinga\Module\Businessprocess\BpConfig;
+use Icinga\Module\Businessprocess\Modification\ProcessChanges;
+use Icinga\Module\Businessprocess\Web\Form\BpConfigBaseForm;
+use Icinga\Module\Monitoring\Backend\MonitoringBackend;
+use Icinga\Web\Session\SessionNamespace;
+use ipl\Html\Html;
+use ipl\Sql\Connection as IcingaDbConnection;
+
+class CleanupNodeForm extends BpConfigBaseForm
+{
+ /** @var MonitoringBackend|IcingaDbConnection */
+ protected $backend;
+
+ /** @var BpConfig */
+ protected $bp;
+
+ /** @var SessionNamespace */
+ protected $session;
+
+ public function setup()
+ {
+ $this->addHtml(Html::tag('h2', $this->translate('Cleanup missing nodes')));
+
+ $this->addElement('checkbox', 'cleanup_all', [
+ 'class' => 'autosubmit',
+ 'label' => $this->translate('Cleanup all missing nodes'),
+ 'description' => $this->translate('Remove all missing nodes from config')
+ ]);
+
+ if ($this->getSentValue('cleanup_all') !== '1') {
+ $this->addElement('multiselect', 'nodes', [
+ 'label' => $this->translate('Select nodes to cleanup'),
+ 'required' => true,
+ 'size' => 8,
+ 'multiOptions' => $this->bp->getMissingChildren()
+ ]);
+ }
+ }
+
+ public function onSuccess()
+ {
+ $changes = ProcessChanges::construct($this->bp, $this->session);
+
+ $nodesToCleanup = $this->getValue('cleanup_all') === '1'
+ ? array_keys($this->bp->getMissingChildren())
+ : $this->getValue('nodes');
+
+ foreach ($nodesToCleanup as $nodeName) {
+ $node = $this->bp->getNode($nodeName);
+ $changes->deleteNode($node);
+ }
+
+ unset($changes);
+
+ parent::onSuccess();
+ }
+}
diff --git a/application/forms/DeleteNodeForm.php b/application/forms/DeleteNodeForm.php
new file mode 100644
index 0000000..dba0710
--- /dev/null
+++ b/application/forms/DeleteNodeForm.php
@@ -0,0 +1,125 @@
+<?php
+
+namespace Icinga\Module\Businessprocess\Forms;
+
+use Icinga\Module\Businessprocess\BpNode;
+use Icinga\Module\Businessprocess\Modification\ProcessChanges;
+use Icinga\Module\Businessprocess\Node;
+use Icinga\Module\Businessprocess\Web\Form\BpConfigBaseForm;
+use Icinga\Web\View;
+
+class DeleteNodeForm extends BpConfigBaseForm
+{
+ /** @var Node */
+ protected $node;
+
+ /** @var ?BpNode */
+ protected $parentNode;
+
+ public function setup()
+ {
+ $node = $this->node;
+ $nodeName = $node->getAlias() ?? $node->getName();
+
+ /** @var View $view */
+ $view = $this->getView();
+ $this->addHtml(
+ '<h2>' . $view->escape(
+ sprintf($this->translate('Delete "%s"'), $nodeName)
+ ) . '</h2>'
+ );
+
+ $biLink = $view->qlink(
+ $nodeName,
+ 'businessprocess/node/impact',
+ array('name' => $node->getName()),
+ array('data-base-target' => '_next')
+ );
+ $this->addHtml(
+ '<p>' . sprintf(
+ $view->escape(
+ $this->translate('Unsure? Show business impact of "%s"')
+ ),
+ $biLink
+ ) . '</p>'
+ );
+
+ if ($this->parentNode) {
+ $yesMsg = sprintf(
+ $this->translate('Delete from %s'),
+ $this->parentNode->getAlias()
+ );
+ } else {
+ $yesMsg = sprintf(
+ $this->translate('Delete root node "%s"'),
+ $nodeName
+ );
+ }
+
+ $this->addElement('select', 'confirm', array(
+ 'label' => $this->translate('Are you sure?'),
+ 'required' => true,
+ 'description' => $this->translate(
+ 'Do you really want to delete this node?'
+ ),
+ 'multiOptions' => $this->optionalEnum(array(
+ 'no' => $this->translate('No'),
+ 'yes' => $yesMsg,
+ 'all' => sprintf($this->translate('Delete all occurrences of %s'), $nodeName),
+ ))
+ ));
+ }
+
+ /**
+ * @param Node $node
+ * @return $this
+ */
+ public function setNode(Node $node)
+ {
+ $this->node = $node;
+ return $this;
+ }
+
+ /**
+ * @param BpNode|null $node
+ * @return $this
+ */
+ public function setParentNode(BpNode $node = null)
+ {
+ $this->parentNode = $node;
+ return $this;
+ }
+
+ public function onSuccess()
+ {
+ $changes = ProcessChanges::construct($this->bp, $this->session);
+
+ $confirm = $this->getValue('confirm');
+ switch ($confirm) {
+ case 'yes':
+ $changes->deleteNode($this->node, $this->parentNode === null ? null : $this->parentNode->getName());
+ break;
+ case 'all':
+ $changes->deleteNode($this->node);
+ break;
+ case 'no':
+ $this->setSuccessMessage($this->translate('Well, maybe next time'));
+ }
+
+ switch ($confirm) {
+ case 'yes':
+ case 'all':
+ if ($this->successUrl === null) {
+ $this->successUrl = clone $this->getRequest()->getUrl();
+ }
+
+ $this->successUrl->getParams()->remove(array('action', 'deletenode'));
+ }
+
+ // Trigger session desctruction to make sure it get's stored.
+ // TODO: figure out why this is necessary, might be an unclean shutdown on redirect
+ unset($changes);
+
+ parent::onSuccess();
+ }
+}
diff --git a/application/forms/EditNodeForm.php b/application/forms/EditNodeForm.php
new file mode 100644
index 0000000..bd1592b
--- /dev/null
+++ b/application/forms/EditNodeForm.php
@@ -0,0 +1,315 @@
+<?php
+
+namespace Icinga\Module\Businessprocess\Forms;
+
+use Icinga\Module\Businessprocess\BpConfig;
+use Icinga\Module\Businessprocess\BpNode;
+use Icinga\Module\Businessprocess\Modification\ProcessChanges;
+use Icinga\Module\Businessprocess\Node;
+use Icinga\Module\Businessprocess\ServiceNode;
+use Icinga\Module\Businessprocess\Web\Form\Element\IplStateOverrides;
+use Icinga\Module\Businessprocess\Web\Form\Validator\HostServiceTermValidator;
+use Icinga\Module\Monitoring\Backend\MonitoringBackend;
+use Icinga\Web\Session\SessionNamespace;
+use ipl\Html\Attributes;
+use ipl\Html\FormattedString;
+use ipl\Html\HtmlElement;
+use ipl\Html\ValidHtml;
+use ipl\I18n\Translation;
+use ipl\Web\Compat\CompatForm;
+use ipl\Web\FormElement\TermInput\ValidatedTerm;
+use ipl\Web\Url;
+
+class EditNodeForm extends CompatForm
+{
+ use Translation;
+
+ /** @var ?BpConfig */
+ protected $bp;
+
+ /** @var ?Node */
+ protected $node;
+
+ /** @var ?BpNode */
+ protected $parent;
+
+ /** @var SessionNamespace */
+ protected $session;
+
+ /**
+ * Set the affected configuration
+ *
+ * @param BpConfig $bp
+ *
+ * @return $this
+ */
+ public function setProcess(BpConfig $bp): self
+ {
+ $this->bp = $bp;
+
+ return $this;
+ }
+
+ /**
+ * Set the affected node
+ *
+ * @param Node $node
+ *
+ * @return $this
+ */
+ public function setNode(Node $node): self
+ {
+ $this->node = $node;
+
+ $this->populate([
+ 'node-search' => $node->getName(),
+ 'node-label' => $node->getAlias()
+ ]);
+
+ return $this;
+ }
+
+ /**
+ * Set the affected sub-process
+ *
+ * @param ?BpNode $node
+ *
+ * @return $this
+ */
+ public function setParentNode(BpNode $node = null): self
+ {
+ $this->parent = $node;
+
+ if ($this->node !== null) {
+ $stateOverrides = $this->parent->getStateOverrides($this->node->getName());
+ if (! empty($stateOverrides)) {
+ $this->populate([
+ 'overrideStates' => 'y',
+ 'stateOverrides' => $stateOverrides
+ ]);
+ }
+ }
+
+ return $this;
+ }
+
+ /**
+ * Set the user's session
+ *
+ * @param SessionNamespace $session
+ *
+ * @return $this
+ */
+ public function setSession(SessionNamespace $session): self
+ {
+ $this->session = $session;
+
+ return $this;
+ }
+
+ /**
+ * Identify and return the node the user has chosen
+ *
+ * @return Node
+ */
+ protected function identifyChosenNode(): Node
+ {
+ $userInput = $this->getPopulatedValue('node');
+ $nodeName = $this->getPopulatedValue('node-search');
+ $nodeLabel = $this->getPopulatedValue('node-label');
+
+ if ($nodeName && $userInput === $nodeLabel) {
+ // User accepted a suggestion and didn't change it manually
+ $node = $this->bp->getNode($nodeName);
+ } elseif ($userInput && (! $nodeLabel || $userInput !== $nodeLabel)) {
+ // User didn't choose a suggestion or changed it manually
+ $node = $this->bp->getNode(BpConfig::joinNodeName($userInput, 'Hoststatus'));
+ } else {
+ // If the search and user input are both empty, it can only be the initial value
+ $node = $this->node;
+ }
+
+ return $node;
+ }
+
+ protected function assemble()
+ {
+ $this->addHtml(new HtmlElement('h2', null, FormattedString::create(
+ $this->translate('Modify "%s"'),
+ $this->node->getAlias() ?? $this->node->getName()
+ )));
+
+ if ($this->node instanceof ServiceNode) {
+ $this->assembleServiceElements();
+ } else {
+ $this->assembleHostElements();
+ }
+
+ $this->addElement('submit', 'btn_submit', [
+ 'label' => $this->translate('Save Changes')
+ ]);
+ }
+
+ protected function assembleServiceElements(): void
+ {
+ if ($this->bp->getBackend() instanceof MonitoringBackend) {
+ $suggestionsPath = 'businessprocess/suggestions/monitoring-service';
+ } else {
+ $suggestionsPath = 'businessprocess/suggestions/icingadb-service';
+ }
+
+ $node = $this->identifyChosenNode();
+
+ $this->addHtml($this->createSearchInput(
+ $this->translate('Service'),
+ $node->getAlias() ?? $node->getName(),
+ $suggestionsPath
+ ));
+
+ $this->addElement('checkbox', 'overrideStates', [
+ 'ignore' => true,
+ 'class' => 'autosubmit',
+ 'label' => $this->translate('Override Service State')
+ ]);
+ if ($this->getPopulatedValue('overrideStates') === 'y') {
+ $this->addElement(new IplStateOverrides('stateOverrides', [
+ 'label' => $this->translate('State Overrides'),
+ 'options' => [
+ 0 => $this->translate('OK'),
+ 1 => $this->translate('WARNING'),
+ 2 => $this->translate('CRITICAL'),
+ 3 => $this->translate('UNKNOWN'),
+ 99 => $this->translate('PENDING'),
+ ]
+ ]));
+ }
+ }
+
+ protected function assembleHostElements(): void
+ {
+ if ($this->bp->getBackend() instanceof MonitoringBackend) {
+ $suggestionsPath = 'businessprocess/suggestions/monitoring-host';
+ } else {
+ $suggestionsPath = 'businessprocess/suggestions/icingadb-host';
+ }
+
+ $node = $this->identifyChosenNode();
+
+ $this->addHtml($this->createSearchInput(
+ $this->translate('Host'),
+ $node->getAlias() ?? $node->getName(),
+ $suggestionsPath
+ ));
+
+ $this->addElement('checkbox', 'overrideStates', [
+ 'ignore' => true,
+ 'class' => 'autosubmit',
+ 'label' => $this->translate('Override Host State')
+ ]);
+ if ($this->getPopulatedValue('overrideStates') === 'y') {
+ $this->addElement(new IplStateOverrides('stateOverrides', [
+ 'label' => $this->translate('State Overrides'),
+ 'options' => [
+ 0 => $this->translate('UP'),
+ 1 => $this->translate('DOWN'),
+ 99 => $this->translate('PENDING')
+ ]
+ ]));
+ }
+ }
+
+ protected function createSearchInput(string $label, string $value, string $suggestionsPath): ValidHtml
+ {
+ $userInput = $this->createElement('text', 'node', [
+ 'ignore' => true,
+ 'required' => true,
+ 'autocomplete' => 'off',
+ 'label' => $label,
+ 'value' => $value,
+ 'data-enrichment-type' => 'completion',
+ 'data-term-suggestions' => '#node-suggestions',
+ 'data-suggest-url' => Url::fromPath($suggestionsPath, [
+ 'node' => isset($this->parent) ? $this->parent->getName() : null,
+ 'config' => $this->bp->getName(),
+ 'showCompact' => true,
+ '_disableLayout' => true
+ ]),
+ 'validators' => ['callback' => function ($_, $validator) {
+ $newName = $this->identifyChosenNode()->getName();
+ if ($newName === $this->node->getName()) {
+ return true;
+ }
+
+ $term = new ValidatedTerm($newName);
+
+ (new HostServiceTermValidator())
+ ->setParent($this->parent)
+ ->isValid($term);
+
+ if (! $term->isValid()) {
+ $validator->addMessage($term->getMessage());
+ return false;
+ }
+
+ return true;
+ }]
+ ]);
+
+ $fieldset = new HtmlElement('fieldset');
+
+ $searchInput = $this->createElement('hidden', 'node-search', ['ignore' => true]);
+ $this->registerElement($searchInput);
+ $fieldset->addHtml($searchInput);
+
+ $labelInput = $this->createElement('hidden', 'node-label', ['ignore' => true]);
+ $this->registerElement($labelInput);
+ $fieldset->addHtml($labelInput);
+
+ $this->registerElement($userInput);
+ $this->decorate($userInput);
+
+ $fieldset->addHtml(
+ $userInput,
+ new HtmlElement('div', Attributes::create([
+ 'id' => 'node-suggestions',
+ 'class' => 'search-suggestions'
+ ]))
+ );
+
+ return $fieldset;
+ }
+
+ protected function onSuccess()
+ {
+ $changes = ProcessChanges::construct($this->bp, $this->session);
+
+ $children = $this->parent->getChildNames();
+ $previousPos = array_search($this->node->getName(), $children, true);
+ $node = $this->identifyChosenNode();
+ $nodeName = $node->getName();
+
+ $changes->deleteNode($this->node, $this->parent->getName());
+ $changes->addChildrenToNode([$nodeName], $this->parent);
+
+ $stateOverrides = $this->getValue('stateOverrides');
+ if (! empty($stateOverrides)) {
+ $changes->modifyNode($this->parent, [
+ 'stateOverrides' => array_merge($this->parent->getStateOverrides(), [
+ $nodeName => $stateOverrides
+ ])
+ ]);
+ }
+
+ if ($this->bp->getMetadata()->isManuallyOrdered() && ($newPos = count($children) - 1) > $previousPos) {
+ $changes->moveNode(
+ $node,
+ $newPos,
+ $previousPos,
+ $this->parent->getName(),
+ $this->parent->getName()
+ );
+ }
+
+ unset($changes);
+ }
+}
diff --git a/application/forms/MoveNodeForm.php b/application/forms/MoveNodeForm.php
new file mode 100644
index 0000000..81d15c7
--- /dev/null
+++ b/application/forms/MoveNodeForm.php
@@ -0,0 +1,172 @@
+<?php
+
+namespace Icinga\Module\Businessprocess\Forms;
+
+use Icinga\Application\Icinga;
+use Icinga\Application\Web;
+use Icinga\Exception\Http\HttpException;
+use Icinga\Module\Businessprocess\BpConfig;
+use Icinga\Module\Businessprocess\BpNode;
+use Icinga\Module\Businessprocess\Exception\ModificationError;
+use Icinga\Module\Businessprocess\Modification\ProcessChanges;
+use Icinga\Module\Businessprocess\Node;
+use Icinga\Module\Businessprocess\Web\Form\BpConfigBaseForm;
+use Icinga\Module\Businessprocess\Web\Form\CsrfToken;
+use Icinga\Web\Session;
+use Icinga\Web\Session\SessionNamespace;
+
+class MoveNodeForm extends BpConfigBaseForm
+{
+ /** @var BpConfig */
+ protected $bp;
+
+ /** @var Node */
+ protected $node;
+
+ /** @var BpNode */
+ protected $parentNode;
+
+ /** @var SessionNamespace */
+ protected $session;
+
+ public function __construct($options = null)
+ {
+ parent::__construct($options);
+
+ // Zend's plugin loader reverses the order of added prefix paths thus trying our paths first before trying
+ // Zend paths
+ $this->addPrefixPaths(array(
+ array(
+ 'prefix' => 'Icinga\\Web\\Form\\Element\\',
+ 'path' => Icinga::app()->getLibraryDir('Icinga/Web/Form/Element'),
+ 'type' => static::ELEMENT
+ ),
+ array(
+ 'prefix' => 'Icinga\\Web\\Form\\Decorator\\',
+ 'path' => Icinga::app()->getLibraryDir('Icinga/Web/Form/Decorator'),
+ 'type' => static::DECORATOR
+ )
+ ));
+ }
+
+ public function setup()
+ {
+ $this->addElement(
+ 'text',
+ 'parent',
+ [
+ 'allowEmpty' => true,
+ 'filters' => ['Null'],
+ 'validators' => [
+ ['Callback', true, [
+ 'callback' => function ($name) {
+ return empty($name) || $this->bp->hasBpNode($name);
+ },
+ 'messages' => [
+ 'callbackValue' => $this->translate('No process found with name %value%')
+ ]
+ ]]
+ ]
+ ]
+ );
+ $this->addElement(
+ 'number',
+ 'from',
+ [
+ 'required' => true,
+ 'min' => 0
+ ]
+ );
+ $this->addElement(
+ 'number',
+ 'to',
+ [
+ 'required' => true,
+ 'min' => 0
+ ]
+ );
+ $this->addElement(
+ 'hidden',
+ 'csrfToken',
+ [
+ 'required' => true
+ ]
+ );
+
+ $this->setSubmitLabel('movenode');
+ }
+
+ /**
+ * @param Node $node
+ * @return $this
+ */
+ public function setNode(Node $node)
+ {
+ $this->node = $node;
+ return $this;
+ }
+
+ /**
+ * @param BpNode|null $node
+ * @return $this
+ */
+ public function setParentNode(BpNode $node = null)
+ {
+ $this->parentNode = $node;
+ return $this;
+ }
+
+ public function onSuccess()
+ {
+ if (! CsrfToken::isValid($this->getValue('csrfToken'))) {
+ throw new HttpException(403, 'nope');
+ }
+
+ $changes = ProcessChanges::construct($this->bp, $this->session);
+ if (! $this->bp->getMetadata()->isManuallyOrdered()) {
+ $changes->applyManualOrder();
+ }
+
+ try {
+ $changes->moveNode(
+ $this->node,
+ $this->getValue('from'),
+ $this->getValue('to'),
+ $this->getValue('parent'),
+ $this->parentNode !== null ? $this->parentNode->getName() : null
+ );
+ } catch (ModificationError $e) {
+ $this->notifyError($e->getMessage());
+ /** @var Web $app */
+ $app = Icinga::app();
+ $app->getResponse()
+ // Web 2's JS forces a content update for non-200s. Our own JS
+ // can't prevent this, hence we're not making this a 400 :(
+ //->setHttpResponseCode(400)
+ ->setHeader('X-Icinga-Container', 'ignore')
+ ->sendResponse();
+ exit;
+ }
+
+ // Trigger session destruction to make sure it get's stored.
+ unset($changes);
+
+ $this->notifySuccess($this->getSuccessMessage($this->translate('Node order updated')));
+
+ $response = $this->getRequest()->getResponse()
+ ->setHeader('X-Icinga-Container', 'ignore')
+ ->setHeader('X-Icinga-Extra-Updates', implode(';', [
+ $this->getRequest()->getHeader('X-Icinga-Container'),
+ $this->getSuccessUrl()->getAbsoluteUrl()
+ ]));
+
+ Session::getSession()->write();
+ $response->sendResponse();
+ exit;
+ }
+
+ public function hasBeenSent()
+ {
+ return true; // This form has no id
+ }
+}
diff --git a/application/forms/ProcessForm.php b/application/forms/ProcessForm.php
new file mode 100644
index 0000000..126fe9b
--- /dev/null
+++ b/application/forms/ProcessForm.php
@@ -0,0 +1,158 @@
+<?php
+
+namespace Icinga\Module\Businessprocess\Forms;
+
+use Icinga\Module\Businessprocess\BpNode;
+use Icinga\Module\Businessprocess\Modification\ProcessChanges;
+use Icinga\Module\Businessprocess\Node;
+use Icinga\Module\Businessprocess\Web\Form\BpConfigBaseForm;
+use Icinga\Web\Notification;
+use Icinga\Web\View;
+
+class ProcessForm extends BpConfigBaseForm
+{
+ /** @var BpNode */
+ protected $node;
+
+ public function setup()
+ {
+ if ($this->node !== null) {
+ /** @var View $view */
+ $view = $this->getView();
+
+ $this->addHtml(
+ '<h2>' . $view->escape(
+ sprintf($this->translate('Modify "%s"'), $this->node->getAlias())
+ ) . '</h2>'
+ );
+ }
+
+ $this->addElement('text', 'name', [
+ 'label' => $this->translate('ID'),
+ 'value' => (string) $this->node,
+ 'required' => true,
+ 'readonly' => $this->node ? true : null,
+ 'description' => $this->translate('This is the unique identifier of this process')
+ ]);
+
+ $this->addElement('text', 'alias', array(
+ 'label' => $this->translate('Display Name'),
+ 'description' => $this->translate(
+ 'Usually this name will be shown for this node. Equals ID'
+ . ' if not given'
+ ),
+ ));
+
+ $this->addElement('select', 'operator', array(
+ 'label' => $this->translate('Operator'),
+ 'required' => true,
+ 'multiOptions' => Node::getOperators()
+ ));
+
+ if ($this->node !== null) {
+ $display = $this->node->getDisplay() ?: 1;
+ } else {
+ $display = 1;
+ }
+ $this->addElement('select', 'display', array(
+ 'label' => $this->translate('Visualization'),
+ 'required' => true,
+ 'description' => $this->translate(
+ 'Where to show this process'
+ ),
+ 'multiOptions' => array(
+ "$display" => $this->translate('Toplevel Process'),
+ '0' => $this->translate('Subprocess only'),
+ )
+ ));
+
+ $this->addElement('text', 'url', array(
+ 'label' => $this->translate('Info URL'),
+ 'description' => $this->translate(
+ 'URL pointing to more information about this node'
+ )
+ ));
+
+ if ($node = $this->node) {
+ if ($node->hasAlias()) {
+ $this->getElement('alias')->setValue($node->getAlias());
+ }
+ $this->getElement('operator')->setValue($node->getOperator());
+ $this->getElement('display')->setValue($node->getDisplay());
+ if ($node->hasInfoUrl()) {
+ $this->getElement('url')->setValue($node->getInfoUrl());
+ }
+ }
+ }
+
+ /**
+ * @param BpNode $node
+ * @return $this
+ */
+ public function setNode(BpNode $node)
+ {
+ $this->node = $node;
+ return $this;
+ }
+
+ public function onSuccess()
+ {
+ $changes = ProcessChanges::construct($this->bp, $this->session);
+
+ $modifications = array();
+ $alias = $this->getValue('alias');
+ $operator = $this->getValue('operator');
+ $display = $this->getValue('display');
+ $url = $this->getValue('url');
+ if (empty($url)) {
+ $url = null;
+ }
+ if (empty($alias)) {
+ $alias = null;
+ }
+ // TODO: rename
+
+ if ($node = $this->node) {
+ if ($display !== $node->getDisplay()) {
+ $modifications['display'] = $display;
+ }
+ if ($operator !== $node->getOperator()) {
+ $modifications['operator'] = $operator;
+ }
+ if ($url !== $node->getInfoUrl()) {
+ $modifications['infoUrl'] = $url;
+ }
+ if ($alias !== $node->getAlias()) {
+ $modifications['alias'] = $alias;
+ }
+ } else {
+ $modifications = array(
+ 'display' => $display,
+ 'operator' => $operator,
+ 'infoUrl' => $url,
+ 'alias' => $alias,
+ );
+ }
+
+ if (! empty($modifications)) {
+ if ($this->node === null) {
+ $changes->createNode($this->getValue('name'), $modifications);
+ } else {
+ $changes->modifyNode($this->node, $modifications);
+ }
+
+ Notification::success(
+ sprintf(
+ 'Process %s has been modified',
+ $this->bp->getName()
+ )
+ );
+ }
+
+ // Trigger session destruction to make sure it get's stored.
+ // TODO: figure out why this is necessary, might be an unclean shutdown on redirect
+ unset($changes);
+
+ parent::onSuccess();
+ }
+}
diff --git a/application/forms/SimulationForm.php b/application/forms/SimulationForm.php
new file mode 100644
index 0000000..04a0f56
--- /dev/null
+++ b/application/forms/SimulationForm.php
@@ -0,0 +1,138 @@
+<?php
+
+namespace Icinga\Module\Businessprocess\Forms;
+
+use Icinga\Module\Businessprocess\MonitoredNode;
+use Icinga\Module\Businessprocess\Simulation;
+use Icinga\Module\Businessprocess\Web\Form\BpConfigBaseForm;
+use Icinga\Web\View;
+
+class SimulationForm extends BpConfigBaseForm
+{
+ /** @var MonitoredNode */
+ protected $node;
+
+ /** @var ?MonitoredNode */
+ protected $simulatedNode;
+
+ /** @var Simulation */
+ protected $simulation;
+
+ public function setup()
+ {
+ $states = $this->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;
+ }
+
+ /** @var View $view */
+ $view = $this->getView();
+ if ($hasSimulation) {
+ $title = $this->translate('Modify simulation for %s');
+ } else {
+ $title = $this->translate('Add simulation for %s');
+ }
+ $this->addHtml(
+ '<h2>'
+ . $view->escape(sprintf($title, $node->getAlias() ?? $node->getName()))
+ . '</h2>'
+ );
+
+ $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;
+ }
+}