2 /***************************************************************
5 * (c) 2008 Patrick Broens (patrick@patrickbroens.nl)
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
26 * Main controller for Forms. All requests come through this class
27 * and are routed to the model and view layers for processing.
29 * @category Controller
32 * @author Patrick Broens <patrick@patrickbroens.nl>
34 class tx_form_Controller_Form
{
37 * The TypoScript array
41 protected $typoscript = array();
44 * @var tx_form_Domain_Factory_Typoscript
46 protected $typoscriptFactory;
49 * @var tx_form_System_Localization
51 protected $localizationHandler;
54 * @var tx_form_System_Request
56 protected $requestHandler;
59 * @var tx_form_System_Validate
66 * @param array $typoscript TS configuration for this cObject
69 public function initialize(array $typoscript) {
70 t3lib_div
::makeInstance(
71 'tx_form_System_Localization',
72 'LLL:EXT:form/Resources/Private/Language/locallang_controller.xml'
75 $this->typoscriptFactory
= t3lib_div
::makeInstance('tx_form_Domain_Factory_Typoscript');
76 $this->localizationHandler
= t3lib_div
::makeInstance('tx_form_System_Localization');
77 $this->requestHandler
= $this->typoscriptFactory
->setRequestHandler($typoscript);
78 $this->validate
= $this->typoscriptFactory
->setRules($typoscript);
80 $this->typoscript
= $typoscript;
84 * Renders the application defined cObject FORM
85 * which overrides the TYPO3 default cObject FORM
87 * First we make a COA_INT out of it, because it does not need to be cached
88 * Then we send a FORM_INT to the COA_INT
89 * When this is read, it will call the FORM class again.
91 * It simply calls execute because this function name is not really descriptive
92 * but is needed by the core of TYPO3
94 * @param string $typoscriptObjectName Name of the object
95 * @param array $typoscript TS configuration for this cObject
96 * @param string $typoscriptKey A string label used for the internal debugging tracking.
97 * @param tslib_cObj $contentObject reference
98 * @return string HTML output
100 public function cObjGetSingleExt(
101 $typoscriptObjectName,
104 tslib_cObj
$contentObject
108 if ($typoscriptObjectName === 'FORM') {
109 if ($contentObject->data
['CType'] === 'mailform') {
110 $bodytext = $contentObject->data
['bodytext'];
111 /** @var $typoScriptParser t3lib_tsparser */
112 $typoScriptParser = t3lib_div
::makeInstance('t3lib_tsparser');
113 $typoScriptParser->parse($bodytext);
114 $typoscript = t3lib_div
::array_merge_recursive_overrule(
115 (array) $typoScriptParser->setup
,
119 $newTyposcript = array(
121 '10.' => $typoscript,
123 $content = $contentObject->COBJ_ARRAY($newTyposcript, 'INT');
124 } elseif ($typoscriptObjectName == 'FORM_INT') {
125 $this->initialize($typoscript);
126 $content = $this->execute();
133 * Build the models and views and renders the output from the views
135 * @return string HTML Output
137 public function execute() {
139 if ($this->showForm()) {
140 $content = $this->renderForm();
142 // Confirmation screen
143 } elseif ($this->showConfirmation()) {
144 $content = $this->renderConfirmation();
146 // We need the post processing
148 $content = $this->doPostProcessing();
155 * Check if the form needs to be displayed
157 * This is TRUE when nothing has been submitted,
158 * when data has been submitted but the validation rules do not fit
159 * or when the user returns from the confirmation screen.
161 * @return boolean TRUE when form needs to be shown
163 protected function showForm() {
166 $submittedByPrefix = $this->requestHandler
->getByMethod();
169 // Nothing has been submitted
170 $submittedByPrefix === NULL ||
172 // Submitted but not valid
174 !empty($submittedByPrefix) &&
175 !$this->validate
->isValid()
178 // Submitted, valid, but not confirmed
180 !empty($submittedByPrefix) &&
181 $this->validate
->isValid() &&
182 $this->requestHandler
->getPost('confirmation') === $this->localizationHandler
->getLocalLanguageLabel('tx_form_view_confirmation.donotconfirm')
194 * @return string The form HTML
196 protected function renderForm() {
197 $this->requestHandler
->destroySession();
199 $form = $this->typoscriptFactory
->buildModelFromTyposcript($this->typoscript
);
201 /** @var $view tx_form_View_Form */
202 $view = t3lib_div
::makeInstance('tx_form_View_Form', $form);
208 * Check if the confirmation message needs to be displayed
210 * This is TRUE when data has been submitted,
211 * the validation rules are valid,
212 * the confirmation screen has been configured in TypoScript
213 * and the confirmation screen has not been submitted
215 * @return boolean TRUE when confirmation screen needs to be shown
217 protected function showConfirmation() {
221 isset($this->typoscript
['confirmation']) &&
222 $this->typoscript
['confirmation'] == 1 &&
223 $this->requestHandler
->getPost('confirmation') === NULL
232 * Render the confirmation screen
234 * Stores the submitted data in a session
236 * @return string The confirmation screen HTML
238 protected function renderConfirmation() {
239 $form = $this->typoscriptFactory
->buildModelFromTyposcript($this->typoscript
);
241 $this->requestHandler
->storeSession();
243 $confirmationTyposcript = array();
244 if (isset($this->typoscript
['confirmation.'])) {
245 $confirmationTyposcript = $this->typoscript
['confirmation.'];
248 /** @var $view tx_form_View_Confirmation */
249 $view = t3lib_div
::makeInstance(
250 'tx_form_View_Confirmation',
252 $confirmationTyposcript
259 * Do the post processing
261 * Destroys the session because it is not needed anymore
263 * @return string The post processing HTML
265 protected function doPostProcessing() {
266 $form = $this->typoscriptFactory
->buildModelFromTyposcript($this->typoscript
);
268 $postProcessorTypoScript = array();
269 if (isset($this->typoscript
['postProcessor.'])) {
270 $postProcessorTypoScript = $this->typoscript
['postProcessor.'];
273 /** @var $postProcessor tx_form_System_Postprocessor */
274 $postProcessor = t3lib_div
::makeInstance(
275 'tx_form_System_Postprocessor',
277 $postProcessorTypoScript
279 $content = $postProcessor->process();
280 $this->requestHandler
->destroySession();