ProcessChanges: Add new action to move processes/nodes

This commit is contained in:
Johannes Meyer 2018-12-17 14:48:46 +01:00
parent c609f0c6b3
commit 0707d1d7e7
2 changed files with 145 additions and 0 deletions

View file

@ -0,0 +1,125 @@
<?php
namespace Icinga\Module\Businessprocess\Modification;
use Icinga\Module\Businessprocess\BpConfig;
use Icinga\Module\Businessprocess\BpNode;
class NodeMoveAction extends NodeAction
{
/**
* @var string
*/
protected $parentName;
/**
* @var int
*/
protected $from;
/**
* @var int
*/
protected $to;
protected $preserveProperties = ['parentName', 'from', 'to'];
public function setParentName($name)
{
$this->parentName = $name;
}
public function getParentName()
{
return $this->parentName;
}
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();
if ($this->parentName !== null) {
if (! $config->hasBpNode($this->parentName)) {
return false;
}
$parent = $config->getBpNode($this->parentName);
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;
}
}
return true;
}
public function applyTo(BpConfig $config)
{
$name = $this->getNodeName();
if ($this->parentName !== null) {
$nodes = $config->getBpNode($this->parentName)->getChildren();
} 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)
);
$nodes = array_merge(
array_slice($nodes, 0, $this->to, true),
[$name => $node],
array_slice($nodes, $this->to, null, true)
);
if ($this->parentName !== null) {
$config->getBpNode($this->parentName)->setChildNames(array_keys($nodes));
} else {
$i = 0;
foreach ($nodes as $name => $node) {
/** @var BpNode $node */
if ($node->getDisplay() > 0) {
$i += 1;
if ($node->getDisplay() !== $i) {
$node->setDisplay($i);
}
}
}
}
}
}

View file

@ -120,6 +120,26 @@ class ProcessChanges
return $this->push($action);
}
/**
* Move the given node
*
* @param Node $node
* @param int $from
* @param int $to
* @param string $parentName
*
* @return $this
*/
public function moveNode(Node $node, $from, $to, $parentName = null)
{
$action = new NodeMoveAction($node);
$action->setParentName($parentName);
$action->setFrom($from);
$action->setTo($to);
return $this->push($action);
}
/**
* Apply manual order on the entire bp configuration file
*