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