From e78a667f4b986654e63d1394dae8acccd5ad52a3 Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Tue, 5 Sep 2023 18:00:17 +0200 Subject: [PATCH] Add different redirect options --- src/Compat/CompatController.php | 64 +++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/src/Compat/CompatController.php b/src/Compat/CompatController.php index 72f33481..83129ff7 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,69 @@ public function sendExtraUpdates(array $updates) $this->getResponse()->setHeader('X-Icinga-Extra-Updates', join(',', $extraUpdates)); } + /** + * Close the modal content and refresh the related view + * + * 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 `__REFRESH__` redirect with the latest Icinga Web version. + * + * This is supposed to be used in combination with a modal view and closes only the modal content, + * and refreshes the modal opener (regardless of whether it is col1 or col2). + * + * @param null|Url|string $url + * @param array $extraUpdates Provide containers other than the redirect target to be refreshed + * after the redirection. Is just for compatibility reasons and won't be used with latest Icinga Web + * versions. See {@see static::sendExtraUpdates()} how to properly provide extra updates. + * + * @return never + */ + public function closeModalAndRefreshRelatedView($url = null, array $extraUpdates = []): void + { + if (version_compare(Version::VERSION, '2.12.0', '<')) { + if (! $url) { + throw new InvalidArgumentException('No redirect url provided'); + } + + $this->sendExtraUpdates($extraUpdates); + + $this->redirectNow($url); + } else { + $this->redirectNow('__REFRESH__'); + } + } + + /** + * Close the modal content and refresh all the remaining views + * + * 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 `__REFRESH__` redirect with the latest Icinga Web version. + * + * This is supposed to be used in combination with a modal view and closes only the modal content. It refreshes + * the modal opener (expects to be always col2) and sends extra updates for col1. + * + * @param null|Url|string $url + * + * @return never + */ + public function closeModalAndRefreshRemainingViews($url = null): void + { + $this->sendExtraUpdates(['#col1']); + + $this->closeModalAndRefreshRelatedView($url); + } + + /** + * Redirect using `__CLOSE__` + * + * Change to a single column layout and refresh col1 + * + * @return never + */ + public function switchToSingleColumnLayout(): void + { + $this->redirectNow('__CLOSE__'); + } + public function postDispatch() { if (empty($this->parts)) {