Prevent nodes from being added twice

fixes #186
fixes #128
fixes #114
This commit is contained in:
Johannes Meyer 2019-01-08 16:09:54 +01:00
parent de3e0e5b6c
commit 510267c7d0
4 changed files with 105 additions and 11 deletions

View file

@ -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;
}
]]
]
]));
}
/**

View file

@ -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;
}
]]
]
));
}

View file

@ -0,0 +1,15 @@
<?php
namespace Icinga\Module\Businessprocess\Web\Form\Element;
use Zend_Form_Element_Multiselect;
class Multiselect extends Zend_Form_Element_Multiselect
{
protected function _getErrorMessages()
{
// The base implementation is prone to message duplication in case of custom error messages.
// Since its actual behavior is not required it's entirely bypassed.
return $this->getErrorMessages();
}
}

View file

@ -19,4 +19,7 @@
<arg name="encoding" value="UTF-8"/>
<rule ref="PSR2"/>
<rule ref="PSR2.Methods.MethodDeclaration.Underscore">
<exclude-pattern>library/Businessprocess/Web/Form/Element/Multiselect.php</exclude-pattern>
</rule>
</ruleset>