[FEATURE] EXT:form - integrate new form framework
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Domain / Finishers / RedirectFinisher.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Form\Domain\Finishers;
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 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Extbase\Mvc\Exception\StopActionException;
20 use TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException;
21 use TYPO3\CMS\Extbase\Mvc\Web\Request;
22 use TYPO3\CMS\Extbase\Mvc\Web\Response;
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 'delay' => 0,
40 'statusCode' => 303,
41 ];
42
43 /**
44 * @var \TYPO3\CMS\Extbase\Mvc\Web\Request
45 */
46 protected $request;
47
48 /**
49 * @var \TYPO3\CMS\Extbase\Mvc\Web\Response
50 */
51 protected $response;
52
53 /**
54 * @var \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder
55 */
56 protected $uriBuilder;
57
58 /**
59 * Executes this finisher
60 * @see AbstractFinisher::execute()
61 *
62 * @return void
63 */
64 protected function executeInternal()
65 {
66 $formRuntime = $this->finisherContext->getFormRuntime();
67 $this->request = $formRuntime->getRequest();
68 $this->response = $formRuntime->getResponse();
69 $this->uriBuilder = $this->objectManager->get(UriBuilder::class);
70 $this->uriBuilder->setRequest($this->request);
71
72 $pageUid = (int)str_replace('pages_', '', $this->parseOption('pageUid'));
73 $additionalParameters = $this->parseOption('additionalParameters');
74 $additionalParameters = '&' . ltrim($additionalParameters, '&');
75 $delay = (int)$this->parseOption('delay');
76 $statusCode = (int)$this->parseOption('statusCode');
77
78 $this->finisherContext->cancel();
79 $this->redirect($pageUid, $additionalParameters, $delay, $statusCode);
80 }
81
82 /**
83 * Redirects the request to another page.
84 *
85 * Redirect will be sent to the client which then performs another request to the new URI.
86 *
87 * NOTE: This method only supports web requests and will thrown an exception
88 * if used with other request types.
89 *
90 * @param int $pageUid Target page uid. If NULL, the current page uid is used
91 * @param string $additionalParameters
92 * @param int $delay (optional) The delay in seconds. Default is no delay.
93 * @param int $statusCode (optional) The HTTP status code for the redirect. Default is "303 See Other
94 * @return void
95 * @throws UnsupportedRequestTypeException If the request is not a web request
96 * @see forward()
97 */
98 protected function redirect(int $pageUid = 1, string $additionalParameters = '', int $delay = 0, int $statusCode = 303)
99 {
100 if (!$this->request instanceof Request) {
101 throw new UnsupportedRequestTypeException('redirect() only supports web requests.', 1471776457);
102 }
103
104 $typolinkConfiguration = [
105 'parameter' => $pageUid,
106 'additionalParams' => $additionalParameters,
107 ];
108 $redirectUri = $this->getTypoScriptFrontendController()->cObj->typoLink_URL($typolinkConfiguration);
109 $this->redirectToUri($redirectUri, $delay, $statusCode);
110 }
111
112 /**
113 * Redirects the web request to another uri.
114 *
115 * NOTE: This method only supports web requests and will thrown an exception if used with other request types.
116 *
117 * @param string $uri A string representation of a URI
118 * @param int $delay (optional) The delay in seconds. Default is no delay.
119 * @param int $statusCode (optional) The HTTP status code for the redirect. Default is "303 See Other
120 * @throws UnsupportedRequestTypeException If the request is not a web request
121 * @throws StopActionException
122 */
123 protected function redirectToUri(string $uri, int $delay = 0, int $statusCode = 303)
124 {
125 if (!$this->request instanceof Request) {
126 throw new UnsupportedRequestTypeException('redirect() only supports web requests.', 1471776458);
127 }
128
129 $uri = $this->addBaseUriIfNecessary($uri);
130 $escapedUri = htmlentities($uri, ENT_QUOTES, 'utf-8');
131
132 $this->response->setContent('<html><head><meta http-equiv="refresh" content="' . (int)$delay . ';url=' . $escapedUri . '"/></head></html>');
133 if ($this->response instanceof \TYPO3\CMS\Extbase\Mvc\Web\Response) {
134 $this->response->setStatus($statusCode);
135 $this->response->setHeader('Location', (string)$uri);
136 }
137 echo $this->response->shutdown();
138 throw new StopActionException('redirectToUri', 1477070964);
139 }
140
141 /**
142 * Adds the base uri if not already in place.
143 *
144 * @param string $uri The URI
145 * @return string
146 */
147 protected function addBaseUriIfNecessary(string $uri): string
148 {
149 return GeneralUtility::locationHeaderUrl((string)$uri);
150 }
151 }