From 510267c7d08a9d4a4efd333aa4e4ed5db7413491 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Tue, 8 Jan 2019 16:09:54 +0100 Subject: [PATCH] Prevent nodes from being added twice fixes #186 fixes #128 fixes #114 --- application/forms/AddNodeForm.php | 64 ++++++++++++++++--- application/forms/EditNodeForm.php | 34 +++++++++- .../Web/Form/Element/Multiselect.php | 15 +++++ phpcs.xml | 3 + 4 files changed, 105 insertions(+), 11 deletions(-) create mode 100644 library/Businessprocess/Web/Form/Element/Multiselect.php diff --git a/application/forms/AddNodeForm.php b/application/forms/AddNodeForm.php index 0b27a35..66a8f62 100644 --- a/application/forms/AddNodeForm.php +++ b/application/forms/AddNodeForm.php @@ -5,6 +5,7 @@ namespace Icinga\Module\Businessprocess\Forms; use Icinga\Module\Businessprocess\BpNode; use Icinga\Module\Businessprocess\BpConfig; use Icinga\Module\Businessprocess\Modification\ProcessChanges; +use Icinga\Module\Businessprocess\Web\Form\Element\Multiselect; use Icinga\Module\Businessprocess\Web\Form\QuickForm; use Icinga\Module\Monitoring\Backend\MonitoringBackend; use Icinga\Web\Session\SessionNamespace; @@ -161,7 +162,7 @@ class AddNodeForm extends QuickForm protected function selectHost() { - $this->addElement('multiselect', 'children', array( + $this->addElement(new Multiselect('children', [ 'label' => $this->translate('Hosts'), 'required' => true, 'size' => 8, @@ -169,8 +170,23 @@ class AddNodeForm extends QuickForm 'multiOptions' => $this->enumHostList(), 'description' => $this->translate( 'Hosts that should be part of this business process node' - ) - )); + ), + 'validators' => [ + ['Callback', true, [ + 'callback' => function ($value) { + if ($this->hasParentNode() && $this->parent->hasChild($value)) { + $el = $this->getElement('children'); + $el->addError(sprintf( + $this->translate('%s is already defined in this process'), + $el->getMultiOptions()[$value] + )); + } + + return true; + } + ]] + ] + ])); } protected function selectService() @@ -196,7 +212,7 @@ class AddNodeForm extends QuickForm protected function addServicesElement($host) { - $this->addElement('multiselect', 'children', array( + $this->addElement(new Multiselect('children', [ 'label' => $this->translate('Services'), 'required' => true, 'size' => 8, @@ -204,13 +220,28 @@ class AddNodeForm extends QuickForm 'multiOptions' => $this->enumServiceList($host), 'description' => $this->translate( 'Services that should be part of this business process node' - ) - )); + ), + 'validators' => [ + ['Callback', true, [ + 'callback' => function ($value) { + if ($this->hasParentNode() && $this->parent->hasChild($value)) { + $el = $this->getElement('children'); + $el->addError(sprintf( + $this->translate('%s is already defined in this process'), + $el->getMultiOptions()[$value] + )); + } + + return true; + } + ]] + ] + ])); } protected function selectProcess() { - $this->addElement('multiselect', 'children', array( + $this->addElement(new Multiselect('children', [ 'label' => $this->translate('Process nodes'), 'required' => true, 'size' => 8, @@ -218,8 +249,23 @@ class AddNodeForm extends QuickForm 'multiOptions' => $this->enumProcesses(), 'description' => $this->translate( 'Other processes that should be part of this business process node' - ) - )); + ), + 'validators' => [ + ['Callback', true, [ + 'callback' => function ($value) { + if ($this->hasParentNode() && $this->parent->hasChild($value)) { + $el = $this->getElement('children'); + $el->addError(sprintf( + $this->translate('%s is already defined in this process'), + $el->getMultiOptions()[$value] + )); + } + + return true; + } + ]] + ] + ])); } /** diff --git a/application/forms/EditNodeForm.php b/application/forms/EditNodeForm.php index 561f533..778d871 100644 --- a/application/forms/EditNodeForm.php +++ b/application/forms/EditNodeForm.php @@ -174,7 +174,22 @@ class EditNodeForm extends QuickForm 'value' => $this->getNode()->getName(), 'multiOptions' => $this->enumHostList(), 'label' => $this->translate('Host'), - 'description' => $this->translate('The host for this business process node') + 'description' => $this->translate('The host for this business process node'), + 'validators' => [ + ['Callback', true, [ + 'callback' => function ($value) { + if ($this->hasParentNode() && $this->parent->hasChild($value)) { + $el = $this->getElement('children'); + $el->addError(sprintf( + $this->translate('%s is already defined in this process'), + $el->getMultiOptions()[$value] + )); + } + + return true; + } + ]] + ] )); } @@ -211,7 +226,22 @@ class EditNodeForm extends QuickForm 'value' => $this->getNode()->getName(), 'multiOptions' => $this->enumServiceList($host), 'label' => $this->translate('Service'), - 'description' => $this->translate('The service for this business process node') + 'description' => $this->translate('The service for this business process node'), + 'validators' => [ + ['Callback', true, [ + 'callback' => function ($value) { + if ($this->hasParentNode() && $this->parent->hasChild($value)) { + $el = $this->getElement('children'); + $el->addError(sprintf( + $this->translate('%s is already defined in this process'), + $el->getMultiOptions()[$value] + )); + } + + return true; + } + ]] + ] )); } diff --git a/library/Businessprocess/Web/Form/Element/Multiselect.php b/library/Businessprocess/Web/Form/Element/Multiselect.php new file mode 100644 index 0000000..90e4b0d --- /dev/null +++ b/library/Businessprocess/Web/Form/Element/Multiselect.php @@ -0,0 +1,15 @@ +getErrorMessages(); + } +} diff --git a/phpcs.xml b/phpcs.xml index f5488c7..d5b9ebc 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -19,4 +19,7 @@ + + library/Businessprocess/Web/Form/Element/Multiselect.php +