diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-14 13:29:16 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-14 13:29:16 +0000 |
commit | 8a985929ed84cdb458a13c66b25f84e41133b24f (patch) | |
tree | 102a3d6e3cb731c6d23263095d0098f99572626d /application/controllers | |
parent | Adding upstream version 0.10.0. (diff) | |
download | icingaweb2-module-reporting-upstream.tar.xz icingaweb2-module-reporting-upstream.zip |
Adding upstream version 1.0.1.upstream/1.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'application/controllers')
-rw-r--r-- | application/controllers/ConfigController.php | 1 | ||||
-rw-r--r-- | application/controllers/ReportController.php | 216 | ||||
-rw-r--r-- | application/controllers/ReportsController.php | 95 | ||||
-rw-r--r-- | application/controllers/TemplateController.php | 107 | ||||
-rw-r--r-- | application/controllers/TemplatesController.php | 75 | ||||
-rw-r--r-- | application/controllers/TestController.php | 47 | ||||
-rw-r--r-- | application/controllers/TimeframeController.php | 41 | ||||
-rw-r--r-- | application/controllers/TimeframesController.php | 61 |
8 files changed, 413 insertions, 230 deletions
diff --git a/application/controllers/ConfigController.php b/application/controllers/ConfigController.php index 30fcc67..7e73c3c 100644 --- a/application/controllers/ConfigController.php +++ b/application/controllers/ConfigController.php @@ -1,4 +1,5 @@ <?php + // Icinga Reporting | (c) 2018 Icinga GmbH | GPLv2 namespace Icinga\Module\Reporting\Controllers; diff --git a/application/controllers/ReportController.php b/application/controllers/ReportController.php index 090c759..0a694f2 100644 --- a/application/controllers/ReportController.php +++ b/application/controllers/ReportController.php @@ -1,55 +1,137 @@ <?php + // Icinga Reporting | (c) 2018 Icinga GmbH | GPLv2 namespace Icinga\Module\Reporting\Controllers; -use GuzzleHttp\Psr7\ServerRequest; +use Exception; use Icinga\Application\Hook; use Icinga\Module\Pdfexport\ProvidedHook\Pdfexport; use Icinga\Module\Reporting\Database; +use Icinga\Module\Reporting\Model; use Icinga\Module\Reporting\Report; use Icinga\Module\Reporting\Web\Controller; use Icinga\Module\Reporting\Web\Forms\ReportForm; use Icinga\Module\Reporting\Web\Forms\ScheduleForm; use Icinga\Module\Reporting\Web\Forms\SendForm; use Icinga\Module\Reporting\Web\Widget\CompatDropdown; +use Icinga\Web\Notification; use ipl\Html\Error; +use ipl\Html\HtmlElement; +use ipl\Stdlib\Filter; use ipl\Web\Url; use ipl\Web\Widget\ActionBar; use Icinga\Util\Environment; +use ipl\Web\Widget\ActionLink; class ReportController extends Controller { - use Database; - /** @var Report */ protected $report; public function init() { - $this->report = Report::fromDb($this->params->getRequired('id')); + $reportId = $this->params->getRequired('id'); + + /** @var Model\Report $report */ + $report = Model\Report::on(Database::get()) + ->with(['timeframe']) + ->filter(Filter::equal('id', $reportId)) + ->first(); + + if ($report === null) { + $this->httpNotFound($this->translate('Report not found')); + } + + $this->report = Report::fromModel($report); } public function indexAction() { $this->addTitleTab($this->report->getName()); + $this->controls->getAttributes()->add('class', 'default-layout'); $this->addControl($this->assembleActions()); + if ($this->isXhr()) { + /** @var string $contentId */ + $contentId = $this->content->getAttributes()->get('id')->getValue(); + $this->sendExtraUpdates([ + $contentId => Url::fromPath('reporting/report/content', ['id' => $this->report->getId()]) + ]); + + // Will be replaced once the report content is rendered + $this->addContent(new HtmlElement('div')); + } else { + Environment::raiseExecutionTime(); + Environment::raiseMemoryLimit(); + + try { + $this->addContent($this->report->toHtml()); + } catch (Exception $e) { + $this->addContent(Error::show($e)); + } + } + } + + public function contentAction(): void + { Environment::raiseExecutionTime(); Environment::raiseMemoryLimit(); + $this->view->compact = true; + $this->_helper->layout()->disableLayout(); + try { - $this->addContent($this->report->toHtml()); - } catch (\Exception $e) { - $this->addContent(Error::show($e)); + $this->getDocument()->addHtml($this->report->toHtml()); + } catch (Exception $e) { + $this->getDocument()->addHtml(Error::show($e)); + } + } + + public function cloneAction() + { + $this->assertPermission('reporting/reports'); + $this->addTitleTab($this->translate('Clone Report')); + + $values = ['timeframe' => (string) $this->report->getTimeframe()->getId()]; + + $reportlet = $this->report->getReportlets()[0]; + + $values['reportlet'] = $reportlet->getClass(); + + foreach ($reportlet->getConfig() as $name => $value) { + if ($name === 'name') { + if (preg_match('/(?:Clone )(\d+)$/', $value, $m)) { + $value = preg_replace('/\d+$/', (string) ((int) $m[1] + 1), $value); + } else { + $value .= ' Clone 1'; + } + } + + $values[$name] = $value; } + + $form = (new ReportForm()) + ->setSubmitButtonLabel($this->translate('Clone Report')) + ->setAction((string) Url::fromRequest()) + ->populate($values) + ->on(ReportForm::ON_SUCCESS, function (ReportForm $form) { + Notification::success($this->translate('Cloned report successfully')); + + $this->sendExtraUpdates(['#col1']); + + $this->redirectNow(Url::fromPath('reporting/report', ['id' => $form->getId()])); + }) + ->handleRequest($this->getServerRequest()); + + $this->addContent($form); } public function editAction() { $this->assertPermission('reporting/reports'); - $this->addTitleTab('Edit Report'); + $this->addTitleTab($this->translate('Edit Report')); $values = [ 'name' => $this->report->getName(), @@ -66,29 +148,44 @@ class ReportController extends Controller $values[$name] = $value; } - $form = new ReportForm(); - $form->setId($this->report->getId()); - $form->populate($values); - $form->handleRequest(ServerRequest::fromGlobals()); - - $this->redirectForm($form, 'reporting/reports'); + $form = ReportForm::fromId($this->report->getId()) + ->setAction((string) Url::fromRequest()) + ->populate($values) + ->on(ReportForm::ON_SUCCESS, function (ReportForm $form) { + $pressedButton = $form->getPressedSubmitElement(); + if ($pressedButton && $pressedButton->getName() === 'remove') { + Notification::success($this->translate('Removed report successfully')); + + $this->switchToSingleColumnLayout(); + } else { + Notification::success($this->translate('Updated report successfully')); + + $this->closeModalAndRefreshRemainingViews( + Url::fromPath('reporting/report', ['id' => $this->report->getId()]) + ); + } + }) + ->handleRequest($this->getServerRequest()); $this->addContent($form); } public function sendAction() { - $this->addTitleTab('Send Report'); + $this->addTitleTab($this->translate('Send Report')); Environment::raiseExecutionTime(); Environment::raiseMemoryLimit(); - $form = new SendForm(); - $form + $form = (new SendForm()) ->setReport($this->report) - ->handleRequest(ServerRequest::fromGlobals()); - - $this->redirectForm($form, "reporting/report?id={$this->report->getId()}"); + ->setAction((string) Url::fromRequest()) + ->on(SendForm::ON_SUCCESS, function () { + $this->closeModalAndRefreshRelatedView( + Url::fromPath('reporting/report', ['id' => $this->report->getId()]) + ); + }) + ->handleRequest($this->getServerRequest()); $this->addContent($form); } @@ -96,16 +193,38 @@ class ReportController extends Controller public function scheduleAction() { $this->assertPermission('reporting/schedules'); - $this->addTitleTab('Schedule'); - - $form = new ScheduleForm(); - $form - ->setReport($this->report) - ->handleRequest(ServerRequest::fromGlobals()); - - $this->redirectForm($form, "reporting/report?id={$this->report->getId()}"); + $this->addTitleTab($this->translate('Schedule')); + + $form = ScheduleForm::fromReport($this->report); + $form->setAction((string) Url::fromRequest()) + ->on(ScheduleForm::ON_SUCCESS, function () use ($form) { + $pressedButton = $form->getPressedSubmitElement(); + if ($pressedButton) { + $pressedButton = $pressedButton->getName(); + } + + if ($pressedButton === 'remove') { + Notification::success($this->translate('Removed schedule successfully')); + } elseif ($pressedButton === 'send') { + Notification::success($this->translate('Report sent successfully')); + } elseif ($this->report->getSchedule() !== null) { + Notification::success($this->translate('Updated schedule successfully')); + } else { + Notification::success($this->translate('Created schedule successfully')); + } + + $this->closeModalAndRefreshRelatedView( + Url::fromPath('reporting/report', ['id' => $this->report->getId()]) + ); + }) + ->handleRequest($this->getServerRequest()); $this->addContent($form); + + $parts = $form->getPartUpdates(); + if (! empty($parts)) { + $this->sendMultipartUpdate(...$parts); + } } public function downloadAction() @@ -124,8 +243,9 @@ class ReportController extends Controller switch ($type) { case 'pdf': - /** @var Hook\PdfexportHook */ - Pdfexport::first()->streamPdfFromHtml($this->report->toPdf(), $name); + /** @var Hook\PdfexportHook $exports */ + $exports = Pdfexport::first(); + $exports->streamPdfFromHtml($this->report->toPdf(), $name); exit; case 'csv': $response = $this->getResponse(); @@ -184,24 +304,42 @@ class ReportController extends Controller $actions = new ActionBar(); if ($this->hasPermission('reporting/reports')) { - $actions->addLink( - 'Modify', - Url::fromPath('reporting/report/edit', ['id' => $reportId]), - 'edit' + $actions->addHtml( + (new ActionLink( + $this->translate('Modify'), + Url::fromPath('reporting/report/edit', ['id' => $reportId]), + 'edit' + ))->openInModal() + ); + + $actions->addHtml( + (new ActionLink( + $this->translate('Clone'), + Url::fromPath('reporting/report/clone', ['id' => $reportId]), + 'clone' + ))->openInModal() ); } if ($this->hasPermission('reporting/schedules')) { - $actions->addLink( - 'Schedule', - Url::fromPath('reporting/report/schedule', ['id' => $reportId]), - 'calendar-empty' + $actions->addHtml( + (new ActionLink( + $this->translate('Schedule'), + Url::fromPath('reporting/report/schedule', ['id' => $reportId]), + 'calendar-empty' + ))->openInModal() ); } $actions ->add($download) - ->addLink('Send', Url::fromPath('reporting/report/send', ['id' => $reportId]), 'forward'); + ->addHtml( + (new ActionLink( + $this->translate('Send'), + Url::fromPath('reporting/report/send', ['id' => $reportId]), + 'forward' + ))->openInModal() + ); return $actions; } diff --git a/application/controllers/ReportsController.php b/application/controllers/ReportsController.php index 7971897..f6aeedd 100644 --- a/application/controllers/ReportsController.php +++ b/application/controllers/ReportsController.php @@ -1,23 +1,23 @@ <?php + // Icinga Reporting | (c) 2018 Icinga GmbH | GPLv2 namespace Icinga\Module\Reporting\Controllers; -use GuzzleHttp\Psr7\ServerRequest; +use Icinga\Module\Icingadb\ProvidedHook\Reporting\HostSlaReport; +use Icinga\Module\Icingadb\ProvidedHook\Reporting\ServiceSlaReport; use Icinga\Module\Reporting\Database; +use Icinga\Module\Reporting\Model\Report; use Icinga\Module\Reporting\Web\Controller; use Icinga\Module\Reporting\Web\Forms\ReportForm; use Icinga\Module\Reporting\Web\ReportsTimeframesAndTemplatesTabs; +use Icinga\Web\Notification; use ipl\Html\Html; -use ipl\Sql\Select; use ipl\Web\Url; use ipl\Web\Widget\ButtonLink; -use ipl\Web\Widget\Icon; -use ipl\Web\Widget\Link; class ReportsController extends Controller { - use Database; use ReportsTimeframesAndTemplatesTabs; public function indexAction() @@ -25,36 +25,42 @@ class ReportsController extends Controller $this->createTabs()->activate('reports'); if ($this->hasPermission('reporting/reports')) { - $this->addControl(new ButtonLink( - $this->translate('New Report'), - Url::fromPath('reporting/reports/new'), - 'plus' - )); + $this->addControl( + (new ButtonLink( + $this->translate('New Report'), + Url::fromPath('reporting/reports/new'), + 'plus' + ))->openInModal() + ); } $tableRows = []; - $select = (new Select()) - ->from('report r') - ->columns(['r.*', 'timeframe' => 't.name']) - ->join('timeframe t', 'r.timeframe_id = t.id') - ->orderBy('r.mtime', SORT_DESC); + $reports = Report::on(Database::get()) + ->withColumns(['report.timeframe.name']); + + $sortControl = $this->createSortControl( + $reports, + [ + 'name' => $this->translate('Name'), + 'author' => $this->translate('Author'), + 'ctime' => $this->translate('Created At'), + 'mtime' => $this->translate('Modified At') + ] + ); + + $this->addControl($sortControl); - foreach ($this->getDb()->select($select) as $report) { + /** @var Report $report */ + foreach ($reports as $report) { $url = Url::fromPath('reporting/report', ['id' => $report->id])->getAbsoluteUrl('&'); $tableRows[] = Html::tag('tr', ['href' => $url], [ Html::tag('td', null, $report->name), Html::tag('td', null, $report->author), - Html::tag('td', null, $report->timeframe), - Html::tag('td', null, date('Y-m-d H:i', $report->ctime / 1000)), - Html::tag('td', null, date('Y-m-d H:i', $report->mtime / 1000)), - Html::tag('td', ['class' => 'icon-col'], [ - new Link( - new Icon('edit'), - Url::fromPath('reporting/report/edit', ['id' => $report->id]) - ) - ]) + Html::tag('td', null, $report->timeframe->name), + Html::tag('td', null, $report->ctime->format('Y-m-d H:i')), + Html::tag('td', null, $report->mtime->format('Y-m-d H:i')), ]); } @@ -94,10 +100,43 @@ class ReportsController extends Controller $this->assertPermission('reporting/reports'); $this->addTitleTab($this->translate('New Report')); - $form = new ReportForm(); - $form->handleRequest(ServerRequest::fromGlobals()); + switch ($this->params->shift('report')) { + case 'host': + $class = HostSlaReport::class; + break; + case 'service': + $class = ServiceSlaReport::class; + break; + default: + $class = null; + break; + } + + $form = (new ReportForm()) + ->setAction((string) Url::fromRequest()) + ->setRenderCreateAndShowButton($class !== null) + ->populate([ + 'filter' => $this->params->shift('filter'), + 'reportlet' => $class + ]) + ->on(ReportForm::ON_SUCCESS, function (ReportForm $form) { + Notification::success($this->translate('Created report successfully')); - $this->redirectForm($form, 'reporting/reports'); + $pressedButton = $form->getPressedSubmitElement(); + if ($pressedButton && $pressedButton->getName() !== 'create_show') { + $this->closeModalAndRefreshRelatedView(Url::fromPath('reporting/reports')); + } else { + $this->redirectNow( + Url::fromPath( + sprintf( + 'reporting/reports#!%s', + Url::fromPath('reporting/report', ['id' => $form->getId()])->getAbsoluteUrl() + ) + ) + ); + } + }) + ->handleRequest($this->getServerRequest()); $this->addContent($form); } 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; } } diff --git a/application/controllers/TemplatesController.php b/application/controllers/TemplatesController.php index 91a82b1..99a5dcb 100644 --- a/application/controllers/TemplatesController.php +++ b/application/controllers/TemplatesController.php @@ -1,22 +1,22 @@ <?php + // Icinga Reporting | (c) 2019 Icinga GmbH | GPLv2 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; use Icinga\Module\Reporting\Web\Forms\TemplateForm; use Icinga\Module\Reporting\Web\ReportsTimeframesAndTemplatesTabs; +use Icinga\Web\Notification; use ipl\Html\Html; -use ipl\Sql\Select; use ipl\Web\Url; use ipl\Web\Widget\ButtonLink; use ipl\Web\Widget\Link; class TemplatesController extends Controller { - use Database; use ReportsTimeframesAndTemplatesTabs; public function indexAction() @@ -26,38 +26,40 @@ class TemplatesController extends Controller $canManage = $this->hasPermission('reporting/templates'); if ($canManage) { - $this->addControl(new ButtonLink( - $this->translate('New Template'), - Url::fromPath('reporting/templates/new'), - 'plus' - )); + $this->addControl( + (new ButtonLink( + $this->translate('New Template'), + Url::fromPath('reporting/templates/new'), + 'plus' + ))->openInModal() + ); } - $select = (new Select()) - ->from('template') - ->columns(['id', 'name', 'author', 'ctime', 'mtime']) - ->orderBy('mtime', SORT_DESC); - - foreach ($this->getDb()->select($select) as $template) { - if ($canManage) { - // Edit URL - $subjectUrl = Url::fromPath( - 'reporting/template/edit', - ['id' => $template->id] - ); - } else { - // Preview URL - $subjectUrl = Url::fromPath( - 'reporting/template', - ['id' => $template->id] - ); - } + $templates = Model\Template::on(Database::get()); + + $sortControl = $this->createSortControl( + $templates, + [ + 'name' => $this->translate('Name'), + 'author' => $this->translate('Author'), + 'ctime' => $this->translate('Created At'), + 'mtime' => $this->translate('Modified At') + ] + ); + $this->addControl($sortControl); + + $tableRows = []; + + /** @var Model\Template $template */ + foreach ($templates as $template) { + // Preview URL + $subjectLink = new Link($template->name, Url::fromPath('reporting/template', ['id' => $template->id])); $tableRows[] = Html::tag('tr', null, [ - Html::tag('td', null, new Link($template->name, $subjectUrl)), + Html::tag('td', null, $subjectLink), Html::tag('td', null, $template->author), - Html::tag('td', null, date('Y-m-d H:i', $template->ctime / 1000)), - Html::tag('td', null, date('Y-m-d H:i', $template->mtime / 1000)) + Html::tag('td', null, $template->ctime->format('Y-m-d H:i')), + Html::tag('td', null, $template->mtime->format('Y-m-d H:i')) ]); } @@ -93,13 +95,16 @@ class TemplatesController extends Controller public function newAction() { $this->assertPermission('reporting/templates'); - $this->addTitleTab('New Template'); - - $form = new TemplateForm(); + $this->addTitleTab($this->translate('New Template')); - $form->handleRequest(ServerRequest::fromGlobals()); + $form = (new TemplateForm()) + ->setAction((string) Url::fromRequest()) + ->on(TemplateForm::ON_SUCCESS, function () { + Notification::success($this->translate('Created template successfully')); - $this->redirectForm($form, 'reporting/templates'); + $this->closeModalAndRefreshRelatedView(Url::fromPath('reporting/templates')); + }) + ->handleRequest($this->getServerRequest()); $this->addContent($form); } diff --git a/application/controllers/TestController.php b/application/controllers/TestController.php deleted file mode 100644 index f666085..0000000 --- a/application/controllers/TestController.php +++ /dev/null @@ -1,47 +0,0 @@ -<?php -// Icinga Reporting | (c) 2018 Icinga GmbH | GPLv2 - -namespace Icinga\Module\Reporting\Controllers; - -use Icinga\Module\Reporting\Database; -use Icinga\Module\Reporting\Timeframe; -use Icinga\Module\Reporting\Web\Controller; -use ipl\Html\Table; -use ipl\Sql\Select; - -class TestController extends Controller -{ - use Database; - - public function timeframesAction() - { - $select = (new Select()) - ->from('timeframe') - ->columns('*'); - - $table = new Table(); - - $table->getAttributes()->add('class', 'common-table'); - - $table->getHeader()->add(Table::row(['Name', 'Title', 'Start', 'End'], null, 'th')); - - foreach ($this->getDb()->select($select) as $row) { - $timeframe = (new Timeframe()) - ->setName($row->name) - ->setTitle($row->title) - ->setStart($row->start) - ->setEnd($row->end); - - $table->getBody()->add(Table::row([ - $timeframe->getName(), - $timeframe->getTitle(), - $timeframe->getTimerange()->getStart()->format('Y-m-d H:i:s'), - $timeframe->getTimerange()->getEnd()->format('Y-m-d H:i:s') - ])); - } - - $this->addTitleTab('Timeframes'); - - $this->addContent($table); - } -} diff --git a/application/controllers/TimeframeController.php b/application/controllers/TimeframeController.php index ca67b0b..01395c2 100644 --- a/application/controllers/TimeframeController.php +++ b/application/controllers/TimeframeController.php @@ -1,24 +1,37 @@ <?php + // Icinga Reporting | (c) 2019 Icinga GmbH | GPLv2 namespace Icinga\Module\Reporting\Controllers; -use GuzzleHttp\Psr7\ServerRequest; +use Exception; use Icinga\Module\Reporting\Database; +use Icinga\Module\Reporting\Model; use Icinga\Module\Reporting\Timeframe; use Icinga\Module\Reporting\Web\Controller; use Icinga\Module\Reporting\Web\Forms\TimeframeForm; +use Icinga\Web\Notification; +use ipl\Html\Form; +use ipl\Web\Url; +use ipl\Stdlib\Filter; class TimeframeController extends Controller { - use Database; - /** @var Timeframe */ protected $timeframe; public function init() { - $this->timeframe = Timeframe::fromDb($this->params->getRequired('id')); + /** @var Model\Timeframe $timeframe */ + $timeframe = Model\Timeframe::on(Database::get()) + ->filter(Filter::equal('id', $this->params->getRequired('id'))) + ->first(); + + if ($timeframe === null) { + throw new Exception('Timeframe not found'); + } + + $this->timeframe = Timeframe::fromModel($timeframe); } public function editAction() @@ -32,15 +45,19 @@ class TimeframeController extends Controller 'end' => $this->timeframe->getEnd() ]; + $form = TimeframeForm::fromId($this->timeframe->getId()) + ->setAction((string) Url::fromRequest()) + ->populate($values) + ->on(TimeframeForm::ON_SUCCESS, function (Form $form) { + $pressedButton = $form->getPressedSubmitElement(); + if ($pressedButton && $pressedButton->getName() === 'remove') { + Notification::success($this->translate('Removed timeframe successfully')); + } else { + Notification::success($this->translate('Update timeframe successfully')); + } - $form = (new TimeframeForm()) - ->setId($this->timeframe->getId()); - - $form->populate($values); - - $form->handleRequest(ServerRequest::fromGlobals()); - - $this->redirectForm($form, 'reporting/timeframes'); + $this->switchToSingleColumnLayout(); + })->handleRequest($this->getServerRequest()); $this->addContent($form); } diff --git a/application/controllers/TimeframesController.php b/application/controllers/TimeframesController.php index 505d8d9..f38c661 100644 --- a/application/controllers/TimeframesController.php +++ b/application/controllers/TimeframesController.php @@ -1,22 +1,22 @@ <?php + // Icinga Reporting | (c) 2019 Icinga GmbH | GPLv2 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; use Icinga\Module\Reporting\Web\Forms\TimeframeForm; use Icinga\Module\Reporting\Web\ReportsTimeframesAndTemplatesTabs; +use Icinga\Web\Notification; use ipl\Html\Html; -use ipl\Sql\Select; use ipl\Web\Url; use ipl\Web\Widget\ButtonLink; use ipl\Web\Widget\Link; class TimeframesController extends Controller { - use Database; use ReportsTimeframesAndTemplatesTabs; public function indexAction() @@ -26,42 +26,56 @@ class TimeframesController extends Controller $canManage = $this->hasPermission('reporting/timeframes'); if ($canManage) { - $this->addControl(new ButtonLink( - $this->translate('New Timeframe'), - Url::fromPath('reporting/timeframes/new'), - 'plus' - )); + $this->addControl( + (new ButtonLink( + $this->translate('New Timeframe'), + Url::fromPath('reporting/timeframes/new'), + 'plus' + ))->openInModal() + ); } $tableRows = []; - $select = (new Select()) - ->from('timeframe t') - ->columns('*'); + $timeframes = Model\Timeframe::on(Database::get()); + + $sortControl = $this->createSortControl( + $timeframes, + [ + 'name' => $this->translate('Name'), + 'ctime' => $this->translate('Created At'), + 'mtime' => $this->translate('Modified At') + ] + ); - foreach ($this->getDb()->select($select) as $timeframe) { + $this->addControl($sortControl); + + foreach ($timeframes as $timeframe) { $subject = $timeframe->name; if ($canManage) { - $subject = new Link($timeframe->name, Url::fromPath( - 'reporting/timeframe/edit', - ['id' => $timeframe->id] - )); + $subject = new Link( + $timeframe->name, + Url::fromPath('reporting/timeframe/edit', ['id' => $timeframe->id]) + ); } $tableRows[] = Html::tag('tr', null, [ Html::tag('td', null, $subject), Html::tag('td', null, $timeframe->start), Html::tag('td', null, $timeframe->end), - Html::tag('td', null, date('Y-m-d H:i', $timeframe->ctime / 1000)), - Html::tag('td', null, date('Y-m-d H:i', $timeframe->mtime / 1000)) + Html::tag('td', null, $timeframe->ctime->format('Y-m-d H:i')), + Html::tag('td', null, $timeframe->mtime->format('Y-m-d H:i')) ]); } if (! empty($tableRows)) { $table = Html::tag( 'table', - ['class' => 'common-table table-row-selectable', 'data-base-target' => '_next'], + [ + 'class' => 'common-table table-row-selectable', + 'data-base-target' => '_next' + ], [ Html::tag( 'thead', @@ -93,10 +107,13 @@ class TimeframesController extends Controller $this->assertPermission('reporting/timeframes'); $this->addTitleTab($this->translate('New Timeframe')); - $form = new TimeframeForm(); - $form->handleRequest(ServerRequest::fromGlobals()); + $form = (new TimeframeForm()) + ->setAction((string) Url::fromRequest()) + ->on(TimeframeForm::ON_SUCCESS, function () { + Notification::success($this->translate('Created timeframe successfully')); - $this->redirectForm($form, 'reporting/timeframes'); + $this->closeModalAndRefreshRelatedView(Url::fromPath('reporting/timeframes')); + })->handleRequest($this->getServerRequest()); $this->addContent($form); } |