From fadbde5e9b7ff4ebdf108e10f00ff9d7c96adbac Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Tue, 25 Jul 2017 10:18:07 +0200 Subject: [PATCH] HostForm: take over logic from base form, clean up --- application/controllers/HostController.php | 17 +++++++++++++ application/forms/IcingaHostForm.php | 16 +++++++++++- application/forms/IcingaServiceForm.php | 4 +++ .../Web/Controller/ObjectController.php | 25 ++++++++++--------- .../Director/Web/Form/DirectorObjectForm.php | 20 +++------------ 5 files changed, 52 insertions(+), 30 deletions(-) diff --git a/application/controllers/HostController.php b/application/controllers/HostController.php index 0e4b28b3..a98c020d 100644 --- a/application/controllers/HostController.php +++ b/application/controllers/HostController.php @@ -8,6 +8,7 @@ use Icinga\Exception\NotFoundError; use Icinga\Module\Director\Db\AppliedServiceSetLoader; use Icinga\Module\Director\Forms\IcingaForgetApiKeyForm; use Icinga\Module\Director\Forms\IcingaGenerateApiKeyForm; +use Icinga\Module\Director\Forms\IcingaHostForm; use Icinga\Module\Director\Forms\IcingaServiceForm; use Icinga\Module\Director\IcingaConfig\AgentWizard; use Icinga\Module\Director\Objects\IcingaHost; @@ -56,6 +57,22 @@ class HostController extends ObjectController return new HostgroupRestriction($this->db(), $this->Auth()); } + /** + * @param IcingaHostForm $form + */ + protected function beforeHandlingAddRequest($form) + { + $form->setApi($this->getApiIfAvailable()); + } + + /** + * @param IcingaHostForm $form + */ + protected function beforeHandlingEditRequest($form) + { + $form->setApi($this->getApiIfAvailable()); + } + public function editAction() { parent::editAction(); diff --git a/application/forms/IcingaHostForm.php b/application/forms/IcingaHostForm.php index fb12492c..abd72bde 100644 --- a/application/forms/IcingaHostForm.php +++ b/application/forms/IcingaHostForm.php @@ -2,11 +2,14 @@ namespace Icinga\Module\Director\Forms; -use Icinga\Module\Director\Restriction\HostgroupRestriction; +use Icinga\Module\Director\Core\CoreApi; use Icinga\Module\Director\Web\Form\DirectorObjectForm; class IcingaHostForm extends DirectorObjectForm { + /** @var CoreApi */ + private $api; + public function setup() { $this->addObjectTypeElement(); @@ -246,4 +249,15 @@ class IcingaHostForm extends DirectorObjectForm return $db->fetchPairs($select); } + + public function setApi($api) + { + $this->api = $api; + return $this; + } + + protected function api() + { + return $this->api; + } } diff --git a/application/forms/IcingaServiceForm.php b/application/forms/IcingaServiceForm.php index 79660f67..29f67daa 100644 --- a/application/forms/IcingaServiceForm.php +++ b/application/forms/IcingaServiceForm.php @@ -273,6 +273,10 @@ class IcingaServiceForm extends DirectorObjectForm } } + /** + * @param IcingaHost $host + * @return $this + */ public function setHost(IcingaHost $host) { $this->host = $host; diff --git a/library/Director/Web/Controller/ObjectController.php b/library/Director/Web/Controller/ObjectController.php index f8debd24..3301a246 100644 --- a/library/Director/Web/Controller/ObjectController.php +++ b/library/Director/Web/Controller/ObjectController.php @@ -163,16 +163,14 @@ abstract class ObjectController extends ActionController $this->tabs()->activate('modify'); $formName = 'icinga' . ucfirst($this->getType()); - $this->content()->add( - $form = $this->loadForm($formName) - ->setDb($this->db()) - ->setAuth($this->Auth()) - ->setApi($this->getApiIfAvailable()) - ->setObject($object) - ->setAuth($this->Auth()) - ->handleRequest() - ); - + $form = $this->loadForm($formName) + ->setDb($this->db()) + ->setAuth($this->Auth()) + ->setObject($object) + ->setAuth($this->Auth()); + $this->beforeHandlingEditRequest($form); + $form->handleRequest(); + $this->content()->add($form); $this->actions()->add($this->createCloneLink()); } @@ -194,11 +192,10 @@ abstract class ObjectController extends ActionController $url = sprintf('director/%ss', $ltype); /** @var DirectorObjectForm $form */ - $form = $this->view->form = $this->loadForm('icinga' . ucfirst($type)) + $form = $this->loadForm('icinga' . ucfirst($type)) ->setDb($this->db()) ->setAuth($this->Auth()) ->presetImports($this->params->shift('imports')) - ->setApi($this->getApiIfAvailable()) ->setSuccessUrl($url); if ($oType = $this->params->shift('type')) { @@ -228,6 +225,10 @@ abstract class ObjectController extends ActionController { } + protected function beforeHandlingEditRequest($form) + { + } + public function cloneAction() { $type = $this->getType(); diff --git a/library/Director/Web/Form/DirectorObjectForm.php b/library/Director/Web/Form/DirectorObjectForm.php index 215d4924..a7b56a4e 100644 --- a/library/Director/Web/Form/DirectorObjectForm.php +++ b/library/Director/Web/Form/DirectorObjectForm.php @@ -4,7 +4,6 @@ namespace Icinga\Module\Director\Web\Form; use Exception; use Icinga\Authentication\Auth; -use Icinga\Module\Director\Core\CoreApi; use Icinga\Module\Director\Db; use Icinga\Module\Director\Data\Db\DbObject; use Icinga\Module\Director\Data\Db\DbObjectWithSettings; @@ -48,9 +47,6 @@ abstract class DirectorObjectForm extends DirectorForm private $allowsExperimental; - /** @var CoreApi */ - private $api; - private $presetImports; private $earlyProperties = array( @@ -919,7 +915,8 @@ abstract class DirectorObjectForm extends DirectorForm $this->object->setConnection($db); } - return parent::setDb($db); + parent::setDb($db); + return $this; } public function optionallyAddFromEnum($enum) @@ -1028,7 +1025,7 @@ abstract class DirectorObjectForm extends DirectorForm protected function addChoiceElement(IcingaTemplateChoice $choice) { - $imports = $this->object()->imports; + $imports = $this->object()->get('imports'); $element = $choice->createFormElement($this, $imports); $this->addElement($element); $this->choiceElements[$element->getName()] = $element; @@ -1505,17 +1502,6 @@ abstract class DirectorObjectForm extends DirectorForm return $set->enumAllowedValues(); } - public function setApi($api) - { - $this->api = $api; - return $this; - } - - protected function api() - { - return $this->api; - } - private function dummyForTranslation() { $this->translate('Host');