From 08bfbc462b8a3fe5860d60075bc656e662097e17 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Mon, 17 Dec 2018 14:49:26 +0100 Subject: [PATCH] Save user defined order of processes/nodes --- application/controllers/ProcessController.php | 8 + application/forms/MoveNodeForm.php | 147 ++++++++++++++++++ 2 files changed, 155 insertions(+) create mode 100644 application/forms/MoveNodeForm.php diff --git a/application/controllers/ProcessController.php b/application/controllers/ProcessController.php index 61633d1..6ba5168 100644 --- a/application/controllers/ProcessController.php +++ b/application/controllers/ProcessController.php @@ -250,6 +250,14 @@ class ProcessController extends Controller ->setNode($bp->getNode($this->params->get('simulationnode'))) ->setSimulation(Simulation::fromSession($this->session())) ->handleRequest(); + } elseif ($action === 'move') { + $form = $this->loadForm('MoveNode') + ->setProcess($bp) + ->setParentNode($node) + ->setSession($this->session()) + ->setNode($bp->getNode($this->params->get('movenode'))) + ->setSuccessUrl(Url::fromRequest()->without(['action', 'movenode'])) + ->handleRequest(); } if ($form) { diff --git a/application/forms/MoveNodeForm.php b/application/forms/MoveNodeForm.php new file mode 100644 index 0000000..04f648e --- /dev/null +++ b/application/forms/MoveNodeForm.php @@ -0,0 +1,147 @@ +addPrefixPaths(array( + array( + 'prefix' => 'Icinga\\Web\\Form\\Element\\', + 'path' => Icinga::app()->getLibraryDir('Icinga/Web/Form/Element'), + 'type' => static::ELEMENT + ), + array( + 'prefix' => 'Icinga\\Web\\Form\\Decorator\\', + 'path' => Icinga::app()->getLibraryDir('Icinga/Web/Form/Decorator'), + 'type' => static::DECORATOR + ) + )); + } + + public function setup() + { + $this->addElement( + 'number', + 'from', + [ + 'required' => true, + 'min' => 0 + ] + ); + $this->addElement( + 'number', + 'to', + [ + 'required' => true, + 'min' => 0 + ] + ); + $this->addElement( + 'hidden', + 'csrfToken', + [ + 'required' => true + ] + ); + + $this->setSubmitLabel('movenode'); + } + + /** + * @param BpConfig $process + * @return $this + */ + public function setProcess(BpConfig $process) + { + $this->bp = $process; + return $this; + } + + /** + * @param Node $node + * @return $this + */ + public function setNode(Node $node) + { + $this->node = $node; + return $this; + } + + /** + * @param BpNode|null $node + * @return $this + */ + public function setParentNode(BpNode $node = null) + { + $this->parentNode = $node; + return $this; + } + + /** + * @param SessionNamespace $session + * @return $this + */ + public function setSession(SessionNamespace $session) + { + $this->session = $session; + return $this; + } + + public function onSuccess() + { + if (! CsrfToken::isValid($this->getValue('csrfToken'))) { + throw new HttpException(403, 'nope'); + } + + $changes = ProcessChanges::construct($this->bp, $this->session); + if (! $this->bp->getMetadata()->isManuallyOrdered()) { + $changes->applyManualOrder(); + } + + $changes->moveNode( + $this->node, + $this->getValue('from'), + $this->getValue('to'), + $this->parentNode !== null ? $this->parentNode->getName() : null + ); + + // Trigger session destruction to make sure it get's stored. + unset($changes); + + $this->setSuccessMessage($this->translate('Node order updated')); + parent::onSuccess(); + } + + public function hasBeenSent() + { + return true; // This form has no id + } +}