[BUGFIX] Wrong content element rendering in system extension form
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Controller / Form.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2008 Patrick Broens (patrick@patrickbroens.nl)
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 * Main controller for Forms. All requests come through this class
27 * and are routed to the model and view layers for processing.
28 *
29 * @category Controller
30 * @package TYPO3
31 * @subpackage form
32 * @author Patrick Broens <patrick@patrickbroens.nl>
33 */
34 class tx_form_Controller_Form {
35
36 /**
37 * The TypoScript array
38 *
39 * @var array
40 */
41 protected $typoscript = array();
42
43 /**
44 * @var tx_form_Domain_Factory_Typoscript
45 */
46 protected $typoscriptFactory;
47
48 /**
49 * @var tx_form_System_Localization
50 */
51 protected $localizationHandler;
52
53 /**
54 * @var tx_form_System_Request
55 */
56 protected $requestHandler;
57
58 /**
59 * @var tx_form_System_Validate
60 */
61 protected $validate;
62
63 /**
64 * Initialisation
65 *
66 * @param array $typoscript TS configuration for this cObject
67 * @return void
68 */
69 public function initialize(array $typoscript) {
70 t3lib_div::makeInstance(
71 'tx_form_System_Localization',
72 'LLL:EXT:form/Resources/Private/Language/locallang_controller.xml'
73 );
74
75 $this->typoscriptFactory = t3lib_div::makeInstance('tx_form_Domain_Factory_Typoscript');
76 $this->localizationHandler = t3lib_div::makeInstance('tx_form_System_Localization');
77 $this->requestHandler = $this->typoscriptFactory->setRequestHandler($typoscript);
78 $this->validate = $this->typoscriptFactory->setRules($typoscript);
79
80 $this->typoscript = $typoscript;
81 }
82
83 /**
84 * Renders the application defined cObject FORM
85 * which overrides the TYPO3 default cObject FORM
86 *
87 * First we make a COA_INT out of it, because it does not need to be cached
88 * Then we send a FORM_INT to the COA_INT
89 * When this is read, it will call the FORM class again.
90 *
91 * It simply calls execute because this function name is not really descriptive
92 * but is needed by the core of TYPO3
93 *
94 * @param string $typoscriptObjectName Name of the object
95 * @param array $typoscript TS configuration for this cObject
96 * @param string $typoscriptKey A string label used for the internal debugging tracking.
97 * @param tslib_cObj $contentObject reference
98 * @return string HTML output
99 */
100 public function cObjGetSingleExt(
101 $typoscriptObjectName,
102 array $typoscript,
103 $typoscriptKey,
104 tslib_cObj $contentObject
105 ) {
106 $content = '';
107
108 if ($typoscriptObjectName === 'FORM') {
109 if ($contentObject->data['CType'] === 'mailform') {
110 $bodytext = $contentObject->data['bodytext'];
111 /** @var $typoScriptParser t3lib_tsparser */
112 $typoScriptParser = t3lib_div::makeInstance('t3lib_tsparser');
113 $typoScriptParser->parse($bodytext);
114 $typoscript = t3lib_div::array_merge_recursive_overrule(
115 (array) $typoScriptParser->setup,
116 (array) $typoscript
117 );
118 }
119 $newTyposcript = array(
120 '10' => 'FORM_INT',
121 '10.' => $typoscript,
122 );
123 $content = $contentObject->COBJ_ARRAY($newTyposcript, 'INT');
124 } elseif ($typoscriptObjectName == 'FORM_INT') {
125 $this->initialize($typoscript);
126 $content = $this->execute();
127 }
128
129 return $content;
130 }
131
132 /**
133 * Build the models and views and renders the output from the views
134 *
135 * @return string HTML Output
136 */
137 public function execute() {
138 // Form
139 if ($this->showForm()) {
140 $content = $this->renderForm();
141
142 // Confirmation screen
143 } elseif ($this->showConfirmation()) {
144 $content = $this->renderConfirmation();
145
146 // We need the post processing
147 } else {
148 $content = $this->doPostProcessing();
149 }
150
151 return $content;
152 }
153
154 /**
155 * Check if the form needs to be displayed
156 *
157 * This is TRUE when nothing has been submitted,
158 * when data has been submitted but the validation rules do not fit
159 * or when the user returns from the confirmation screen.
160 *
161 * @return boolean TRUE when form needs to be shown
162 */
163 protected function showForm() {
164 $show = FALSE;
165
166 $submittedByPrefix = $this->requestHandler->getByMethod();
167
168 if (
169 // Nothing has been submitted
170 $submittedByPrefix === NULL ||
171
172 // Submitted but not valid
173 (
174 !empty($submittedByPrefix) &&
175 !$this->validate->isValid()
176 ) ||
177
178 // Submitted, valid, but not confirmed
179 (
180 !empty($submittedByPrefix) &&
181 $this->validate->isValid() &&
182 $this->requestHandler->getPost('confirmation') === $this->localizationHandler->getLocalLanguageLabel('tx_form_view_confirmation.donotconfirm')
183 )
184 ) {
185 $show = TRUE;
186 }
187
188 return $show;
189 }
190
191 /**
192 * Render the form
193 *
194 * @return string The form HTML
195 */
196 protected function renderForm() {
197 $this->requestHandler->destroySession();
198
199 $form = $this->typoscriptFactory->buildModelFromTyposcript($this->typoscript);
200
201 /** @var $view tx_form_View_Form */
202 $view = t3lib_div::makeInstance('tx_form_View_Form', $form);
203
204 return $view->get();
205 }
206
207 /**
208 * Check if the confirmation message needs to be displayed
209 *
210 * This is TRUE when data has been submitted,
211 * the validation rules are valid,
212 * the confirmation screen has been configured in TypoScript
213 * and the confirmation screen has not been submitted
214 *
215 * @return boolean TRUE when confirmation screen needs to be shown
216 */
217 protected function showConfirmation() {
218 $show = FALSE;
219
220 if (
221 isset($this->typoscript['confirmation']) &&
222 $this->typoscript['confirmation'] == 1 &&
223 $this->requestHandler->getPost('confirmation') === NULL
224 ) {
225 $show = TRUE;
226 }
227
228 return $show;
229 }
230
231 /**
232 * Render the confirmation screen
233 *
234 * Stores the submitted data in a session
235 *
236 * @return string The confirmation screen HTML
237 */
238 protected function renderConfirmation() {
239 $form = $this->typoscriptFactory->buildModelFromTyposcript($this->typoscript);
240
241 $this->requestHandler->storeSession();
242
243 $confirmationTyposcript = array();
244 if (isset($this->typoscript['confirmation.'])) {
245 $confirmationTyposcript = $this->typoscript['confirmation.'];
246 }
247
248 /** @var $view tx_form_View_Confirmation */
249 $view = t3lib_div::makeInstance(
250 'tx_form_View_Confirmation',
251 $form,
252 $confirmationTyposcript
253 );
254
255 return $view->get();
256 }
257
258 /**
259 * Do the post processing
260 *
261 * Destroys the session because it is not needed anymore
262 *
263 * @return string The post processing HTML
264 */
265 protected function doPostProcessing() {
266 $form = $this->typoscriptFactory->buildModelFromTyposcript($this->typoscript);
267
268 $postProcessorTypoScript = array();
269 if (isset($this->typoscript['postProcessor.'])) {
270 $postProcessorTypoScript = $this->typoscript['postProcessor.'];
271 }
272
273 /** @var $postProcessor tx_form_System_Postprocessor */
274 $postProcessor = t3lib_div::makeInstance(
275 'tx_form_System_Postprocessor',
276 $form,
277 $postProcessorTypoScript
278 );
279 $content = $postProcessor->process();
280 $this->requestHandler->destroySession();
281
282 return $content;
283 }
284 }
285 ?>