Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance modal form redirects #194

Merged
merged 5 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 46 additions & 9 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
36 changes: 20 additions & 16 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,6 +24,7 @@ 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(
Expand Down Expand Up @@ -65,16 +65,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 +119,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
90 changes: 50 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,44 @@ 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',
[
'data-icinga-modal' => true,
'data-no-icinga-ajax' => true
]
)
);

return $actions;
}
}
21 changes: 3 additions & 18 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 @@ -58,20 +57,8 @@ public function indexAction()

/** @var Model\Template $template */
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 @@ -119,9 +106,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->closeModalAndRefreshRelatedView(Url::fromPath('reporting/templates'));
})
->handleRequest($this->getServerRequest());

Expand Down
13 changes: 10 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 @@ -48,10 +50,15 @@ 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) {
$pressedButton = $form->getPressedSubmitElement();
if ($pressedButton && $pressedButton->getName() === 'remove') {
Notification::success($this->translate('Removed timeframe successfully'));
} else {
Notification::success($this->translate('Update timeframe successfully'));
}

$this->redirectNow('__CLOSE__');
$this->switchToSingleColumnLayout();
})->handleRequest($this->getServerRequest());

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