diff --git a/application/controllers/TemplateController.php b/application/controllers/TemplateController.php index 26dbeb2d..549d0e8d 100644 --- a/application/controllers/TemplateController.php +++ b/application/controllers/TemplateController.php @@ -7,7 +7,6 @@ use DateTime; use Exception; use GuzzleHttp\Psr7\ServerRequest; -use Icinga\Application\Version; use Icinga\Module\Reporting\Database; use Icinga\Module\Reporting\Model; use Icinga\Module\Reporting\Web\Controller; @@ -15,16 +14,22 @@ use Icinga\Module\Reporting\Web\Widget\Template; use Icinga\Web\Notification; use ipl\Html\Form; +use ipl\Html\ValidHtml; use ipl\Stdlib\Filter; use ipl\Web\Url; +use ipl\Web\Widget\ActionBar; +use ipl\Web\Widget\ActionLink; class TemplateController extends Controller { use Database; - public function indexAction() + /** @var Model\Template */ + protected $template; + + public function init() { - $this->createTabs()->activate('preview'); + parent::init(); $template = Model\Template::on($this->getDb()) ->filter(Filter::equal('id', $this->params->getRequired('id'))) @@ -34,7 +39,17 @@ public function indexAction() throw new Exception('Template not found'); } - $template = Template::fromModel($template) + $this->template = $template; + } + + public function indexAction() + { + $this->addTitleTab($this->translate('Preview')); + + $this->controls->getAttributes()->add('class', 'default-layout'); + $this->addControl($this->createActionBars()); + + $template = Template::fromModel($this->template) ->setMacros([ 'date' => (new DateTime())->format('jS M, Y'), 'time_frame' => 'Time Frame', @@ -49,63 +64,42 @@ public function indexAction() public function editAction() { $this->assertPermission('reporting/templates'); + $this->addTitleTab($this->translate('Edit Template')); - $this->createTabs()->activate('edit'); - - $template = Model\Template::on($this->getDb()) - ->filter(Filter::equal('id', $this->params->getRequired('id'))) - ->first(); - - if ($template === false) { - throw new Exception('Template not found'); - } - - $template->settings = json_decode($template->settings, true); - - $form = TemplateForm::fromTemplate($template) + $form = TemplateForm::fromTemplate($this->template) ->setAction((string) Url::fromRequest()) ->on(TemplateForm::ON_SUCCESS, function (Form $form) { - $extraUpdates = ['#col1']; + $url = '__CLOSE__'; if ($form->getPressedSubmitElement()->getName() === 'remove') { Notification::success($this->translate('Removed template successfully')); - - $extraUpdates['col2'] = '__CLOSE__'; } else { - Notification::success($this->translate('Updated template successfully')); - } + $url = Url::fromPath('reporting/template', ['id' => $this->template->id]); - // Older Icinga Web 2 versions close #col2 container automatically when redirecting using close. - // So, we need to send the extra updates only when using Icinga Web 2.12 or newer. - if (version_compare(Version::VERSION, '2.12', '>=')) { - $this->sendExtraUpdates($extraUpdates); + Notification::success($this->translate('Updated template successfully')); } - $this->redirectNow('__CLOSE__'); + $this->redirectNow($url); }) ->handleRequest(ServerRequest::fromGlobals()); - $this->setTitle($this->translate('Edit template')); $this->addContent($form); } - protected function createTabs() + protected function createActionBars(): ValidHtml { - $tabs = $this->getTabs(); - - if ($this->hasPermission('reporting/templates')) { - $tabs->add('edit', [ - 'title' => $this->translate('Edit template'), - 'label' => $this->translate('Edit Template'), - 'url' => 'reporting/template/edit?id=' . $this->params->getRequired('id') - ]); - } - - $tabs->add('preview', [ - 'title' => $this->translate('Preview template'), - 'label' => $this->translate('Preview'), - 'url' => 'reporting/template?id=' . $this->params->getRequired('id') - ]); - - return $tabs; + $actions = new ActionBar(); + $actions->addHtml( + new ActionLink( + $this->translate('Modify'), + Url::fromPath('reporting/template/edit', ['id' => $this->template->id]), + 'edit', + [ + 'data-icinga-modal' => true, + 'data-no-icinga-ajax' => true + ] + ) + ); + + return $actions; } } diff --git a/application/controllers/TemplatesController.php b/application/controllers/TemplatesController.php index eee39c22..d709340a 100644 --- a/application/controllers/TemplatesController.php +++ b/application/controllers/TemplatesController.php @@ -4,7 +4,6 @@ namespace Icinga\Module\Reporting\Controllers; -use GuzzleHttp\Psr7\ServerRequest; use Icinga\Module\Reporting\Database; use Icinga\Module\Reporting\Model; use Icinga\Module\Reporting\Web\Controller; @@ -54,20 +53,8 @@ public function indexAction() $this->addControl($sortControl); foreach ($templates as $template) { - if ($canManage) { - $subjectLink = new Link( - $template->name, - Url::fromPath('reporting/template/edit', ['id' => $template->id]), - [ - 'data-icinga-modal' => true, - 'data-no-icinga-ajax' => true - ] - ); - } else { - // Preview URL - $subjectLink = new Link($template->name, Url::fromPath('reporting/template', ['id' => $template->id])); - } - + // Preview URL + $subjectLink = new Link($template->name, Url::fromPath('reporting/template', ['id' => $template->id])); $tableRows[] = Html::tag('tr', null, [ Html::tag('td', null, $subjectLink), Html::tag('td', null, $template->author), @@ -115,7 +102,7 @@ public function newAction() ->on(TemplateForm::ON_SUCCESS, function () { Notification::success($this->translate('Created template successfully')); - $this->redirectNow('reporting/templates'); + $this->redirectNow(Url::fromPath('reporting/templates')); }) ->handleRequest($this->getServerRequest()); diff --git a/library/Reporting/Web/Forms/TemplateForm.php b/library/Reporting/Web/Forms/TemplateForm.php index 56088331..44f58b91 100644 --- a/library/Reporting/Web/Forms/TemplateForm.php +++ b/library/Reporting/Web/Forms/TemplateForm.php @@ -8,6 +8,7 @@ use GuzzleHttp\Psr7\UploadedFile; use Icinga\Authentication\Auth; use Icinga\Module\Reporting\Database; +use Icinga\Util\Json; use ipl\Html\Contract\FormSubmitElement; use ipl\Html\Html; use ipl\Web\Compat\CompatForm; @@ -34,6 +35,7 @@ public static function fromTemplate($template): self { $form = new static(); + $template->settings = Json::decode($template->settings, true); $form->template = $template; if ($template->settings) {