From ba36072c18538b4109d3710cc0361357d4915e54 Mon Sep 17 00:00:00 2001 From: Timm Ortloff Date: Wed, 1 Feb 2023 12:33:06 +0100 Subject: [PATCH] Remove multiple submit button hack and replace `setId()` with `fromId()` as a static factory method --- .../controllers/TimeframeController.php | 14 +++---- .../controllers/TimeframesController.php | 9 +++-- library/Reporting/Web/Forms/TimeframeForm.php | 37 ++++++++++++------- 3 files changed, 35 insertions(+), 25 deletions(-) diff --git a/application/controllers/TimeframeController.php b/application/controllers/TimeframeController.php index 7411cb15..6783cff5 100644 --- a/application/controllers/TimeframeController.php +++ b/application/controllers/TimeframeController.php @@ -34,14 +34,12 @@ public function editAction() ]; - $form = (new TimeframeForm()) - ->setId($this->timeframe->getId()); - - $form->populate($values); - - $form->handleRequest(ServerRequest::fromGlobals()); - - $this->redirectForm($form, 'reporting/timeframes'); + $form = TimeframeForm::fromId($this->timeframe->getId()) + ->on(TimeframeForm::ON_SUCCESS, function () { + $this->redirectNow('reporting/timeframes'); + }) + ->populate($values) + ->handleRequest(ServerRequest::fromGlobals()); $this->addContent($form); } diff --git a/application/controllers/TimeframesController.php b/application/controllers/TimeframesController.php index c434e3ae..99233e03 100644 --- a/application/controllers/TimeframesController.php +++ b/application/controllers/TimeframesController.php @@ -94,10 +94,11 @@ public function newAction() $this->assertPermission('reporting/timeframes'); $this->addTitleTab($this->translate('New Timeframe')); - $form = new TimeframeForm(); - $form->handleRequest(ServerRequest::fromGlobals()); - - $this->redirectForm($form, 'reporting/timeframes'); + $form = (new TimeframeForm()) + ->on(TimeframeForm::ON_SUCCESS, function () { + $this->redirectNow('reporting/timeframes'); + }) + ->handleRequest(ServerRequest::fromGlobals()); $this->addContent($form); } diff --git a/library/Reporting/Web/Forms/TimeframeForm.php b/library/Reporting/Web/Forms/TimeframeForm.php index e83454f6..54f1ccad 100644 --- a/library/Reporting/Web/Forms/TimeframeForm.php +++ b/library/Reporting/Web/Forms/TimeframeForm.php @@ -15,13 +15,28 @@ class TimeframeForm extends CompatForm use Database; use DecoratedElement; + /** @var int */ protected $id; - public function setId($id) + /** + * Create a new form instance with the given report + * + * @param int $id + * + * @return static + */ + public static function fromId(int $id): self { - $this->id = $id; + $form = new static(); - return $this; + $form->id = $id; + + return $form; + } + + public function hasBeenSubmitted(): bool + { + return $this->hasBeenSent() && ($this->getPopulatedValue('submit') || $this->getPopulatedValue('remove')); } protected function assemble() @@ -64,16 +79,6 @@ protected function assemble() ]); $this->registerElement($removeButton); $this->getElement('submit')->getWrapper()->prepend($removeButton); - - if ($removeButton->hasBeenPressed()) { - $this->getDb()->delete('timeframe', ['id = ?' => $this->id]); - - // Stupid cheat because ipl/html is not capable of multiple submit buttons - $this->getSubmitButton()->setValue($this->getSubmitButton()->getButtonLabel()); - $this->valid = true; - - return; - } } } @@ -81,6 +86,12 @@ public function onSuccess() { $db = $this->getDb(); + if ($this->getPopulatedValue('remove')) { + $db->delete('timeframe', ['id = ?' => $this->id]); + + return; + } + $values = $this->getValues(); $now = time() * 1000;