DeleteNode: quite some rework, provide more...

...possibilities, remove "not implemented" exception
This commit is contained in:
Thomas Gelf 2017-01-03 11:23:05 +01:00
parent 65524e7a8b
commit 8bd1e10f95
4 changed files with 141 additions and 29 deletions

View file

@ -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(
'<h2>' . $this->getView()->escape(
sprintf($this->translate('Delete %s'), $this->node->getAlias())
'<h2>' . $view->escape(
sprintf($this->translate('Delete "%s"'), $node->getAlias())
) . '</h2>'
);
$biLink = $view->qlink(
$node->getAlias(),
'director/node/impact',
array('node' => $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"'),
$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);

View file

@ -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();

View file

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

View file

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