diff --git a/src/Compat/CompatController.php b/src/Compat/CompatController.php index 72f33481..9dab9996 100644 --- a/src/Compat/CompatController.php +++ b/src/Compat/CompatController.php @@ -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; @@ -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)) {