1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
<?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 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
{
/** @var Model\Template */
protected $template;
public function init(): void
{
parent::init();
/** @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');
}
$this->template = $template;
}
public function indexAction(): void
{
$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',
'time_frame_absolute' => 'Time Frame (absolute)',
'title' => 'Icinga Report Preview'
])
->setPreview(true);
$this->addContent($template);
}
public function editAction(): void
{
$this->assertPermission('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 createActionBars(): ValidHtml
{
$actions = new ActionBar();
$actions->addHtml(
(new ActionLink(
$this->translate('Modify'),
Url::fromPath('reporting/template/edit', ['id' => $this->template->id]),
'edit'
))->openInModal()
);
return $actions;
}
}
|