controllers: refactor even more of them

This commit is contained in:
Thomas Gelf 2017-08-16 23:51:15 +02:00
parent 23249dcf7d
commit 1a72e89f05
3 changed files with 59 additions and 67 deletions

View File

@ -8,7 +8,9 @@ use Icinga\Module\Director\Web\Controller\ObjectController;
use Icinga\Module\Director\Objects\IcingaServiceSet;
use Icinga\Module\Director\Objects\IcingaService;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
use Icinga\Module\Director\Web\Table\IcingaAppliedServiceTable;
use Icinga\Web\Widget\Tab;
use ipl\Html\Link;
use ipl\Web\Widget\Tabs;
@ -84,6 +86,7 @@ class ServiceController extends ObjectController
protected function addParamToTabs($name, $value)
{
foreach ($this->tabs()->getTabs() as $tab) {
/** @var Tab $tab */
$tab->getUrl()->setParam($name, $value);
}
@ -108,12 +111,9 @@ class ServiceController extends ObjectController
}
}
/**
* @param IcingaServiceForm $form
*/
protected function beforeHandlingAddRequest($form)
protected function onObjectFormLoaded(DirectorObjectForm $form)
{
if ($this->apply) {
if ($this->object === null && $this->apply) {
$form->createApplyRuleFor($this->apply);
}
}
@ -153,7 +153,7 @@ class ServiceController extends ObjectController
}
$form->handleRequest();
$this->actions()->add($this->createCloneLink());
$this->addActionClone();
if ($this->host) {
$this->view->subtitle = sprintf(
@ -229,12 +229,12 @@ class ServiceController extends ObjectController
$db = $this->db();
if ($this->host) {
$this->view->host = $this->host;
// $this->view->host = $this->host;
$params['host_id'] = $this->host->id;
}
if ($this->set) {
$this->view->set = $this->set;
// $this->view->set = $this->set;
$params['service_set_id'] = $this->set->id;
}
$this->object = IcingaService::load($params, $db);
@ -242,9 +242,6 @@ class ServiceController extends ObjectController
parent::loadObject();
}
}
$this->view->undeployedChanges = $this->countUndeployedChanges();
$this->view->totalUndeployedChanges = $this->db()
->countActivitiesSinceLastDeployedConfig();
return $this->object;
}

View File

@ -2,45 +2,43 @@
namespace Icinga\Module\Director\Controllers;
use Icinga\Module\Director\Forms\IcingaServiceSetForm;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaServiceSet;
use Icinga\Module\Director\Web\Controller\ObjectController;
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
use Icinga\Module\Director\Web\Table\IcingaServiceSetHostTable;
use Icinga\Module\Director\Web\Table\IcingaServiceSetServiceTable;
use ipl\Html\Link;
class ServicesetController extends ObjectController
{
/** @var IcingaHost */
protected $host;
protected function checkDirectorPermissions()
{
$this->assertPermission('director/servicesets');
}
public function init()
{
if ($host = $this->params->get('host')) {
if (null !== ($host = $this->params->get('host'))) {
$this->host = IcingaHost::load($host, $this->db());
}
parent::init();
if ($this->object) {
$tabs = $this->tabs();
$tabs->add('services', array(
'url' => 'director/serviceset/services',
'urlParams' => array('name' => $this->object->object_name),
'label' => 'Services'
));
$tabs->add('hosts', array(
'url' => 'director/serviceset/hosts',
'urlParams' => array('name' => $this->object->object_name),
'label' => 'Hosts'
));
$this->addServiceSetTabs();
}
}
public function loadForm($name)
protected function onObjectFormLoaded(DirectorObjectForm $form)
{
$form = parent::loadForm($name);
if ($name === 'icingaServiceSet' && $this->host) {
if ($this->host) {
/** @var IcingaServiceSetForm $form */
$form->setHost($this->host);
}
return $form;
}
public function addAction()
@ -49,65 +47,70 @@ class ServicesetController extends ObjectController
if ($this->host) {
$this->addTitle(
$this->translate('Add a service set to "%s"'),
$this->host->object_name
$this->host->getObjectName()
);
}
}
public function servicesAction()
{
$db = $this->db();
/** @var IcingaServiceSet $set */
$set = $this->object;
$name = $set->getObjectName();
$this->tabs()->activate('services');
$this->addTitle(
$this->translate('Services in this set: %s'),
$set->object_name
$name
);
$this->actions()->add(Link::create(
$this->translate('Add service'),
'director/service/add',
['set' => $set->object_name],
['set' => $name],
['class' => 'icon-plus']
));
// TODO!!
$this->view->stayHere = true;
$this->content()->add(
$this->loadTable('IcingaServiceSetService')
->setServiceSet($set)
->setConnection($db)
);
IcingaServiceSetServiceTable::load($set)->renderTo($this);
}
public function hostsAction()
{
$db = $this->db();
/** @var IcingaServiceSet $set */
$set = $this->object;
$this->tabs()->activate('hosts');
$this->addTitle(
$this->translate('Hosts using this set: %s'),
$set->object_name
$set->getObjectName()
);
$this->content()->add(
$this->loadTable('IcingaServiceSetHost')
->setServiceSet($set)
->setConnection($db)
);
IcingaServiceSetHostTable::load($set)->renderTo($this);
}
protected function addServiceSetTabs()
{
$tabs = $this->tabs();
$name = $this->object->getObjectName();
$tabs->add('services', [
'url' => 'director/serviceset/services',
'urlParams' => ['name' => $name],
'label' => 'Services'
])->add('hosts', [
'url' => 'director/serviceset/hosts',
'urlParams' => ['name' => $name],
'label' => 'Hosts'
]);
return $this;
}
protected function loadObject()
{
if ($this->object === null) {
if ($name = $this->params->get('name')) {
$params = array('object_name' => $name);
if (null !== ($name = $this->params->get('name'))) {
$params = ['object_name' => $name];
$db = $this->db();
if ($this->host) {
$this->view->host = $this->host;
$params['host_id'] = $this->host->id;
$params['host_id'] = $this->host->get('id');
}
$this->object = IcingaServiceSet::load($params, $db);

View File

@ -5,7 +5,7 @@ namespace Icinga\Module\Director\Controllers;
use Icinga\Module\Director\Forms\IcingaTimePeriodRangeForm;
use Icinga\Module\Director\Objects\IcingaTimePeriod;
use Icinga\Module\Director\Web\Controller\ObjectController;
use ipl\Html\Link;
use Icinga\Module\Director\Web\Table\IcingaTimePeriodRangeTable;
class TimeperiodController extends ObjectController
{
@ -16,26 +16,18 @@ class TimeperiodController extends ObjectController
$this->tabs()->activate('ranges');
$this->addTitle($this->translate('Time period ranges'));
$form = IcingaTimePeriodRangeForm::load()
->setTimePeriod($object)
->setDb($this->db());
->setTimePeriod($object);
if ($name = $this->params->get('range')) {
$this->actions()->add(new Link(
$this->translate('back'),
$this->getRequest()->getUrl()->without('range'),
null,
['class' => 'icon-left-big']
));
if (null !== ($name = $this->params->get('range'))) {
$this->addBackLink($this->url()->without('range'));
$form->loadObject([
'timeperiod_id' => $this->object->id,
'timeperiod_id' => $object->get('id'),
'range_key' => $name,
'range_type' => $this->params->get('range_type')
]);
}
$form->handleRequest();
$table = $this->loadTable('icingaTimePeriodRange')
->setTimePeriod($this->object);
$this->content()->add([$form, $table]);
$this->content()->add($form->handleRequest());
IcingaTimePeriodRangeTable::load($object)->renderTo($this);
}
}