mirror of
https://github.com/Icinga/icingaweb2-module-director.git
synced 2025-07-30 01:04:12 +02:00
Merge pull request #1841 from Icinga/feature/generic-object-form-hook
IcingaObjectFormHook: new generic hook
This commit is contained in:
commit
93a882f8c5
22
library/Director/Hook/IcingaObjectFormHook.php
Normal file
22
library/Director/Hook/IcingaObjectFormHook.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Icinga\Module\Director\Hook;
|
||||
|
||||
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
|
||||
use Icinga\Web\Hook;
|
||||
|
||||
abstract class IcingaObjectFormHook
|
||||
{
|
||||
protected $settings = [];
|
||||
|
||||
abstract public function onSetup(DirectorObjectForm $form);
|
||||
|
||||
public static function callOnSetup(DirectorObjectForm $form)
|
||||
{
|
||||
/** @var static[] $implementations */
|
||||
$implementations = Hook::all('director/IcingaObjectForm');
|
||||
foreach ($implementations as $implementation) {
|
||||
$implementation->onSetup($form);
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
|
@ -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');
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user