[BUGFIX] Broken tests due to modified PostProcessor constructor
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Controller / FormController.php
1 <?php
2 namespace TYPO3\CMS\Form\Controller;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2008-2013 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 * @author Patrick Broens <patrick@patrickbroens.nl>
32 */
33 class FormController {
34
35 /**
36 * The TypoScript array
37 *
38 * @var array
39 */
40 protected $typoscript = array();
41
42 /**
43 * @var \TYPO3\CMS\Form\Domain\Factory\TypoScriptFactory
44 */
45 protected $typoscriptFactory;
46
47 /**
48 * @var \TYPO3\CMS\Form\Localization
49 */
50 protected $localizationHandler;
51
52 /**
53 * @var \TYPO3\CMS\Form\Request
54 */
55 protected $requestHandler;
56
57 /**
58 * @var \TYPO3\CMS\Form\Layout
59 */
60 protected $layoutHandler;
61
62 /**
63 * @var \TYPO3\CMS\Form\Utility\ValidatorUtility
64 */
65 protected $validate;
66
67 /**
68 * Initialisation
69 *
70 * @param array $typoscript TS configuration for this cObject
71 * @return void
72 */
73 public function initialize(array $typoscript) {
74 $this->typoscriptFactory = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Form\\Domain\\Factory\\TypoScriptFactory');
75 $this->localizationHandler = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Form\\Localization');
76 $this->layoutHandler = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Form\\Layout');
77 $this->requestHandler = $this->typoscriptFactory->setRequestHandler($typoscript);
78 $this->validate = $this->typoscriptFactory->setRules($typoscript);
79 $this->typoscript = $typoscript;
80 }
81
82 /**
83 * Renders the application defined cObject FORM
84 * which overrides the TYPO3 default cObject FORM
85 *
86 * First we make a COA_INT out of it, because it does not need to be cached
87 * Then we send a FORM_INT to the COA_INT
88 * When this is read, it will call the FORM class again.
89 *
90 * It simply calls execute because this function name is not really descriptive
91 * but is needed by the core of TYPO3
92 *
93 * @param string $typoScriptObjectName Name of the object
94 * @param array $typoScript TS configuration for this cObject
95 * @param string $typoScriptKey A string label used for the internal debugging tracking.
96 * @param \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer $contentObject reference
97 * @return string HTML output
98 */
99 public function cObjGetSingleExt($typoScriptObjectName, array $typoScript, $typoScriptKey, \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer $contentObject) {
100 $content = '';
101 if ($typoScriptObjectName === 'FORM' && !empty($typoScript['useDefaultContentObject'])) {
102 $content = $contentObject->getContentObject($typoScriptObjectName)->render($typoScript);
103 } elseif ($typoScriptObjectName === 'FORM') {
104 if ($contentObject->data['CType'] === 'mailform') {
105 $bodytext = $contentObject->data['bodytext'];
106 /** @var $typoScriptParser \TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser */
107 $typoScriptParser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\TypoScript\\Parser\\TypoScriptParser');
108 $typoScriptParser->parse($bodytext);
109 $mergedTypoScript = (array)$typoScriptParser->setup;
110 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($mergedTypoScript, (array)$typoScript);
111 // Disables content elements since TypoScript is handled that could contain insecure settings:
112 $mergedTypoScript[\TYPO3\CMS\Form\Domain\Factory\TypoScriptFactory::PROPERTY_DisableContentElement] = TRUE;
113 }
114 $newTypoScript = array(
115 '10' => 'FORM_INT',
116 '10.' => is_array($mergedTypoScript) ? $mergedTypoScript : $typoScript,
117 );
118 $content = $contentObject->COBJ_ARRAY($newTypoScript, 'INT');
119 // Only apply stdWrap to TypoScript that was NOT created by the wizard:
120 if (isset($typoScript['stdWrap.'])) {
121 $content = $contentObject->stdWrap($content, $typoScript['stdWrap.']);
122 }
123 } elseif ($typoScriptObjectName === 'FORM_INT') {
124 $this->initialize($typoScript);
125 $content = $this->execute();
126 }
127 return $content;
128 }
129
130 /**
131 * Build the models and views and renders the output from the views
132 *
133 * @return string HTML Output
134 */
135 public function execute() {
136 // Form
137 if ($this->showForm()) {
138 $content = $this->renderForm();
139 } elseif ($this->showConfirmation()) {
140 $content = $this->renderConfirmation();
141 } else {
142 $content = $this->doPostProcessing();
143 }
144 return $content;
145 }
146
147 /**
148 * Check if the form needs to be displayed
149 *
150 * This is TRUE when nothing has been submitted,
151 * when data has been submitted but the validation rules do not fit
152 * or when the user returns from the confirmation screen.
153 *
154 * @return boolean TRUE when form needs to be shown
155 */
156 protected function showForm() {
157 $show = FALSE;
158 $submittedByPrefix = $this->requestHandler->getByMethod();
159 if (
160 $submittedByPrefix === NULL ||
161 !empty($submittedByPrefix) && !$this->validate->isValid() ||
162 !empty($submittedByPrefix) && $this->validate->isValid() &&
163 $this->requestHandler->getPost('confirmation-false', NULL) !== NULL
164 ) {
165 $show = TRUE;
166 }
167 return $show;
168 }
169
170 /**
171 * Render the form
172 *
173 * @return string The form HTML
174 */
175 protected function renderForm() {
176 $layout = $this->typoscriptFactory->getLayoutFromTypoScript($this->typoscript['form.']);
177 $this->layoutHandler->setLayout($layout);
178 $this->requestHandler->destroySession();
179
180 $form = $this->typoscriptFactory->buildModelFromTyposcript($this->typoscript);
181 /** @var $view \TYPO3\CMS\Form\View\Form\FormView */
182 $view = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Form\\View\\Form\\FormView', $form);
183 return $view->get();
184 }
185
186 /**
187 * Check if the confirmation message needs to be displayed
188 *
189 * This is TRUE when data has been submitted,
190 * the validation rules are valid,
191 * the confirmation screen has been configured in TypoScript
192 * and the confirmation screen has not been submitted
193 *
194 * @return boolean TRUE when confirmation screen needs to be shown
195 */
196 protected function showConfirmation() {
197 $show = FALSE;
198 if (isset($this->typoscript['confirmation']) && $this->typoscript['confirmation'] == 1 && $this->requestHandler->getPost('confirmation-true', NULL) === NULL) {
199 $show = TRUE;
200 }
201 return $show;
202 }
203
204 /**
205 * Render the confirmation screen
206 *
207 * Stores the submitted data in a session
208 *
209 * @return string The confirmation screen HTML
210 */
211 protected function renderConfirmation() {
212 $confirmationTyposcript = array();
213 if (isset($this->typoscript['confirmation.'])) {
214 $confirmationTyposcript = $this->typoscript['confirmation.'];
215 }
216
217 $layout = $this->typoscriptFactory->getLayoutFromTypoScript($confirmationTyposcript);
218 $form = $this->typoscriptFactory->buildModelFromTyposcript($this->typoscript);
219
220 $this->layoutHandler->setLayout($layout);
221 $this->requestHandler->storeSession();
222 /** @var $view \TYPO3\CMS\Form\View\Confirmation\ConfirmationView */
223 $view = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Form\\View\\Confirmation\\ConfirmationView', $form, $confirmationTyposcript);
224 return $view->get();
225 }
226
227 /**
228 * Do the post processing
229 *
230 * Destroys the session because it is not needed anymore
231 *
232 * @return string The post processing HTML
233 */
234 protected function doPostProcessing() {
235 $form = $this->typoscriptFactory->buildModelFromTyposcript($this->typoscript);
236 $postProcessorTypoScript = array();
237 if (isset($this->typoscript['postProcessor.'])) {
238 $postProcessorTypoScript = $this->typoscript['postProcessor.'];
239 }
240 /** @var $postProcessor \TYPO3\CMS\Form\PostProcess\PostProcessor */
241 $postProcessor = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Form\\PostProcess\\PostProcessor', $form, $postProcessorTypoScript);
242 $content = $postProcessor->process();
243 $this->requestHandler->destroySession();
244 return $content;
245 }
246
247 }