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