63cdd2b7626d5f1314493239aa84f90fa9a9cf7b
[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 script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * An abstract base class for Controllers
27 *
28 * @package TYPO3
29 * @subpackage extbase
30 * @version $ID:$
31 */
32 abstract class Tx_ExtBase_MVC_Controller_AbstractController implements Tx_ExtBase_MVC_Controller_ControllerInterface {
33
34 /**
35 * @var string Key of the extension this controller belongs to
36 */
37 protected $extensionName;
38
39 /**
40 * Contains the settings of the current extension
41 *
42 * @var array
43 */
44 protected $settings;
45
46 /**
47 * @var Tx_ExtBase_MVC_Request The current request
48 */
49 protected $request;
50
51 /**
52 * @var Tx_ExtBase_MVC_Response The response which will be returned by this action controller
53 */
54 protected $response;
55
56 /**
57 * @var Tx_ExtBase_MVC_Controller_Arguments Arguments passed to the controller
58 */
59 protected $arguments;
60
61 /**
62 * Actions that schould not be cached (changes the invocated dispatcher to a USER_INT cObject)
63 * @var array
64 */
65 protected $nonCachableActions = array();
66
67 /**
68 * @var Tx_ExtBase_MVC_URIHelper
69 */
70 protected $URIHelper;
71
72 /**
73 * Constructs the controller.
74 *
75 * @param F3_FLOW3_Object_FactoryInterface $objectFactory A reference to the Object Factory
76 * @param F3_FLOW3_Package_ManagerInterface $packageManager A reference to the Package Manager
77 */
78 public function __construct() {
79 // SK: Set $this->extensionName, could be done the same way as it is done in Fluid
80 $this->arguments = t3lib_div::makeInstance('Tx_ExtBase_MVC_Controller_Arguments');
81 $this->URIHelper = t3lib_div::makeInstance('Tx_ExtBase_MVC_Web_URIHelper');
82 }
83
84 /**
85 * Injects the settings of the extension.
86 *
87 * @param array $settings Settings container of the current extension
88 * @return void
89 */
90 public function injectSettings(array $settings) {
91 $this->settings = $settings;
92 }
93
94 /**
95 * Processes a general request. The result can be returned by altering the given response.
96 *
97 * @param Tx_ExtBase_MVC_Request $request The request object
98 * @param Tx_ExtBase_MVC_Response $response The response, modified by this handler
99 * @return void
100 * @throws Tx_ExtBase_Exception_UnsupportedRequestType if the controller doesn't support the current request type
101 */
102 public function processRequest(Tx_ExtBase_MVC_Request $request, Tx_ExtBase_MVC_Response $response) {
103 $this->request = $request;
104 $this->request->setDispatched(TRUE);
105 $this->response = $response;
106
107 $this->initializeArguments();
108 $this->mapRequestArgumentsToLocalArguments();
109 }
110
111 /**
112 * Initializes (registers / defines) arguments of this controller.
113 *
114 * Override this method to add arguments which can later be accessed
115 * by the action methods.
116 *
117 * @return void
118 */
119 protected function initializeArguments() {
120 }
121
122 /**
123 * Forwards the request to another controller.
124 *
125 * @return void
126 * @throws Tx_ExtBase_Exception_StopAction
127 */
128 public function forward($actionName, $controllerName = NULL, $extensionName = NULL, Tx_ExtBase_MVC_Controller_Arguments $arguments = NULL) {
129 $this->request->setDispatched(FALSE);
130 $this->request->setControllerActionName($actionName);
131 if ($controllerName !== NULL) $this->request->setControllerName($controllerName);
132 if ($extensionName !== NULL) $this->request->setExtensionName($extensionName);
133 if ($arguments !== NULL) $this->request->setArguments($arguments);
134 throw new Tx_ExtBase_Exception_StopAction();
135 }
136
137 /**
138 * Forwards the request to another action and / or controller.
139 *
140 * NOTE: This method only supports web requests and will thrown an exception
141 * if used with other request types.
142 *
143 * @param string $actionName Name of the action to forward to
144 * @param string $controllerName Unqualified object name of the controller to forward to. If not specified, the current controller is used.
145 * @param string $extensionKey Key of the extension containing the controller to forward to. If not specified, the current extension is assumed.
146 * @param integer $delay (optional) The delay in seconds. Default is no delay.
147 * @param integer $statusCode (optional) The HTTP status code for the redirect. Default is "303 See Other"
148 * @param Tx_ExtBase_MVC_Controller_Arguments $arguments Arguments to pass to the target action
149 * @return void
150 * @throws Tx_ExtBase_Exception_UnsupportedRequestType If the request is not a web request
151 * @throws Tx_ExtBase_Exception_StopAction
152 */
153 protected function redirect($actionName, $controllerName = '', $extensionKey = '', array $arguments = NULL, $delay = 0, $statusCode = 303) {
154 if (!$this->request instanceof Tx_ExtBase_MVC_Web_Request) throw new Tx_ExtBase_Exception_UnsupportedRequestType('redirect() only supports web requests.', 1220539734);
155
156 $uri = $this->URIHelper->URIFor($this->request, $actionName, $arguments, $controllerName, $extensionKey);
157 $this->redirectToURI($uri, $delay, $statusCode);
158 }
159
160 /**
161 * Redirects the web request to another uri.
162 *
163 * NOTE: This method only supports web requests and will thrown an exception if used with other request types.
164 *
165 * @param mixed $uri Either a string representation of a URI or a \F3\FLOW3\Property\DataType\URI object
166 * @param integer $delay (optional) The delay in seconds. Default is no delay.
167 * @param integer $statusCode (optional) The HTTP status code for the redirect. Default is "303 See Other"
168 * @throws Tx_ExtBase_Exception_UnsupportedRequestType If the request is not a web request
169 * @throws Tx_ExtBase_Exception_StopAction
170 */
171 protected function redirectToURI($uri, $delay = 0, $statusCode = 303) {
172 if (!$this->request instanceof Tx_ExtBase_MVC_Web_Request) throw new Tx_ExtBase_Exception_UnsupportedRequestType('redirect() only supports web requests.', 1220539734);
173
174 $uri = $this->request->getBaseURI() . (string)$uri;
175 $escapedUri = htmlentities($uri, ENT_QUOTES, 'utf-8');
176 $this->response->setContent('<html><head><meta http-equiv="refresh" content="' . intval($delay) . ';url=' . $escapedUri . '"/></head></html>');
177 $this->response->setStatus($statusCode);
178 $this->response->setHeader('Location', (string)$uri);
179 throw new Tx_ExtBase_Exception_StopAction();
180 }
181
182 /**
183 * Sends the specified HTTP status immediately.
184 *
185 * NOTE: This method only supports web requests and will thrown an exception if used with other request types.
186 *
187 * @param integer $statusCode The HTTP status code
188 * @param string $statusMessage A custom HTTP status message
189 * @param string $content Body content which further explains the status
190 * @throws Tx_ExtBase_Exception_UnsupportedRequestType If the request is not a web request
191 * @throws Tx_ExtBase_Exception_StopAction
192 */
193 public function throwStatus($statusCode, $statusMessage = NULL, $content = NULL) {
194 if (!$this->request instanceof Tx_ExtBase_MVC_Web_Request) throw new Tx_ExtBase_Exception_UnsupportedRequestType('throwStatus() only supports web requests.', 1220539739);
195
196 $this->response->setStatus($statusCode, $statusMessage);
197 if ($content === NULL) $content = $this->response->getStatus();
198 $this->response->setContent($content);
199 throw new Tx_ExtBase_Exception_StopAction();
200 }
201
202 /**
203 * Maps arguments delivered by the request object to the local controller arguments.
204 *
205 * @return void
206 */
207 protected function mapRequestArgumentsToLocalArguments() {
208 $requestArguments = $this->request->getArguments();
209 foreach ($this->arguments as $argument) {
210 $argumentName = $argument->getName();
211 $argumentShortName = $argument->getShortName();
212 if (array_key_exists($argumentName, $requestArguments)) {
213 $argument->setValue($requestArguments[$argumentName]);
214 } elseif ($argumentShortName !== NULL && array_key_exists($argumentShortName, $requestArguments)) {
215 $argument->setValue($requestArguments[$argumentShortName]);
216 }
217 }
218 }
219 }
220
221 ?>