[TASK] Comment legacy files to be removed with 6.2
[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 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 * Main controller for Forms. All requests come through this class
28 * and are routed to the model and view layers for processing.
29 *
30 * @category Controller
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\Utility\ValidatorUtility
59 */
60 protected $validate;
61
62 /**
63 * Initialisation
64 *
65 * @param array $typoscript TS configuration for this cObject
66 * @return void
67 */
68 public function initialize(array $typoscript) {
69 $this->typoscriptFactory = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Form\\Domain\\Factory\\TypoScriptFactory');
70 $this->localizationHandler = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Form\\Localization');
71 $this->requestHandler = $this->typoscriptFactory->setRequestHandler($typoscript);
72 $this->validate = $this->typoscriptFactory->setRules($typoscript);
73 $this->typoscript = $typoscript;
74 }
75
76 /**
77 * Renders the application defined cObject FORM
78 * which overrides the TYPO3 default cObject FORM
79 *
80 * First we make a COA_INT out of it, because it does not need to be cached
81 * Then we send a FORM_INT to the COA_INT
82 * When this is read, it will call the FORM class again.
83 *
84 * It simply calls execute because this function name is not really descriptive
85 * but is needed by the core of TYPO3
86 *
87 * @param string $typoScriptObjectName Name of the object
88 * @param array $typoScript TS configuration for this cObject
89 * @param string $typoScriptKey A string label used for the internal debugging tracking.
90 * @param \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer $contentObject reference
91 * @return string HTML output
92 */
93 public function cObjGetSingleExt($typoScriptObjectName, array $typoScript, $typoScriptKey, \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer $contentObject) {
94 $content = '';
95 if ($typoScriptObjectName === 'FORM') {
96 if ($contentObject->data['CType'] === 'mailform') {
97 $bodytext = $contentObject->data['bodytext'];
98 /** @var $typoScriptParser \TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser */
99 $typoScriptParser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\TypoScript\\Parser\\TypoScriptParser');
100 $typoScriptParser->parse($bodytext);
101 $mergedTypoScript = \TYPO3\CMS\Core\Utility\GeneralUtility::array_merge_recursive_overrule((array) $typoScriptParser->setup, (array) $typoScript);
102 // Disables content elements since TypoScript is handled that could contain insecure settings:
103 $mergedTypoScript[\TYPO3\CMS\Form\Domain\Factory\TypoScriptFactory::PROPERTY_DisableContentElement] = TRUE;
104 }
105 $newTypoScript = array(
106 '10' => 'FORM_INT',
107 '10.' => $mergedTypoScript
108 );
109 $content = $contentObject->COBJ_ARRAY($newTypoScript, 'INT');
110 // Only apply stdWrap to TypoScript that was NOT created by the wizard:
111 if (isset($typoScript['stdWrap.'])) {
112 $content = $contentObject->stdWrap($content, $typoScript['stdWrap.']);
113 }
114 } elseif ($typoScriptObjectName === 'FORM_INT') {
115 $this->initialize($typoScript);
116 $content = $this->execute();
117 }
118 return $content;
119 }
120
121 /**
122 * Build the models and views and renders the output from the views
123 *
124 * @return string HTML Output
125 */
126 public function execute() {
127 // Form
128 if ($this->showForm()) {
129 $content = $this->renderForm();
130 } elseif ($this->showConfirmation()) {
131 $content = $this->renderConfirmation();
132 } else {
133 $content = $this->doPostProcessing();
134 }
135 return $content;
136 }
137
138 /**
139 * Check if the form needs to be displayed
140 *
141 * This is TRUE when nothing has been submitted,
142 * when data has been submitted but the validation rules do not fit
143 * or when the user returns from the confirmation screen.
144 *
145 * @return boolean TRUE when form needs to be shown
146 */
147 protected function showForm() {
148 $show = FALSE;
149 $submittedByPrefix = $this->requestHandler->getByMethod();
150 if ($submittedByPrefix === NULL || !empty($submittedByPrefix) && !$this->validate->isValid() || !empty($submittedByPrefix) && $this->validate->isValid() && $this->requestHandler->getPost('confirmation-false', NULL) !== NULL) {
151 $show = TRUE;
152 }
153 return $show;
154 }
155
156 /**
157 * Render the form
158 *
159 * @return string The form HTML
160 */
161 protected function renderForm() {
162 $this->requestHandler->destroySession();
163 $form = $this->typoscriptFactory->buildModelFromTyposcript($this->typoscript);
164 /** @var $view \TYPO3\CMS\Form\View\Form\FormView */
165 $view = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Form\\View\\Form\\FormView', $form);
166 return $view->get();
167 }
168
169 /**
170 * Check if the confirmation message needs to be displayed
171 *
172 * This is TRUE when data has been submitted,
173 * the validation rules are valid,
174 * the confirmation screen has been configured in TypoScript
175 * and the confirmation screen has not been submitted
176 *
177 * @return boolean TRUE when confirmation screen needs to be shown
178 */
179 protected function showConfirmation() {
180 $show = FALSE;
181 if (isset($this->typoscript['confirmation']) && $this->typoscript['confirmation'] == 1 && $this->requestHandler->getPost('confirmation-true', NULL) === NULL) {
182 $show = TRUE;
183 }
184 return $show;
185 }
186
187 /**
188 * Render the confirmation screen
189 *
190 * Stores the submitted data in a session
191 *
192 * @return string The confirmation screen HTML
193 */
194 protected function renderConfirmation() {
195 $form = $this->typoscriptFactory->buildModelFromTyposcript($this->typoscript);
196 $this->requestHandler->storeSession();
197 $confirmationTyposcript = array();
198 if (isset($this->typoscript['confirmation.'])) {
199 $confirmationTyposcript = $this->typoscript['confirmation.'];
200 }
201 /** @var $view \TYPO3\CMS\Form\View\Confirmation\ConfirmationView */
202 $view = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Form\\View\\Confirmation\\ConfirmationView', $form, $confirmationTyposcript);
203 return $view->get();
204 }
205
206 /**
207 * Do the post processing
208 *
209 * Destroys the session because it is not needed anymore
210 *
211 * @return string The post processing HTML
212 */
213 protected function doPostProcessing() {
214 $form = $this->typoscriptFactory->buildModelFromTyposcript($this->typoscript);
215 $postProcessorTypoScript = array();
216 if (isset($this->typoscript['postProcessor.'])) {
217 $postProcessorTypoScript = $this->typoscript['postProcessor.'];
218 }
219 /** @var $postProcessor \TYPO3\CMS\Form\PostProcess\PostProcessor */
220 $postProcessor = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Form\\PostProcess\\PostProcessor', $form, $postProcessorTypoScript);
221 $content = $postProcessor->process();
222 $this->requestHandler->destroySession();
223 return $content;
224 }
225
226 }
227
228
229 ?>