added view->initializeView()!
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / MVC / Controller / ActionController.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 * A multi action controller
27 *
28 * @package TYPO3
29 * @subpackage extbase
30 * @version $ID:$
31 */
32 // SK: fill initializeArguments() so it parses the arguments for a given view. We need to discuss how this parsing can be
33 // SK: done effectively.
34 class Tx_ExtBase_MVC_Controller_ActionController extends Tx_ExtBase_MVC_Controller_AbstractController {
35
36 /**
37 * @var boolean If initializeView() should be called on an action invocation.
38 */
39 protected $initializeView = TRUE;
40
41 /**
42 * @var Tx_ExtBase_MVC_View_AbstractView By default a view with the same name as the current action is provided. Contains NULL if none was found.
43 */
44 protected $view = NULL;
45
46 /**
47 * By default a matching view will be resolved. If this property is set, automatic resolving is disabled and the specified object is used instead.
48 * @var string
49 */
50 protected $defaultViewObjectName = NULL;
51
52 /**
53 * Pattern after which the view object name is built
54 *
55 * @var string
56 */
57 // SK: Decision: Do we support "format"?
58 protected $viewObjectNamePattern = 'Tx_@extension_View_@controller@action';
59
60 /**
61 * Name of the action method
62 * @var string
63 */
64 protected $actionMethodName = 'indexAction';
65
66 /**
67 * Handles a request. The result output is returned by altering the given response.
68 *
69 * @param Tx_ExtBase_MVC_Request $request The request object
70 * @param Tx_ExtBase_MVC_Response $response The response, modified by this handler
71 * @return void
72 */
73 public function processRequest(Tx_ExtBase_MVC_Request $request, Tx_ExtBase_MVC_Response $response) {
74 $this->request = $request;
75 $this->request->setDispatched(TRUE);
76 $this->response = $response;
77
78 $this->actionMethodName = $this->resolveActionMethodName();
79 $this->initializeArguments();
80 $this->mapRequestArgumentsToLocalArguments();
81 if ($this->initializeView) $this->initializeView();
82 $this->initializeAction();
83 $this->callActionMethod();
84 }
85
86 /**
87 * Determines the action method and assures that the method exists.
88 *
89 * @return string The action method name
90 * @throws Tx_ExtBase_Exception_NoSuchAction if the action specified in the request object does not exist (and if there's no default action either).
91 */
92 protected function resolveActionMethodName() {
93 $actionMethodName = $this->request->getControllerActionName() . 'Action';
94 if (!method_exists($this, $actionMethodName)) throw new Tx_ExtBase_Exception_NoSuchAction('An action "' . $actionMethodName . '" does not exist in controller "' . get_class($this) . '".', 1186669086);
95 return $actionMethodName;
96 }
97
98 /**
99 * Returns TRUE if the given action (a name of an action like 'show'; without
100 * trailing 'Action') should be cached, otherwise it returns FALSE.
101 *
102 * @param string $actionName
103 * @return void
104 * @author Jochen Rau <jochen.rau@typoplanet.de>
105 */
106 public function isCachableAction($actionName) {
107 return !in_array($actionName, $this->nonCachableActions);
108 }
109
110 /**
111 * Calls the specified action method and passes the arguments.
112 * If the action returns a string, it is appended to the content in the
113 * response object.
114 *
115 * @param string $actionMethodName Name of the action method
116 * @return void
117 */
118 protected function callActionMethod() {
119 $actionResult = call_user_func_array(array($this, $this->actionMethodName), array());
120 if ($actionResult === NULL && $this->view instanceof Tx_ExtBase_MVC_View_ViewInterface) {
121 $this->response->appendContent($this->view->render());
122 } elseif (is_string($actionResult) && strlen($actionResult) > 0) {
123 $this->response->appendContent($actionResult);
124 }
125 }
126
127 /**
128 * Prepares a view for the current action and stores it in $this->view.
129 * By default, this method tries to locate a view with a name matching
130 * the current action.
131 *
132 * @return void
133 */
134 protected function initializeView() {
135 $viewObjectName = ($this->defaultViewObjectName === NULL) ? $this->resolveViewObjectName() : $this->defaultViewObjectName;
136 if (!class_exists($viewObjectName)) $viewObjectName = 'Tx_ExtBase_MVC_View_EmptyView';
137
138 $this->view = t3lib_div::makeInstance($viewObjectName);
139 $this->view->setRequest($this->request);
140 $this->view->initializeView();
141 }
142
143 /**
144 * Determines the fully qualified view object name.
145 *
146 * @return string The fully qualified view object name
147 */
148 protected function resolveViewObjectName() {
149 $possibleViewName = $this->viewObjectNamePattern;
150 $extensionName = $this->request->getExtensionName();
151 $possibleViewName = str_replace('@extension', $extensionName, $possibleViewName);
152 $possibleViewName = str_replace('@controller', $this->request->getControllerName(), $possibleViewName);
153 $possibleViewName = str_replace('@action', ucfirst($this->request->getControllerActionName()), $possibleViewName);
154 return $possibleViewName;
155 }
156
157 /**
158 * Initializes the controller before invoking an action method.
159 *
160 * Override this method to solve tasks which all actions have in
161 * common.
162 *
163 * @return void
164 */
165 protected function initializeAction() {
166 }
167
168 /**
169 * The default action of this controller.
170 *
171 * This method should always be overridden by the concrete action
172 * controller implementation.
173 *
174 * @return void
175 */
176 protected function indexAction() {
177 return 'No index action has been implemented yet for this controller.';
178 }
179 }
180 ?>