From cdb500134d7aa2cd05d6c00d2db590595d97df29 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Tue, 23 Apr 2019 18:16:35 +0200 Subject: [PATCH] IcingaObjectFormHook: new generic hook --- .../Director/Hook/IcingaObjectFormHook.php | 22 +++++++++++++ library/Director/Web/Form/DirectorForm.php | 2 +- .../Director/Web/Form/DirectorObjectForm.php | 7 +++- library/Director/Web/Form/QuickForm.php | 33 +++++++++++++++++++ 4 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 library/Director/Hook/IcingaObjectFormHook.php diff --git a/library/Director/Hook/IcingaObjectFormHook.php b/library/Director/Hook/IcingaObjectFormHook.php new file mode 100644 index 00000000..1d20ee10 --- /dev/null +++ b/library/Director/Hook/IcingaObjectFormHook.php @@ -0,0 +1,22 @@ +onSetup($form); + } + } +} diff --git a/library/Director/Web/Form/DirectorForm.php b/library/Director/Web/Form/DirectorForm.php index 770b7c22..145be5ba 100644 --- a/library/Director/Web/Form/DirectorForm.php +++ b/library/Director/Web/Form/DirectorForm.php @@ -38,7 +38,7 @@ abstract class DirectorForm extends QuickForm ]); } - protected function addBoolean($key, $options, $default = null) + public function addBoolean($key, $options, $default = null) { if ($default === null) { return $this->addElement('OptionalYesNo', $key, $options); diff --git a/library/Director/Web/Form/DirectorObjectForm.php b/library/Director/Web/Form/DirectorObjectForm.php index f5a6d1bf..0b7b155e 100644 --- a/library/Director/Web/Form/DirectorObjectForm.php +++ b/library/Director/Web/Form/DirectorObjectForm.php @@ -8,6 +8,7 @@ use Icinga\Module\Director\Db; use Icinga\Module\Director\Data\Db\DbObject; use Icinga\Module\Director\Data\Db\DbObjectWithSettings; use Icinga\Module\Director\Exception\NestingError; +use Icinga\Module\Director\Hook\IcingaObjectFormHook; use Icinga\Module\Director\IcingaConfig\StateFilterSet; use Icinga\Module\Director\IcingaConfig\TypeFilterSet; use Icinga\Module\Director\Objects\IcingaTemplateChoice; @@ -772,14 +773,18 @@ abstract class DirectorObjectForm extends DirectorForm $this->setDefaultsFromObject($this->object); } $this->prepareFields($this->object()); + IcingaObjectFormHook::callOnSetup($this); if ($this->hasBeenSent()) { $this->handlePost(); } try { $this->loadInheritedProperties(); $this->addFields(); + $this->callOnRequestCallables(); } catch (Exception $e) { $this->addUniqueException($e); + + return; } if ($this->shouldBeDeleted()) { @@ -918,7 +923,7 @@ abstract class DirectorObjectForm extends DirectorForm return $this->getSentValue($name) === $this->getElement($name)->getLabel(); } - protected function abortDeletion() + public function abortDeletion() { if ($this->hasDeleteButton()) { $this->setSentValue($this->deleteButtonName, 'ABORTED'); diff --git a/library/Director/Web/Form/QuickForm.php b/library/Director/Web/Form/QuickForm.php index 0f3b5f65..e35bccec 100644 --- a/library/Director/Web/Form/QuickForm.php +++ b/library/Director/Web/Form/QuickForm.php @@ -68,6 +68,10 @@ abstract class QuickForm extends QuickBaseForm protected $calledSuccessCallbacks = false; + protected $onRequestCallbacks = []; + + protected $calledOnRequestCallbacks = false; + public function __construct($options = null) { parent::__construct($options); @@ -447,6 +451,32 @@ abstract class QuickForm extends QuickBaseForm $this->redirectOnSuccess(); } + /** + * @param callable $callable + * @return $this + */ + public function callOnRequest($callable) + { + if (! is_callable($callable)) { + throw new InvalidArgumentException( + 'callOnRequest() expects a callable' + ); + } + $this->onRequestCallbacks[] = $callable; + + return $this; + } + + protected function callOnRequestCallables() + { + if (! $this->calledOnRequestCallbacks) { + $this->calledOnRequestCallbacks = true; + foreach ($this->onRequestCallbacks as $callable) { + $callable($this); + } + } + } + /** * @param callable $callable * @return $this @@ -539,6 +569,7 @@ abstract class QuickForm extends QuickBaseForm protected function onRequest() { + $this->callOnRequestCallables(); } public function setRequest(Request $request) @@ -550,6 +581,8 @@ abstract class QuickForm extends QuickBaseForm $this->request = $request; $this->prepareElements(); $this->onRequest(); + $this->callOnRequestCallables(); + return $this; }