Skip to content

Commit

Permalink
Enhance modal form redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Jul 27, 2023
1 parent 971c7e9 commit e8642bf
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 14 deletions.
29 changes: 27 additions & 2 deletions application/controllers/ReportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Icinga\Module\Reporting\Controllers;

use Icinga\Application\Hook;
use Icinga\Application\Version;
use Icinga\Module\Pdfexport\ProvidedHook\Pdfexport;
use Icinga\Module\Reporting\Database;
use Icinga\Module\Reporting\Model;
Expand Down Expand Up @@ -89,7 +90,12 @@ public function cloneAction()
->setAction((string) Url::fromRequest())
->populate($values)
->on(ReportForm::ON_SUCCESS, function () {
$this->redirectNow('__CLOSE__');
Notification::success($this->translate('Cloned report successfully'));

// Refresh the col1 container after cloning a report
$this->sendExtraUpdates(['#col1']);

$this->redirectNow("reporting/report?id={$this->report->getId()}");
})
->handleRequest($this->getServerRequest());

Expand Down Expand Up @@ -119,7 +125,26 @@ public function editAction()
$form = ReportForm::fromId($this->report->getId())
->setAction((string) Url::fromRequest())
->populate($values)
->on(ReportForm::ON_SUCCESS, function () {
->on(ReportForm::ON_SUCCESS, function (ReportForm $form) {
$extraUpdates = ['#col1'];
if ($form->getPressedSubmitElement()->getName() === 'remove') {
$extraUpdates['col2'] = '__CLOSE__';

Notification::success($this->translate('Removed report successfully'));
} else {
if ($this->params->shift('fromCol1', false) === false) {
$extraUpdates[] = '#col2';
}

Notification::success($this->translate('Updated report successfully'));
}

// Older Icinga Web 2 versions close the #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);
}

$this->redirectNow('__CLOSE__');
})
->handleRequest($this->getServerRequest());
Expand Down
12 changes: 10 additions & 2 deletions application/controllers/ReportsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

namespace Icinga\Module\Reporting\Controllers;

use Icinga\Application\Version;
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\Web\Url;
use ipl\Web\Widget\ButtonLink;
Expand Down Expand Up @@ -67,7 +69,7 @@ public function indexAction()
Html::tag('td', ['class' => 'icon-col'], [
new Link(
new Icon('edit'),
Url::fromPath('reporting/report/edit', ['id' => $report->id]),
Url::fromPath('reporting/report/edit', ['fromCol1' => true, 'id' => $report->id]),
[
'data-icinga-modal' => true,
'data-no-icinga-ajax' => true
Expand Down Expand Up @@ -132,7 +134,13 @@ public function newAction()
'reportlet' => $class
])
->on(ReportForm::ON_SUCCESS, function () {
$this->getResponse()->setHeader('X-Icinga-Container', 'modal-content', true);
Notification::success($this->translate('Created report successfully'));

if (version_compare(Version::VERSION, '2.12.0', '<')) {
// Former Icinga Web 2 versions don't refresh the modal openers container when
// redirecting using __CLOSE__.
$this->sendExtraUpdates(['#col1']);
}

$this->redirectNow('__CLOSE__');
})
Expand Down
19 changes: 17 additions & 2 deletions application/controllers/TemplateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
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\Stdlib\Filter;
use ipl\Web\Url;

Expand Down Expand Up @@ -62,8 +64,21 @@ public function editAction()

$form = TemplateForm::fromTemplate($template)
->setAction((string) Url::fromRequest())
->on(TemplateForm::ON_SUCCESS, function () {
Notification::success($this->translate('Updated template successfully'));
->on(TemplateForm::ON_SUCCESS, function (Form $form) {
$extraUpdates = ['#col1'];
if ($form->getPressedSubmitElement()->getName() === 'remove') {
Notification::success($this->translate('Removed template successfully'));

$extraUpdates['col2'] = '__CLOSE__';
} else {
Notification::success($this->translate('Updated template successfully'));
}

// 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);
}

$this->redirectNow('__CLOSE__');
})
Expand Down
4 changes: 1 addition & 3 deletions application/controllers/TemplatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@ public function newAction()
->on(TemplateForm::ON_SUCCESS, function () {
Notification::success($this->translate('Created template successfully'));

$this->getResponse()->setHeader('X-Icinga-Container', 'modal-content', true);

$this->redirectNow('__CLOSE__');
$this->redirectNow('reporting/templates');
})
->handleRequest($this->getServerRequest());

Expand Down
12 changes: 9 additions & 3 deletions application/controllers/TimeframeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
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;

Expand Down Expand Up @@ -47,10 +49,14 @@ public function editAction()
$form = TimeframeForm::fromId($this->timeframe->getId())
->setAction((string) Url::fromRequest())
->populate($values)
->on(TimeframeForm::ON_SUCCESS, function () {
$this->getResponse()->setHeader('X-Icinga-Container', 'modal-content', true);
->on(TimeframeForm::ON_SUCCESS, function (Form $form) {
if ($form->getPressedSubmitElement()->getName() === 'remove') {
Notification::success($this->translate('Removed timeframe successfully'));
} else {
Notification::success($this->translate('Update timeframe successfully'));
}

$this->redirectNow('__CLOSE__');
$this->redirectNow('reporting/timeframes');
})->handleRequest($this->getServerRequest());

$this->addContent($form);
Expand Down
5 changes: 3 additions & 2 deletions application/controllers/TimeframesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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\Web\Url;
use ipl\Web\Widget\ButtonLink;
Expand Down Expand Up @@ -116,9 +117,9 @@ public function newAction()
$form = (new TimeframeForm())
->setAction((string) Url::fromRequest())
->on(TimeframeForm::ON_SUCCESS, function () {
$this->getResponse()->setHeader('X-Icinga-Container', 'modal-content', true);
Notification::success($this->translate('Created timeframe successfully'));

$this->redirectNow('__CLOSE__');
$this->redirectNow('reporting/timeframes');
})->handleRequest($this->getServerRequest());

$this->addContent($form);
Expand Down

0 comments on commit e8642bf

Please sign in to comment.