FlexFormElementContainer.php 11.3 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
<?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\Backend\Form\ElementConditionMatcher;
18
use TYPO3\CMS\Core\Migrations\TcaMigration;
Christian Kuhn's avatar
Christian Kuhn committed
19
20
21
22
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
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Lang\LanguageService;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Type\Bitmask\JsConfirmation;
use TYPO3\CMS\Backend\Form\NodeFactory;
use TYPO3\CMS\Backend\Form\Utility\FormEngineUtility;
use TYPO3\CMS\Backend\Utility\BackendUtility;

/**
 * The container handles single elements.
 *
 * This one is called by FlexFormTabsContainer, FlexFormNoTabsContainer or FlexFormContainerContainer.
 * For single fields, the code is similar to SingleFieldContainer, processing will end up in single
 * element classes depending on specific type of an element. Additionally, it determines if a
 * section is handled and hands over to FlexFormSectionContainer in this case.
 */
class FlexFormElementContainer extends AbstractContainer {

	/**
	 * Entry method
	 *
	 * @return array As defined in initializeResultArray() of AbstractNode
	 */
	public function render() {
		$table = $this->globalOptions['table'];
		$row = $this->globalOptions['databaseRow'];
		$fieldName = $this->globalOptions['fieldName'];
		$flexFormDataStructureArray = $this->globalOptions['flexFormDataStructureArray'];
		$flexFormRowData = $this->globalOptions['flexFormRowData'];
		$flexFormCurrentLanguage = $this->globalOptions['flexFormCurrentLanguage'];
		$flexFormNoEditDefaultLanguage = $this->globalOptions['flexFormNoEditDefaultLanguage'];
		$flexFormFormPrefix = $this->globalOptions['flexFormFormPrefix'];
		$parameterArray = $this->globalOptions['parameterArray'];

		$languageService = $this->getLanguageService();
		$resultArray = $this->initializeResultArray();
		foreach ($flexFormDataStructureArray as $flexFormFieldName => $flexFormFieldArray) {
			if (
				// No item array found at all
				!is_array($flexFormFieldArray)
				// Not a section or container and not a list of single items
				|| (!isset($flexFormFieldArray['type']) && !is_array($flexFormFieldArray['TCEforms']['config']))
			) {
				continue;
			}

			if ($flexFormFieldArray['type'] === 'array') {
				// Section
				if (empty($flexFormFieldArray['section'])) {
					$resultArray['html'] = LF . 'Section expected at ' . $flexFormFieldName . ' but not found';
					continue;
				}

				$sectionTitle = '';
				if (!empty($flexFormFieldArray['title'])) {
					$sectionTitle = $languageService->sL($flexFormFieldArray['title']);
				}

				$options = $this->globalOptions;
				$options['flexFormDataStructureArray'] = $flexFormFieldArray['el'];
79
				$options['flexFormRowData'] = is_array($flexFormRowData[$flexFormFieldName]['el']) ? $flexFormRowData[$flexFormFieldName]['el'] : array();
Christian Kuhn's avatar
Christian Kuhn committed
80
81
				$options['flexFormSectionType'] = $flexFormFieldName;
				$options['flexFormSectionTitle'] = $sectionTitle;
82
				$options['renderType'] = 'flexFormSectionContainer';
83
84
85
				/** @var NodeFactory $nodeFactory */
				$nodeFactory = $this->globalOptions['nodeFactory'];
				$sectionContainerResult = $nodeFactory->create($options)->render();
Christian Kuhn's avatar
Christian Kuhn committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
				$resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $sectionContainerResult);
			} else {
				// Single element
				$vDEFkey = 'vDEF';

				$displayConditionResult = TRUE;
				if (!empty($flexFormFieldArray['TCEforms']['displayCond'])) {
					$conditionData = is_array($flexFormRowData) ? $flexFormRowData : array();
					$conditionData['parentRec'] = $row;
					/** @var $elementConditionMatcher ElementConditionMatcher */
					$elementConditionMatcher = GeneralUtility::makeInstance(ElementConditionMatcher::class);
					$displayConditionResult = $elementConditionMatcher->match($flexFormFieldArray['TCEforms']['displayCond'], $conditionData, $vDEFkey);
				}
				if (!$displayConditionResult) {
					continue;
				}

103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
				// On-the-fly migration for flex form "TCA"
				// @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8. This can be removed *if* no additional TCA migration is added with CMS 8, see class TcaMigration
				$dummyTca = array(
					'dummyTable' => array(
						'columns' => array(
							'dummyField' => $flexFormFieldArray['TCEforms'],
						),
					),
				);
				$tcaMigration = GeneralUtility::makeInstance(TcaMigration::class);
				$migratedTca = $tcaMigration->migrate($dummyTca);
				$messages = $tcaMigration->getMessages();
				if (!empty($messages)) {
					$context = 'FormEngine did an on-the-fly migration of a flex form data structure. This is deprecated and will be removed'
						. ' with TYPO3 CMS 8. Merge the following changes into the flex form definition of table ' . $table . ' in field ' . $fieldName . ':';
					array_unshift($messages, $context);
					GeneralUtility::deprecationLog(implode(LF, $messages));
				}
				$flexFormFieldArray['TCEforms'] = $migratedTca['dummyTable']['columns']['dummyField'];

Christian Kuhn's avatar
Christian Kuhn committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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
				// Set up options for single element
				$fakeParameterArray = array(
					'fieldConf' => array(
						'label' => $languageService->sL(trim($flexFormFieldArray['TCEforms']['label'])),
						'config' => $flexFormFieldArray['TCEforms']['config'],
						'defaultExtras' => $flexFormFieldArray['TCEforms']['defaultExtras'],
						'onChange' => $flexFormFieldArray['TCEforms']['onChange'],
					),
				);

				// Force a none field if default language can not be edited
				if ($flexFormNoEditDefaultLanguage && $flexFormCurrentLanguage === 'lDEF') {
					$fakeParameterArray['fieldConf']['config'] = array(
						'type' => 'none',
						'rows' => 2
					);
				}

				$alertMsgOnChange = '';
				if (
					$fakeParameterArray['fieldConf']['onChange'] === 'reload'
					|| !empty($GLOBALS['TCA'][$table]['ctrl']['type']) && $GLOBALS['TCA'][$table]['ctrl']['type'] === $flexFormFieldName
					|| !empty($GLOBALS['TCA'][$table]['ctrl']['requestUpdate']) && GeneralUtility::inList($GLOBALS['TCA'][$table]['ctrl']['requestUpdate'], $flexFormFieldName)
				) {
					if ($this->getBackendUserAuthentication()->jsConfirmation(JsConfirmation::TYPE_CHANGE)) {
						$alertMsgOnChange = 'if (confirm(TBE_EDITOR.labels.onChangeAlert) && TBE_EDITOR.checkSubmit(-1)){ TBE_EDITOR.submitForm() };';
					} else {
						$alertMsgOnChange = 'if (TBE_EDITOR.checkSubmit(-1)){ TBE_EDITOR.submitForm();}';
					}
				}
				$fakeParameterArray['fieldChangeFunc'] = $parameterArray['fieldChangeFunc'];
				if ($alertMsgOnChange) {
					$fakeParameterArray['fieldChangeFunc']['alert'] = $alertMsgOnChange;
				}

				$fakeParameterArray['onFocus'] = $parameterArray['onFocus'];
				$fakeParameterArray['label'] = $parameterArray['label'];
				$fakeParameterArray['itemFormElName'] = $parameterArray['itemFormElName'] . $flexFormFormPrefix . '[' . $flexFormFieldName . '][' . $vDEFkey . ']';
				$fakeParameterArray['itemFormElID'] = $fakeParameterArray['itemFormElName'];
				if (isset($flexFormRowData[$flexFormFieldName][$vDEFkey])) {
					$fakeParameterArray['itemFormElValue'] = $flexFormRowData[$flexFormFieldName][$vDEFkey];
				} else {
					$fakeParameterArray['itemFormElValue'] = $fakeParameterArray['fieldConf']['config']['default'];
				}

				$options = $this->globalOptions;
				$options['parameterArray'] = $fakeParameterArray;
170
				$options['elementBaseName'] = $this->globalOptions['elementBaseName'] . $flexFormFormPrefix . '[' . $flexFormFieldName . '][' . $vDEFkey . ']';
171
172
173
174
175
176
177

				if (!empty($flexFormFieldArray['TCEforms']['config']['renderType'])) {
					$options['renderType'] = $flexFormFieldArray['TCEforms']['config']['renderType'];
				} else {
					// Fallback to type if no renderType is given
					$options['renderType'] = $flexFormFieldArray['TCEforms']['config']['type'];
				}
Christian Kuhn's avatar
Christian Kuhn committed
178
				/** @var NodeFactory $nodeFactory */
179
180
				$nodeFactory = $this->globalOptions['nodeFactory'];
				$childResult = $nodeFactory->create($options)->render();
Christian Kuhn's avatar
Christian Kuhn committed
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
220
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

				$theTitle = htmlspecialchars($fakeParameterArray['fieldConf']['label']);
				$defInfo = array();
				if (!$flexFormNoEditDefaultLanguage) {
					$previewLanguages = $this->globalOptions['additionalPreviewLanguages'];
					foreach ($previewLanguages as $previewLanguage) {
						$defInfo[] = '<div class="t3-form-original-language">';
						$defInfo[] = 	FormEngineUtility::getLanguageIcon($table, $row, ('v' . $previewLanguage['ISOcode']));
						$defInfo[] = 	$this->previewFieldValue($flexFormRowData[$flexFormFieldName][('v' . $previewLanguage['ISOcode'])], $fakeParameterArray['fieldConf'], $fieldName);
						$defInfo[] = '</div>';
					}
				}

				$languageIcon = '';
				if ($vDEFkey !== 'vDEF') {
					$languageIcon = FormEngineUtility::getLanguageIcon($table, $row, $vDEFkey);
				}
				// Possible line breaks in the label through xml: \n => <br/>, usage of nl2br() not possible, so it's done through str_replace (?!)
				$processedTitle = str_replace('\\n', '<br />', $theTitle);
				// @todo: Similar to the processing within SingleElementContainer ... use it from there?!
				$html = array();
				$html[] = '<div class="form-section">';
				$html[] = 	'<div class="form-group t3js-formengine-palette-field">';
				$html[] = 		'<label class="t3js-formengine-label">';
				$html[] = 			$languageIcon;
				$html[] = 			BackendUtility::wrapInHelp($parameterArray['_cshKey'], $flexFormFieldName, $processedTitle);
				$html[] = 		'</label>';
				$html[] = 		'<div class="t3js-formengine-field-item">';
				$html[] = 			$childResult['html'];
				$html[] = 			implode(LF, $defInfo);
				$html[] = 			$this->renderVDEFDiff($flexFormRowData[$flexFormFieldName], $vDEFkey);
				$html[]	= 		'</div>';
				$html[] = 	'</div>';
				$html[] = '</div>';

				$resultArray['html'] .= implode(LF, $html);
				$childResult['html'] = '';
				$resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $childResult);
			}
		}

		return $resultArray;
	}

	/**
	 * Renders the diff-view of vDEF fields in flex forms
	 *
	 * @param array $vArray Record array of the record being edited
	 * @param string $vDEFkey HTML of the form field. This is what we add the content to.
	 * @return string Item string returned again, possibly with the original value added to.
	 */
	protected function renderVDEFDiff($vArray, $vDEFkey) {
		$item = NULL;
		if (
			$GLOBALS['TYPO3_CONF_VARS']['BE']['flexFormXMLincludeDiffBase'] && isset($vArray[$vDEFkey . '.vDEFbase'])
			&& (string)$vArray[$vDEFkey . '.vDEFbase'] !== (string)$vArray['vDEF']
		) {
			// Create diff-result:
			$diffUtility = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Utility\DiffUtility::class);
			$diffres = $diffUtility->makeDiffDisplay($vArray[$vDEFkey . '.vDEFbase'], $vArray['vDEF']);
			$item = '<div class="typo3-TCEforms-diffBox">' . '<div class="typo3-TCEforms-diffBox-header">'
				. htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:labels.changeInOrig')) . ':</div>' . $diffres . '</div>';
		}
		return $item;
	}

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

	/**
	 * @return BackendUserAuthentication
	 */
	protected function getBackendUserAuthentication() {
		return $GLOBALS['BE_USER'];
	}

}