PaletteAndSingleContainer.php 16.7 KB
Newer Older
Christian Kuhn's avatar
Christian Kuhn committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
namespace TYPO3\CMS\Backend\Form\Container;

/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Lang\LanguageService;
use TYPO3\CMS\Backend\Utility\IconUtility;
use TYPO3\CMS\Backend\Form\Utility\FormEngineUtility;
use TYPO3\CMS\Backend\Utility\BackendUtility;
22
use TYPO3\CMS\Backend\Form\NodeFactory;
Christian Kuhn's avatar
Christian Kuhn committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

/**
 * Handle palettes and single fields.
 *
 * This container is called by TabsContainer, NoTabsContainer and ListOfFieldsContainer.
 *
 * This container mostly operates on TCA showItem of a specific type - the value is
 * coming in from upper containers as "fieldArray". It handles palettes with all its
 * different options and prepares rendering of single fields for the SingleFieldContainer.
 */
class PaletteAndSingleContainer extends AbstractContainer {

	/**
	 * Final result array accumulating results from children and final HTML
	 *
	 * @var array
	 */
	protected $resultArray = array();

	/**
	 * Entry method
	 *
	 * @return array As defined in initializeResultArray() of AbstractNode
	 */
	public function render() {
		$languageService = $this->getLanguageService();
		$table = $this->globalOptions['table'];

		/**
		 * The first code block creates a target structure array to later create the final
		 * HTML string. The single fields and sub containers are rendered here already and
		 * other parts of the return array from children except html are accumulated in
		 * $this->resultArray
		 *
		$targetStructure = array(
			0 => array(
				'type' => 'palette',
				'fieldName' => 'palette1',
				'fieldLabel' => 'palette1',
				'elements' => array(
					0 => array(
						'type' => 'single',
						'fieldName' => 'palettenName',
						'fieldLabel' => 'element1',
						'fieldHtml' => 'element1',
					),
					1 => array(
						'type' => 'linebreak',
					),
					2 => array(
						'type' => 'single',
						'fieldName' => 'palettenName',
						'fieldLabel' => 'element2',
						'fieldHtml' => 'element2',
					),
				),
			),
			1 => array( // has 2 as "additional palette"
				'type' => 'single',
				'fieldName' => 'element3',
				'fieldLabel' => 'element3',
				'fieldHtml' => 'element3',
			),
			2 => array( // do only if 1 had result
				'type' => 'palette2',
				'fieldName' => 'palette2',
				'fieldLabel' => '', // label missing because label of 1 is displayed only
				'canNotCollapse' => TRUE, // An "additional palette" can not be collapsed
				'elements' => array(
					0 => array(
						'type' => 'single',
						'fieldName' => 'element4',
						'fieldLabel' => 'element4',
						'fieldHtml' => 'element4',
					),
					1 => array(
						'type' => 'linebreak',
					),
					2 => array(
						'type' => 'single',
						'fieldName' => 'element5',
						'fieldLabel' => 'element5',
						'fieldHtml' => 'element5',
					),
				),
			),
		);
		 */

		// Create an intermediate structure of rendered sub elements and elements nested in palettes
		$targetStructure = array();
		$mainStructureCounter = -1;
		$fieldsArray = $this->globalOptions['fieldsArray'];
		$this->resultArray = $this->initializeResultArray();
		foreach ($fieldsArray as $fieldString) {
			$fieldConfiguration = $this->explodeSingleFieldShowItemConfiguration($fieldString);
			$fieldName = $fieldConfiguration['fieldName'];
			if ($fieldName === '--palette--') {
				$paletteElementArray = $this->createPaletteContentArray($fieldConfiguration['paletteName']);
				if (!empty($paletteElementArray)) {
					$mainStructureCounter ++;
					$targetStructure[$mainStructureCounter] = array(
						'type' => 'palette',
						'fieldName' => $fieldConfiguration['paletteName'],
						'fieldLabel' => $languageService->sL($fieldConfiguration['fieldLabel']),
						'elements' => $paletteElementArray,
					);
				}
			} else {
				if (!is_array($GLOBALS['TCA'][$table]['columns'][$fieldName])) {
					continue;
				}

				$options = $this->globalOptions;
				$options['fieldName'] = $fieldName;

139
				$options['renderType'] = 'singleFieldContainer';
140
141
142
				/** @var NodeFactory $nodeFactory */
				$nodeFactory = $this->globalOptions['nodeFactory'];
				$childResultArray = $nodeFactory->create($options)->render();
Christian Kuhn's avatar
Christian Kuhn committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219

				if (!empty($childResultArray['html'])) {
					$mainStructureCounter ++;

					$targetStructure[$mainStructureCounter] = array(
						'type' => 'single',
						'fieldName' => $fieldConfiguration['fieldName'],
						'fieldLabel' => $this->getSingleFieldLabel($fieldName, $fieldConfiguration['fieldLabel']),
						'fieldHtml' => $childResultArray['html'],
					);

					// If the third part of a show item field is given, this is a name of a palette that should be rendered
					// below the single field - without palette header and only if single field produced content
					if (!empty($childResultArray['html']) && !empty($fieldConfiguration['paletteName'])) {
						$paletteElementArray = $this->createPaletteContentArray($fieldConfiguration['paletteName']);
						if (!empty($paletteElementArray)) {
							$mainStructureCounter ++;
							$targetStructure[$mainStructureCounter] = array(
								'type' => 'palette',
								'fieldName' => $fieldConfiguration['paletteName'],
								'fieldLabel' => '', // An "additional palette" has no show label
								'canNotCollapse' => TRUE,
								'elements' => $paletteElementArray,
							);
						}
					}
				}

				$childResultArray['html'] = '';
				$this->resultArray = $this->mergeChildReturnIntoExistingResult($this->resultArray, $childResultArray);
			}
		}

		// Compile final content
		$content = array();
		foreach ($targetStructure as $element) {
			if ($element['type'] === 'palette') {
				$paletteName = $element['fieldName'];
				$paletteElementsHtml = $this->renderInnerPaletteContent($element);

				$isHiddenPalette = !empty($GLOBALS['TCA'][$table]['palettes'][$paletteName]['isHiddenPalette']);

				$renderUnCollapseButtonWrapper = TRUE;
				// No button if the palette is hidden
				if ($isHiddenPalette) {
					$renderUnCollapseButtonWrapper = FALSE;
				}
				// No button if palette can not collapse on ctrl level
				if (!empty($GLOBALS['TCA'][$table]['ctrl']['canNotCollapse'])) {
					$renderUnCollapseButtonWrapper = FALSE;
				}
				// No button if palette can not collapse on palette definition level
				if (!empty($GLOBALS['TCA'][$table]['palettes'][$paletteName]['canNotCollapse'])) {
					$renderUnCollapseButtonWrapper = FALSE;
				}
				// No button if palettes are not collapsed - this is the checkbox at the end of the form
				if (!$this->globalOptions['palettesCollapsed']) {
					$renderUnCollapseButtonWrapper = FALSE;
				}
				// No button if palette is set to no collapse on element level - this is the case if palette is an "additional palette" after a casual field
				if (!empty($element['canNotCollapse'])) {
					$renderUnCollapseButtonWrapper = FALSE;
				}

				if ($renderUnCollapseButtonWrapper) {
					$cssId = 'FORMENGINE_' . $this->globalOptions['table'] . '_' . $paletteName . '_' . $this->globalOptions['databaseRow']['uid'];
					$paletteElementsHtml = $this->wrapPaletteWithCollapseButton($paletteElementsHtml, $cssId);
				} else {
					$paletteElementsHtml = '<div class="row">' . $paletteElementsHtml . '</div>';
				}

				$content[] = $this->fieldSetWrap($paletteElementsHtml, $isHiddenPalette, $element['fieldLabel']);
			} else {
				// Return raw HTML only in case of user element with no wrapping requested
				if ($this->isUserNoTableWrappingField($element)) {
					$content[] = $element['fieldHtml'];
				} else {
220
					$content[] = $this->fieldSetWrap($this->wrapSingleFieldContentWithLabelAndOuterDiv($element));
Christian Kuhn's avatar
Christian Kuhn committed
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
				}
			}
		}

		$finalResultArray = $this->resultArray;
		$finalResultArray['html'] = implode(LF, $content);
		return $finalResultArray;
	}

	/**
	 * Render single fields of a given palette
	 *
	 * @param string $paletteName The palette to render
	 * @return array
	 */
	protected function createPaletteContentArray($paletteName) {
		$table = $this->globalOptions['table'];
		$excludeElements = $this->globalOptions['excludeElements'];

		// palette needs a palette name reference, otherwise it does not make sense to try rendering of it
		if (empty($paletteName) || empty($GLOBALS['TCA'][$table]['palettes'][$paletteName]['showitem'])) {
			return array();
		}

		$resultStructure = array();
		$foundRealElement = FALSE; // Set to true if not only line breaks were rendered
		$fieldsArray = GeneralUtility::trimExplode(',', $GLOBALS['TCA'][$table]['palettes'][$paletteName]['showitem'], TRUE);
		foreach ($fieldsArray as $fieldString) {
			$fieldArray = $this->explodeSingleFieldShowItemConfiguration($fieldString);
			$fieldName = $fieldArray['fieldName'];
			if ($fieldName === '--linebreak--') {
				$resultStructure[] = array(
					'type' => 'linebreak',
				);
			} else {
				if (in_array($fieldName, $excludeElements, TRUE) || !is_array($GLOBALS['TCA'][$table]['columns'][$fieldName])) {
					continue;
				}
				$options = $this->globalOptions;
				$options['fieldName'] = $fieldName;

262
				$options['renderType'] = 'singleFieldContainer';
263
264
265
				/** @var NodeFactory $nodeFactory */
				$nodeFactory = $this->globalOptions['nodeFactory'];
				$singleFieldContentArray = $nodeFactory->create($options)->render();
Christian Kuhn's avatar
Christian Kuhn committed
266
267
268
269
270
271
272
273
274
275
276

				if (!empty($singleFieldContentArray['html'])) {
					$foundRealElement = TRUE;
					$resultStructure[] = array(
						'type' => 'single',
						'fieldName' => $fieldName,
						'fieldLabel' => $this->getSingleFieldLabel($fieldName, $fieldArray['fieldLabel']),
						'fieldHtml' => $singleFieldContentArray['html'],
					);
					$singleFieldContentArray['html'] = '';
				}
277
				$this->resultArray = $this->mergeChildReturnIntoExistingResult($this->resultArray, $singleFieldContentArray);
Christian Kuhn's avatar
Christian Kuhn committed
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
			}
		}

		if ($foundRealElement) {
			return $resultStructure;
		} else {
			return array();
		}
	}

	/**
	 * Renders inner content of single elements of a palette and wrap it as needed
	 *
	 * @param array $elementArray Array of elements
	 * @return string Wrapped content
	 */
	protected function renderInnerPaletteContent(array $elementArray) {
		// Group fields
		$groupedFields = array();
		$row = 0;
		$lastLineWasLinebreak = TRUE;
		foreach ($elementArray['elements'] as $element) {
			if ($element['type'] === 'linebreak') {
				if (!$lastLineWasLinebreak) {
					$row++;
					$groupedFields[$row][] = $element;
					$row++;
					$lastLineWasLinebreak = TRUE;
				}
			} else {
				$lastLineWasLinebreak = FALSE;
				$groupedFields[$row][] = $element;
			}
		}

		$result = array();
		// Process fields
		foreach ($groupedFields as $fields) {
			$numberOfItems = count($fields);
			$colWidth = (int)floor(12 / $numberOfItems);
			// Column class calculation
			$colClass = "col-md-12";
			$colClear = array();
			if ($colWidth == 6) {
				$colClass = "col-sm-6";
				$colClear = array(
					2 => 'visible-sm-block visible-md-block visible-lg-block',
				);
			} elseif ($colWidth === 4) {
				$colClass = "col-sm-4";
				$colClear = array(
					3 => 'visible-sm-block visible-md-block visible-lg-block',
				);
			} elseif ($colWidth === 3) {
				$colClass = "col-sm-6 col-md-3";
				$colClear = array(
					2 => 'visible-sm-block',
					4 => 'visible-md-block visible-lg-block',
				);
			} elseif ($colWidth <= 2) {
				$colClass = "checkbox-column col-sm-6 col-md-3 col-lg-2";
				$colClear = array(
					2 => 'visible-sm-block',
					4 => 'visible-md-block',
					6 => 'visible-lg-block'
				);
			}

			// Render fields
			for ($counter = 0; $counter < $numberOfItems; $counter++) {
				$element = $fields[$counter];
				if ($element['type'] === 'linebreak') {
					if ($counter !== $numberOfItems) {
						$result[] = '<div class="clearfix"></div>';
					}
				} else {
354
					$result[] = $this->wrapSingleFieldContentWithLabelAndOuterDiv($element, array($colClass));
Christian Kuhn's avatar
Christian Kuhn committed
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424

					// Breakpoints
					if ($counter + 1 < $numberOfItems && !empty($colClear)) {
						foreach ($colClear as $rowBreakAfter => $clearClass) {
							if (($counter + 1) % $rowBreakAfter === 0) {
								$result[] = '<div class="clearfix '. $clearClass . '"></div>';
							}
						}
					}
				}
			}
		}

		return implode(LF, $result);
	}

	/**
	 * Add a "collapsible" button around given content
	 *
	 * @param string $elementHtml HTML of handled palette content
	 * @param string $cssId A css id to be added
	 * @return string Wrapped content
	 */
	protected function wrapPaletteWithCollapseButton($elementHtml, $cssId) {
		$content = array();
		$content[] = '<p>';
		$content[] = 	'<button class="btn btn-default" type="button" data-toggle="collapse" data-target="#' . $cssId . '" aria-expanded="false" aria-controls="' . $cssId . '">';
		$content[] = 		IconUtility::getSpriteIcon('actions-system-options-view');
		$content[] = 		htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:labels.moreOptions'));
		$content[] = 	'</button>';
		$content[] = '</p>';
		$content[] = '<div id="' . $cssId . '" class="form-section-collapse collapse">';
		$content[] = 	'<div class="row">' . $elementHtml . '</div>';
		$content[] = '</div>';
		return implode(LF, $content);
	}

	/**
	 * Wrap content in a field set
	 *
	 * @param string $content Incoming content
	 * @param bool $paletteHidden TRUE if the palette is hidden
	 * @param string $label Given label
	 * @return string Wrapped content
	 */
	protected function fieldSetWrap($content, $paletteHidden = FALSE, $label = '') {
		$fieldSetClass = 'form-section';
		if ($paletteHidden) {
			$fieldSetClass = 'hide';
		}

		$result = array();
		$result[] = '<fieldset class="' . $fieldSetClass . '">';

		if (!empty($label)) {
			$result[] = '<h4 class="form-section-headline">' . htmlspecialchars($label) . '</h4>';
		}

		$result[] = $content;
		$result[] = '</fieldset>';
		return implode(LF, $result);
	}

	/**
	 * Wrap a single element
	 *
	 * @param array $element Given element as documented above
	 * @param array $additionalPaletteClasses Additional classes to be added to HTML
	 * @return string Wrapped element
	 */
425
	protected function wrapSingleFieldContentWithLabelAndOuterDiv(array $element, array $additionalPaletteClasses = array()) {
Christian Kuhn's avatar
Christian Kuhn committed
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
		$fieldName = $element['fieldName'];

		$paletteFieldClasses = array(
			'form-group',
			't3js-formengine-palette-field',
		);
		foreach ($additionalPaletteClasses as $class) {
			$paletteFieldClasses[] = $class;
		}

		$label = BackendUtility::wrapInHelp($this->globalOptions['table'], $fieldName, htmlspecialchars($element['fieldLabel']));

		$content = array();
		$content[] = '<div class="' . implode(' ', $paletteFieldClasses) . '">';
		$content[] = 	'<label class="t3js-formengine-label">';
		$content[] = 		$label;
		$content[] = 		'<img name="req_' . $this->globalOptions['table'] . '_' . $this->globalOptions['databaseRow']['uid'] . '_' . $fieldName . '" src="clear.gif" class="t3js-formengine-field-required" alt="" />';
		$content[] = 	'</label>';
444
		$content[] = 	$element['fieldHtml'];
Christian Kuhn's avatar
Christian Kuhn committed
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
		$content[] = '</div>';

		return implode(LF, $content);
	}

	/**
	 * Determine label of a single field (not a palette label)
	 *
	 * @param string $fieldName The field name to calculate the label for
	 * @param string $labelFromShowItem Given label, typically from show item configuration
	 * @return string Field label
	 */
	protected function getSingleFieldLabel($fieldName, $labelFromShowItem) {
		$languageService = $this->getLanguageService();
		$table = $this->globalOptions['table'];
		$label = $labelFromShowItem;
		if (!empty($GLOBALS['TCA'][$table]['columns'][$fieldName]['label'])) {
			$label = $GLOBALS['TCA'][$table]['columns'][$fieldName]['label'];
		}
		if (!empty($labelFromShowItem)) {
			$label = $labelFromShowItem;
		}
		$fieldTSConfig = FormEngineUtility::getTSconfigForTableRow($table, $this->globalOptions['databaseRow'], $fieldName);
		if (!empty($fieldTSConfig['label'])) {
			$label = $fieldTSConfig['label'];
		}
		if (!empty($fieldTSConfig['label.'][$languageService->lang])) {
			$label = $fieldTSConfig['label.'][$languageService->lang];
		}
		return $languageService->sL($label);
	}

	/**
	 * TRUE if field is of type user and to wrapping is requested
	 *
	 * @param array $element Current element from "target structure" array
	 * @return boolean TRUE if user and noTableWrapping is set
	 */
	protected function isUserNoTableWrappingField($element) {
		$table = $this->globalOptions['table'];
		$fieldName = $element['fieldName'];
		if (
			$GLOBALS['TCA'][$table]['columns'][$fieldName]['config']['type'] === 'user'
			&& !empty($GLOBALS['TCA'][$table]['columns'][$fieldName]['config']['noTableWrapping'])
		) {
			return TRUE;
		}
		return FALSE;
	}

	/**
	 * @return LanguageService
	 */
	protected function getLanguageService() {
		return $GLOBALS['LANG'];
	}

}