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 Sep 12, 2023
1 parent 244c5b1 commit 71934bc
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 56 deletions.
56 changes: 47 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 @@ -48,17 +50,34 @@ public function init()

public function indexAction()
{
$this->getTabs()->getAttributes()->set('data-base-target', '_main');
$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 +108,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 +143,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 +173,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 +205,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
35 changes: 19 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 Down Expand Up @@ -65,16 +64,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 +118,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
19 changes: 15 additions & 4 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 @@ -64,10 +66,19 @@ public function editAction()

$form = TemplateForm::fromTemplate($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());

Expand Down
4 changes: 1 addition & 3 deletions application/controllers/TemplatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,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
21 changes: 9 additions & 12 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 @@ -58,18 +59,11 @@ public function indexAction()
if ($canManage) {
$subject = new Link(
$timeframe->name,
Url::fromPath('reporting/timeframe/edit', ['id' => $timeframe->id]),
[
'data-icinga-modal' => true,
'data-no-icinga-ajax' => true
]
Url::fromPath('reporting/timeframe/edit', ['id' => $timeframe->id])
);
}

$tableRows[] = Html::tag('tr', [
'data-icinga-modal' => true,
'data-no-icinga-ajax' => true
], [
$tableRows[] = Html::tag('tr', null, [
Html::tag('td', null, $subject),
Html::tag('td', null, $timeframe->start),
Html::tag('td', null, $timeframe->end),
Expand All @@ -81,7 +75,10 @@ public function indexAction()
if (! empty($tableRows)) {
$table = Html::tag(
'table',
['class' => 'common-table table-row-selectable'],
[
'class' => 'common-table table-row-selectable',
'data-base-target' => '_next'
],
[
Html::tag(
'thead',
Expand Down Expand Up @@ -116,9 +113,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->closeModalAndRefreshRelatedView(Url::fromPath('reporting/timeframes'));
})->handleRequest($this->getServerRequest());

$this->addContent($form);
Expand Down
39 changes: 38 additions & 1 deletion library/Reporting/Web/Forms/ReportForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class ReportForm extends CompatForm
/** @var string Label to use for the submit button */
protected $submitButtonLabel;

/** @var bool Whether to render the create and show submit button (is only used from DB Web's object detail) */
protected $renderCreateAndShowButton = false;

/**
* Create a new form instance with the given report id
*
Expand All @@ -38,6 +41,11 @@ public static function fromId($id): self
return $form;
}

public function getId(): ?int
{
return $this->id;
}

/**
* Set the label of the submit button
*
Expand Down Expand Up @@ -66,9 +74,27 @@ public function getSubmitButtonLabel(): string
return $this->id === null ? $this->translate('Create Report') : $this->translate('Update Report');
}

/**
* Set whether the create and show submit button should be rendered
*
* @param bool $renderCreateAndShowButton
*
* @return $this
*/
public function setRenderCreateAndShowButton(bool $renderCreateAndShowButton): self
{
$this->renderCreateAndShowButton = $renderCreateAndShowButton;

return $this;
}

public function hasBeenSubmitted(): bool
{
return $this->hasBeenSent() && ($this->getPopulatedValue('submit') || $this->getPopulatedValue('remove'));
return $this->hasBeenSent() && (
$this->getPopulatedValue('submit')
|| $this->getPopulatedValue('create_show')
|| $this->getPopulatedValue('remove')
);
}

protected function assemble()
Expand Down Expand Up @@ -153,6 +179,15 @@ protected function assemble()
/** @var HtmlDocument $wrapper */
$wrapper = $this->getElement('submit')->getWrapper();
$wrapper->prepend($removeButton);
} elseif ($this->renderCreateAndShowButton) {
$createAndShow = $this->createElement('submit', 'create_show', [
'label' => $this->translate('Create and Show'),
]);
$this->registerElement($createAndShow);

/** @var HtmlDocument $wrapper */
$wrapper = $this->getElement('submit')->getWrapper();
$wrapper->prepend($createAndShow);
}
}

Expand Down Expand Up @@ -223,5 +258,7 @@ public function onSuccess()
}

$db->commitTransaction();

$this->id = $reportId;
}
}
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ parameters:

- '#Call to an undefined method Icinga\\Module\\Reporting\\RetryConnection::lastInsertId\(\)#'

- '#Call to an undefined method Zend_Controller_Action_HelperBroker::layout\(\)#'

universalObjectCratesClasses:
- Icinga\Web\View
- ipl\Orm\Model
Loading

0 comments on commit 71934bc

Please sign in to comment.