From 8bd1e10f95f6f7e031fbb2bc416d3651060e929a Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Tue, 3 Jan 2017 11:23:05 +0100 Subject: [PATCH] DeleteNode: quite some rework, provide more... ...possibilities, remove "not implemented" exception --- application/forms/DeleteNodeForm.php | 69 +++++++++++++++---- library/Businessprocess/BpNode.php | 18 +++++ library/Businessprocess/BusinessProcess.php | 51 +++++++++++++- .../Modification/NodeRemoveAction.php | 32 +++++---- 4 files changed, 141 insertions(+), 29 deletions(-) diff --git a/application/forms/DeleteNodeForm.php b/application/forms/DeleteNodeForm.php index 2cb9c08..5d4c5c6 100644 --- a/application/forms/DeleteNodeForm.php +++ b/application/forms/DeleteNodeForm.php @@ -5,6 +5,7 @@ namespace Icinga\Module\Businessprocess\Forms; use Icinga\Module\Businessprocess\BpNode; use Icinga\Module\Businessprocess\BusinessProcess; use Icinga\Module\Businessprocess\Modification\ProcessChanges; +use Icinga\Module\Businessprocess\Node; use Icinga\Module\Businessprocess\Web\Form\QuickForm; use Icinga\Module\Monitoring\Backend\MonitoringBackend; use Icinga\Web\Session\SessionNamespace; @@ -17,32 +18,62 @@ class DeleteNodeForm extends QuickForm /** @var BusinessProcess */ protected $bp; - /** @var BpNode */ + /** @var Node */ protected $node; - /** @var array */ - protected $path; + /** @var BpNode */ + protected $parentNode; /** @var SessionNamespace */ protected $session; public function setup() { + $node = $this->node; + $view = $this->getView(); $this->addHtml( - '

' . $this->getView()->escape( - sprintf($this->translate('Delete %s'), $this->node->getAlias()) + '

' . $view->escape( + sprintf($this->translate('Delete "%s"'), $node->getAlias()) ) . '

' ); + + $biLink = $view->qlink( + $node->getAlias(), + 'director/node/impact', + array('node' => $node->getName()), + array('data-base-target' => '_next') + ); + $this->addHtml( + '

' . sprintf( + $view->escape( + $this->translate('Unsure? Show business impact of "%s"') + ), + $biLink + ) . '

' + ); + + if ($this->parentNode) { + $yesMsg = sprintf( + $this->translate('Delete from %s'), + $this->parentNode->getAlias() + ); + } else { + $yesMsg = sprintf( + $this->translate('Delete root node "%s"'), + $this->node->getAlias() + ); + } + $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( + 'multiOptions' => $this->optionalEnum(array( 'no' => $this->translate('No'), - 'yes' => $this->translate('Yes'), + 'yes' => $yesMsg, + 'all' => sprintf($this->translate('Delete all occurrences of %s'), $node->getAlias()), )) )); } @@ -69,22 +100,22 @@ class DeleteNodeForm extends QuickForm } /** - * @param BpNode $node + * @param Node $node * @return $this */ - public function setNode(BpNode $node) + public function setNode(Node $node) { $this->node = $node; return $this; } /** - * @param array $path + * @param BpNode|null $node * @return $this */ - public function setPath(array $path) + public function setParentNode(BpNode $node = null) { - $this->path = $path; + $this->parentNode = $node; return $this; } @@ -101,7 +132,17 @@ class DeleteNodeForm extends QuickForm public function onSuccess() { $changes = ProcessChanges::construct($this->bp, $this->session); - $changes->deleteNode($this->node, $this->path); + + switch ($this->getValue('confirm')) { + case 'yes': + $changes->deleteNode($this->node, $this->path); + break; + case 'all': + $changes->deleteNode($this->node); + break; + case 'no': + $this->setSuccessMessage($this->translate('Well, maybe next time')); + } // 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); diff --git a/library/Businessprocess/BpNode.php b/library/Businessprocess/BpNode.php index 7c8b9c7..1a8d91e 100644 --- a/library/Businessprocess/BpNode.php +++ b/library/Businessprocess/BpNode.php @@ -132,6 +132,24 @@ class BpNode extends Node return $problems; } + public function hasChild($name) + { + return in_array($name, $this->childNames); + } + + public function removeChild($name) + { + if (($key = array_search($name, $this->childNames)) !== false) { + unset($this->childNames[$key]); + + if (! empty($this->children)) { + unset($this->children[$name]); + } + } + + return $this; + } + public function getProblemTree() { $tree = array(); diff --git a/library/Businessprocess/BusinessProcess.php b/library/Businessprocess/BusinessProcess.php index c411dec..30687bd 100644 --- a/library/Businessprocess/BusinessProcess.php +++ b/library/Businessprocess/BusinessProcess.php @@ -498,6 +498,47 @@ class BusinessProcess return $node; } + public function hasNodeByPath($nodeName, $path = array()) + { + if (! $this->hasNode($nodeName)) { + return false; + } + + $node = $this->getNode($nodeName); + $parents = $node->getParents(); + foreach ($parents as $parent) { + + } + while (! empty($path)) { + + } + + return empty($path); + } + + public function getNodeByPath($nodeName, $path = array()) + { + if (! $this->hasNode($nodeName)) { + throw new NotFoundError( + 'Node %s not found at %s', + $nodeName, + implode(' -> ', $path) + ); + } + } + + public function getPathsToNode($node) + { + $paths = array(); + foreach ($node->getParents() as $parent) { + foreach ($parent->getPathsToNode() as $path) { + $paths[] = $path; + } + } + + return $paths; + } + /** * @param $name * @return Node @@ -577,7 +618,15 @@ class BusinessProcess public function removeNode($name) { - throw new ProgrammingError('Not implemented yet'); + unset($this->nodes[$name]); + if (array_key_exists($name, $this->root_nodes)) { + unset($this->root_nodes[$name]); + } + foreach ($this->getBpNodes() as $node) { + if ($node->hasChild($name)) { + $node->removeChild($name); + } + } } /** diff --git a/library/Businessprocess/Modification/NodeRemoveAction.php b/library/Businessprocess/Modification/NodeRemoveAction.php index 62a48bf..a8a27b1 100644 --- a/library/Businessprocess/Modification/NodeRemoveAction.php +++ b/library/Businessprocess/Modification/NodeRemoveAction.php @@ -13,26 +13,26 @@ use Icinga\Module\Businessprocess\BusinessProcess; */ class NodeRemoveAction extends NodeAction { - protected $preserveProperties = array('path'); + protected $preserveProperties = array('parentName'); - protected $path; + protected $parentName; /** - * @param array $path + * @param $parentName * @return $this */ - public function setPath(array $path) + public function setParentName($parentName = null) { - $this->path = $path; + $this->parentName = $parentName; return $this; } /** * @return mixed */ - public function getPath() + public function getParentName() { - return $this->path; + return $this->parentName; } /** @@ -40,11 +40,11 @@ class NodeRemoveAction extends NodeAction */ public function appliesTo(BusinessProcess $bp) { - $path = $this->getPath(); - if ($path === null) { - return $bp->hasNodeByPath($this->getNodeName(), $this->getPath()); - } else { + $parent = $this->getParentName(); + if ($parent === null) { return $bp->hasNode($this->getNodeName()); + } else { + return $bp->hasNode($this->getNodeName()) && $bp->hasNode($this->getParentName()) ; } } @@ -53,11 +53,15 @@ class NodeRemoveAction extends NodeAction */ public function applyTo(BusinessProcess $bp) { - $path = $this->getPath(); - if ($path === null) { + $parent = $this->getParentName(); + if ($parent === null) { $bp->removeNode($this->getNodeName()); } else { - $bp->removeNodeByPath($this->getNodeName(), $this->getPath()); + $node = $bp->getNode($this->getNodeName()); + $node->removeParent($parent); + if (! $node->hasParents()) { + $bp->removeNode($this->getNodeName()); + } } } }