ExtBase:
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / 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_Controller_AbstractController implements Tx_ExtBase_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_Request The current request
48 */
49 protected $request;
50
51 /**
52 * @var Tx_ExtBase_Response The response which will be returned by this action controller
53 */
54 protected $response;
55
56 /**
57 * @var Tx_ExtBase_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 * Constructs the controller.
69 *
70 * @param F3_FLOW3_Object_FactoryInterface $objectFactory A reference to the Object Factory
71 * @param F3_FLOW3_Package_ManagerInterface $packageManager A reference to the Package Manager
72 */
73 public function __construct() {
74 // SK: Set $this->extensionName, could be done the same way as it is done in Fluid
75 $this->arguments = t3lib_div::makeInstance('Tx_ExtBase_Controller_Arguments');
76 }
77
78 /**
79 * Injects the settings of the extension.
80 *
81 * @param array $settings Settings container of the current extension
82 * @return void
83 */
84 public function injectSettings(array $settings) {
85 $this->settings = $settings;
86 }
87
88 /**
89 * Processes a general request. The result can be returned by altering the given response.
90 *
91 * @param Tx_ExtBase_Request $request The request object
92 * @param Tx_ExtBase_Response $response The response, modified by this handler
93 * @return void
94 * @throws Tx_ExtBase_Exception_UnsupportedRequestType if the controller doesn't support the current request type
95 */
96 public function processRequest(Tx_ExtBase_Request $request, Tx_ExtBase_Response $response) {
97 $this->request = $request;
98 $this->request->setDispatched(TRUE);
99 $this->response = $response;
100
101 $this->initializeArguments();
102 $this->mapRequestArgumentsToLocalArguments();
103 }
104
105 /**
106 * Initializes (registers / defines) arguments of this controller.
107 *
108 * Override this method to add arguments which can later be accessed
109 * by the action methods.
110 *
111 * @return void
112 */
113 protected function initializeArguments() {
114 }
115
116 /**
117 * Forwards the request to another controller.
118 *
119 * @return void
120 * @throws Tx_ExtBase_Exception_StopAction
121 */
122 public function forward($actionName, $controllerName = NULL, $extensionName = NULL, Tx_ExtBase_Controller_Arguments $arguments = NULL) {
123 $this->request->setDispatched(FALSE);
124 $this->request->setControllerActionName($actionName);
125 if ($controllerName !== NULL) $this->request->setControllerName($controllerName);
126 if ($extensionName !== NULL) $this->request->setExtensionName($extensionName);
127 if ($arguments !== NULL) $this->request->setArguments($arguments);
128 throw new Tx_ExtBase_Exception_StopAction();
129 }
130
131 /**
132 * Redirects the web request to another uri.
133 *
134 * NOTE: This method only supports web requests and will thrown an exception if used with other request types.
135 *
136 * @param mixed $uri Either a string representation of a URI or a F3_FLOW3_Property_DataType_URI object
137 * @param integer $delay (optional) The delay in seconds. Default is no delay.
138 * @param integer $statusCode (optional) The HTTP status code for the redirect. Default is "303 See Other"
139 * @throws Tx_ExtBase_Exception_UnsupportedRequestType If the request is not a web request
140 * @throws Tx_ExtBase_Exception_StopAction
141 */
142 public function redirect($uri, $delay = 0, $statusCode = 303) {
143 if (!$this->request instanceof Tx_ExtBase_Web_Request) throw new Tx_ExtBase_Exception_UnsupportedRequestType('redirect() only supports web requests.', 1220539734);
144
145 $escapedUri = htmlentities($uri, ENT_QUOTES, 'utf-8');
146 $this->response->setContent('<html><head><meta http-equiv="refresh" content="' . intval($delay) . ';url=' . $escapedUri . '"/></head></html>');
147 $this->response->setStatus($statusCode);
148 throw new Tx_ExtBase_Exception_StopAction();
149 }
150
151 /**
152 * Sends the specified HTTP status immediately.
153 *
154 * NOTE: This method only supports web requests and will thrown an exception if used with other request types.
155 *
156 * @param integer $statusCode The HTTP status code
157 * @param string $statusMessage A custom HTTP status message
158 * @param string $content Body content which further explains the status
159 * @throws Tx_ExtBase_Exception_UnsupportedRequestType If the request is not a web request
160 * @throws Tx_ExtBase_Exception_StopAction
161 */
162 public function throwStatus($statusCode, $statusMessage = NULL, $content = NULL) {
163 if (!$this->request instanceof Tx_ExtBase_Web_Request) throw new Tx_ExtBase_Exception_UnsupportedRequestType('throwStatus() only supports web requests.', 1220539739);
164
165 $this->response->setStatus($statusCode, $statusMessage);
166 if ($content === NULL) $content = $this->response->getStatus();
167 $this->response->setContent($content);
168 throw new Tx_ExtBase_Exception_StopAction();
169 }
170
171 /**
172 * Maps arguments delivered by the request object to the local controller arguments.
173 *
174 * @return void
175 */
176 protected function mapRequestArgumentsToLocalArguments() {
177 $requestArguments = $this->request->getArguments();
178 foreach ($this->arguments as $argument) {
179 $argumentName = $argument->getName();
180 $argumentShortName = $argument->getShortName();
181 if (array_key_exists($argumentName, $requestArguments)) {
182 $argument->setValue($requestArguments[$argumentName]);
183 } elseif ($argumentShortName !== NULL && array_key_exists($argumentShortName, $requestArguments)) {
184 $argument->setValue($requestArguments[$argumentShortName]);
185 }
186 }
187 }
188
189 }
190
191 ?>