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()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$name = $this->getNodeName();
|
2019-01-14 02:48:20 -05:00
|
|
|
if ($this->parent !== null) {
|
|
|
|
|
if (! $config->hasBpNode($this->parent)) {
|
2018-12-17 08:48:46 -05:00
|
|
|
return false;
|
|
|
|
|
}
|
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)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$nodes = $parent->getChildNames();
|
|
|
|
|
if (! isset($nodes[$this->from]) || $nodes[$this->from] !== $name) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (! $config->hasNode($name)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($config->getBpNode($name)->getDisplay() !== $this->getFrom()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 02:48:20 -05:00
|
|
|
if ($this->parent !== $this->newParent) {
|
|
|
|
|
if ($this->newParent !== null) {
|
|
|
|
|
if (! $config->hasBpNode($this->newParent)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$childrenCount = $config->getBpNode($this->newParent)->countChildren();
|
|
|
|
|
} else {
|
|
|
|
|
$childrenCount = $config->countChildren();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($this->getTo() > 0 && $childrenCount < $this->getTo()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
foreach ($newNodes as $_ => $newNode) {
|
|
|
|
|
/** @var BpNode $newNode */
|
|
|
|
|
if ($newNode->getDisplay() > 0) {
|
|
|
|
|
$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);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|