HostForm: take over logic from base form, clean up

This commit is contained in:
Thomas Gelf 2017-07-25 10:18:07 +02:00
parent b675d37b6f
commit fadbde5e9b
5 changed files with 52 additions and 30 deletions

View File

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

View File

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

View File

@ -273,6 +273,10 @@ class IcingaServiceForm extends DirectorObjectForm
}
}
/**
* @param IcingaHost $host
* @return $this
*/
public function setHost(IcingaHost $host)
{
$this->host = $host;

View File

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

View File

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