52d251454dc42b7f4affa59c2d9cbc371fd8b274
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / MVC / Controller / AbstractController.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * An abstract base class for Controllers
30 *
31 * @package Extbase
32 * @subpackage MVC\Controller
33 * @version $ID:$
34 * @api
35 */
36 abstract class Tx_Extbase_MVC_Controller_AbstractController implements Tx_Extbase_MVC_Controller_ControllerInterface {
37 /**
38 * @var Tx_Extbase_Object_ObjectManagerInterface
39 */
40 protected $objectManager;
41
42 /**
43 * @var Tx_Extbase_MVC_Web_Routing_UriBuilder
44 */
45 protected $uriBuilder;
46
47 /**
48 * @var string Key of the extension this controller belongs to
49 */
50 protected $extensionName;
51
52 /**
53 * Contains the settings of the current extension
54 *
55 * @var array
56 * @api
57 */
58 protected $settings;
59
60 /**
61 * The current request.
62 *
63 * @var Tx_Extbase_MVC_Request
64 * @api
65 */
66 protected $request;
67
68 /**
69 * The response which will be returned by this action controller
70 *
71 * @var Tx_Extbase_MVC_Response
72 * @api
73 */
74 protected $response;
75
76 /**
77 * @var Tx_Extbase_Property_Mapper
78 */
79 protected $propertyMapper;
80
81 /**
82 * @var Tx_Extbase_Validation_ValidatorResolver
83 */
84 protected $validatorResolver;
85
86 /**
87 * @var Tx_Extbase_MVC_Controller_Arguments Arguments passed to the controller
88 */
89 protected $arguments;
90
91 /**
92 * The results of the mapping of request arguments to controller arguments
93 * @var Tx_Extbase_Property_MappingResults
94 * @api
95 */
96 protected $argumentsMappingResults;
97
98 /**
99 * An array of supported request types. By default only web requests are supported.
100 * Modify or replace this array if your specific controller supports certain
101 * (additional) request types.
102 * @var array
103 */
104 protected $supportedRequestTypes = array('Tx_Extbase_MVC_Request');
105
106 /**
107 * @var Tx_Extbase_MVC_Controller_ControllerContext
108 * @api
109 */
110 protected $controllerContext;
111
112 /**
113 * The flash messages. DEPRECATED. Use $this->flashMessageContainer instead.
114 *
115 * @var Tx_Extbase_MVC_Controller_FlashMessages
116 * @deprecated
117 */
118 protected $flashMessages;
119
120 /**
121 * The flash messages. Use $this->flashMessageContainer->add(...) to add a new Flash message.
122 *
123 * @var Tx_Extbase_MVC_Controller_FlashMessages
124 * @api
125 */
126 protected $flashMessageContainer;
127
128 /**
129 * Constructs the controller.
130 */
131 public function __construct() {
132 list(, $this->extensionName) = explode('_', get_class($this));
133
134 }
135
136 /**
137 * Injects the property mapper
138 *
139 * @param Tx_Extbase_Property_Mapper $propertyMapper The property mapper
140 * @return void
141 */
142 public function injectPropertyMapper(Tx_Extbase_Property_Mapper $propertyMapper) {
143 $this->propertyMapper = $propertyMapper;
144 }
145
146 /**
147 * Injects the settings of the extension.
148 *
149 * @param array $settings Settings container of the current extension
150 * @return void
151 */
152 public function setSettings(array $settings) {
153 $this->settings = $settings;
154 }
155
156 /**
157 * Injects the object manager
158 *
159 * @param Tx_Extbase_Object_ObjectManagerInterface $objectManager
160 * @return void
161 */
162 public function injectObjectManager(Tx_Extbase_Object_ObjectManagerInterface $objectManager) {
163 $this->objectManager = $objectManager;
164 $this->arguments = $this->objectManager->create('Tx_Extbase_MVC_Controller_Arguments');
165 }
166
167 /**
168 * Injects the validator resolver
169 *
170 * @param Tx_Extbase_Validation_ValidatorResolver $validatorResolver
171 * @return void
172 */
173 public function injectValidatorResolver(Tx_Extbase_Validation_ValidatorResolver $validatorResolver) {
174 $this->validatorResolver = $validatorResolver;
175 }
176
177 /**
178 * Injects the flash messages container
179 *
180 * @param Tx_Extbase_MVC_Controller_FlashMessages $flashMessages
181 * @return void
182 */
183 public function injectFlashMessageContainer(Tx_Extbase_MVC_Controller_FlashMessages $flashMessageContainer) {
184 $this->flashMessageContainer = $flashMessageContainer;
185 $this->flashMessages = $flashMessageContainer; // deprecated, but should still work.
186 }
187
188 /**
189 * Checks if the current request type is supported by the controller.
190 *
191 * If your controller only supports certain request types, either
192 * replace / modify the supporteRequestTypes property or override this
193 * method.
194 *
195 * @param Tx_Extbase_MVC_Request $request The current request
196 * @return boolean TRUE if this request type is supported, otherwise FALSE
197 * @api
198 */
199 public function canProcessRequest(Tx_Extbase_MVC_Request $request) {
200 foreach ($this->supportedRequestTypes as $supportedRequestType) {
201 if ($request instanceof $supportedRequestType) return TRUE;
202 }
203 return FALSE;
204 }
205
206 /**
207 * Processes a general request. The result can be returned by altering the given response.
208 *
209 * @param Tx_Extbase_MVC_Request $request The request object
210 * @param Tx_Extbase_MVC_Response $response The response, modified by this handler
211 * @return void
212 * @throws Tx_Extbase_MVC_Exception_UnsupportedRequestType if the controller doesn't support the current request type
213 * @api
214 */
215 public function processRequest(Tx_Extbase_MVC_Request $request, Tx_Extbase_MVC_Response $response) {
216 if (!$this->canProcessRequest($request)) throw new Tx_Extbase_MVC_Exception_UnsupportedRequestType(get_class($this) . ' does not support requests of type "' . get_class($request) . '". Supported types are: ' . implode(' ', $this->supportedRequestTypes) , 1187701131);
217
218 $this->request = $request;
219 $this->request->setDispatched(TRUE);
220 $this->response = $response;
221
222 $this->uriBuilder = $this->objectManager->create('Tx_Extbase_MVC_Web_Routing_UriBuilder');
223 $this->uriBuilder->setRequest($request);
224
225 $this->initializeControllerArgumentsBaseValidators();
226 $this->mapRequestArgumentsToControllerArguments();
227 $this->controllerContext = $this->buildControllerContext();
228 }
229
230 /**
231 * Initialize the controller context
232 *
233 * @return Tx_Extbase_MVC_Controller_ControllerContext ControllerContext to be passed to the view
234 * @api
235 */
236 protected function buildControllerContext() {
237 $controllerContext = $this->objectManager->create('Tx_Extbase_MVC_Controller_ControllerContext');
238 $controllerContext->setRequest($this->request);
239 $controllerContext->setResponse($this->response);
240 if ($this->arguments !== NULL) {
241 $controllerContext->setArguments($this->arguments);
242 }
243 if ($this->argumentsMappingResults !== NULL) {
244 $controllerContext->setArgumentsMappingResults($this->argumentsMappingResults);
245 }
246 $controllerContext->setUriBuilder($this->uriBuilder);
247 $controllerContext->setFlashMessageContainer($this->flashMessageContainer);
248 return $controllerContext;
249 }
250
251 /**
252 * Forwards the request to another controller.
253 *
254 * @param string $actionName Name of the action to forward to
255 * @param string $controllerName Unqualified object name of the controller to forward to. If not specified, the current controller is used.
256 * @param string $extensionName Name of the extension containing the controller to forward to. If not specified, the current extension is assumed.
257 * @param Tx_Extbase_MVC_Controller_Arguments $arguments Arguments to pass to the target action
258 * @return void
259 * @throws Tx_Extbase_MVC_Exception_StopAction
260 * @api
261 */
262 public function forward($actionName, $controllerName = NULL, $extensionName = NULL, array $arguments = NULL) {
263 $this->request->setDispatched(FALSE);
264 $this->request->setControllerActionName($actionName);
265 if ($controllerName !== NULL) $this->request->setControllerName($controllerName);
266 if ($extensionName !== NULL) $this->request->setControllerExtensionName($extensionName);
267 if ($arguments !== NULL) $this->request->setArguments($arguments);
268 throw new Tx_Extbase_MVC_Exception_StopAction();
269 }
270
271 /**
272 * Forwards the request to another action and / or controller.
273 *
274 * NOTE: This method only supports web requests and will thrown an exception
275 * if used with other request types.
276 *
277 * @param string $actionName Name of the action to forward to
278 * @param string $controllerName Unqualified object name of the controller to forward to. If not specified, the current controller is used.
279 * @param string $extensionName Name of the extension containing the controller to forward to. If not specified, the current extension is assumed.
280 * @param Tx_Extbase_MVC_Controller_Arguments $arguments Arguments to pass to the target action
281 * @param integer $pageUid Target page uid. If NULL, the current page uid is used
282 * @param integer $delay (optional) The delay in seconds. Default is no delay.
283 * @param integer $statusCode (optional) The HTTP status code for the redirect. Default is "303 See Other"
284 * @return void
285 * @throws Tx_Extbase_MVC_Exception_UnsupportedRequestType If the request is not a web request
286 * @throws Tx_Extbase_MVC_Exception_StopAction
287 * @api
288 */
289 protected function redirect($actionName, $controllerName = NULL, $extensionName = NULL, array $arguments = NULL, $pageUid = NULL, $delay = 0, $statusCode = 303) {
290 if (!$this->request instanceof Tx_Extbase_MVC_Web_Request) throw new Tx_Extbase_MVC_Exception_UnsupportedRequestType('redirect() only supports web requests.', 1220539734);
291
292 if ($controllerName === NULL) {
293 $controllerName = $this->request->getControllerName();
294 }
295
296 $uri = $this->uriBuilder
297 ->reset()
298 ->setTargetPageUid($pageUid)
299 ->uriFor($actionName, $arguments, $controllerName, $extensionName);
300 $this->redirectToURI($uri, $delay, $statusCode);
301 }
302
303 /**
304 * Redirects the web request to another uri.
305 *
306 * NOTE: This method only supports web requests and will thrown an exception if used with other request types.
307 *
308 * @param mixed $uri A string representation of a URI
309 * @param integer $delay (optional) The delay in seconds. Default is no delay.
310 * @param integer $statusCode (optional) The HTTP status code for the redirect. Default is "303 See Other"
311 * @throws Tx_Extbase_MVC_Exception_UnsupportedRequestType If the request is not a web request
312 * @throws Tx_Extbase_MVC_Exception_StopAction
313 * @api
314 */
315 protected function redirectToURI($uri, $delay = 0, $statusCode = 303) {
316 if (!$this->request instanceof Tx_Extbase_MVC_Web_Request) throw new Tx_Extbase_MVC_Exception_UnsupportedRequestType('redirect() only supports web requests.', 1220539734);
317
318 $uri = $this->addBaseUriIfNecessary($uri);
319 $escapedUri = htmlentities($uri, ENT_QUOTES, 'utf-8');
320 $this->response->setContent('<html><head><meta http-equiv="refresh" content="' . intval($delay) . ';url=' . $escapedUri . '"/></head></html>');
321 $this->response->setStatus($statusCode);
322 $this->response->setHeader('Location', (string)$uri);
323 throw new Tx_Extbase_MVC_Exception_StopAction();
324 }
325
326 /**
327 * Adds the base uri if not already in place.
328 *
329 * @param string $uri The URI
330 * @return void
331 */
332 protected function addBaseUriIfNecessary($uri) {
333 $baseUri = $this->request->getBaseURI();
334 if(stripos($uri, $baseUri) !== 0) {
335 $uri = $baseUri . (string)$uri;
336 }
337 return $uri;
338 }
339
340 /**
341 * Sends the specified HTTP status immediately.
342 *
343 * NOTE: This method only supports web requests and will thrown an exception if used with other request types.
344 *
345 * @param integer $statusCode The HTTP status code
346 * @param string $statusMessage A custom HTTP status message
347 * @param string $content Body content which further explains the status
348 * @throws Tx_Extbase_MVC_Exception_UnsupportedRequestType If the request is not a web request
349 * @throws Tx_Extbase_MVC_Exception_StopAction
350 * @api
351 */
352 public function throwStatus($statusCode, $statusMessage = NULL, $content = NULL) {
353 if (!$this->request instanceof Tx_Extbase_MVC_Web_Request) throw new Tx_Extbase_MVC_Exception_UnsupportedRequestType('throwStatus() only supports web requests.', 1220539739);
354
355 $this->response->setStatus($statusCode, $statusMessage);
356 if ($content === NULL) $content = $this->response->getStatus();
357 $this->response->setContent($content);
358 throw new Tx_Extbase_MVC_Exception_StopAction();
359 }
360
361 /**
362 * Collects the base validators which were defined for the data type of each
363 * controller argument and adds them to the argument's validator chain.
364 *
365 * @return void
366 */
367 public function initializeControllerArgumentsBaseValidators() {
368 foreach ($this->arguments as $argument) {
369 $validator = $this->validatorResolver->getBaseValidatorConjunction($argument->getDataType());
370 if ($validator !== NULL) $argument->setValidator($validator);
371 }
372 }
373
374 /**
375 * Maps arguments delivered by the request object to the local controller arguments.
376 *
377 * @return void
378 */
379 protected function mapRequestArgumentsToControllerArguments() {
380 $optionalPropertyNames = array();
381 $allPropertyNames = $this->arguments->getArgumentNames();
382 foreach ($allPropertyNames as $propertyName) {
383 if ($this->arguments[$propertyName]->isRequired() === FALSE) $optionalPropertyNames[] = $propertyName;
384 }
385
386 $validator = $this->objectManager->create('Tx_Extbase_MVC_Controller_ArgumentsValidator');
387 $this->propertyMapper->mapAndValidate($allPropertyNames, $this->request->getArguments(), $this->arguments, $optionalPropertyNames, $validator);
388
389 $this->argumentsMappingResults = $this->propertyMapper->getMappingResults();
390 }
391 }
392 ?>