Skip to content

Commit

Permalink
Enhance modal form redirects (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
nilmerg authored Sep 12, 2023
2 parents 244c5b1 + 141bae1 commit f9faac1
Show file tree
Hide file tree
Showing 12 changed files with 241 additions and 194 deletions.
95 changes: 58 additions & 37 deletions application/controllers/ReportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Icinga\Module\Reporting\Controllers;

use Exception;
use Icinga\Application\Hook;
use Icinga\Module\Pdfexport\ProvidedHook\Pdfexport;
use Icinga\Module\Reporting\Database;
Expand All @@ -16,6 +17,7 @@
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;
Expand Down Expand Up @@ -50,15 +52,31 @@ public function indexAction()
{
$this->addTitleTab($this->report->getName());

$this->controls->getAttributes()->add('class', 'default-layout');
$this->addControl($this->assembleActions());

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

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

Expand Down Expand Up @@ -89,8 +107,12 @@ public function cloneAction()
->setSubmitButtonLabel($this->translate('Clone Report'))
->setAction((string) Url::fromRequest())
->populate($values)
->on(ReportForm::ON_SUCCESS, function () {
$this->redirectNow('__CLOSE__');
->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());

Expand Down Expand Up @@ -120,8 +142,19 @@ public function editAction()
$form = ReportForm::fromId($this->report->getId())
->setAction((string) Url::fromRequest())
->populate($values)
->on(ReportForm::ON_SUCCESS, function () {
$this->redirectNow('__CLOSE__');
->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());

Expand All @@ -139,7 +172,9 @@ public function sendAction()
->setReport($this->report)
->setAction((string) Url::fromRequest())
->on(SendForm::ON_SUCCESS, function () {
$this->redirectNow("reporting/report?id={$this->report->getId()}");
$this->closeModalAndRefreshRelatedView(
Url::fromPath('reporting/report', ['id' => $this->report->getId()])
);
})
->handleRequest($this->getServerRequest());

Expand Down Expand Up @@ -169,7 +204,9 @@ public function scheduleAction()
Notification::success($this->translate('Created schedule successfully'));
}

$this->redirectNow("reporting/report?id={$this->report->getId()}");
$this->closeModalAndRefreshRelatedView(
Url::fromPath('reporting/report', ['id' => $this->report->getId()])
);
})
->handleRequest($this->getServerRequest());

Expand Down Expand Up @@ -259,56 +296,40 @@ protected function assembleActions()

if ($this->hasPermission('reporting/reports')) {
$actions->addHtml(
new ActionLink(
(new ActionLink(
$this->translate('Modify'),
Url::fromPath('reporting/report/edit', ['id' => $reportId]),
'edit',
[
'data-icinga-modal' => true,
'data-no-icinga-ajax' => true
]
)
'edit'
))->openInModal()
);

$actions->addHtml(
new ActionLink(
(new ActionLink(
$this->translate('Clone'),
Url::fromPath('reporting/report/clone', ['id' => $reportId]),
'clone',
[
'data-icinga-modal' => true,
'data-no-icinga-ajax' => true
]
)
'clone'
))->openInModal()
);
}

if ($this->hasPermission('reporting/schedules')) {
$actions->addHtml(
new ActionLink(
(new ActionLink(
$this->translate('Schedule'),
Url::fromPath('reporting/report/schedule', ['id' => $reportId]),
'calendar-empty',
[
'data-icinga-modal' => true,
'data-no-icinga-ajax' => true
]
)
'calendar-empty'
))->openInModal()
);
}

$actions
->add($download)
->addHtml(
new ActionLink(
(new ActionLink(
$this->translate('Send'),
Url::fromPath('reporting/report/send', ['id' => $reportId]),
'forward',
[
'data-icinga-modal' => true,
'data-no-icinga-ajax' => true
]
)
'forward'
))->openInModal()
);

return $actions;
Expand Down
52 changes: 27 additions & 25 deletions application/controllers/ReportsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
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;
use ipl\Web\Widget\Icon;
use ipl\Web\Widget\Link;

class ReportsController extends Controller
{
Expand All @@ -25,17 +24,16 @@ class ReportsController extends Controller
public function indexAction()
{
$this->createTabs()->activate('reports');
$this->getTabs()->getAttributes()->set('data-base-target', '_main');

if ($this->hasPermission('reporting/reports')) {
$this->addControl(new ButtonLink(
$this->translate('New Report'),
Url::fromPath('reporting/reports/new'),
'plus',
[
'data-icinga-modal' => true,
'data-no-icinga-ajax' => true
]
));
$this->addControl(
(new ButtonLink(
$this->translate('New Report'),
Url::fromPath('reporting/reports/new'),
'plus'
))->openInModal()
);
}

$tableRows = [];
Expand Down Expand Up @@ -65,16 +63,7 @@ public function indexAction()
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')),
Html::tag('td', ['class' => 'icon-col'], [
new Link(
new Icon('edit'),
Url::fromPath('reporting/report/edit', ['id' => $report->id]),
[
'data-icinga-modal' => true,
'data-no-icinga-ajax' => true
]
)
])
Html::tag('td', null, $report->mtime->format('Y-m-d H:i'))
]);
}

Expand Down Expand Up @@ -128,14 +117,27 @@ public function newAction()

$form = (new ReportForm())
->setAction((string) Url::fromRequest())
->setRenderCreateAndShowButton($class !== null)
->populate([
'filter' => $this->params->shift('filter'),
'reportlet' => $class
])
->on(ReportForm::ON_SUCCESS, function () {
$this->getResponse()->setHeader('X-Icinga-Container', 'modal-content', true);

$this->redirectNow('__CLOSE__');
->on(ReportForm::ON_SUCCESS, function (ReportForm $form) {
Notification::success($this->translate('Created report successfully'));

$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());

Expand Down
86 changes: 46 additions & 40 deletions application/controllers/TemplateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,23 @@
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();

/** @var Model\Template $template */
$template = Model\Template::on($this->getDb())
Expand All @@ -33,7 +40,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 @@ -48,51 +65,40 @@ public function indexAction()
public function editAction()
{
$this->assertPermission('reporting/templates');
$this->addTitleTab($this->translate('Edit Template'));

$this->createTabs()->activate('edit');

/** @var Model\Template $template */
$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 () {
Notification::success($this->translate('Updated template successfully'));

$this->redirectNow('__CLOSE__');
->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->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'
))->openInModal()
);

return $actions;
}
}
Loading

0 comments on commit f9faac1

Please sign in to comment.