[TASK] Deprecate extbase StopActionException
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Domain / Finishers / RedirectFinisher.php
1 <?php
2
3 declare(strict_types=1);
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 namespace TYPO3\CMS\Form\Domain\Finishers;
19
20 use TYPO3\CMS\Core\Http\PropagateResponseException;
21 use TYPO3\CMS\Core\Http\RedirectResponse;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
24
25 /**
26 * This finisher redirects to another Controller.
27 *
28 * Scope: frontend
29 */
30 class RedirectFinisher extends AbstractFinisher
31 {
32
33 /**
34 * @var array
35 */
36 protected $defaultOptions = [
37 'pageUid' => 1,
38 'additionalParameters' => '',
39 'statusCode' => 303,
40 ];
41
42 /**
43 * @var \TYPO3\CMS\Extbase\Mvc\Request
44 */
45 protected $request;
46
47 /**
48 * @var \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder
49 */
50 protected $uriBuilder;
51
52 /**
53 * Executes this finisher
54 * @see AbstractFinisher::execute()
55 */
56 protected function executeInternal()
57 {
58 $formRuntime = $this->finisherContext->getFormRuntime();
59 $this->request = $formRuntime->getRequest();
60 $this->uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
61 $this->uriBuilder->setRequest($this->request);
62
63 $pageUid = $this->parseOption('pageUid');
64 $pageUid = (int)str_replace('pages_', '', (string)$pageUid);
65 $additionalParameters = $this->parseOption('additionalParameters');
66 $additionalParameters = is_string($additionalParameters) ? $additionalParameters : '';
67 $additionalParameters = '&' . ltrim($additionalParameters, '&');
68 $statusCode = (int)$this->parseOption('statusCode');
69
70 $this->finisherContext->cancel();
71 $this->redirect($pageUid, $additionalParameters, $statusCode);
72 }
73
74 /**
75 * Redirects the request to another page.
76 *
77 * Redirect will be sent to the client which then performs another request to the new URI.
78 *
79 * NOTE: This method only supports web requests and will thrown an exception
80 * if used with other request types.
81 *
82 * @param int $pageUid Target page uid. If NULL, the current page uid is used
83 * @param string $additionalParameters
84 * @param int $statusCode (optional) The HTTP status code for the redirect. Default is "303 See Other
85 * @see forward()
86 */
87 protected function redirect(int $pageUid = 1, string $additionalParameters = '', int $statusCode = 303)
88 {
89 $typolinkConfiguration = [
90 'parameter' => $pageUid,
91 'additionalParams' => $additionalParameters,
92 ];
93 $redirectUri = $this->getTypoScriptFrontendController()->cObj->typoLink_URL($typolinkConfiguration);
94 $this->redirectToUri($redirectUri, $statusCode);
95 }
96
97 /**
98 * Redirects the web request to another uri.
99 *
100 * NOTE: This method only supports web requests and will thrown an exception if used with other request types.
101 *
102 * @param string $uri A string representation of a URI
103 * @param int $statusCode (optional) The HTTP status code for the redirect. Default is "303 See Other
104 * @throws PropagateResponseException
105 */
106 protected function redirectToUri(string $uri, int $statusCode = 303)
107 {
108 $uri = $this->addBaseUriIfNecessary($uri);
109 $response = new RedirectResponse($uri, $statusCode);
110 // End processing and dispatching by throwing a PropagateResponseException with our response.
111 // @todo: Should be changed to *return* a response instead, but this requires the ContentObjectRender
112 // @todo: to deal with responses instead of strings, if the form is used in a fluid template rendered by the
113 // @todo: FluidTemplateContentObject and the extbase bootstrap isn't used.
114 throw new PropagateResponseException($response, 1477070964);
115 }
116
117 /**
118 * Adds the base uri if not already in place.
119 *
120 * @param string $uri The URI
121 * @return string
122 */
123 protected function addBaseUriIfNecessary(string $uri): string
124 {
125 return GeneralUtility::locationHeaderUrl((string)$uri);
126 }
127 }