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