From ea0a04065d38ee7cab48b24d69d7d513f1cf3b1e Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Mon, 14 Jan 2019 08:48:20 +0100 Subject: [PATCH] Add support to move nodes between processes --- application/forms/MoveNodeForm.php | 19 +++ .../Modification/NodeMoveAction.php | 114 ++++++++++++++---- .../Modification/ProcessChanges.php | 8 +- 3 files changed, 114 insertions(+), 27 deletions(-) diff --git a/application/forms/MoveNodeForm.php b/application/forms/MoveNodeForm.php index 04f648e..f2146e4 100644 --- a/application/forms/MoveNodeForm.php +++ b/application/forms/MoveNodeForm.php @@ -48,6 +48,24 @@ class MoveNodeForm extends QuickForm public function setup() { + $this->addElement( + 'text', + 'parent', + [ + 'allowEmpty' => true, + 'filters' => ['Null'], + 'validators' => [ + ['Callback', true, [ + 'callback' => function($name) { + return empty($name) || $this->bp->hasBpNode($name); + }, + 'messages' => [ + 'callbackValue' => $this->translate('No process found with name %value%') + ] + ]] + ] + ] + ); $this->addElement( 'number', 'from', @@ -130,6 +148,7 @@ class MoveNodeForm extends QuickForm $this->node, $this->getValue('from'), $this->getValue('to'), + $this->getValue('parent'), $this->parentNode !== null ? $this->parentNode->getName() : null ); diff --git a/library/Businessprocess/Modification/NodeMoveAction.php b/library/Businessprocess/Modification/NodeMoveAction.php index ebe5aea..f7428fc 100644 --- a/library/Businessprocess/Modification/NodeMoveAction.php +++ b/library/Businessprocess/Modification/NodeMoveAction.php @@ -10,7 +10,12 @@ class NodeMoveAction extends NodeAction /** * @var string */ - protected $parentName; + protected $parent; + + /** + * @var string + */ + protected $newParent; /** * @var int @@ -22,16 +27,26 @@ class NodeMoveAction extends NodeAction */ protected $to; - protected $preserveProperties = ['parentName', 'from', 'to']; + protected $preserveProperties = ['parent', 'newParent', 'from', 'to']; - public function setParentName($name) + public function setParent($name) { - $this->parentName = $name; + $this->parent = $name; } - public function getParentName() + public function getParent() { - return $this->parentName; + return $this->parent; + } + + public function setNewParent($name) + { + $this->newParent = $name; + } + + public function getNewParent() + { + return $this->newParent; } public function setFrom($from) @@ -61,11 +76,11 @@ class NodeMoveAction extends NodeAction } $name = $this->getNodeName(); - if ($this->parentName !== null) { - if (! $config->hasBpNode($this->parentName)) { + if ($this->parent !== null) { + if (! $config->hasBpNode($this->parent)) { return false; } - $parent = $config->getBpNode($this->parentName); + $parent = $config->getBpNode($this->parent); if (! $parent->hasChild($name)) { return false; } @@ -84,14 +99,30 @@ class NodeMoveAction extends NodeAction } } + 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; + } + } + return true; } public function applyTo(BpConfig $config) { $name = $this->getNodeName(); - if ($this->parentName !== null) { - $nodes = $config->getBpNode($this->parentName)->getChildren(); + if ($this->parent !== null) { + $nodes = $config->getBpNode($this->parent)->getChildren(); } else { $nodes = $config->getRootNodes(); } @@ -101,22 +132,57 @@ class NodeMoveAction extends NodeAction 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)); + 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(); + } + + $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)); + } else { + if ($this->newParent !== null) { + $config->removeRootNode($name); + } + $i = 0; - foreach ($nodes as $name => $node) { - /** @var BpNode $node */ - if ($node->getDisplay() > 0) { + foreach ($nodes as $_ => $oldNode) { + /** @var BpNode $oldNode */ + if ($oldNode->getDisplay() > 0) { $i += 1; - if ($node->getDisplay() !== $i) { - $node->setDisplay($i); + if ($oldNode->getDisplay() !== $i) { + $oldNode->setDisplay($i); } } } diff --git a/library/Businessprocess/Modification/ProcessChanges.php b/library/Businessprocess/Modification/ProcessChanges.php index 8dc324a..50a6226 100644 --- a/library/Businessprocess/Modification/ProcessChanges.php +++ b/library/Businessprocess/Modification/ProcessChanges.php @@ -126,14 +126,16 @@ class ProcessChanges * @param Node $node * @param int $from * @param int $to - * @param string $parentName + * @param string $newParent + * @param string $parent * * @return $this */ - public function moveNode(Node $node, $from, $to, $parentName = null) + public function moveNode(Node $node, $from, $to, $newParent, $parent = null) { $action = new NodeMoveAction($node); - $action->setParentName($parentName); + $action->setParent($parent); + $action->setNewParent($newParent); $action->setFrom($from); $action->setTo($to);