2015-03-16 04:08:00 -04:00
|
|
|
<?php
|
|
|
|
|
|
2016-11-23 06:27:55 -05:00
|
|
|
namespace Icinga\Module\Businessprocess\Modification;
|
|
|
|
|
|
2017-01-11 08:04:45 -05:00
|
|
|
use Icinga\Module\Businessprocess\BpConfig;
|
2015-03-16 04:08:00 -04:00
|
|
|
|
2016-11-22 15:11:26 -05:00
|
|
|
/**
|
|
|
|
|
* NodeRemoveAction
|
|
|
|
|
*
|
|
|
|
|
* Tracks removed nodes
|
|
|
|
|
*
|
|
|
|
|
* @package Icinga\Module\Businessprocess
|
|
|
|
|
*/
|
2015-03-16 04:08:00 -04:00
|
|
|
class NodeRemoveAction extends NodeAction
|
|
|
|
|
{
|
2017-01-03 05:23:05 -05:00
|
|
|
protected $preserveProperties = array('parentName');
|
2016-12-16 13:32:36 -05:00
|
|
|
|
2017-01-03 05:23:05 -05:00
|
|
|
protected $parentName;
|
2016-12-16 13:32:36 -05:00
|
|
|
|
|
|
|
|
/**
|
2017-01-03 05:23:05 -05:00
|
|
|
* @param $parentName
|
2016-12-16 13:32:36 -05:00
|
|
|
* @return $this
|
|
|
|
|
*/
|
2017-01-03 05:23:05 -05:00
|
|
|
public function setParentName($parentName = null)
|
2016-12-16 13:32:36 -05:00
|
|
|
{
|
2017-01-03 05:23:05 -05:00
|
|
|
$this->parentName = $parentName;
|
2016-12-16 13:32:36 -05:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2017-01-03 05:23:05 -05:00
|
|
|
public function getParentName()
|
2016-12-16 13:32:36 -05:00
|
|
|
{
|
2017-01-03 05:23:05 -05:00
|
|
|
return $this->parentName;
|
2016-12-16 13:32:36 -05:00
|
|
|
}
|
|
|
|
|
|
2016-11-22 15:11:26 -05:00
|
|
|
/**
|
|
|
|
|
* @inheritdoc
|
|
|
|
|
*/
|
2017-01-11 11:36:32 -05:00
|
|
|
public function appliesTo(BpConfig $config)
|
2015-03-16 04:08:00 -04:00
|
|
|
{
|
2019-01-14 08:57:16 -05:00
|
|
|
$name = $this->getNodeName();
|
2017-01-03 05:23:05 -05:00
|
|
|
$parent = $this->getParentName();
|
|
|
|
|
if ($parent === null) {
|
2019-01-14 08:57:16 -05:00
|
|
|
if (!$config->hasNode($name)) {
|
|
|
|
|
$this->error('Toplevel process "%s" not found', $name);
|
|
|
|
|
}
|
2017-01-03 05:23:05 -05:00
|
|
|
} else {
|
2019-01-14 08:57:16 -05:00
|
|
|
if (! $config->hasNode($parent)) {
|
|
|
|
|
$this->error('Parent process "%s" missing', $parent);
|
|
|
|
|
} elseif (! $config->getBpNode($parent)->hasChild($name)) {
|
|
|
|
|
$this->error('Node "%s" not found in process "%s"', $name, $parent);
|
|
|
|
|
}
|
2016-12-16 13:32:36 -05:00
|
|
|
}
|
2019-01-14 08:57:16 -05:00
|
|
|
|
|
|
|
|
return true;
|
2015-03-16 04:08:00 -04:00
|
|
|
}
|
|
|
|
|
|
2016-11-22 15:11:26 -05:00
|
|
|
/**
|
|
|
|
|
* @inheritdoc
|
|
|
|
|
*/
|
2017-01-11 11:36:32 -05:00
|
|
|
public function applyTo(BpConfig $config)
|
2015-03-16 04:08:00 -04:00
|
|
|
{
|
2017-01-27 08:57:57 -05:00
|
|
|
$config->calculateAllStates();
|
|
|
|
|
$name = $this->getNodeName();
|
|
|
|
|
$parentName = $this->getParentName();
|
|
|
|
|
if ($parentName === null) {
|
2019-01-15 03:35:57 -05:00
|
|
|
$oldDisplay = $config->getBpNode($name)->getDisplay();
|
2017-01-27 08:57:57 -05:00
|
|
|
$config->removeNode($name);
|
2019-01-15 03:35:57 -05:00
|
|
|
if ($config->getMetadata()->isManuallyOrdered()) {
|
|
|
|
|
foreach ($config->getRootNodes() as $_ => $node) {
|
|
|
|
|
$nodeDisplay = $node->getDisplay();
|
|
|
|
|
if ($nodeDisplay > $oldDisplay) {
|
|
|
|
|
$node->setDisplay($node->getDisplay() - 1);
|
|
|
|
|
} elseif ($nodeDisplay === $oldDisplay) {
|
|
|
|
|
break; // Stop immediately to not make things worse ;)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-12-16 13:32:36 -05:00
|
|
|
} else {
|
2017-01-27 08:57:57 -05:00
|
|
|
$node = $config->getNode($name);
|
|
|
|
|
$parent = $config->getBpNode($parentName);
|
|
|
|
|
$parent->getState();
|
|
|
|
|
$parent->removeChild($name);
|
|
|
|
|
$node->removeParent($parentName);
|
2017-01-03 05:23:05 -05:00
|
|
|
if (! $node->hasParents()) {
|
2017-01-27 08:57:57 -05:00
|
|
|
$config->removeNode($name);
|
2017-01-03 05:23:05 -05:00
|
|
|
}
|
2016-12-16 13:32:36 -05:00
|
|
|
}
|
2015-03-16 04:08:00 -04:00
|
|
|
}
|
|
|
|
|
}
|