From ceebe08ecd07f43d5a0c962eb5728d4273f9eeba Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Wed, 5 Jul 2017 05:46:16 +0200 Subject: [PATCH] TemplateChoice: work without form loader --- .../controllers/TemplatechoiceController.php | 32 +++++++------------ .../forms/IcingaTemplateChoiceForm.php | 20 ++++++++++-- .../Web/Controller/ActionController.php | 4 +++ .../Director/Web/Form/DirectorObjectForm.php | 12 ++++++- library/Director/Web/Form/QuickForm.php | 2 +- 5 files changed, 44 insertions(+), 26 deletions(-) diff --git a/application/controllers/TemplatechoiceController.php b/application/controllers/TemplatechoiceController.php index c981e320..7a29741f 100644 --- a/application/controllers/TemplatechoiceController.php +++ b/application/controllers/TemplatechoiceController.php @@ -2,7 +2,7 @@ namespace Icinga\Module\Director\Controllers; -use Icinga\Module\Director\Objects\IcingaTemplateChoiceHost; +use Icinga\Module\Director\Forms\IcingaTemplateChoiceForm; use Icinga\Module\Director\Objects\IcingaTemplateChoiceService; use Icinga\Module\Director\Web\Controller\ActionController; @@ -15,31 +15,21 @@ class TemplatechoiceController extends ActionController public function hostAction() { + $form = IcingaTemplateChoiceForm::create('host', $this->db()) + ->optionallyLoad($this->params->get('name')) + ->handleRequest(); $this->addSingleTab('Choice') - ->addTitle($this->translate('Host template choice')); - $this->content()->add( - $form = $this->loadForm('IcingaTemplateChoice') - ->setDb($this->db()) - ->setChoiceType('host') - ); - if ($name = $this->params->get('name')) { - $form->setObject(IcingaTemplateChoiceHost::load($name, $this->db())); - } - $form->handleRequest(); + ->addTitle($this->translate('Host template choice')) + ->content()->add($form); } public function serviceAction() { + $form = IcingaTemplateChoiceForm::create('service', $this->db()) + ->optionallyLoad($this->params->get('name')) + ->handleRequest(); $this->addSingleTab('Choice') - ->addTitle($this->translate('Service template choice')); - $this->content()->add( - $form = $this->loadForm('IcingaTemplateChoice') - ->setDb($this->db()) - ->setChoiceType('service') - ); - if ($name = $this->params->get('name')) { - $form->setObject(IcingaTemplateChoiceService::load($name, $this->db())); - } - $form->handleRequest(); + ->addTitle($this->translate('Service template choice')) + ->content()->add($form); } } diff --git a/application/forms/IcingaTemplateChoiceForm.php b/application/forms/IcingaTemplateChoiceForm.php index 8d4e17d7..a0b33b0e 100644 --- a/application/forms/IcingaTemplateChoiceForm.php +++ b/application/forms/IcingaTemplateChoiceForm.php @@ -2,6 +2,7 @@ namespace Icinga\Module\Director\Forms; +use Icinga\Module\Director\Db; use Icinga\Module\Director\Objects\IcingaTemplateChoice; use Icinga\Module\Director\Web\Form\DirectorObjectForm; @@ -9,6 +10,22 @@ class IcingaTemplateChoiceForm extends DirectorObjectForm { private $choiceType; + public static function create($type, Db $db) + { + return static::load()->setDb($db)->setChoiceType($type); + } + + public function optionallyLoad($name) + { + if ($name !== null) { + /** @var IcingaTemplateChoice $class - cheating IDE */ + $class = $this->getObjectClassName(); + $this->setObject($class::load($name, $this->getDb())); + } + + return $this; + } + protected function getObjectClassname() { if ($this->className === null) { @@ -27,9 +44,6 @@ class IcingaTemplateChoiceForm extends DirectorObjectForm public function setup() { - /** @var IcingaTemplateChoice $object */ - $object = $this->object(); - $this->addElement('text', 'object_name', array( 'label' => $this->translate('Choice name'), 'required' => true, diff --git a/library/Director/Web/Controller/ActionController.php b/library/Director/Web/Controller/ActionController.php index 6c9bc166..72335409 100644 --- a/library/Director/Web/Controller/ActionController.php +++ b/library/Director/Web/Controller/ActionController.php @@ -17,6 +17,7 @@ use Icinga\Module\Director\Web\Table\QuickTable; use Icinga\Module\Director\Web\Table\TableLoader; use Icinga\Security\SecurityException; use Icinga\Web\Controller; +use Icinga\Web\UrlParams; use Icinga\Web\Widget; use ipl\Web\Component\ControlsAndContent; use ipl\Web\Controller\Extension\ControlsAndContentHelper; @@ -31,6 +32,9 @@ abstract class ActionController extends Controller implements ControlsAndContent protected $isApified = false; + /** @var UrlParams Hint for IDE, somehow does not work in web */ + protected $params; + /** @var Monitoring */ private $monitoring; diff --git a/library/Director/Web/Form/DirectorObjectForm.php b/library/Director/Web/Form/DirectorObjectForm.php index e1bb846e..a597922b 100644 --- a/library/Director/Web/Form/DirectorObjectForm.php +++ b/library/Director/Web/Form/DirectorObjectForm.php @@ -3,6 +3,7 @@ namespace Icinga\Module\Director\Web\Form; use Exception; +use Icinga\Application\Icinga; use Icinga\Authentication\Auth; use Icinga\Module\Director\Core\CoreApi; use Icinga\Module\Director\Db; @@ -13,7 +14,6 @@ use Icinga\Module\Director\IcingaConfig\StateFilterSet; use Icinga\Module\Director\IcingaConfig\TypeFilterSet; use Icinga\Module\Director\Objects\IcingaTemplateChoiceHost; use Icinga\Module\Director\Objects\IcingaObject; -use Icinga\Module\Director\Restriction\ObjectRestriction; use Icinga\Module\Director\Util; use Zend_Form_Element as ZfElement; use Zend_Form_Element_Select as ZfSelect; @@ -70,6 +70,16 @@ abstract class DirectorObjectForm extends QuickForm 'event_command_id', ); + /** + * @return static + */ + public static function load() + { + return new static([ + 'icingaModule' => Icinga::App()->getModuleManager()->getModule('director') + ]); + } + public function setPreferredObjectType($type) { $this->preferredObjectType = $type; diff --git a/library/Director/Web/Form/QuickForm.php b/library/Director/Web/Form/QuickForm.php index 3952e59b..9d11d3d8 100644 --- a/library/Director/Web/Form/QuickForm.php +++ b/library/Director/Web/Form/QuickForm.php @@ -197,7 +197,7 @@ abstract class QuickForm extends QuickBaseForm public function isApiRequest() { - return $this->isApiRequest; + return $this->isApiRequest || $this->getRequest()->isApiRequest(); } public function regenerateCsrfToken()