diff options
Diffstat (limited to 'application/controllers/TemplateController.php')
-rw-r--r-- | application/controllers/TemplateController.php | 107 |
1 files changed, 60 insertions, 47 deletions
diff --git a/application/controllers/TemplateController.php b/application/controllers/TemplateController.php index bb37b3c..70cf9f0 100644 --- a/application/controllers/TemplateController.php +++ b/application/controllers/TemplateController.php @@ -1,31 +1,54 @@ <?php + // Icinga Reporting | (c) 2019 Icinga GmbH | GPLv2 namespace Icinga\Module\Reporting\Controllers; use DateTime; +use Exception; use GuzzleHttp\Psr7\ServerRequest; use Icinga\Module\Reporting\Database; +use Icinga\Module\Reporting\Model; use Icinga\Module\Reporting\Web\Controller; use Icinga\Module\Reporting\Web\Forms\TemplateForm; use Icinga\Module\Reporting\Web\Widget\Template; -use ipl\Sql\Select; +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; + /** @var Model\Template */ + protected $template; - public function indexAction() + public function init() { - $this->createTabs()->activate('preview'); + parent::init(); - $template = Template::fromDb($this->params->getRequired('id')); + /** @var Model\Template $template */ + $template = Model\Template::on(Database::get()) + ->filter(Filter::equal('id', $this->params->getRequired('id'))) + ->first(); if ($template === null) { - throw new \Exception('Template not found'); + throw new Exception('Template not found'); } - $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', @@ -40,50 +63,40 @@ class TemplateController extends Controller public function editAction() { $this->assertPermission('reporting/templates'); - - $this->createTabs()->activate('edit'); - - $select = (new Select()) - ->from('template') - ->columns(['id', 'settings']) - ->where(['id = ?' => $this->params->getRequired('id')]); - - $template = $this->getDb()->select($select)->fetch(); - - if ($template === false) { - throw new \Exception('Template not found'); - } - - $template->settings = json_decode($template->settings, true); - - $form = (new TemplateForm()) - ->setTemplate($template); - - $form->handleRequest(ServerRequest::fromGlobals()); - - $this->redirectForm($form, 'reporting/templates'); + $this->addTitleTab($this->translate('Edit Template')); + + $form = TemplateForm::fromTemplate($this->template) + ->setAction((string) Url::fromRequest()) + ->on(TemplateForm::ON_SUCCESS, function (Form $form) { + $pressedButton = $form->getPressedSubmitElement(); + if ($pressedButton && $pressedButton->getName() === 'remove') { + Notification::success($this->translate('Removed template successfully')); + + $this->switchToSingleColumnLayout(); + } else { + Notification::success($this->translate('Updated template successfully')); + + $this->closeModalAndRefreshRemainingViews( + Url::fromPath('reporting/template', ['id' => $this->template->id]) + ); + } + }) + ->handleRequest(ServerRequest::fromGlobals()); $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' + ))->openInModal() + ); + + return $actions; } } |