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