From 0707d1d7e79f8b304d944153cf63830ed523e673 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Mon, 17 Dec 2018 14:48:46 +0100 Subject: [PATCH] ProcessChanges: Add new action to move processes/nodes --- .../Modification/NodeMoveAction.php | 125 ++++++++++++++++++ .../Modification/ProcessChanges.php | 20 +++ 2 files changed, 145 insertions(+) create mode 100644 library/Businessprocess/Modification/NodeMoveAction.php diff --git a/library/Businessprocess/Modification/NodeMoveAction.php b/library/Businessprocess/Modification/NodeMoveAction.php new file mode 100644 index 0000000..ebe5aea --- /dev/null +++ b/library/Businessprocess/Modification/NodeMoveAction.php @@ -0,0 +1,125 @@ +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); + } + } + } + } + } +} diff --git a/library/Businessprocess/Modification/ProcessChanges.php b/library/Businessprocess/Modification/ProcessChanges.php index 8a9f618..8dc324a 100644 --- a/library/Businessprocess/Modification/ProcessChanges.php +++ b/library/Businessprocess/Modification/ProcessChanges.php @@ -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 *