Skip to content

Commit

Permalink
Add different redirect options
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Sep 6, 2023
1 parent 72bb31c commit bfee188
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/Compat/CompatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ipl\Web\Compat;

use GuzzleHttp\Psr7\ServerRequest;
use Icinga\Application\Version;
use InvalidArgumentException;
use Icinga\Web\Controller;
use ipl\Html\BaseHtmlElement;
Expand Down Expand Up @@ -420,6 +421,55 @@ public function sendExtraUpdates(array $updates)
$this->getResponse()->setHeader('X-Icinga-Extra-Updates', join(',', $extraUpdates));
}

/**
* Close the current active view and refresh all the remaining/related columns
*
* NOTE: When you use this with older Icinga Web versions (< 2.12), you will need to specify a valid redirect url,
* that will produce the same result as using the `__CLOSE__` redirect with the latest Icinga Web version.
*
* If this is used in combination with a modal view, only the modal content is closed, and refreshes the modal
* opener (regardless of whether it is col1 or col2). Whereas with a non-modal view, col2 will always be closed
* and col1 is refreshed.
*
* @param null|Url|string $url
*/
public function closeCurrentAndRefreshRelatedView($url = null): void
{
if (version_compare(Version::VERSION, '2.12.0', '<')) {
if (! $url) {
throw new InvalidArgumentException('No redirect url provided');
}

$this->redirectNow($url);
} else {
/** @var string $targetId */
$targetId = $this->getRequest()->getHeader('X-Icinga-Container');
if ($targetId === 'modal-content') {
// This will close only the modal view instead of the modal view and col2 container.
$this->getResponse()->setHeader('X-Icinga-Container', 'modal', true);

$extraUpdates = ['#col1'];
if ($this->getRequest()->getHeader('X-Icinga-Modal-Opener') === 'col2') {
$extraUpdates[] = '#col2';
}

$this->sendExtraUpdates($extraUpdates);
}

$this->redirectNow($url ?: '__CLOSE__');
}
}

/**
* Redirect using `__CLOSE__`
*
* Change to layout 1 column and refresh col1
*/
public function switchToLayout1Column(): void
{
$this->redirectNow('__CLOSE__');
}

public function postDispatch()
{
if (empty($this->parts)) {
Expand Down

0 comments on commit bfee188

Please sign in to comment.