[TASK] Merge submodule dbal into core
[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\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 (
151 $submittedByPrefix === NULL ||
152 !empty($submittedByPrefix) && !$this->validate->isValid() ||
153 !empty($submittedByPrefix) && $this->validate->isValid() &&
154 $this->requestHandler->getPost('confirmation-false', NULL) !== NULL
155 ) {
156 $show = TRUE;
157 }
158 return $show;
159 }
160
161 /**
162 * Render the form
163 *
164 * @return string The form HTML
165 */
166 protected function renderForm() {
167 $this->requestHandler->destroySession();
168 $form = $this->typoscriptFactory->buildModelFromTyposcript($this->typoscript);
169 /** @var $view \TYPO3\CMS\Form\View\Form\FormView */
170 $view = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Form\\View\\Form\\FormView', $form);
171 return $view->get();
172 }
173
174 /**
175 * Check if the confirmation message needs to be displayed
176 *
177 * This is TRUE when data has been submitted,
178 * the validation rules are valid,
179 * the confirmation screen has been configured in TypoScript
180 * and the confirmation screen has not been submitted
181 *
182 * @return boolean TRUE when confirmation screen needs to be shown
183 */
184 protected function showConfirmation() {
185 $show = FALSE;
186 if (isset($this->typoscript['confirmation']) && $this->typoscript['confirmation'] == 1 && $this->requestHandler->getPost('confirmation-true', NULL) === NULL) {
187 $show = TRUE;
188 }
189 return $show;
190 }
191
192 /**
193 * Render the confirmation screen
194 *
195 * Stores the submitted data in a session
196 *
197 * @return string The confirmation screen HTML
198 */
199 protected function renderConfirmation() {
200 $form = $this->typoscriptFactory->buildModelFromTyposcript($this->typoscript);
201 $this->requestHandler->storeSession();
202 $confirmationTyposcript = array();
203 if (isset($this->typoscript['confirmation.'])) {
204 $confirmationTyposcript = $this->typoscript['confirmation.'];
205 }
206 /** @var $view \TYPO3\CMS\Form\View\Confirmation\ConfirmationView */
207 $view = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Form\\View\\Confirmation\\ConfirmationView', $form, $confirmationTyposcript);
208 return $view->get();
209 }
210
211 /**
212 * Do the post processing
213 *
214 * Destroys the session because it is not needed anymore
215 *
216 * @return string The post processing HTML
217 */
218 protected function doPostProcessing() {
219 $form = $this->typoscriptFactory->buildModelFromTyposcript($this->typoscript);
220 $postProcessorTypoScript = array();
221 if (isset($this->typoscript['postProcessor.'])) {
222 $postProcessorTypoScript = $this->typoscript['postProcessor.'];
223 }
224 /** @var $postProcessor \TYPO3\CMS\Form\PostProcess\PostProcessor */
225 $postProcessor = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Form\\PostProcess\\PostProcessor', $form, $postProcessorTypoScript);
226 $content = $postProcessor->process();
227 $this->requestHandler->destroySession();
228 return $content;
229 }
230
231 }
232
233 ?>