Skip to content

Commit

Permalink
Merge pull request #1841 from Icinga/feature/generic-object-form-hook
Browse files Browse the repository at this point in the history
IcingaObjectFormHook: new generic hook
  • Loading branch information
Thomas-Gelf authored Apr 23, 2019
2 parents db6deb2 + cdb5001 commit 93a882f
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
22 changes: 22 additions & 0 deletions library/Director/Hook/IcingaObjectFormHook.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
2 changes: 1 addition & 1 deletion library/Director/Web/Form/DirectorForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static function load()
]);
}

protected function addBoolean($key, $options, $default = null)
public function addBoolean($key, $options, $default = null)
{
if ($default === null) {
return $this->addElement('OptionalYesNo', $key, $options);
Expand Down
7 changes: 6 additions & 1 deletion library/Director/Web/Form/DirectorObjectForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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;
Expand Down Expand Up @@ -772,14 +773,18 @@ protected function onRequest()
$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()) {
Expand Down Expand Up @@ -918,7 +923,7 @@ public function shouldBeDeleted()
return $this->getSentValue($name) === $this->getElement($name)->getLabel();
}

protected function abortDeletion()
public function abortDeletion()
{
if ($this->hasDeleteButton()) {
$this->setSentValue($this->deleteButtonName, 'ABORTED');
Expand Down
33 changes: 33 additions & 0 deletions library/Director/Web/Form/QuickForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -447,6 +451,32 @@ public function onSuccess()
$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
Expand Down Expand Up @@ -539,6 +569,7 @@ protected function setHttpResponseCode($code)

protected function onRequest()
{
$this->callOnRequestCallables();
}

public function setRequest(Request $request)
Expand All @@ -550,6 +581,8 @@ public function setRequest(Request $request)
$this->request = $request;
$this->prepareElements();
$this->onRequest();
$this->callOnRequestCallables();

return $this;
}

Expand Down

0 comments on commit 93a882f

Please sign in to comment.