eb1470e2470c20a6a8abe218e5ff4a07288bc38e
[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 // SK: PHPDoc
106 public function isCachableAction($action) {
107 if (in_array($action, $this->nonCachableActions)) return FALSE;
108 return TRUE;
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_EXTMVC_Exception_StopAction
127 */
128 public function forward($actionName, $controllerName = NULL, $extensionKey = NULL, TX_EXTMVC_Controller_Arguments $arguments = NULL) {
129 $this->request->setDispatched(FALSE);
130 $this->request->setControllerActionName($actionName);
131 if ($controllerName !== NULL) $this->request->setControllerName($controllerName);
132 if ($extensionKey !== NULL) $this->request->setControllerExtensionKey($extensionKey);
133 if ($arguments !== NULL) $this->request->setArguments($arguments);
134 throw new TX_EXTMVC_Exception_StopAction();
135 }
136
137 /**
138 * Redirects the web request to another uri.
139 *
140 * NOTE: This method only supports web requests and will thrown an exception if used with other request types.
141 *
142 * @param mixed $uri Either a string representation of a URI or a F3_FLOW3_Property_DataType_URI object
143 * @param integer $delay (optional) The delay in seconds. Default is no delay.
144 * @param integer $statusCode (optional) The HTTP status code for the redirect. Default is "303 See Other"
145 * @throws TX_EXTMVC_Exception_UnsupportedRequestType If the request is not a web request
146 * @throws TX_EXTMVC_Exception_StopAction
147 */
148 public function redirect($uri, $delay = 0, $statusCode = 303) {
149 if (!$this->request instanceof TX_EXTMVC_Web_Request) throw new TX_EXTMVC_Exception_UnsupportedRequestType('redirect() only supports web requests.', 1220539734);
150
151 $escapedUri = htmlentities($uri, ENT_QUOTES, 'utf-8');
152 $this->response->setContent('<html><head><meta http-equiv="refresh" content="' . intval($delay) . ';url=' . $escapedUri . '"/></head></html>');
153 $this->response->setStatus($statusCode);
154 // $this->response->setHeader('Location', (string)$uri);
155 throw new TX_EXTMVC_Exception_StopAction();
156 }
157
158 /**
159 * Sends the specified HTTP status immediately.
160 *
161 * NOTE: This method only supports web requests and will thrown an exception if used with other request types.
162 *
163 * @param integer $statusCode The HTTP status code
164 * @param string $statusMessage A custom HTTP status message
165 * @param string $content Body content which further explains the status
166 * @throws TX_EXTMVC_Exception_UnsupportedRequestType If the request is not a web request
167 * @throws TX_EXTMVC_Exception_StopAction
168 */
169 public function throwStatus($statusCode, $statusMessage = NULL, $content = NULL) {
170 if (!$this->request instanceof TX_EXTMVC_Web_Request) throw new TX_EXTMVC_Exception_UnsupportedRequestType('throwStatus() only supports web requests.', 1220539739);
171
172 $this->response->setStatus($statusCode, $statusMessage);
173 if ($content === NULL) $content = $this->response->getStatus();
174 $this->response->setContent($content);
175 throw new TX_EXTMVC_Exception_StopAction();
176 }
177
178 /**
179 * Maps arguments delivered by the request object to the local controller arguments.
180 *
181 * @return void
182 */
183 // SK: needs to be adjusted, as $this->request->getArguments will only return an array (see the comments in the dispatcher)
184 protected function mapRequestArgumentsToLocalArguments() {
185 $requestArguments = $this->request->getArguments();
186 foreach ($this->arguments as $argument) {
187 $argumentName = $argument->getName();
188 $argumentShortName = $argument->getShortName();
189 if (array_key_exists($argumentName, $requestArguments)) {
190 $argument->setValue($requestArguments[$argumentName]->getValue());
191 } elseif ($argumentShortName !== NULL && array_key_exists($argumentShortName, $requestArguments)) {
192 $argument->setValue($requestArguments[$argumentShortName]->getValue());
193 }
194 }
195 }
196
197 }
198
199 ?>