EXTMVC:
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Controller / TX_EXTMVC_Controller_AbstractController.php
1 <?php
2
3 /* *
4 * This script belongs to the FLOW3 framework. *
5 * *
6 * It is free software; you can redistribute it and/or modify it under *
7 * the terms of the GNU Lesser General Public License as published by the *
8 * Free Software Foundation, either version 3 of the License, or (at your *
9 * option) any later version. *
10 * *
11 * This script is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
13 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
14 * General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with the script. *
18 * If not, see http://www.gnu.org/licenses/lgpl.html *
19 * *
20 * The TYPO3 project - inspiring people to share! *
21 * */
22
23 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Controller/TX_EXTMVC_Controller_ControllerInterface.php');
24 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Controller/TX_EXTMVC_Controller_Arguments.php');
25 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Exception/TX_EXTMVC_Exception_StopAction.php');
26
27 /**
28 * An abstract base class for Controllers
29 *
30 * @version $Id:$
31 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
32 */
33 abstract class TX_EXTMVC_Controller_AbstractController implements TX_EXTMVC_Controller_ControllerInterface {
34
35 /**
36 * @var string Key of the extension this controller belongs to
37 */
38 protected $extensionKey;
39
40 /**
41 * Contains the settings of the current extension
42 *
43 * @var array
44 */
45 protected $settings;
46
47 /**
48 * @var TX_EXTMVC_Request The current request
49 */
50 protected $request;
51
52 /**
53 * @var TX_EXTMVC_Response The response which will be returned by this action controller
54 */
55 protected $response;
56
57 /**
58 * @var TX_EXTMVC_Controller_Arguments Arguments passed to the controller
59 */
60 protected $arguments;
61
62 /**
63 * Actions that schould not be cached (changes the invocated dispatcher to a USER_INT cObject)
64 * @var array
65 */
66 protected $nonCachableActions = array();
67
68 /**
69 * Constructs the controller.
70 *
71 * @param F3_FLOW3_Object_FactoryInterface $objectFactory A reference to the Object Factory
72 * @param F3_FLOW3_Package_ManagerInterface $packageManager A reference to the Package Manager
73 */
74 public function __construct() {
75 // SK: Set $this->extensionKey, could be done the same way as it is done in Fluid
76 $this->arguments = t3lib_div::makeInstance('TX_EXTMVC_Controller_Arguments');
77 }
78
79 /**
80 * Injects the settings of the extension.
81 *
82 * @param array $settings Settings container of the current extension
83 * @return void
84 */
85 public function injectSettings(array $settings) {
86 $this->settings = $settings;
87 }
88
89 /**
90 * Processes a general request. The result can be returned by altering the given response.
91 *
92 * @param TX_EXTMVC_Request $request The request object
93 * @param TX_EXTMVC_Response $response The response, modified by this handler
94 * @return void
95 * @throws TX_EXTMVC_Exception_UnsupportedRequestType if the controller doesn't support the current request type
96 */
97 public function processRequest(TX_EXTMVC_Request $request, TX_EXTMVC_Response $response) {
98 $this->request = $request;
99 $this->request->setDispatched(TRUE);
100 $this->response = $response;
101
102 $this->initializeArguments();
103 $this->mapRequestArgumentsToLocalArguments();
104 }
105
106 /**
107 * Returns TRUE if the given action (a name of an action like 'show'; without
108 * trailing 'Action') should be cached, otherwise it returns FALSE.
109 *
110 * @param string $actionName
111 * @return void
112 * @author Jochen Rau <jochen.rau@typoplanet.de>
113 */
114 public function isCachableAction($actionName) {
115 if (in_array($actionName, $this->nonCachableActions)) return FALSE;
116 return TRUE;
117 }
118
119 /**
120 * Initializes (registers / defines) arguments of this controller.
121 *
122 * Override this method to add arguments which can later be accessed
123 * by the action methods.
124 *
125 * @return void
126 */
127 protected function initializeArguments() {
128 }
129
130 /**
131 * Forwards the request to another controller.
132 *
133 * @return void
134 * @throws TX_EXTMVC_Exception_StopAction
135 */
136 public function forward($actionName, $controllerName = NULL, $extensionKey = NULL, TX_EXTMVC_Controller_Arguments $arguments = NULL) {
137 $this->request->setDispatched(FALSE);
138 $this->request->setControllerActionName($actionName);
139 if ($controllerName !== NULL) $this->request->setControllerName($controllerName);
140 if ($extensionKey !== NULL) $this->request->setControllerExtensionKey($extensionKey);
141 if ($arguments !== NULL) $this->request->setArguments($arguments);
142 throw new TX_EXTMVC_Exception_StopAction();
143 }
144
145 /**
146 * Redirects the web request to another uri.
147 *
148 * NOTE: This method only supports web requests and will thrown an exception if used with other request types.
149 *
150 * @param mixed $uri Either a string representation of a URI or a F3_FLOW3_Property_DataType_URI object
151 * @param integer $delay (optional) The delay in seconds. Default is no delay.
152 * @param integer $statusCode (optional) The HTTP status code for the redirect. Default is "303 See Other"
153 * @throws TX_EXTMVC_Exception_UnsupportedRequestType If the request is not a web request
154 * @throws TX_EXTMVC_Exception_StopAction
155 */
156 public function redirect($uri, $delay = 0, $statusCode = 303) {
157 if (!$this->request instanceof TX_EXTMVC_Web_Request) throw new TX_EXTMVC_Exception_UnsupportedRequestType('redirect() only supports web requests.', 1220539734);
158
159 $escapedUri = htmlentities($uri, ENT_QUOTES, 'utf-8');
160 $this->response->setContent('<html><head><meta http-equiv="refresh" content="' . intval($delay) . ';url=' . $escapedUri . '"/></head></html>');
161 $this->response->setStatus($statusCode);
162 throw new TX_EXTMVC_Exception_StopAction();
163 }
164
165 /**
166 * Sends the specified HTTP status immediately.
167 *
168 * NOTE: This method only supports web requests and will thrown an exception if used with other request types.
169 *
170 * @param integer $statusCode The HTTP status code
171 * @param string $statusMessage A custom HTTP status message
172 * @param string $content Body content which further explains the status
173 * @throws TX_EXTMVC_Exception_UnsupportedRequestType If the request is not a web request
174 * @throws TX_EXTMVC_Exception_StopAction
175 */
176 public function throwStatus($statusCode, $statusMessage = NULL, $content = NULL) {
177 if (!$this->request instanceof TX_EXTMVC_Web_Request) throw new TX_EXTMVC_Exception_UnsupportedRequestType('throwStatus() only supports web requests.', 1220539739);
178
179 $this->response->setStatus($statusCode, $statusMessage);
180 if ($content === NULL) $content = $this->response->getStatus();
181 $this->response->setContent($content);
182 throw new TX_EXTMVC_Exception_StopAction();
183 }
184
185 /**
186 * Maps arguments delivered by the request object to the local controller arguments.
187 *
188 * @return void
189 */
190 // SK: needs to be adjusted, as $this->request->getArguments will only return an array (see the comments in the dispatcher)
191 protected function mapRequestArgumentsToLocalArguments() {
192 $requestArguments = $this->request->getArguments();
193 foreach ($this->arguments as $argument) {
194 $argumentName = $argument->getName();
195 $argumentShortName = $argument->getShortName();
196 if (array_key_exists($argumentName, $requestArguments)) {
197 $argument->setValue($requestArguments[$argumentName]->getValue());
198 } elseif ($argumentShortName !== NULL && array_key_exists($argumentShortName, $requestArguments)) {
199 $argument->setValue($requestArguments[$argumentShortName]->getValue());
200 }
201 }
202 }
203
204 }
205
206 ?>