Skip to content

Commit

Permalink
Remove multiple submit button hack and replace setId() with `fromId…
Browse files Browse the repository at this point in the history
…()` as a static factory method
  • Loading branch information
Timm Ortloff committed Feb 1, 2023
1 parent e6d4695 commit ba36072
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 25 deletions.
14 changes: 6 additions & 8 deletions application/controllers/TimeframeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
9 changes: 5 additions & 4 deletions application/controllers/TimeframesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
37 changes: 24 additions & 13 deletions library/Reporting/Web/Forms/TimeframeForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -64,23 +79,19 @@ 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;
}
}
}

public function onSuccess()
{
$db = $this->getDb();

if ($this->getPopulatedValue('remove')) {
$db->delete('timeframe', ['id = ?' => $this->id]);

return;
}

$values = $this->getValues();

$now = time() * 1000;
Expand Down

0 comments on commit ba36072

Please sign in to comment.