Skip to content

Commit

Permalink
Render only template preview in detail view & use action bars
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Aug 11, 2023
1 parent 4a22a05 commit 5a08682
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 62 deletions.
86 changes: 40 additions & 46 deletions application/controllers/TemplateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,29 @@
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;
use Icinga\Module\Reporting\Web\Forms\TemplateForm;
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')))
Expand All @@ -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',
Expand All @@ -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;
}
}
19 changes: 3 additions & 16 deletions application/controllers/TemplatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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());

Expand Down
2 changes: 2 additions & 0 deletions library/Reporting/Web/Forms/TemplateForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down

0 comments on commit 5a08682

Please sign in to comment.