44
55use Icinga \Web \UrlParams ;
66use ipl \Html \Form ;
7+ use ipl \Web \Compat \CompatController ;
8+ use ipl \Web \Control \LimitControl ;
9+ use ipl \Web \Control \PaginationControl ;
710use ipl \Web \Control \ViewModeSwitcher ;
11+ use ipl \Web \Url ;
812use Psr \Http \Message \ServerRequestInterface ;
913
1014/**
1115 * Provides factory methods to prepare reusable web controls and allows to handle their requests
16+ *
17+ * @phpstan-require-extends CompatController
1218 */
1319trait Controls
1420{
1521 /** @var Form[] Controls registered for handling by {@see self::handleControls()} */
1622 private array $ trackedControls = [];
1723
24+ /** @var ?Url Url to redirect to in {@see self::handleControls()} */
25+ private ?Url $ redirectUrl = null ;
26+
1827 /**
1928 * Create a {@see ViewModeSwitcher} control
2029 *
30+ * The control is registered via {@see self::trackControl()} and gets a default {@see Form::ON_SUBMIT} handler
31+ * that writes the chosen view mode to the {@see self::getRedirectUrl()}.
32+ *
2133 * @param UrlParams $params The url params; the view mode param is shifted out of them
2234 * @param ?string $viewModeParam Custom view mode param, null uses the default of the given class
2335 * @param class-string<ViewModeSwitcher> $viewModeSwitcherClass
@@ -40,9 +52,36 @@ public function createViewModeSwitcher(
4052
4153 $ this ->trackControl ($ viewModeSwitcher );
4254
55+ $ viewModeSwitcher ->on (ViewModeSwitcher::ON_SUBMIT , function (ViewModeSwitcher $ switcher ): void {
56+ $ this ->getRedirectUrl ()->setParam ($ switcher ->getViewModeParam (), $ switcher ->getViewMode ());
57+ });
58+
4359 return $ viewModeSwitcher ;
4460 }
4561
62+ /**
63+ * Double the default item limit and page size for `minimal` view mode
64+ *
65+ * @param ViewModeSwitcher $viewModeSwitcher
66+ * @param LimitControl $limitControl
67+ * @param ?PaginationControl $paginationControl
68+ *
69+ * @return void
70+ */
71+ protected function applyViewModeLimit (
72+ ViewModeSwitcher $ viewModeSwitcher ,
73+ LimitControl $ limitControl ,
74+ ?PaginationControl $ paginationControl = null
75+ ): void {
76+ if ($ viewModeSwitcher ->getViewMode () === 'minimal ' ) {
77+ $ limitControl ->setDefaultLimit ($ limitControl ->getDefaultLimit () * 2 );
78+
79+ $ paginationControl
80+ ?->setDefaultPageSize($ paginationControl ->getDefaultPageSize () * 2 )
81+ ->apply ();
82+ }
83+ }
84+
4685 /**
4786 * Register the given control to be handled by {@see self::handleControls()}
4887 *
@@ -58,7 +97,18 @@ protected function trackControl(Form $control): static
5897 }
5998
6099 /**
61- * Call {@see Form::handleRequest()} on every control in {@see self::$trackedControls}
100+ * Get the Url {@see self::handleControls()} redirects to
101+ *
102+ * @return Url
103+ */
104+ protected function getRedirectUrl (): Url
105+ {
106+ return $ this ->redirectUrl ??= Url::fromRequest ();
107+ }
108+
109+ /**
110+ * Call {@see Form::handleRequest()} on every control in {@see self::$trackedControls}. If {@see self::$redirectUrl}
111+ * has been set, a redirect is performed afterwards.
62112 *
63113 * @param ServerRequestInterface $request
64114 *
@@ -70,6 +120,10 @@ protected function handleControls(ServerRequestInterface $request): static
70120 $ control ->handleRequest ($ request );
71121 }
72122
123+ if ($ this ->redirectUrl !== null ) {
124+ $ this ->redirectNow ($ this ->redirectUrl );
125+ }
126+
73127 return $ this ;
74128 }
75129}
0 commit comments