[TASK] Remove a series of SOBE assignments
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / Wizard / TableController.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Backend\Controller\Wizard;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use Psr\Http\Message\ResponseInterface;
19 use Psr\Http\Message\ServerRequestInterface;
20 use TYPO3\CMS\Backend\Template\Components\ButtonBar;
21 use TYPO3\CMS\Backend\Template\ModuleTemplate;
22 use TYPO3\CMS\Backend\Utility\BackendUtility;
23 use TYPO3\CMS\Core\Compatibility\PublicPropertyDeprecationTrait;
24 use TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools;
25 use TYPO3\CMS\Core\DataHandling\DataHandler;
26 use TYPO3\CMS\Core\Http\HtmlResponse;
27 use TYPO3\CMS\Core\Http\RedirectResponse;
28 use TYPO3\CMS\Core\Imaging\Icon;
29 use TYPO3\CMS\Core\Imaging\IconFactory;
30 use TYPO3\CMS\Core\Utility\GeneralUtility;
31 use TYPO3\CMS\Core\Utility\HttpUtility;
32 use TYPO3\CMS\Core\Utility\MathUtility;
33
34 /**
35 * Script Class for rendering the Table Wizard
36 */
37 class TableController extends AbstractWizardController
38 {
39 use PublicPropertyDeprecationTrait;
40
41 /**
42 * Properties which have been moved to protected status from public
43 *
44 * @var array
45 */
46 protected $deprecatedPublicProperties = [
47 'content' => 'Using $content of class TableController from the outside is discouraged, as this variable is only used for internal storage.',
48 'inputStyle' => 'Using $inputStyle of class TableController from the outside is discouraged, as this variable is only used for internal storage.',
49 'xmlStorage' => 'Using $xmlStorage of class TableController from the outside is discouraged, as this variable is only used for internal storage.',
50 'numNewRows' => 'Using $numNewRows of class TableController from the outside is discouraged, as this variable is only used for internal storage.',
51 'colsFieldName' => 'Using $colsFieldName of class TableController from the outside is discouraged, as this variable is only used for internal storage.',
52 'P' => 'Using $P of class TableController from the outside is discouraged, as this variable is only used for internal storage.',
53 'TABLECFG' => 'Using $TABLECFG of class TableController from the outside is discouraged, as this variable is only used for internal storage.',
54 'tableParsing_quote' => 'Using $tableParsing_quote of class TableController from the outside is discouraged, as this variable is only used for internal storage.',
55 'tableParsing_delimiter' => 'Using $tableParsing_delimiter of class TableController from the outside is discouraged, as this variable is only used for internal storage.',
56 ];
57
58 /**
59 * Content accumulation for the module.
60 *
61 * @var string
62 */
63 protected $content;
64
65 /**
66 * If TRUE, <input> fields are shown instead of textareas.
67 *
68 * @var bool
69 */
70 protected $inputStyle = false;
71
72 /**
73 * If set, the string version of the content is interpreted/written as XML
74 * instead of the original line-based kind. This variable still needs binding
75 * to the wizard parameters - but support is ready!
76 *
77 * @var int
78 */
79 protected $xmlStorage = 0;
80
81 /**
82 * Number of new rows to add in bottom of wizard
83 *
84 * @var int
85 */
86 protected $numNewRows = 1;
87
88 /**
89 * Name of field in parent record which MAY contain the number of columns for the table
90 * here hardcoded to the value of tt_content. Should be set by FormEngine parameters (from P)
91 *
92 * @var string
93 */
94 protected $colsFieldName = 'cols';
95
96 /**
97 * Wizard parameters, coming from FormEngine linking to the wizard.
98 *
99 * @var array
100 */
101 protected $P;
102
103 /**
104 * The array which is constantly submitted by the multidimensional form of this wizard.
105 *
106 * @var array
107 */
108 protected $TABLECFG;
109
110 /**
111 * Table parsing
112 * quoting of table cells
113 *
114 * @var string
115 */
116 protected $tableParsing_quote;
117
118 /**
119 * delimiter between table cells
120 *
121 * @var string
122 */
123 protected $tableParsing_delimiter;
124
125 /**
126 * @var IconFactory
127 */
128 protected $iconFactory;
129
130 /**
131 * ModuleTemplate object
132 *
133 * @var ModuleTemplate
134 */
135 protected $moduleTemplate;
136
137 /**
138 * Constructor
139 */
140 public function __construct()
141 {
142 $this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
143 $this->getLanguageService()->includeLLFile('EXT:core/Resources/Private/Language/locallang_wizards.xlf');
144
145 // @deprecated since v9, will be moved out of __construct() in v10
146 $this->init($GLOBALS['TYPO3_REQUEST']);
147 }
148
149 /**
150 * Injects the request object for the current request or subrequest
151 * As this controller goes only through the main() method, it is rather simple for now
152 *
153 * @param ServerRequestInterface $request
154 * @return ResponseInterface
155 */
156 public function mainAction(ServerRequestInterface $request): ResponseInterface
157 {
158 $response = $this->renderContent($request);
159
160 if (empty($response)) {
161 $response = new HtmlResponse($this->moduleTemplate->renderContent());
162 }
163
164 return $response;
165 }
166
167 /**
168 * Main function, rendering the table wizard
169 *
170 * @deprecated since v9, will be removed in v10
171 */
172 public function main()
173 {
174 trigger_error('Method main() will be replaced by protected method renderContent() in v10. Do not call from other extensions', E_USER_DEPRECATED);
175
176 $response = $this->renderContent($GLOBALS['TYPO3_REQUEST']);
177
178 if ($response instanceof RedirectResponse) {
179 HttpUtility::redirect($response->getHeaders()['location'][0]);
180 }
181 }
182
183 /**
184 * Draws the table wizard content
185 *
186 * @return string HTML content for the form.
187 * @throws \RuntimeException
188 *
189 * @deprecated since v9, will be removed in v10
190 */
191 public function tableWizard()
192 {
193 trigger_error('Method tableWizard() will be replaced by protected method renderTableWizard() in v10. Do not call from other extensions', E_USER_DEPRECATED);
194
195 $result = $this->renderTableWizard($GLOBALS['TYPO3_REQUEST']);
196
197 if ($result instanceof RedirectResponse) {
198 HttpUtility::redirect($result->getHeaders()['location'][0]);
199 }
200
201 return $result;
202 }
203
204 /**
205 * Will get and return the configuration code string
206 * Will also save (and possibly redirect/exit) the content if a save button has been pressed
207 *
208 * @param array $row Current parent record row
209 * @return array Table config code in an array
210 *
211 * @deprecated since v9, will be removed in v10
212 */
213 public function getConfigCode($row)
214 {
215 trigger_error('Method getConfigCode() will be replaced by protected method getConfiguration() in v10. Do not call from other extensions', E_USER_DEPRECATED);
216
217 $result = $this->getConfiguration($row, $GLOBALS['TYPO3_REQUEST']);
218
219 if ($result instanceof RedirectResponse) {
220 HttpUtility::redirect($result->getHeaders()['location'][0]);
221 }
222
223 return $result;
224 }
225
226 /**
227 * Creates the HTML for the Table Wizard:
228 *
229 * @param array $configuration Table config array
230 * @return string HTML for the table wizard
231 * @internal
232 *
233 * @deprecated since v9, will be removed in v10
234 */
235 public function getTableHTML($configuration)
236 {
237 trigger_error('Method getTableHTML() will be replaced by protected method getTableWizard() in v10. Do not call from other extensions', E_USER_DEPRECATED);
238 return $this->getTableWizard($configuration);
239 }
240
241 /**
242 * Detects if a control button (up/down/around/delete) has been pressed for an item and accordingly it will
243 * manipulate the internal TABLECFG array
244 *
245 * @internal
246 *
247 * @deprecated since v9, will be removed in v10
248 */
249 public function changeFunc()
250 {
251 trigger_error('Method changeFunc() will be replaced by protected method manipulateTable() in v10. Do not call from other extensions', E_USER_DEPRECATED);
252 $this->manipulateTable();
253 }
254
255 /**
256 * Converts the input array to a configuration code string
257 *
258 * @param array $cfgArr Array of table configuration (follows the input structure from the table wizard POST form)
259 * @return string The array converted into a string with line-based configuration.
260 * @see cfgString2CfgArray()
261 *
262 * @deprecated since v9, will be removed in v10
263 */
264 public function cfgArray2CfgString($cfgArr)
265 {
266 trigger_error('Method cfgArray2CfgString() will be replaced by protected method configurationArrayToString() in v10. Do not call from other extensions', E_USER_DEPRECATED);
267 return $this->configurationArrayToString($cfgArr);
268 }
269
270 /**
271 * Converts the input configuration code string into an array
272 *
273 * @param string $configurationCode Configuration code
274 * @param int $columns Default number of columns
275 * @return array Configuration array
276 * @see cfgArray2CfgString()
277 *
278 * @deprecated since v9, will be removed in v10
279 */
280 public function cfgString2CfgArray($configurationCode, $columns)
281 {
282 trigger_error('Method cfgString2CfgArray() will be replaced by protected method configurationStringToArray() in v10. Do not call from other extensions', E_USER_DEPRECATED);
283 return $this->configurationStringToArray($configurationCode, $columns);
284 }
285
286 /**
287 * Initialization of the class
288 *
289 * @param ServerRequestInterface $request
290 */
291 protected function init(ServerRequestInterface $request): void
292 {
293 $parsedBody = $request->getParsedBody();
294 $queryParams = $request->getQueryParams();
295 // GPvars:
296 $this->P = $parsedBody['P'] ?? $queryParams['P'] ?? null;
297 $this->TABLECFG = $parsedBody['TABLE'] ?? $queryParams['TABLE'] ?? null;
298 // Setting options:
299 $this->xmlStorage = $this->P['params']['xmlOutput'];
300 $this->numNewRows = MathUtility::forceIntegerInRange($this->P['params']['numNewRows'], 1, 50, 5);
301 // Textareas or input fields:
302 $this->inputStyle = isset($this->TABLECFG['textFields']) ? (bool)$this->TABLECFG['textFields'] : true;
303 $this->tableParsing_delimiter = '|';
304 $this->tableParsing_quote = '';
305 }
306
307 /**
308 * Main function, rendering the table wizard
309 *
310 * @param ServerRequestInterface $request
311 * @return ResponseInterface|null
312 */
313 protected function renderContent(ServerRequestInterface $request): ?ResponseInterface
314 {
315 $normalizedParams = $request->getAttribute('normalizedParams');
316 $requestUri = $normalizedParams->getRequestUri();
317 list($rUri) = explode('#', $requestUri);
318 $this->content .= '<form action="' . htmlspecialchars($rUri) . '" method="post" id="TableController" name="wizardForm">';
319 if ($this->P['table'] && $this->P['field'] && $this->P['uid']) {
320 $tableWizard = $this->renderTableWizard($request);
321
322 if ($tableWizard instanceof RedirectResponse) {
323 return $tableWizard;
324 }
325
326 $this->content .= '<h2>' . htmlspecialchars($this->getLanguageService()->getLL('table_title')) . '</h2>'
327 . '<div>' . $tableWizard . '</div>';
328 } else {
329 $this->content .= '<h2>' . htmlspecialchars($this->getLanguageService()->getLL('table_title')) . '</h2>'
330 . '<div><span class="text-danger">' . htmlspecialchars($this->getLanguageService()->getLL('table_noData')) . '</span></div>';
331 }
332 $this->content .= '</form>';
333 // Setting up the buttons and markers for docHeader
334 $this->getButtons();
335 // Build the <body> for the module
336 $this->moduleTemplate->setContent($this->content);
337
338 return null;
339 }
340
341 /**
342 * Create the panel of buttons for submitting the form or otherwise perform operations.
343 */
344 protected function getButtons(): void
345 {
346 $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
347 if ($this->P['table'] && $this->P['field'] && $this->P['uid']) {
348 // CSH
349 $cshButton = $buttonBar->makeHelpButton()
350 ->setModuleName('xMOD_csh_corebe')
351 ->setFieldName('wizard_table_wiz');
352 $buttonBar->addButton($cshButton);
353 // Close
354 $closeButton = $buttonBar->makeLinkButton()
355 ->setHref($this->P['returnUrl'])
356 ->setTitle($this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:rm.closeDoc'))
357 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-close', Icon::SIZE_SMALL));
358 $buttonBar->addButton($closeButton);
359 // Save
360 $saveButton = $buttonBar->makeInputButton()
361 ->setName('_savedok')
362 ->setValue('1')
363 ->setForm('TableController')
364 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-document-save', Icon::SIZE_SMALL))
365 ->setTitle($this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:rm.saveDoc'));
366 // Save & Close
367 $saveAndCloseButton = $buttonBar->makeInputButton()
368 ->setName('_saveandclosedok')
369 ->setValue('1')
370 ->setForm('TableController')
371 ->setTitle($this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:rm.saveCloseDoc'))
372 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon(
373 'actions-document-save-close',
374 Icon::SIZE_SMALL
375 ));
376 $splitButtonElement = $buttonBar->makeSplitButton()
377 ->addItem($saveButton)
378 ->addItem($saveAndCloseButton);
379
380 $buttonBar->addButton($splitButtonElement, ButtonBar::BUTTON_POSITION_LEFT, 3);
381 // Reload
382 $reloadButton = $buttonBar->makeInputButton()
383 ->setName('_refresh')
384 ->setValue('1')
385 ->setForm('TableController')
386 ->setTitle($this->getLanguageService()->getLL('forms_refresh'))
387 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-refresh', Icon::SIZE_SMALL));
388 $buttonBar->addButton($reloadButton);
389 }
390 }
391
392 /**
393 * Draws the table wizard content
394 *
395 * @param ServerRequestInterface $request
396 * @return string|ResponseInterface HTML content for the form.
397 * @throws \RuntimeException
398 */
399 protected function renderTableWizard(ServerRequestInterface $request)
400 {
401 if (!$this->checkEditAccess($this->P['table'], $this->P['uid'])) {
402 throw new \RuntimeException('Wizard Error: No access', 1349692692);
403 }
404 // First, check the references by selecting the record:
405 $row = BackendUtility::getRecord($this->P['table'], $this->P['uid']);
406 if (!is_array($row)) {
407 throw new \RuntimeException('Wizard Error: No reference to record', 1294587125);
408 }
409 // This will get the content of the form configuration code field to us - possibly cleaned up,
410 // saved to database etc. if the form has been submitted in the meantime.
411 $tableCfgArray = $this->getConfiguration($row, $request);
412
413 if ($tableCfgArray instanceof ResponseInterface) {
414 return $tableCfgArray;
415 }
416
417 // Generation of the Table Wizards HTML code:
418 $content = $this->getTableWizard($tableCfgArray);
419 // Return content:
420 return $content;
421 }
422
423 /**
424 * Will get and return the configuration code string
425 * Will also save (and possibly redirect/exit) the content if a save button has been pressed
426 *
427 * @param array $row Current parent record row
428 * @param ServerRequestInterface $request
429 * @return array|ResponseInterface Table config code in an array
430 */
431 protected function getConfiguration(array $row, ServerRequestInterface $request)
432 {
433 // Get delimiter settings
434 $this->tableParsing_quote = $row['table_enclosure'] ? chr((int)$row['table_enclosure']) : '';
435 $this->tableParsing_delimiter = $row['table_delimiter'] ? chr((int)$row['table_delimiter']) : '|';
436 // If some data has been submitted, then construct
437 if (isset($this->TABLECFG['c'])) {
438 // Process incoming:
439 $this->manipulateTable();
440 // Convert to string (either line based or XML):
441 if ($this->xmlStorage) {
442 // Convert the input array to XML:
443 $bodyText = '<?xml version="1.0" encoding="utf-8" standalone="yes" ?>' . LF . GeneralUtility::array2xml($this->TABLECFG['c'], '', 0, 'T3TableWizard');
444 // Setting cfgArr directly from the input:
445 $configuration = $this->TABLECFG['c'];
446 } else {
447 // Convert the input array to a string of configuration code:
448 $bodyText = $this->configurationArrayToString($this->TABLECFG['c']);
449 // Create cfgArr from the string based configuration - that way it is cleaned up
450 // and any incompatibilities will be removed!
451 $configuration = $this->configurationStringToArray($bodyText, (int)$row[$this->colsFieldName]);
452 }
453 // If a save button has been pressed, then save the new field content:
454 if ($_POST['_savedok'] || $_POST['_saveandclosedok']) {
455 // Get DataHandler object:
456 /** @var DataHandler $dataHandler */
457 $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
458 // Put content into the data array:
459 $data = [];
460 if ($this->P['flexFormPath']) {
461 // Current value of flexForm path:
462 $currentFlexFormData = GeneralUtility::xml2array($row[$this->P['field']]);
463 /** @var FlexFormTools $flexFormTools */
464 $flexFormTools = GeneralUtility::makeInstance(FlexFormTools::class);
465 $flexFormTools->setArrayValueByPath($this->P['flexFormPath'], $currentFlexFormData, $bodyText);
466 $data[$this->P['table']][$this->P['uid']][$this->P['field']] = $currentFlexFormData;
467 } else {
468 $data[$this->P['table']][$this->P['uid']][$this->P['field']] = $bodyText;
469 }
470 // Perform the update:
471 $dataHandler->start($data, []);
472 $dataHandler->process_datamap();
473 // If the save/close button was pressed, then redirect the screen:
474 if ($_POST['_saveandclosedok']) {
475 return new RedirectResponse(GeneralUtility::sanitizeLocalUrl($this->P['returnUrl']));
476 }
477 }
478 } else {
479 // If nothing has been submitted, load the $bodyText variable from the selected database row:
480 if ($this->xmlStorage) {
481 $configuration = GeneralUtility::xml2array($row[$this->P['field']]);
482 } else {
483 if ($this->P['flexFormPath']) {
484 // Current value of flexForm path:
485 $currentFlexFormData = GeneralUtility::xml2array($row[$this->P['field']]);
486 /** @var FlexFormTools $flexFormTools */
487 $flexFormTools = GeneralUtility::makeInstance(FlexFormTools::class);
488 $configuration = $flexFormTools->getArrayValueByPath(
489 $this->P['flexFormPath'],
490 $currentFlexFormData
491 );
492 $configuration = $this->configurationStringToArray($configuration, 0);
493 } else {
494 // Regular line based table configuration:
495 $columns = $row[$this->colsFieldName] ?? 0;
496 $configuration = $this->configurationStringToArray($row[$this->P['field']], (int)$columns);
497 }
498 }
499 $configuration = is_array($configuration) ? $configuration : [];
500 }
501 return $configuration;
502 }
503
504 /**
505 * Creates the HTML for the Table Wizard:
506 *
507 * @param array $configuration Table config array
508 * @return string HTML for the table wizard
509 */
510 protected function getTableWizard(array $configuration): string
511 {
512 // Traverse the rows:
513 $tRows = [];
514 $k = 0;
515 $countLines = count($configuration);
516 foreach ($configuration as $cellArr) {
517 if (is_array($cellArr)) {
518 // Initialize:
519 $cells = [];
520 $a = 0;
521 // Traverse the columns:
522 foreach ($cellArr as $cellContent) {
523 if ($this->inputStyle) {
524 $cells[] = '<input class="form-control" type="text" name="TABLE[c][' . ($k + 1) * 2 . '][' . ($a + 1) * 2 . ']" value="' . htmlspecialchars($cellContent) . '" />';
525 } else {
526 $cellContent = preg_replace('/<br[ ]?[\\/]?>/i', LF, $cellContent);
527 $cells[] = '<textarea class="form-control" rows="6" name="TABLE[c][' . ($k + 1) * 2 . '][' . ($a + 1) * 2 . ']">' . htmlspecialchars($cellContent) . '</textarea>';
528 }
529 // Increment counter:
530 $a++;
531 }
532 // CTRL panel for a table row (move up/down/around):
533 $onClick = 'document.wizardForm.action+=' . GeneralUtility::quoteJSvalue('#ANC_' . (($k + 1) * 2 - 2)) . ';';
534 $onClick = ' onclick="' . htmlspecialchars($onClick) . '"';
535 $ctrl = '';
536 if ($k !== 0) {
537 $ctrl .= '<button class="btn btn-default" name="TABLE[row_up][' . ($k + 1) * 2 . ']" title="' . htmlspecialchars($this->getLanguageService()->getLL('table_up')) . '"' . $onClick . '><span class="t3-icon fa fa-fw fa-angle-up"></span></button>';
538 } else {
539 $ctrl .= '<button class="btn btn-default" name="TABLE[row_bottom][' . ($k + 1) * 2 . ']" title="' . htmlspecialchars($this->getLanguageService()->getLL('table_bottom')) . '"' . $onClick . '><span class="t3-icon fa fa-fw fa-angle-double-down"></span></button>';
540 }
541 if ($k + 1 !== $countLines) {
542 $ctrl .= '<button class="btn btn-default" name="TABLE[row_down][' . ($k + 1) * 2 . ']" title="' . htmlspecialchars($this->getLanguageService()->getLL('table_down')) . '"' . $onClick . '><span class="t3-icon fa fa-fw fa-angle-down"></span></button>';
543 } else {
544 $ctrl .= '<button class="btn btn-default" name="TABLE[row_top][' . ($k + 1) * 2 . ']" title="' . htmlspecialchars($this->getLanguageService()->getLL('table_top')) . '"' . $onClick . '><span class="t3-icon fa fa-fw fa-angle-double-up"></span></button>';
545 }
546 $ctrl .= '<button class="btn btn-default" name="TABLE[row_remove][' . ($k + 1) * 2 . ']" title="' . htmlspecialchars($this->getLanguageService()->getLL('table_removeRow')) . '"' . $onClick . '><span class="t3-icon fa fa-fw fa-trash"></span></button>';
547 $ctrl .= '<button class="btn btn-default" name="TABLE[row_add][' . ($k + 1) * 2 . ']" title="' . htmlspecialchars($this->getLanguageService()->getLL('table_addRow')) . '"' . $onClick . '><span class="t3-icon fa fa-fw fa-plus"></span></button>';
548 $tRows[] = '
549 <tr>
550 <td>
551 <a name="ANC_' . ($k + 1) * 2 . '"></a>
552 <span class="btn-group' . ($this->inputStyle ? '' : '-vertical') . '">' . $ctrl . '</span>
553 </td>
554 <td>' . implode('</td>
555 <td>', $cells) . '</td>
556 </tr>';
557 // Increment counter:
558 $k++;
559 }
560 }
561 // CTRL panel for a table column (move left/right/around/delete)
562 $cells = [];
563 $cells[] = '';
564 // Finding first row:
565 $firstRow = reset($configuration);
566 if (is_array($firstRow)) {
567 $cols = count($firstRow);
568 for ($a = 1; $a <= $cols; $a++) {
569 $b = $a * 2;
570 $ctrl = '';
571 if ($a !== 1) {
572 $ctrl .= '<button class="btn btn-default" name="TABLE[col_left][' . $b . ']" title="' . htmlspecialchars($this->getLanguageService()->getLL('table_left')) . '"><span class="t3-icon fa fa-fw fa-angle-left"></span></button>';
573 } else {
574 $ctrl .= '<button class="btn btn-default" name="TABLE[col_end][' . $b . ']" title="' . htmlspecialchars($this->getLanguageService()->getLL('table_end')) . '"><span class="t3-icon fa fa-fw fa-angle-double-right"></span></button>';
575 }
576 if ($a != $cols) {
577 $ctrl .= '<button class="btn btn-default" name="TABLE[col_right][' . $b . ']" title="' . htmlspecialchars($this->getLanguageService()->getLL('table_right')) . '"><span class="t3-icon fa fa-fw fa-angle-right"></span></button>';
578 } else {
579 $ctrl .= '<button class="btn btn-default" name="TABLE[col_start][' . $b . ']" title="' . htmlspecialchars($this->getLanguageService()->getLL('table_start')) . '"><span class="t3-icon fa fa-fw fa-angle-double-left"></span></button>';
580 }
581 $ctrl .= '<button class="btn btn-default" name="TABLE[col_remove][' . $b . ']" title="' . htmlspecialchars($this->getLanguageService()->getLL('table_removeColumn')) . '"><span class="t3-icon fa fa-fw fa-trash"></span></button>';
582 $ctrl .= '<button class="btn btn-default" name="TABLE[col_add][' . $b . ']" title="' . htmlspecialchars($this->getLanguageService()->getLL('table_addColumn')) . '"><span class="t3-icon fa fa-fw fa-plus"></span></button>';
583 $cells[] = '<span class="btn-group">' . $ctrl . '</span>';
584 }
585 $tRows[] = '
586 <tfoot>
587 <tr>
588 <td>' . implode('</td>
589 <td>', $cells) . '</td>
590 </tr>
591 </tfoot>';
592 }
593 $content = '';
594 $addSubmitOnClick = 'onclick="document.getElementById(\'TableController\').submit();"';
595 // Implode all table rows into a string, wrapped in table tags.
596 $content .= '
597
598 <!-- Table wizard -->
599 <div class="table-fit table-fit-inline-block">
600 <table id="typo3-tablewizard" class="table table-center">
601 ' . implode('', $tRows) . '
602 </table>
603 </div>';
604 // Input type checkbox:
605 $content .= '
606
607 <!-- Input mode check box: -->
608 <div class="checkbox">
609 <input type="hidden" name="TABLE[textFields]" value="0" />
610 <label for="textFields">
611 <input type="checkbox" ' . $addSubmitOnClick . ' name="TABLE[textFields]" id="textFields" value="1"' . ($this->inputStyle ? ' checked="checked"' : '') . ' />
612 ' . $this->getLanguageService()->getLL('table_smallFields') . '
613 </label>
614 </div>';
615 return $content;
616 }
617
618 /**
619 * Detects if a control button (up/down/around/delete) has been pressed for an item and accordingly it will
620 * manipulate the internal TABLECFG array
621 */
622 protected function manipulateTable(): void
623 {
624 if ($this->TABLECFG['col_remove']) {
625 $kk = key($this->TABLECFG['col_remove']);
626 $cmd = 'col_remove';
627 } elseif ($this->TABLECFG['col_add']) {
628 $kk = key($this->TABLECFG['col_add']);
629 $cmd = 'col_add';
630 } elseif ($this->TABLECFG['col_start']) {
631 $kk = key($this->TABLECFG['col_start']);
632 $cmd = 'col_start';
633 } elseif ($this->TABLECFG['col_end']) {
634 $kk = key($this->TABLECFG['col_end']);
635 $cmd = 'col_end';
636 } elseif ($this->TABLECFG['col_left']) {
637 $kk = key($this->TABLECFG['col_left']);
638 $cmd = 'col_left';
639 } elseif ($this->TABLECFG['col_right']) {
640 $kk = key($this->TABLECFG['col_right']);
641 $cmd = 'col_right';
642 } elseif ($this->TABLECFG['row_remove']) {
643 $kk = key($this->TABLECFG['row_remove']);
644 $cmd = 'row_remove';
645 } elseif ($this->TABLECFG['row_add']) {
646 $kk = key($this->TABLECFG['row_add']);
647 $cmd = 'row_add';
648 } elseif ($this->TABLECFG['row_top']) {
649 $kk = key($this->TABLECFG['row_top']);
650 $cmd = 'row_top';
651 } elseif ($this->TABLECFG['row_bottom']) {
652 $kk = key($this->TABLECFG['row_bottom']);
653 $cmd = 'row_bottom';
654 } elseif ($this->TABLECFG['row_up']) {
655 $kk = key($this->TABLECFG['row_up']);
656 $cmd = 'row_up';
657 } elseif ($this->TABLECFG['row_down']) {
658 $kk = key($this->TABLECFG['row_down']);
659 $cmd = 'row_down';
660 } else {
661 $kk = '';
662 $cmd = '';
663 }
664 if ($cmd && MathUtility::canBeInterpretedAsInteger($kk)) {
665 if (strpos($cmd, 'row_') === 0) {
666 switch ($cmd) {
667 case 'row_remove':
668 unset($this->TABLECFG['c'][$kk]);
669 break;
670 case 'row_add':
671 for ($a = 1; $a <= $this->numNewRows; $a++) {
672 // Checking if set: The point is that any new row between existing rows
673 // will be TRUE after one row is added while if rows are added in the bottom
674 // of the table there will be no existing rows to stop the addition of new rows
675 // which means it will add up to $this->numNewRows rows then.
676 if (!isset($this->TABLECFG['c'][$kk + $a])) {
677 $this->TABLECFG['c'][$kk + $a] = [];
678 } else {
679 break;
680 }
681 }
682 break;
683 case 'row_top':
684 $this->TABLECFG['c'][1] = $this->TABLECFG['c'][$kk];
685 unset($this->TABLECFG['c'][$kk]);
686 break;
687 case 'row_bottom':
688 $this->TABLECFG['c'][10000000] = $this->TABLECFG['c'][$kk];
689 unset($this->TABLECFG['c'][$kk]);
690 break;
691 case 'row_up':
692 $this->TABLECFG['c'][$kk - 3] = $this->TABLECFG['c'][$kk];
693 unset($this->TABLECFG['c'][$kk]);
694 break;
695 case 'row_down':
696 $this->TABLECFG['c'][$kk + 3] = $this->TABLECFG['c'][$kk];
697 unset($this->TABLECFG['c'][$kk]);
698 break;
699 }
700 ksort($this->TABLECFG['c']);
701 }
702 if (strpos($cmd, 'col_') === 0) {
703 foreach ($this->TABLECFG['c'] as $cAK => $value) {
704 switch ($cmd) {
705 case 'col_remove':
706 unset($this->TABLECFG['c'][$cAK][$kk]);
707 break;
708 case 'col_add':
709 $this->TABLECFG['c'][$cAK][$kk + 1] = '';
710 break;
711 case 'col_start':
712 $this->TABLECFG['c'][$cAK][1] = $this->TABLECFG['c'][$cAK][$kk];
713 unset($this->TABLECFG['c'][$cAK][$kk]);
714 break;
715 case 'col_end':
716 $this->TABLECFG['c'][$cAK][1000000] = $this->TABLECFG['c'][$cAK][$kk];
717 unset($this->TABLECFG['c'][$cAK][$kk]);
718 break;
719 case 'col_left':
720 $this->TABLECFG['c'][$cAK][$kk - 3] = $this->TABLECFG['c'][$cAK][$kk];
721 unset($this->TABLECFG['c'][$cAK][$kk]);
722 break;
723 case 'col_right':
724 $this->TABLECFG['c'][$cAK][$kk + 3] = $this->TABLECFG['c'][$cAK][$kk];
725 unset($this->TABLECFG['c'][$cAK][$kk]);
726 break;
727 }
728 ksort($this->TABLECFG['c'][$cAK]);
729 }
730 }
731 }
732 // Convert line breaks to <br /> tags:
733 foreach ($this->TABLECFG['c'] as $a => $value) {
734 foreach ($this->TABLECFG['c'][$a] as $b => $value2) {
735 $this->TABLECFG['c'][$a][$b] = str_replace(
736 [CR, LF],
737 ['', '<br />'],
738 $this->TABLECFG['c'][$a][$b]
739 );
740 }
741 }
742 }
743
744 /**
745 * Converts the input array to a configuration code string
746 *
747 * @param array $cfgArr Array of table configuration (follows the input structure from the table wizard POST form)
748 * @return string The array converted into a string with line-based configuration.
749 * @see configurationStringToArray()
750 */
751 protected function configurationArrayToString(array $cfgArr): string
752 {
753 $inLines = [];
754 // Traverse the elements of the table wizard and transform the settings into configuration code.
755 foreach ($cfgArr as $valueA) {
756 $thisLine = [];
757 foreach ($valueA as $valueB) {
758 $thisLine[] = $this->tableParsing_quote
759 . str_replace($this->tableParsing_delimiter, '', $valueB) . $this->tableParsing_quote;
760 }
761 $inLines[] = implode($this->tableParsing_delimiter, $thisLine);
762 }
763 // Finally, implode the lines into a string:
764 return implode(LF, $inLines);
765 }
766
767 /**
768 * Converts the input configuration code string into an array
769 *
770 * @param string $configurationCode Configuration code
771 * @param int $columns Default number of columns
772 * @return array Configuration array
773 * @see configurationArrayToString()
774 */
775 protected function configurationStringToArray(string $configurationCode, int $columns): array
776 {
777 // Explode lines in the configuration code - each line is a table row.
778 $tableLines = explode(LF, $configurationCode);
779 // Setting number of columns
780 // auto...
781 if (!$columns && trim($tableLines[0])) {
782 $columns = count(explode($this->tableParsing_delimiter, $tableLines[0]));
783 }
784 $columns = $columns ?: 4;
785 // Traverse the number of table elements:
786 $configurationArray = [];
787 foreach ($tableLines as $key => $value) {
788 // Initialize:
789 $valueParts = explode($this->tableParsing_delimiter, $value);
790 // Traverse columns:
791 for ($a = 0; $a < $columns; $a++) {
792 if ($this->tableParsing_quote
793 && $valueParts[$a][0] === $this->tableParsing_quote
794 && substr($valueParts[$a], -1, 1) === $this->tableParsing_quote
795 ) {
796 $valueParts[$a] = substr(trim($valueParts[$a]), 1, -1);
797 }
798 $configurationArray[$key][$a] = (string)$valueParts[$a];
799 }
800 }
801 return $configurationArray;
802 }
803 }