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