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