TemplateChoice: work without form loader

This commit is contained in:
Thomas Gelf 2017-07-05 05:46:16 +02:00
parent 0ec5ec8197
commit ceebe08ecd
5 changed files with 44 additions and 26 deletions

View File

@ -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);
}
}

View File

@ -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,

View File

@ -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;

View File

@ -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;

View File

@ -197,7 +197,7 @@ abstract class QuickForm extends QuickBaseForm
public function isApiRequest()
{
return $this->isApiRequest;
return $this->isApiRequest || $this->getRequest()->isApiRequest();
}
public function regenerateCsrfToken()