ImportsourceController: clean up, explicitly load

...with autoincement id
This commit is contained in:
Thomas Gelf 2018-10-04 06:05:27 +02:00
parent b7ed665d35
commit d13c6f2ecf
1 changed files with 54 additions and 40 deletions

View File

@ -2,6 +2,7 @@
namespace Icinga\Module\Director\Controllers; namespace Icinga\Module\Director\Controllers;
use Icinga\Exception\NotFoundError;
use Icinga\Module\Director\Forms\ImportRowModifierForm; use Icinga\Module\Director\Forms\ImportRowModifierForm;
use Icinga\Module\Director\Forms\ImportSourceForm; use Icinga\Module\Director\Forms\ImportSourceForm;
use Icinga\Module\Director\Web\ActionBar\AutomationObjectActionBar; use Icinga\Module\Director\Web\ActionBar\AutomationObjectActionBar;
@ -13,22 +14,30 @@ use Icinga\Module\Director\Web\Table\ImportsourceHookTable;
use Icinga\Module\Director\Web\Table\PropertymodifierTable; use Icinga\Module\Director\Web\Table\PropertymodifierTable;
use Icinga\Module\Director\Web\Tabs\ImportsourceTabs; use Icinga\Module\Director\Web\Tabs\ImportsourceTabs;
use Icinga\Module\Director\Web\Widget\ImportSourceDetails; use Icinga\Module\Director\Web\Widget\ImportSourceDetails;
use InvalidArgumentException;
use dipl\Html\Link; use dipl\Html\Link;
class ImportsourceController extends ActionController class ImportsourceController extends ActionController
{ {
/** @var ImportSource|null */
private $importSource;
private $id;
/** /**
* @throws \Icinga\Exception\AuthenticationException * @throws \Icinga\Exception\AuthenticationException
* @throws \Icinga\Exception\Http\HttpNotFoundException
* @throws \Icinga\Exception\NotFoundError * @throws \Icinga\Exception\NotFoundError
* @throws \Icinga\Security\SecurityException * @throws \Icinga\Security\SecurityException
*/ */
public function init() public function init()
{ {
parent::init(); parent::init();
$id = $this->params->get('source_id', $this->params->get('id')); $id = $this->params->get('source_id', $this->params->get('id'));
$tabs = $this->tabs(new ImportsourceTabs($id)); if ($id !== null && is_numeric($id)) {
$this->id = (int) $id;
}
$tabs = $this->tabs(new ImportsourceTabs($this->id));
$action = $this->getRequest()->getActionName(); $action = $this->getRequest()->getActionName();
if ($tabs->has($action)) { if ($tabs->has($action)) {
$tabs->activate($action); $tabs->activate($action);
@ -43,15 +52,13 @@ class ImportsourceController extends ActionController
} }
/** /**
* @throws \Icinga\Exception\ConfigurationError
* @throws \Icinga\Exception\IcingaException * @throws \Icinga\Exception\IcingaException
* @throws \Icinga\Exception\MissingParameterException
* @throws \Icinga\Exception\NotFoundError * @throws \Icinga\Exception\NotFoundError
*/ */
public function indexAction() public function indexAction()
{ {
$this->addMainActions(); $this->addMainActions();
$source = ImportSource::load($this->params->getRequired('id'), $this->db()); $source = $this->getImportSource();
if ($this->params->get('format') === 'json') { if ($this->params->get('format') === 'json') {
$this->sendJson($this->getResponse(), $source->export()); $this->sendJson($this->getResponse(), $source->export());
return; return;
@ -63,9 +70,6 @@ class ImportsourceController extends ActionController
$this->content()->add(new ImportSourceDetails($source)); $this->content()->add(new ImportSourceDetails($source));
} }
/**
* @throws \Icinga\Exception\ConfigurationError
*/
public function addAction() public function addAction()
{ {
$this->addTitle($this->translate('Add import source')) $this->addTitle($this->translate('Add import source'))
@ -77,16 +81,14 @@ class ImportsourceController extends ActionController
} }
/** /**
* @throws \Icinga\Exception\ConfigurationError * @throws NotFoundError
* @throws \Icinga\Exception\MissingParameterException
*/ */
public function editAction() public function editAction()
{ {
$this->addMainActions(); $this->addMainActions();
$this->tabs()->activateMainWithPostfix($this->translate('Modify')); $this->activateTabWithPostfix($this->translate('Modify'));
$id = $this->params->getRequired('id'); $form = ImportSourceForm::load()
$form = ImportSourceForm::load()->setDb($this->db()) ->setObject($this->getImportSource())
->loadObject($id)
->setListUrl('director/importsources') ->setListUrl('director/importsources')
->handleRequest(); ->handleRequest();
$this->addTitle( $this->addTitle(
@ -98,16 +100,13 @@ class ImportsourceController extends ActionController
} }
/** /**
* @throws \Icinga\Exception\ConfigurationError
* @throws \Icinga\Exception\MissingParameterException
* @throws \Icinga\Exception\NotFoundError * @throws \Icinga\Exception\NotFoundError
*/ */
public function cloneAction() public function cloneAction()
{ {
$this->addMainActions(); $this->addMainActions();
$this->tabs()->activateMainWithPostfix($this->translate('Clone')); $this->activateTabWithPostfix($this->translate('Clone'));
$id = $this->params->getRequired('id'); $source = $this->getImportSource();
$source = ImportSource::load($id, $this->db());
$this->addTitle('Clone: %s', $source->get('source_name')); $this->addTitle('Clone: %s', $source->get('source_name'));
$form = new CloneImportSourceForm($source); $form = new CloneImportSourceForm($source);
$this->content()->add($form); $this->content()->add($form);
@ -115,13 +114,11 @@ class ImportsourceController extends ActionController
} }
/** /**
* @throws \Icinga\Exception\ConfigurationError
* @throws \Icinga\Exception\MissingParameterException
* @throws \Icinga\Exception\NotFoundError * @throws \Icinga\Exception\NotFoundError
*/ */
public function previewAction() public function previewAction()
{ {
$source = ImportSource::load($this->params->getRequired('id'), $this->db()); $source = $this->getImportSource();
$this->addTitle( $this->addTitle(
$this->translate('Import source preview: %s'), $this->translate('Import source preview: %s'),
@ -136,13 +133,11 @@ class ImportsourceController extends ActionController
/** /**
* @return ImportSource * @return ImportSource
* @throws \Icinga\Exception\ConfigurationError
* @throws \Icinga\Exception\MissingParameterException
* @throws \Icinga\Exception\NotFoundError * @throws \Icinga\Exception\NotFoundError
*/ */
protected function requireImportSourceAndAddModifierTable() protected function requireImportSourceAndAddModifierTable()
{ {
$source = ImportSource::load($this->params->getRequired('source_id'), $this->db()); $source = $this->getImportSource();
PropertymodifierTable::load($source, $this->url()) PropertymodifierTable::load($source, $this->url())
->handleSortPriorityActions($this->getRequest(), $this->getResponse()) ->handleSortPriorityActions($this->getRequest(), $this->getResponse())
->renderTo($this); ->renderTo($this);
@ -151,8 +146,6 @@ class ImportsourceController extends ActionController
} }
/** /**
* @throws \Icinga\Exception\ConfigurationError
* @throws \Icinga\Exception\MissingParameterException
* @throws \Icinga\Exception\NotFoundError * @throws \Icinga\Exception\NotFoundError
*/ */
public function modifierAction() public function modifierAction()
@ -162,19 +155,17 @@ class ImportsourceController extends ActionController
$this->addAddLink( $this->addAddLink(
$this->translate('Add property modifier'), $this->translate('Add property modifier'),
'director/importsource/addmodifier', 'director/importsource/addmodifier',
['source_id' => $source->getId()], ['source_id' => $source->get('id')],
'_self' '_self'
); );
} }
/** /**
* @throws \Icinga\Exception\ConfigurationError
* @throws \Icinga\Exception\MissingParameterException
* @throws \Icinga\Exception\NotFoundError * @throws \Icinga\Exception\NotFoundError
*/ */
public function historyAction() public function historyAction()
{ {
$source = ImportSource::load($this->params->getRequired('id'), $this->db()); $source = $this->getImportSource();
$this->addTitle($this->translate('Import run history: %s'), $source->get('source_name')); $this->addTitle($this->translate('Import run history: %s'), $source->get('source_name'));
// TODO: temporarily disabled, find a better place for stats: // TODO: temporarily disabled, find a better place for stats:
@ -183,9 +174,6 @@ class ImportsourceController extends ActionController
} }
/** /**
* @throws \Icinga\Exception\ConfigurationError
* @throws \Icinga\Exception\Http\HttpNotFoundException
* @throws \Icinga\Exception\MissingParameterException
* @throws \Icinga\Exception\NotFoundError * @throws \Icinga\Exception\NotFoundError
*/ */
public function addmodifierAction() public function addmodifierAction()
@ -202,14 +190,12 @@ class ImportsourceController extends ActionController
->setSource($source) ->setSource($source)
->setSuccessUrl( ->setSuccessUrl(
'director/importsource/modifier', 'director/importsource/modifier',
['source_id' => $source->getId()] ['source_id' => $source->get('id')]
)->handleRequest() )->handleRequest()
); );
} }
/** /**
* @throws \Icinga\Exception\ConfigurationError
* @throws \Icinga\Exception\Http\HttpNotFoundException
* @throws \Icinga\Exception\MissingParameterException * @throws \Icinga\Exception\MissingParameterException
* @throws \Icinga\Exception\NotFoundError * @throws \Icinga\Exception\NotFoundError
*/ */
@ -217,7 +203,7 @@ class ImportsourceController extends ActionController
{ {
// We need to load the table AFTER adding the title, otherwise search // We need to load the table AFTER adding the title, otherwise search
// will not be placed next to the title // will not be placed next to the title
$source = ImportSource::load($this->params->getRequired('source_id'), $this->db()); $source = $this->getImportSource();
$this->addTitle( $this->addTitle(
$this->translate('%s: Property Modifier'), $this->translate('%s: Property Modifier'),
@ -237,6 +223,34 @@ class ImportsourceController extends ActionController
); );
} }
/**
* @return ImportSource
* @throws NotFoundError
*/
protected function getImportSource()
{
if ($this->importSource === null) {
if ($this->id === null) {
throw new InvalidArgumentException('Got no ImportSource id');
}
$this->importSource = ImportSource::loadWithAutoIncId(
$this->id,
$this->db()
);
}
return $this->importSource;
}
protected function activateTabWithPostfix($title)
{
/** @var ImportsourceTabs $tabs */
$tabs = $this->tabs();
$tabs->activateMainWithPostfix($title);
return $this;
}
/** /**
* @param ImportSource $source * @param ImportSource $source
* @return $this * @return $this
@ -247,7 +261,7 @@ class ImportsourceController extends ActionController
Link::create( Link::create(
$this->translate('back'), $this->translate('back'),
'director/importsource/modifier', 'director/importsource/modifier',
['source_id' => $source->getId()], ['source_id' => $source->get('id')],
['class' => 'icon-left-big'] ['class' => 'icon-left-big']
) )
); );