[TASK] BACK_PATH RecordList and friends
[Packages/TYPO3.CMS.git] / typo3 / sysext / compatibility6 / Classes / Controller / Wizard / FormsController.php
1 <?php
2 namespace TYPO3\CMS\Compatibility6\Controller\Wizard;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Backend\Utility\IconUtility;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * API comments:
23 *
24 * The form wizard can help you to create forms - it allows you to create almost any kind of HTML form elements and in any order and amount.
25 *
26 * The format for the resulting configuration code can be either a line-based configuration. That can look like this:
27 *
28 * Your name: | *name=input | (input your name here!)
29 * Your Email: | *email=input
30 * Your address: | address=textarea,40,10
31 * Your Haircolor: | hair=radio |
32 * upload | attachment=file
33 * | quoted_printable=hidden | 0
34 * | formtype_mail=submit | Send form
35 * | html_enabled=hidden
36 * | subject=hidden | This is the subject
37 *
38 *
39 * Alternatively it can be XML. The same configuration from above looks like this in XML:
40 *
41 * <T3FormWizard>
42 * <n2>
43 * <type>input</type>
44 * <label>Your name:</label>
45 * <required>1</required>
46 * <fieldname>name</fieldname>
47 * <size></size>
48 * <max></max>
49 * <default>(input your name here!)</default>
50 * </n2>
51 * <n4>
52 * <type>input</type>
53 * <label>Your Email:</label>
54 * <required>1</required>
55 * <fieldname>email</fieldname>
56 * <size></size>
57 * <max></max>
58 * <default></default>
59 * </n4>
60 * <n6>
61 * <type>textarea</type>
62 * <label>Your address:</label>
63 * <fieldname>address</fieldname>
64 * <cols>40</cols>
65 * <rows>10</rows>
66 * <default></default>
67 * </n6>
68 * <n8>
69 * <type>radio</type>
70 * <label>Your Haircolor:</label>
71 * <fieldname>hair</fieldname>
72 * <options></options>
73 * </n8>
74 * <n10>
75 * <type>file</type>
76 * <label>upload</label>
77 * <fieldname>attachment</fieldname>
78 * <size></size>
79 * </n10>
80 * <n12>
81 * <type>hidden</type>
82 * <label></label>
83 * <fieldname>quoted_printable</fieldname>
84 * <default>0</default>
85 * </n12>
86 * <n2000>
87 * <fieldname>formtype_mail</fieldname>
88 * <type>submit</type>
89 * <default>Send form</default>
90 * </n2000>
91 * <n2002>
92 * <fieldname>html_enabled</fieldname>
93 * <type>hidden</type>
94 * </n2002>
95 * <n2004>
96 * <fieldname>subject</fieldname>
97 * <type>hidden</type>
98 * <default>This is the subject</default>
99 * </n2004>
100 * <n20>
101 * <content></content>
102 * </n20>
103 * </T3FormWizard>
104 *
105 *
106 * The XML/phpArray structure is the internal format of the wizard.
107 */
108 class FormsController extends \TYPO3\CMS\Backend\Controller\Wizard\AbstractWizardController {
109
110 /**
111 * document template object
112 *
113 * @var \TYPO3\CMS\Backend\Template\DocumentTemplate
114 */
115 public $doc;
116
117 /**
118 * Content accumulation for the module.
119 *
120 * @var string
121 */
122 public $content;
123
124 /**
125 * Used to numerate attachments automatically.
126 *
127 * @var int
128 */
129 public $attachmentCounter = 0;
130
131 /**
132 * If set, the string version of the content is interpreted/written as XML instead of
133 * the original linebased kind. This variable still needs binding to the wizard parameters
134 * - but support is ready!
135 *
136 * @var int
137 */
138 public $xmlStorage = 0;
139
140 /**
141 * Wizard parameters, coming from TCEforms linking to the wizard.
142 *
143 * @var array
144 */
145 public $P;
146
147 /**
148 * The array which is constantly submitted by the multidimensional form of this wizard.
149 *
150 * @var array
151 */
152 public $FORMCFG;
153
154 /**
155 * Indicates if the form is of a dedicated type, like "formtype_mail" (for tt_content element "Form")
156 *
157 * @var string
158 */
159 public $special;
160
161 /**
162 * Constructor
163 */
164 public function __construct() {
165 $this->getLanguageService()->includeLLFile('EXT:lang/locallang_wizards.xlf');
166 $GLOBALS['SOBE'] = $this;
167
168 $this->init();
169 }
170
171 /**
172 * Initialization the class
173 *
174 * @return void
175 */
176 protected function init() {
177 // GPvars:
178 $this->P = GeneralUtility::_GP('P');
179 $this->special = GeneralUtility::_GP('special');
180 $this->FORMCFG = GeneralUtility::_GP('FORMCFG');
181 // Setting options:
182 $this->xmlStorage = $this->P['params']['xmlOutput'];
183 // Document template object:
184 $this->doc = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Template\DocumentTemplate::class);
185 $this->doc->setModuleTemplate('EXT:compatibility6/Resources/Private/Templates/Wizard/Forms.html');
186 // Setting form tag:
187 list($rUri) = explode('#', GeneralUtility::getIndpEnv('REQUEST_URI'));
188 $this->doc->form = '<form action="' . htmlspecialchars($rUri) . '" method="post" name="wizardForm">';
189 }
190
191 /**
192 * Main function for rendering the form wizard HTML
193 *
194 * @return void
195 */
196 public function main() {
197 if ($this->P['table'] && $this->P['field'] && $this->P['uid']) {
198 $this->content .= $this->doc->section($this->getLanguageService()->getLL('forms_title'), $this->formsWizard(), 0, 1);
199 } else {
200 $this->content .= $this->doc->section($this->getLanguageService()->getLL('forms_title'), '<span class="text-danger">' . $this->getLanguageService()->getLL('table_noData', 1) . '</span>', 0, 1);
201 }
202 // Setting up the buttons and markers for docheader
203 $docHeaderButtons = $this->getButtons();
204 $markers['CSH'] = $docHeaderButtons['csh'];
205 $markers['CONTENT'] = $this->content;
206 // Build the <body> for the module
207 $this->content = $this->doc->startPage('Form Wizard');
208 $this->content .= $this->doc->moduleBody($this->pageinfo, $docHeaderButtons, $markers);
209 $this->content .= $this->doc->endPage();
210 $this->content = $this->doc->insertStylesAndJS($this->content);
211 }
212
213 /**
214 * Outputting the accumulated content to screen
215 *
216 * @return void
217 */
218 public function printContent() {
219 echo $this->content;
220 }
221
222 /**
223 * Create the panel of buttons for submitting the form or otherwise perform operations.
224 *
225 * @return array All available buttons as an assoc. array
226 */
227 protected function getButtons() {
228 $buttons = array(
229 'csh' => '',
230 'csh_buttons' => '',
231 'close' => '',
232 'save' => '',
233 'save_close' => '',
234 'reload' => ''
235 );
236 if ($this->P['table'] && $this->P['field'] && $this->P['uid']) {
237 // CSH
238 $buttons['csh'] = BackendUtility::cshItem('xMOD_csh_corebe', 'wizard_forms_wiz');
239 // CSH Buttons
240 $buttons['csh_buttons'] = BackendUtility::cshItem('xMOD_csh_corebe', 'wizard_forms_wiz_buttons');
241 // Close
242 $buttons['close'] = '<a href="#" onclick="' . htmlspecialchars(('jumpToUrl(unescape(\'' . rawurlencode(GeneralUtility::sanitizeLocalUrl($this->P['returnUrl'])) . '\')); return false;')) . '">' . IconUtility::getSpriteIcon('actions-document-close', array('title' => $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:rm.closeDoc', TRUE))) . '</a>';
243 // Save
244 $buttons['save'] = '<button class="c-inputButton" name="savedok">' . IconUtility::getSpriteIcon('actions-document-save', array('title' => $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:rm.saveDoc', TRUE))) . '</button>';
245 // Save & Close
246 $buttons['save_close'] = '<button class="c-inputButton" name="saveandclosedok">' . IconUtility::getSpriteIcon('actions-document-save-close', array('title' => $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:rm.saveCloseDoc', TRUE))) . '</button>';
247 // Reload
248 $buttons['reload'] = '<button class="c-inputButton" name="_refresh">' . IconUtility::getSpriteIcon('actions-system-refresh', array('title' => $this->getLanguageService()->getLL('forms_refresh', TRUE))) . '</button>';
249 }
250 return $buttons;
251 }
252
253 /**
254 * Draws the form wizard content
255 *
256 * @return string HTML content for the form.
257 */
258 public function formsWizard() {
259 if (!$this->checkEditAccess($this->P['table'], $this->P['uid'])) {
260 throw new \RuntimeException('Wizard Error: No access', 1385807526);
261 }
262 // First, check the references by selecting the record:
263 $row = BackendUtility::getRecord($this->P['table'], $this->P['uid']);
264 if (!is_array($row)) {
265 throw new \RuntimeException('Wizard Error: No reference to record', 1294587124);
266 }
267 // This will get the content of the form configuration code field to us - possibly
268 // cleaned up, saved to database etc. if the form has been submitted in the meantime.
269 $formCfgArray = $this->getConfigCode($row);
270 // Generation of the Form Wizards HTML code:
271 $content = $this->getFormHTML($formCfgArray, $row);
272 // Return content:
273 return $content;
274 }
275
276 /****************************
277 *
278 * Helper functions
279 *
280 ***************************/
281 /**
282 * Will get and return the configuration code string
283 * Will also save (and possibly redirect/exit) the content if a save button has been pressed
284 *
285 * @param array $row Current parent record row (passed by value!)
286 * @return array Configuration Array
287 * @access private
288 */
289 public function getConfigCode(&$row) {
290 // If some data has been submitted, then construct
291 if (isset($this->FORMCFG['c'])) {
292 // Process incoming:
293 $this->changeFunc();
294 // Convert to string (either line based or XML):
295 if ($this->xmlStorage) {
296 // Convert the input array to XML:
297 $bodyText = GeneralUtility::array2xml_cs($this->FORMCFG['c'], 'T3FormWizard');
298 // Setting cfgArr directly from the input:
299 $cfgArr = $this->FORMCFG['c'];
300 } else {
301 // Convert the input array to a string of configuration code:
302 $bodyText = $this->cfgArray2CfgString($this->FORMCFG['c']);
303 // Create cfgArr from the string based configuration - that way it is cleaned
304 // up and any incompatibilities will be removed!
305 $cfgArr = $this->cfgString2CfgArray($bodyText);
306 }
307 // If a save button has been pressed, then save the new field content:
308 if ($_POST['savedok_x'] || $_POST['saveandclosedok_x']) {
309 // Make TCEmain object:
310 $tce = GeneralUtility::makeInstance(\TYPO3\CMS\Core\DataHandling\DataHandler::class);
311 $tce->stripslashes_values = 0;
312 // Put content into the data array:
313 $data = array();
314 $data[$this->P['table']][$this->P['uid']][$this->P['field']] = $bodyText;
315 if ($this->special == 'formtype_mail') {
316 $data[$this->P['table']][$this->P['uid']]['subheader'] = $this->FORMCFG['recipient'];
317 }
318 // Perform the update:
319 $tce->start($data, array());
320 $tce->process_datamap();
321 // Re-load the record content:
322 $row = BackendUtility::getRecord($this->P['table'], $this->P['uid']);
323 // If the save/close button was pressed, then redirect the screen:
324 if ($_POST['saveandclosedok_x']) {
325 \TYPO3\CMS\Core\Utility\HttpUtility::redirect(GeneralUtility::sanitizeLocalUrl($this->P['returnUrl']));
326 }
327 }
328 } else {
329 // If nothing has been submitted, load the $bodyText variable from the selected database row:
330 if ($this->xmlStorage) {
331 $cfgArr = GeneralUtility::xml2array($row[$this->P['field']]);
332 } else {
333 // Regular linebased form configuration:
334 $cfgArr = $this->cfgString2CfgArray($row[$this->P['field']]);
335 }
336 $cfgArr = is_array($cfgArr) ? $cfgArr : array();
337 }
338 // Return configuration code:
339 return $cfgArr;
340 }
341
342 /**
343 * Creates the HTML for the Form Wizard:
344 *
345 * @param string $formCfgArray Form config array
346 * @param array $row Current parent record array
347 * @return string HTML for the form wizard
348 * @access private
349 */
350 public function getFormHTML($formCfgArray, $row) {
351 // Initialize variables:
352 $specParts = array();
353 $hiddenFields = array();
354 $tRows = array();
355 // Set header row:
356 $cells = array(
357 $this->getLanguageService()->getLL('forms_preview', TRUE) . ':',
358 $this->getLanguageService()->getLL('forms_element', TRUE) . ':',
359 $this->getLanguageService()->getLL('forms_config', TRUE) . ':'
360 );
361 $tRows[] = '
362 <tr class="bgColor2" id="typo3-formWizardHeader">
363 <td>&nbsp;</td>
364 <td><strong>' . implode('</strong></td>
365 <td><strong>', $cells) . '</strong></td>
366 </tr>';
367 // Traverse the number of form elements:
368 $k = 0;
369 foreach ($formCfgArray as $confData) {
370 // Initialize:
371 $cells = array();
372 // If there is a configuration line which is active, then render it:
373 if (!isset($confData['comment'])) {
374 // Special parts:
375 if ($this->special == 'formtype_mail' && GeneralUtility::inList('formtype_mail,subject,html_enabled', $confData['fieldname'])) {
376 $specParts[$confData['fieldname']] = $confData['default'];
377 } else {
378 // Render title/field preview COLUMN
379 $cells[] = $confData['type'] != 'hidden' ? '<strong>' . htmlspecialchars($confData['label']) . '</strong>' : '';
380 // Render general type/title COLUMN:
381 $temp_cells = array();
382 // Field type selector:
383 $opt = array();
384 $opt[] = '<option value=""></option>';
385 $types = explode(',', 'input,textarea,select,check,radio,password,file,hidden,submit,property,label');
386 foreach ($types as $t) {
387 $opt[] = '
388 <option value="' . $t . '"' . ($confData['type'] == $t ? ' selected="selected"' : '') . '>' . $this->getLanguageService()->getLL(('forms_type_' . $t), TRUE) . '</option>';
389 }
390 $temp_cells[$this->getLanguageService()->getLL('forms_type')] = '
391 <select name="FORMCFG[c][' . ($k + 1) * 2 . '][type]">
392 ' . implode('
393 ', $opt) . '
394 </select>';
395 // Title field:
396 if (!GeneralUtility::inList('hidden,submit', $confData['type'])) {
397 $temp_cells[$this->getLanguageService()->getLL('forms_label')] = '<input type="text"' . $this->doc->formWidth(15) . ' name="FORMCFG[c][' . ($k + 1) * 2 . '][label]" value="' . htmlspecialchars($confData['label']) . '" />';
398 }
399 // Required checkbox:
400 if (!GeneralUtility::inList('check,hidden,submit,label', $confData['type'])) {
401 $temp_cells[$this->getLanguageService()->getLL('forms_required')] = '<input type="checkbox" name="FORMCFG[c][' . ($k + 1) * 2 . '][required]" value="1"' . ($confData['required'] ? ' checked="checked"' : '') . ' title="' . $this->getLanguageService()->getLL('forms_required', TRUE) . '" />';
402 }
403 // Put sub-items together into table cell:
404 $cells[] = $this->formatCells($temp_cells);
405 // Render specific field configuration COLUMN:
406 $temp_cells = array();
407 // Fieldname
408 if ($this->special == 'formtype_mail' && $confData['type'] == 'file') {
409 $confData['fieldname'] = 'attachment' . ++$this->attachmentCounter;
410 }
411 if (!GeneralUtility::inList('label', $confData['type'])) {
412 $temp_cells[$this->getLanguageService()->getLL('forms_fieldName')] = '<input type="text"' . $this->doc->formWidth(10) . ' name="FORMCFG[c][' . ($k + 1) * 2 . '][fieldname]" value="' . htmlspecialchars($confData['fieldname']) . '" title="' . $this->getLanguageService()->getLL('forms_fieldName', TRUE) . '" />';
413 }
414 // Field configuration depending on the fields type:
415 switch ((string)$confData['type']) {
416 case 'textarea':
417 $temp_cells[$this->getLanguageService()->getLL('forms_cols')] = '<input type="text"' . $this->doc->formWidth(5) . ' name="FORMCFG[c][' . ($k + 1) * 2 . '][cols]" value="' . htmlspecialchars($confData['cols']) . '" title="' . $this->getLanguageService()->getLL('forms_cols', TRUE) . '" />';
418 $temp_cells[$this->getLanguageService()->getLL('forms_rows')] = '<input type="text"' . $this->doc->formWidth(5) . ' name="FORMCFG[c][' . ($k + 1) * 2 . '][rows]" value="' . htmlspecialchars($confData['rows']) . '" title="' . $this->getLanguageService()->getLL('forms_rows', TRUE) . '" />';
419 $temp_cells[$this->getLanguageService()->getLL('forms_extra')] = '<input type="checkbox" name="FORMCFG[c][' . ($k + 1) * 2 . '][extra]" value="OFF"' . ($confData['extra'] == 'OFF' ? ' checked="checked"' : '') . ' title="' . $this->getLanguageService()->getLL('forms_extra', TRUE) . '" />';
420 break;
421 case 'input':
422
423 case 'password':
424 $temp_cells[$this->getLanguageService()->getLL('forms_size')] = '<input type="text"' . $this->doc->formWidth(5) . ' name="FORMCFG[c][' . ($k + 1) * 2 . '][size]" value="' . htmlspecialchars($confData['size']) . '" title="' . $this->getLanguageService()->getLL('forms_size', TRUE) . '" />';
425 $temp_cells[$this->getLanguageService()->getLL('forms_max')] = '<input type="text"' . $this->doc->formWidth(5) . ' name="FORMCFG[c][' . ($k + 1) * 2 . '][max]" value="' . htmlspecialchars($confData['max']) . '" title="' . $this->getLanguageService()->getLL('forms_max', TRUE) . '" />';
426 break;
427 case 'file':
428 $temp_cells[$this->getLanguageService()->getLL('forms_size')] = '<input type="text"' . $this->doc->formWidth(5) . ' name="FORMCFG[c][' . ($k + 1) * 2 . '][size]" value="' . htmlspecialchars($confData['size']) . '" title="' . $this->getLanguageService()->getLL('forms_size', TRUE) . '" />';
429 break;
430 case 'select':
431 $temp_cells[$this->getLanguageService()->getLL('forms_size')] = '<input type="text"' . $this->doc->formWidth(5) . ' name="FORMCFG[c][' . ($k + 1) * 2 . '][size]" value="' . htmlspecialchars($confData['size']) . '" title="' . $this->getLanguageService()->getLL('forms_size', TRUE) . '" />';
432 $temp_cells[$this->getLanguageService()->getLL('forms_autosize')] = '<input type="checkbox" name="FORMCFG[c][' . ($k + 1) * 2 . '][autosize]" value="1"' . ($confData['autosize'] ? ' checked="checked"' : '') . ' title="' . $this->getLanguageService()->getLL('forms_autosize', TRUE) . '" />';
433 $temp_cells[$this->getLanguageService()->getLL('forms_multiple')] = '<input type="checkbox" name="FORMCFG[c][' . ($k + 1) * 2 . '][multiple]" value="1"' . ($confData['multiple'] ? ' checked="checked"' : '') . ' title="' . $this->getLanguageService()->getLL('forms_multiple', TRUE) . '" />';
434 break;
435 }
436 // Field configuration depending on the fields type:
437 switch ((string)$confData['type']) {
438 case 'textarea':
439
440 case 'input':
441
442 case 'password':
443 if (trim($confData['specialEval']) !== '') {
444 $hiddenFields[] = '<input type="hidden" name="FORMCFG[c][' . ($k + 1) * 2 . '][specialEval]" value="' . htmlspecialchars($confData['specialEval']) . '" />';
445 }
446 break;
447 }
448 // Default data
449 if ($confData['type'] == 'select' || $confData['type'] == 'radio') {
450 $temp_cells[$this->getLanguageService()->getLL('forms_options')] = '<textarea ' . $this->doc->formWidth(15) . ' rows="4" name="FORMCFG[c][' . ($k + 1) * 2 . '][options]" title="' . $this->getLanguageService()->getLL('forms_options', TRUE) . '">' . htmlspecialchars($confData['default']) . '</textarea>';
451 } elseif ($confData['type'] == 'check') {
452 $temp_cells[$this->getLanguageService()->getLL('forms_checked')] = '<input type="checkbox" name="FORMCFG[c][' . ($k + 1) * 2 . '][default]" value="1"' . (trim($confData['default']) ? ' checked="checked"' : '') . ' title="' . $this->getLanguageService()->getLL('forms_checked', TRUE) . '" />';
453 } elseif ($confData['type'] && $confData['type'] != 'file') {
454 $temp_cells[$this->getLanguageService()->getLL('forms_default')] = '<input type="text"' . $this->doc->formWidth(15) . ' name="FORMCFG[c][' . ($k + 1) * 2 . '][default]" value="' . htmlspecialchars($confData['default']) . '" title="' . $this->getLanguageService()->getLL('forms_default', TRUE) . '" />';
455 }
456 $cells[] = $confData['type'] ? $this->formatCells($temp_cells) : '';
457 // CTRL panel for an item (move up/down/around):
458 $ctrl = '';
459 $onClick = 'document.wizardForm.action+=\'#ANC_' . (($k + 1) * 2 - 2) . '\';';
460 $onClick = ' onclick="' . htmlspecialchars($onClick) . '"';
461 // @todo $inputStyle undefined
462 $brTag = $inputStyle ? '' : '<br />';
463 if ($k != 1) {
464 $ctrl .= '<button name="FORMCFG[row_top][' . ($k + 1) * 2 . ']"' . $onClick . '>' . IconUtility::getSpriteIcon('actions-move-to-top', array('title' => $this->getLanguageService()->getLL('table_top', TRUE))) . '</button>' . $brTag;
465 $ctrl .= '<button name="FORMCFG[row_up][' . ($k + 1) * 2 . ']"' . $onClick . '>' . IconUtility::getSpriteIcon('actions-move-up', array('title' => $this->getLanguageService()->getLL('table_up', TRUE))) . '</button>' . $brTag;
466 }
467 $ctrl .= '<button name="FORMCFG[row_remove][' . ($k + 1) * 2 . ']" ' . $onClick . '>' . IconUtility::getSpriteIcon('actions-edit-delete', array('title' => $this->getLanguageService()->getLL('table_removeRow', TRUE))) . '</button>' . $brTag;
468
469 if ($k != (count($formCfgArray)/2)) {
470 $ctrl .= '<button name="FORMCFG[row_down][' . ($k + 1) * 2 . ']"' . $onClick . '>' . IconUtility::getSpriteIcon('actions-move-down', array('title' => $this->getLanguageService()->getLL('table_down', TRUE))) . '</button>' . $brTag;
471 $ctrl .= '<button name="FORMCFG[row_bottom][' . ($k + 1) * 2 . ']"' . $onClick . '>' . IconUtility::getSpriteIcon('actions-move-to-bottom', array('title' => $this->getLanguageService()->getLL('table_bottom', TRUE))) . '</button>' . $brTag;
472 }
473
474 $ctrl .= '<button name="FORMCFG[row_add][' . ($k + 1) * 2 . ']"' . $onClick . ' title="' . $this->getLanguageService()->getLL('table_addRow', TRUE) . '">' . IconUtility::getSpriteIcon('actions-template-new') . '</button>' . $brTag;
475 $ctrl = '<span class="c-wizButtonsV">' . $ctrl . '</span>';
476 // Finally, put together the full row from the generated content above:
477 $bgC = $confData['type'] ? ' class="bgColor5"' : '';
478 $tRows[] = '
479 <tr' . $bgC . '>
480 <td><a name="ANC_' . ($k + 1) * 2 . '"></a>' . $ctrl . '</td>
481 <td class="bgColor4">' . implode('</td>
482 <td valign="top">', $cells) . '</td>
483 </tr>';
484 }
485 } else {
486 $hiddenFields[] = '<input type="hidden" name="FORMCFG[c][' . ($k + 1) * 2 . '][comment]" value="' . htmlspecialchars($confData['comment']) . '" />';
487 }
488 // Increment counter:
489 $k++;
490 }
491 // If the form is of the special type "formtype_mail" (used for tt_content elements):
492 if ($this->special == 'formtype_mail') {
493 // Blank spacer:
494 $tRows[] = '
495 <tr>
496 <td colspan="4">&nbsp;</td>
497 </tr>';
498 // Header:
499 $tRows[] = '
500 <tr>
501 <td colspan="2" class="bgColor2">&nbsp;</td>
502 <td colspan="2" class="bgColor2"><strong>' . $this->getLanguageService()->getLL('forms_special_eform', TRUE) . ':</strong>' . BackendUtility::cshItem('xMOD_csh_corebe', 'wizard_forms_wiz_formmail_info') . '</td>
503 </tr>';
504 // "FORM type":
505 $tRows[] = '
506 <tr class="bgColor5">
507 <td>&nbsp;</td>
508 <td class="bgColor4">&nbsp;</td>
509 <td>' . $this->getLanguageService()->getLL('forms_eform_formtype_mail', TRUE) . ':</td>
510 <td>
511 <input type="hidden" name="FORMCFG[c][' . 1000 * 2 . '][fieldname]" value="formtype_mail" />
512 <input type="hidden" name="FORMCFG[c][' . 1000 * 2 . '][type]" value="submit" />
513 <input type="text"' . $this->doc->formWidth(15) . ' name="FORMCFG[c][' . 1000 * 2 . '][default]" value="' . htmlspecialchars($specParts['formtype_mail']) . '" />
514 </td>
515 </tr>';
516 // "Send HTML mail":
517 $tRows[] = '
518 <tr class="bgColor5">
519 <td>&nbsp;</td>
520 <td class="bgColor4">&nbsp;</td>
521 <td>' . $this->getLanguageService()->getLL('forms_eform_html_enabled', TRUE) . ':</td>
522 <td>
523 <input type="hidden" name="FORMCFG[c][' . 1001 * 2 . '][fieldname]" value="html_enabled" />
524 <input type="hidden" name="FORMCFG[c][' . 1001 * 2 . '][type]" value="hidden" />
525 <input type="checkbox" name="FORMCFG[c][' . 1001 * 2 . '][default]" value="1"' . ($specParts['html_enabled'] ? ' checked="checked"' : '') . ' />
526 </td>
527 </tr>';
528 // "Subject":
529 $tRows[] = '
530 <tr class="bgColor5">
531 <td>&nbsp;</td>
532 <td class="bgColor4">&nbsp;</td>
533 <td>' . $this->getLanguageService()->getLL('forms_eform_subject', TRUE) . ':</td>
534 <td>
535 <input type="hidden" name="FORMCFG[c][' . 1002 * 2 . '][fieldname]" value="subject" />
536 <input type="hidden" name="FORMCFG[c][' . 1002 * 2 . '][type]" value="hidden" />
537 <input type="text"' . $this->doc->formWidth(15) . ' name="FORMCFG[c][' . 1002 * 2 . '][default]" value="' . htmlspecialchars($specParts['subject']) . '" />
538 </td>
539 </tr>';
540 // Recipient:
541 $tRows[] = '
542 <tr class="bgColor5">
543 <td>&nbsp;</td>
544 <td class="bgColor4">&nbsp;</td>
545 <td>' . $this->getLanguageService()->getLL('forms_eform_recipient', TRUE) . ':</td>
546 <td>
547 <input type="text"' . $this->doc->formWidth(15) . ' name="FORMCFG[recipient]" value="' . htmlspecialchars($row['subheader']) . '" />
548 </td>
549 </tr>';
550 }
551 $content = '';
552 // Implode all table rows into a string, wrapped in table tags.
553 $content .= '
554
555 <!--
556 Form wizard
557 -->
558 <table border="0" cellpadding="1" cellspacing="1" id="typo3-formwizard">
559 ' . implode('', $tRows) . '
560 </table>';
561 // Add hidden fields:
562 $content .= implode('', $hiddenFields);
563 // Return content:
564 return $content;
565 }
566
567 /**
568 * Detects if a control button (up/down/around/delete) has been pressed for an item and accordingly it will manipulate the internal FORMCFG array
569 *
570 * @return void
571 * @access private
572 */
573 public function changeFunc() {
574 if ($this->FORMCFG['row_remove']) {
575 $kk = key($this->FORMCFG['row_remove']);
576 $cmd = 'row_remove';
577 } elseif ($this->FORMCFG['row_add']) {
578 $kk = key($this->FORMCFG['row_add']);
579 $cmd = 'row_add';
580 } elseif ($this->FORMCFG['row_top']) {
581 $kk = key($this->FORMCFG['row_top']);
582 $cmd = 'row_top';
583 } elseif ($this->FORMCFG['row_bottom']) {
584 $kk = key($this->FORMCFG['row_bottom']);
585 $cmd = 'row_bottom';
586 } elseif ($this->FORMCFG['row_up']) {
587 $kk = key($this->FORMCFG['row_up']);
588 $cmd = 'row_up';
589 } elseif ($this->FORMCFG['row_down']) {
590 $kk = key($this->FORMCFG['row_down']);
591 $cmd = 'row_down';
592 }
593 if ($cmd && \TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($kk)) {
594 if (substr($cmd, 0, 4) == 'row_') {
595 switch ($cmd) {
596 case 'row_remove':
597 unset($this->FORMCFG['c'][$kk]);
598 break;
599 case 'row_add':
600 $this->FORMCFG['c'][$kk + 1] = array();
601 break;
602 case 'row_top':
603 $this->FORMCFG['c'][1] = $this->FORMCFG['c'][$kk];
604 unset($this->FORMCFG['c'][$kk]);
605 break;
606 case 'row_bottom':
607 $this->FORMCFG['c'][1000000] = $this->FORMCFG['c'][$kk];
608 unset($this->FORMCFG['c'][$kk]);
609 break;
610 case 'row_up':
611 $this->FORMCFG['c'][$kk - 3] = $this->FORMCFG['c'][$kk];
612 unset($this->FORMCFG['c'][$kk]);
613 break;
614 case 'row_down':
615 $this->FORMCFG['c'][$kk + 3] = $this->FORMCFG['c'][$kk];
616 unset($this->FORMCFG['c'][$kk]);
617 break;
618 }
619 ksort($this->FORMCFG['c']);
620 }
621 }
622 }
623
624 /**
625 * Converts the input array to a configuration code string
626 *
627 * @param array $cfgArr Array of form configuration (follows the input structure from the form wizard POST form)
628 * @return string The array converted into a string with line-based configuration.
629 * @see cfgString2CfgArray()
630 */
631 public function cfgArray2CfgString($cfgArr) {
632 // Initialize:
633 $inLines = array();
634 // Traverse the elements of the form wizard and transform the settings into configuration code.
635 foreach ($cfgArr as $vv) {
636 // If "content" is found, then just pass it over.
637 if ($vv['comment']) {
638 $inLines[] = trim($vv['comment']);
639 } else {
640 // Begin to put together the single-line configuration code of this field:
641 // Reset:
642 $thisLine = array();
643 // Set Label:
644 $thisLine[0] = str_replace('|', '', $vv['label']);
645 // Set Type:
646 if ($vv['type']) {
647 $thisLine[1] = ($vv['required'] ? '*' : '') . str_replace(',', '', (($vv['fieldname'] ? $vv['fieldname'] . '=' : '') . $vv['type']));
648 // Default:
649 $tArr = array('', '', '', '', '', '');
650 switch ((string)$vv['type']) {
651 case 'textarea':
652 if ((int)$vv['cols']) {
653 $tArr[0] = (int)$vv['cols'];
654 }
655 if ((int)$vv['rows']) {
656 $tArr[1] = (int)$vv['rows'];
657 }
658 if (trim($vv['extra'])) {
659 $tArr[2] = trim($vv['extra']);
660 }
661 if ($vv['specialEval'] !== '') {
662 // Preset blank default value so position 3 can get a value...
663 $thisLine[2] = '';
664 $thisLine[3] = $vv['specialEval'];
665 }
666 break;
667 case 'input':
668 case 'password':
669 if ((int)$vv['size']) {
670 $tArr[0] = (int)$vv['size'];
671 }
672 if ((int)$vv['max']) {
673 $tArr[1] = (int)$vv['max'];
674 }
675 if ($vv['specialEval'] !== '') {
676 // Preset blank default value so position 3 can get a value...
677 $thisLine[2] = '';
678 $thisLine[3] = $vv['specialEval'];
679 }
680 break;
681 case 'file':
682 if ((int)$vv['size']) {
683 $tArr[0] = (int)$vv['size'];
684 }
685 break;
686 case 'select':
687 if ((int)$vv['size']) {
688 $tArr[0] = (int)$vv['size'];
689 }
690 if ($vv['autosize']) {
691 $tArr[0] = 'auto';
692 }
693 if ($vv['multiple']) {
694 $tArr[1] = 'm';
695 }
696 break;
697 }
698 $tArr = $this->cleanT($tArr);
699 if (!empty($tArr)) {
700 $thisLine[1] .= ',' . implode(',', $tArr);
701 }
702 $thisLine[1] = str_replace('|', '', $thisLine[1]);
703 // Default:
704 if ($vv['type'] == 'select' || $vv['type'] == 'radio') {
705 $options = str_replace(',', '', $vv['options']);
706 $options = str_replace(
707 array(CRLF, CR, LF),
708 ', ',
709 $options);
710 $thisLine[2] = $options;
711 } elseif ($vv['type'] == 'check') {
712 if ($vv['default']) {
713 $thisLine[2] = 1;
714 }
715 } elseif (trim($vv['default']) !== '') {
716 $thisLine[2] = $vv['default'];
717 }
718 if (isset($thisLine[2])) {
719 $thisLine[2] = str_replace('|', '', $thisLine[2]);
720 }
721 }
722 // Compile the final line:
723 $inLines[] = preg_replace('/[
724
725 ]*/', '', implode(' | ', $thisLine));
726 }
727 }
728 // Finally, implode the lines into a string, and return it:
729 return implode(LF, $inLines);
730 }
731
732 /**
733 * Converts the input configuration code string into an array
734 *
735 * @param string $cfgStr Configuration code
736 * @return array Configuration array
737 * @see cfgArray2CfgString()
738 */
739 public function cfgString2CfgArray($cfgStr) {
740 // Traverse the number of form elements:
741 $tLines = explode(LF, $cfgStr);
742 $attachmentCounter = 0;
743 foreach ($tLines as $k => $v) {
744 // Initialize:
745 $confData = array();
746 $val = trim($v);
747 // Accept a line as configuration if a) it is blank(! - because blank lines indicates new,
748 // unconfigured fields) or b) it is NOT a comment.
749 if (!$val || strcspn($val, '#/')) {
750 // Split:
751 $parts = GeneralUtility::trimExplode('|', $val);
752 // Label:
753 $confData['label'] = trim($parts[0]);
754 // Field:
755 $fParts = GeneralUtility::trimExplode(',', $parts[1]);
756 $fParts[0] = trim($fParts[0]);
757 if ($fParts[0][0] === '*') {
758 $confData['required'] = 1;
759 $fParts[0] = substr($fParts[0], 1);
760 }
761 $typeParts = GeneralUtility::trimExplode('=', $fParts[0]);
762 $confData['type'] = trim(strtolower(end($typeParts)));
763 if ($confData['type']) {
764 if (count($typeParts) === 1) {
765 $confData['fieldname'] = substr(preg_replace('/[^a-zA-Z0-9_]/', '', str_replace(' ', '_', trim($parts[0]))), 0, 30);
766 // Attachment names...
767 if ($confData['type'] == 'file') {
768 $confData['fieldname'] = 'attachment' . $attachmentCounter;
769 $attachmentCounter = (int)$attachmentCounter + 1;
770 }
771 } else {
772 $confData['fieldname'] = str_replace(' ', '_', trim($typeParts[0]));
773 }
774 switch ((string)$confData['type']) {
775 case 'select':
776 case 'radio':
777 $confData['default'] = implode(LF, GeneralUtility::trimExplode(',', $parts[2]));
778 break;
779 default:
780 $confData['default'] = trim($parts[2]);
781 }
782 // Field configuration depending on the fields type:
783 switch ((string)$confData['type']) {
784 case 'textarea':
785 $confData['cols'] = $fParts[1];
786 $confData['rows'] = $fParts[2];
787 $confData['extra'] = strtoupper($fParts[3]) == 'OFF' ? 'OFF' : '';
788 $confData['specialEval'] = trim($parts[3]);
789 break;
790 case 'input':
791 case 'password':
792 $confData['size'] = $fParts[1];
793 $confData['max'] = $fParts[2];
794 $confData['specialEval'] = trim($parts[3]);
795 break;
796 case 'file':
797 $confData['size'] = $fParts[1];
798 break;
799 case 'select':
800 $confData['size'] = (int)$fParts[1] ? $fParts[1] : '';
801 $confData['autosize'] = strtolower(trim($fParts[1])) === 'auto' ? 1 : 0;
802 $confData['multiple'] = strtolower(trim($fParts[2])) === 'm' ? 1 : 0;
803 break;
804 }
805 }
806 } else {
807 // No configuration, only a comment:
808 $confData = array(
809 'comment' => $val
810 );
811 }
812 // Adding config array:
813 $cfgArr[] = $confData;
814 }
815 // Return cfgArr
816 return $cfgArr;
817 }
818
819 /**
820 * Removes any "trailing elements" in the array which consists of whitespace (little like trim() does for strings, so this does for arrays)
821 *
822 * @param array $tArr Single dim array
823 * @return array Processed array
824 * @access private
825 */
826 public function cleanT($tArr) {
827 for ($a = count($tArr); $a > 0; $a--) {
828 if ((string)$tArr[$a - 1] !== '') {
829 break;
830 } else {
831 unset($tArr[$a - 1]);
832 }
833 }
834 return $tArr;
835 }
836
837 /**
838 * Wraps items in $fArr in table cells/rows, displaying them vertically.
839 *
840 * @param array $fArr Array of label/HTML pairs.
841 * @return string HTML table
842 * @access private
843 */
844 public function formatCells($fArr) {
845 // Traverse the elements in $fArr and wrap them in table cells:
846 $lines = array();
847 foreach ($fArr as $l => $c) {
848 $lines[] = '
849 <tr>
850 <td nowrap="nowrap">' . htmlspecialchars(($l . ':')) . '&nbsp;</td>
851 <td>' . $c . '</td>
852 </tr>';
853 }
854 // Add a cell which will set a minimum width:
855 $lines[] = '
856 <tr>
857 <td nowrap="nowrap"><span style="width: 70px; height: 1px;"></span></td>
858 <td></td>
859 </tr>';
860 // Wrap in table and return:
861 return '
862 <table border="0" cellpadding="0" cellspacing="0">
863 ' . implode('', $lines) . '
864 </table>';
865 }
866
867 }