From dac5dbdcc019b1ed86984d9891fb3e839b4b13d3 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Thu, 27 Jul 2017 11:34:12 +0200 Subject: [PATCH] TemplateUsage: make code generic, do no longer... ...require concrete implementations fixes #1028 fixes #1032 --- .../TimeperiodtemplateController.php | 135 +------------ .../controllers/UsertemplateController.php | 16 ++ .../Web/Controller/TemplateController.php | 184 ++++++++++++++++++ .../Director/Web/Table/TemplateUsageTable.php | 26 ++- 4 files changed, 223 insertions(+), 138 deletions(-) create mode 100644 application/controllers/UsertemplateController.php create mode 100644 library/Director/Web/Controller/TemplateController.php diff --git a/application/controllers/TimeperiodtemplateController.php b/application/controllers/TimeperiodtemplateController.php index 649431de..a7b26a8b 100644 --- a/application/controllers/TimeperiodtemplateController.php +++ b/application/controllers/TimeperiodtemplateController.php @@ -2,142 +2,11 @@ namespace Icinga\Module\Director\Controllers; -use Icinga\Module\Director\Objects\IcingaObject; use Icinga\Module\Director\Objects\IcingaTimePeriod; -use Icinga\Module\Director\Web\Controller\Extension\DirectorDb; -use Icinga\Module\Director\Web\Table\ObjectsTable; -use Icinga\Module\Director\Web\Table\TemplatesTable; -use Icinga\Module\Director\Web\Table\TemplateUsageTable; -use ipl\Html\FormattedString; -use ipl\Html\Html; -use ipl\Html\Link; -use ipl\Web\CompatController; -use ipl\Web\Widget\UnorderedList; +use Icinga\Module\Director\Web\Controller\TemplateController; -class TimeperiodtemplateController extends CompatController +class TimeperiodtemplateController extends TemplateController { - use DirectorDb; - - public function objectsAction() - { - $template = $this->requireTemplate(); - $type = $template->getShortTableName(); - $this - ->addSingleTab($this->translate('Timeperiods')) - ->setAutorefreshInterval(10) - ->addTitle( - $this->translate('Timeperiods based on %s'), - $template->getObjectName() - )->addBackToUsageLink($template); - - ObjectsTable::create($type, $this->db()) - ->setAuth($this->Auth()) - ->filterTemplate($template, $this->params->get('inheritance', 'direct')) - ->renderTo($this); - } - - public function templatesAction() - { - $template = $this->requireTemplate(); - $type = $template->getShortTableName(); - $this - ->addSingleTab($this->translate('Timeperiod Templates')) - ->setAutorefreshInterval(10) - ->addTitle( - $this->translate('Timeperiod templates based on %s'), - $template->getObjectName() - )->addBackToUsageLink($template); - - $table = TemplatesTable::create($type, $this->db()); - $table->filterTemplate($template, $this->params->get('inheritance', 'direct')); - $table->renderTo($this); - } - - protected function addBackToUsageLink(IcingaObject $template) - { - $this->actions()->add( - Link::create( - $this->translate('Back'), - 'director/timeperiodtemplate/usage', - ['name' => $template->getObjectName()], - ['class' => 'icon-left-big'] - ) - ); - - return $this; - } - - public function usageAction() - { - $template = $this->requireTemplate(); - $templateName = $template->getObjectName(); - - $this - ->addSingleTab($this->translate('Timeperiod Template Usage')) - ->addTitle($this->translate('Template: %s'), $templateName) - ->setAutorefreshInterval(10); - - $type = $template->getShortTableName(); - $this->actions()->add([ - Link::create( - $this->translate('Modify'), - "director/$type/edit", - ['name' => $templateName], - ['class' => 'icon-edit'] - ), - Link::create( - $this->translate('Preview'), - "director/$type/render", - ['name' => $templateName], - [ - 'title' => $this->translate('Template rendering preview'), - 'class' => 'icon-doc-text' - ] - ), - Link::create( - $this->translate('History'), - "director/$type/history", - ['name' => $templateName], - [ - 'title' => $this->translate('Template history'), - 'class' => 'icon-history' - ] - ) - ]); - - $this->content()->addPrintf( - $this->translate( - 'This is the "%s" Timeperiod Template. Based on this, you might want to:' - ), - $templateName - )->add( - new UnorderedList([ - new FormattedString($this->translate('Create a new %s inheriting from this one'), [ - Link::create( - $this->translate('Object'), - 'director/timeperiod/add', - ['import' => $templateName] - ) - ]), - new FormattedString($this->translate('Create a new %s inheriting from this one'), [ - Link::create( - $this->translate('Template'), - 'director/timeperiod/add', - ['import' => $templateName, 'type' => 'template'] - ) - ]) - ], [ - 'class' => 'vertical-action-list' - ]) - )->add( - Html::tag('h2', null, $this->translate('Current Template Usage')) - ); - - $this->content()->add( - TemplateUsageTable::forTemplate($template) - ); - } - protected function requireTemplate() { return IcingaTimePeriod::load([ diff --git a/application/controllers/UsertemplateController.php b/application/controllers/UsertemplateController.php new file mode 100644 index 00000000..41fce869 --- /dev/null +++ b/application/controllers/UsertemplateController.php @@ -0,0 +1,16 @@ + $this->params->get('name') + ], $this->db()); + } +} diff --git a/library/Director/Web/Controller/TemplateController.php b/library/Director/Web/Controller/TemplateController.php new file mode 100644 index 00000000..e93aed5b --- /dev/null +++ b/library/Director/Web/Controller/TemplateController.php @@ -0,0 +1,184 @@ +requireTemplate(); + $plural = $this->getTranslatedPluralType(); + $this + ->addSingleTab($plural) + ->setAutorefreshInterval(10) + ->addTitle( + $this->translate('%s based on %s'), + $plural, + $template->getObjectName() + )->addBackToUsageLink($template); + + ObjectsTable::create($this->getType(), $this->db()) + ->setAuth($this->Auth()) + ->filterTemplate($template, $this->params->get('inheritance', 'direct')) + ->renderTo($this); + } + + public function templatesAction() + { + $template = $this->requireTemplate(); + $typeName = $this->getTranslatedType(); + $this + ->addSingleTab(sprintf($this->translate('%s Templates'), $typeName)) + ->setAutorefreshInterval(10) + ->addTitle( + $this->translate('%s templates based on %s'), + $typeName, + $template->getObjectName() + )->addBackToUsageLink($template); + + $table = TemplatesTable::create($this->getType(), $this->db()); + $table->filterTemplate($template, $this->params->get('inheritance', 'direct')); + $table->renderTo($this); + } + + protected function addBackToUsageLink(IcingaObject $template) + { + $type = $this->getType(); + $this->actions()->add( + Link::create( + $this->translate('Back'), + "director/${type}template/usage", + ['name' => $template->getObjectName()], + ['class' => 'icon-left-big'] + ) + ); + + return $this; + } + + public function usageAction() + { + $template = $this->requireTemplate(); + $templateName = $template->getObjectName(); + + $this + ->addSingleTab($this->translate('Template Usage')) + ->addTitle($this->translate('Template: %s'), $templateName) + ->setAutorefreshInterval(10); + + $type = $this->getType(); + $this->actions()->add([ + Link::create( + $this->translate('Modify'), + "director/$type/edit", + ['name' => $templateName], + ['class' => 'icon-edit'] + ), + Link::create( + $this->translate('Preview'), + "director/$type/render", + ['name' => $templateName], + [ + 'title' => $this->translate('Template rendering preview'), + 'class' => 'icon-doc-text' + ] + ), + Link::create( + $this->translate('History'), + "director/$type/history", + ['name' => $templateName], + [ + 'title' => $this->translate('Template history'), + 'class' => 'icon-history' + ] + ) + ]); + + $typeName = $this->getTranslatedType(); + $this->content()->addPrintf( + $this->translate( + 'This is the "%s" %s Template. Based on this, you might want to:' + ), + $typeName, + $templateName + )->add( + new UnorderedList([ + new FormattedString($this->translate('Create a new %s inheriting from this one'), [ + Link::create( + $this->translate('Object'), + "director/$type/add", + ['import' => $templateName] + ) + ]), + new FormattedString($this->translate('Create a new %s inheriting from this one'), [ + Link::create( + $this->translate('Template'), + "director/$type/add", + ['import' => $templateName, 'type' => 'template'] + ) + ]) + ], [ + 'class' => 'vertical-action-list' + ]) + )->add( + Html::tag('h2', null, $this->translate('Current Template Usage')) + ); + + $this->content()->add( + TemplateUsageTable::forTemplate($template) + ); + } + + protected function getType() + { + return $this->template()->getShortTableName(); + } + + protected function getPluralType() + { + return $this->template()->getShortTableName() . 's'; + } + + protected function getTranslatedType() + { + return $this->translate(ucfirst($this->getType())); + } + + protected function getTranslatedPluralType() + { + return $this->translate(ucfirst($this->getPluralType())); + } + + /** + * @return IcingaObject + */ + protected function template() + { + if ($this->template === null) { + $this->template = $this->requireTemplate(); + } + + return $this->template; + } + + /** + * @return IcingaObject + */ + abstract protected function requireTemplate(); +} diff --git a/library/Director/Web/Table/TemplateUsageTable.php b/library/Director/Web/Table/TemplateUsageTable.php index d213139f..32c76611 100644 --- a/library/Director/Web/Table/TemplateUsageTable.php +++ b/library/Director/Web/Table/TemplateUsageTable.php @@ -9,7 +9,7 @@ use ipl\Html\Link; use ipl\Html\Table; use ipl\Translation\TranslationHelper; -abstract class TemplateUsageTable extends Table +class TemplateUsageTable extends Table { use TranslationHelper; @@ -17,7 +17,21 @@ abstract class TemplateUsageTable extends Table protected $objectType; - abstract public function getTypes(); + public function getTypes() + { + return [ + 'templates' => $this->translate('Templates'), + 'objects' => $this->translate('Objects'), + ]; + } + + protected function getTypeSummaryDefinitions() + { + return [ + 'templates' => $this->getSummaryLine('template'), + 'objects' => $this->getSummaryLine('object'), + ]; + } /** * @param IcingaObject $template @@ -27,7 +41,11 @@ abstract class TemplateUsageTable extends Table { $type = ucfirst($template->getShortTableName()); $class = __NAMESPACE__ . "\\${type}TemplateUsageTable"; - return new $class($template); + if (class_exists($class)) { + return new $class($template); + } else { + return new static($template); + } } protected function __construct(IcingaObject $template) @@ -83,8 +101,6 @@ abstract class TemplateUsageTable extends Table } } - abstract protected function getTypeSummaryDefinitions(); - protected function getUsageSummary(IcingaObject $template) { $id = $template->getAutoincId();