2018-12-17 08:48:46 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Icinga\Module\Businessprocess\Modification;
|
|
|
|
|
|
|
|
|
|
use Icinga\Module\Businessprocess\BpConfig;
|
|
|
|
|
use Icinga\Module\Businessprocess\BpNode;
|
|
|
|
|
|
|
|
|
|
class NodeMoveAction extends NodeAction
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2019-01-14 02:48:20 -05:00
|
|
|
protected $parent;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
protected $newParent;
|
2018-12-17 08:48:46 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var int
|
|
|
|
|
*/
|
|
|
|
|
protected $from;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var int
|
|
|
|
|
*/
|
|
|
|
|
protected $to;
|
|
|
|
|
|
2019-01-14 02:48:20 -05:00
|
|
|
protected $preserveProperties = ['parent', 'newParent', 'from', 'to'];
|
2018-12-17 08:48:46 -05:00
|
|
|
|
2019-01-14 02:48:20 -05:00
|
|
|
public function setParent($name)
|
2018-12-17 08:48:46 -05:00
|
|
|
{
|
2019-01-14 02:48:20 -05:00
|
|
|
$this->parent = $name;
|
2018-12-17 08:48:46 -05:00
|
|
|
}
|
|
|
|
|
|
2019-01-14 02:48:20 -05:00
|
|
|
public function getParent()
|
2018-12-17 08:48:46 -05:00
|
|
|
{
|
2019-01-14 02:48:20 -05:00
|
|
|
return $this->parent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function setNewParent($name)
|
|
|
|
|
{
|
|
|
|
|
$this->newParent = $name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getNewParent()
|
|
|
|
|
{
|
|
|
|
|
return $this->newParent;
|
2018-12-17 08:48:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function setFrom($from)
|
|
|
|
|
{
|
|
|
|
|
$this->from = (int) $from;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getFrom()
|
|
|
|
|
{
|
|
|
|
|
return $this->from;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function setTo($to)
|
|
|
|
|
{
|
|
|
|
|
$this->to = (int) $to;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getTo()
|
|
|
|
|
{
|
|
|
|
|
return $this->to;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function appliesTo(BpConfig $config)
|
|
|
|
|
{
|
|
|
|
|
if (! $config->getMetadata()->isManuallyOrdered()) {
|
2019-01-14 08:57:16 -05:00
|
|
|
$this->error('Process configuration is not manually ordered yet');
|
2018-12-17 08:48:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$name = $this->getNodeName();
|
2019-01-14 02:48:20 -05:00
|
|
|
if ($this->parent !== null) {
|
|
|
|
|
if (! $config->hasBpNode($this->parent)) {
|
2019-01-14 08:57:16 -05:00
|
|
|
$this->error('Parent process "%s" missing', $this->parent);
|
2018-12-17 08:48:46 -05:00
|
|
|
}
|
2019-01-14 02:48:20 -05:00
|
|
|
$parent = $config->getBpNode($this->parent);
|
2018-12-17 08:48:46 -05:00
|
|
|
if (! $parent->hasChild($name)) {
|
2019-01-14 08:57:16 -05:00
|
|
|
$this->error('Node "%s" not found in process "%s"', $name, $this->parent);
|
2018-12-17 08:48:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$nodes = $parent->getChildNames();
|
|
|
|
|
if (! isset($nodes[$this->from]) || $nodes[$this->from] !== $name) {
|
2019-01-14 08:57:16 -05:00
|
|
|
$this->error('Node "%s" not found at position %d', $name, $this->from);
|
2018-12-17 08:48:46 -05:00
|
|
|
}
|
|
|
|
|
} else {
|
2019-01-15 03:50:14 -05:00
|
|
|
if (! $config->hasRootNode($name)) {
|
2019-01-14 08:57:16 -05:00
|
|
|
$this->error('Toplevel process "%s" not found', $name);
|
2018-12-17 08:48:46 -05:00
|
|
|
}
|
|
|
|
|
|
2019-01-15 03:50:14 -05:00
|
|
|
$nodes = $config->listRootNodes();
|
|
|
|
|
if (! isset($nodes[$this->from]) || $nodes[$this->from] !== $name) {
|
|
|
|
|
$this->error('Toplevel process "%s" not found at position %d', $name, $this->from);
|
2018-12-17 08:48:46 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 02:48:20 -05:00
|
|
|
if ($this->parent !== $this->newParent) {
|
|
|
|
|
if ($this->newParent !== null) {
|
|
|
|
|
if (! $config->hasBpNode($this->newParent)) {
|
2019-01-14 08:57:16 -05:00
|
|
|
$this->error('New parent process "%s" missing', $this->newParent);
|
2019-01-14 09:00:39 -05:00
|
|
|
} elseif ($config->getBpNode($this->newParent)->hasChild($name)) {
|
|
|
|
|
$this->error(
|
|
|
|
|
'New parent process "%s" already has a node with the name "%s"',
|
|
|
|
|
$this->newParent,
|
|
|
|
|
$name
|
|
|
|
|
);
|
2019-01-14 02:48:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$childrenCount = $config->getBpNode($this->newParent)->countChildren();
|
2019-01-14 08:57:16 -05:00
|
|
|
if ($this->to > 0 && $childrenCount < $this->to) {
|
|
|
|
|
$this->error(
|
|
|
|
|
'New parent process "%s" has not enough children. Target position %d out of range',
|
|
|
|
|
$this->newParent,
|
|
|
|
|
$this->to
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-01-14 02:48:20 -05:00
|
|
|
} else {
|
2019-01-14 09:00:39 -05:00
|
|
|
if ($config->hasRootNode($name)) {
|
|
|
|
|
$this->error('Process "%s" is already a toplevel process', $name);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 02:48:20 -05:00
|
|
|
$childrenCount = $config->countChildren();
|
2019-01-14 08:57:16 -05:00
|
|
|
if ($this->to > 0 && $childrenCount < $this->to) {
|
|
|
|
|
$this->error(
|
|
|
|
|
'Process configuration has not enough toplevel processes. Target position %d out of range',
|
|
|
|
|
$this->to
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-01-14 02:48:20 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-17 08:48:46 -05:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function applyTo(BpConfig $config)
|
|
|
|
|
{
|
|
|
|
|
$name = $this->getNodeName();
|
2019-01-14 02:48:20 -05:00
|
|
|
if ($this->parent !== null) {
|
|
|
|
|
$nodes = $config->getBpNode($this->parent)->getChildren();
|
2018-12-17 08:48:46 -05:00
|
|
|
} else {
|
|
|
|
|
$nodes = $config->getRootNodes();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$node = $nodes[$name];
|
|
|
|
|
$nodes = array_merge(
|
|
|
|
|
array_slice($nodes, 0, $this->from, true),
|
|
|
|
|
array_slice($nodes, $this->from + 1, null, true)
|
|
|
|
|
);
|
2019-01-14 02:48:20 -05:00
|
|
|
if ($this->parent === $this->newParent) {
|
|
|
|
|
$nodes = array_merge(
|
|
|
|
|
array_slice($nodes, 0, $this->to, true),
|
|
|
|
|
[$name => $node],
|
|
|
|
|
array_slice($nodes, $this->to, null, true)
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
if ($this->newParent !== null) {
|
|
|
|
|
$newNodes = $config->getBpNode($this->newParent)->getChildren();
|
|
|
|
|
} else {
|
|
|
|
|
$newNodes = $config->getRootNodes();
|
|
|
|
|
}
|
2018-12-17 08:48:46 -05:00
|
|
|
|
2019-01-14 02:48:20 -05:00
|
|
|
$newNodes = array_merge(
|
|
|
|
|
array_slice($newNodes, 0, $this->to, true),
|
|
|
|
|
[$name => $node],
|
|
|
|
|
array_slice($newNodes, $this->to, null, true)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($this->newParent !== null) {
|
|
|
|
|
$config->getBpNode($this->newParent)->setChildNames(array_keys($newNodes));
|
|
|
|
|
} else {
|
|
|
|
|
$config->addRootNode($name);
|
|
|
|
|
|
|
|
|
|
$i = 0;
|
2019-01-14 09:54:05 -05:00
|
|
|
foreach ($newNodes as $newName => $newNode) {
|
2019-01-14 02:48:20 -05:00
|
|
|
/** @var BpNode $newNode */
|
2019-01-14 09:54:05 -05:00
|
|
|
if ($newNode->getDisplay() > 0 || $newName === $name) {
|
2019-01-14 02:48:20 -05:00
|
|
|
$i += 1;
|
|
|
|
|
if ($newNode->getDisplay() !== $i) {
|
|
|
|
|
$newNode->setDisplay($i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($this->parent !== null) {
|
|
|
|
|
$config->getBpNode($this->parent)->setChildNames(array_keys($nodes));
|
2018-12-17 08:48:46 -05:00
|
|
|
} else {
|
2019-01-14 02:48:20 -05:00
|
|
|
if ($this->newParent !== null) {
|
|
|
|
|
$config->removeRootNode($name);
|
2019-01-14 09:54:05 -05:00
|
|
|
$node->setDisplay(0);
|
2019-01-14 02:48:20 -05:00
|
|
|
}
|
|
|
|
|
|
2018-12-17 08:48:46 -05:00
|
|
|
$i = 0;
|
2019-01-14 02:48:20 -05:00
|
|
|
foreach ($nodes as $_ => $oldNode) {
|
|
|
|
|
/** @var BpNode $oldNode */
|
|
|
|
|
if ($oldNode->getDisplay() > 0) {
|
2018-12-17 08:48:46 -05:00
|
|
|
$i += 1;
|
2019-01-14 02:48:20 -05:00
|
|
|
if ($oldNode->getDisplay() !== $i) {
|
|
|
|
|
$oldNode->setDisplay($i);
|
2018-12-17 08:48:46 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|