75da98f4e0bb90d876f3b203b0ed7528f9e16c9f
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / Container / SingleFieldContainer.php
1 <?php
2 namespace TYPO3\CMS\Backend\Form\Container;
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\Form\ElementConditionMatcher;
18 use TYPO3\CMS\Backend\Form\NodeFactory;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
21 use TYPO3\CMS\Backend\Form\Utility\FormEngineUtility;
22 use TYPO3\CMS\Core\Utility\ArrayUtility;
23 use TYPO3\CMS\Lang\LanguageService;
24 use TYPO3\CMS\Backend\Utility\BackendUtility;
25 use TYPO3\CMS\Backend\Utility\IconUtility;
26 use TYPO3\CMS\Core\Utility\DiffUtility;
27 use TYPO3\CMS\Backend\Form\InlineStackProcessor;
28 use TYPO3\CMS\Core\Database\RelationHandler;
29
30 /**
31 * Container around a "single field".
32 *
33 * This container is the last one in the chain before processing is hand over to single element classes.
34 * If a single field is of type flex or inline, it however creates FlexFormContainer or InlineControlContainer.
35 *
36 * The container does various checks and processing for a given single fields, for example it resolves
37 * display conditions and the HTML to compare compare different languages.
38 */
39 class SingleFieldContainer extends AbstractContainer {
40
41 /**
42 * Entry method
43 *
44 * @return array As defined in initializeResultArray() of AbstractNode
45 */
46 public function render() {
47 $backendUser = $this->getBackendUserAuthentication();
48 $languageService = $this->getLanguageService();
49 $resultArray = $this->initializeResultArray();
50
51 $table = $this->globalOptions['table'];
52 $row = $this->globalOptions['databaseRow'];
53 $fieldName = $this->globalOptions['fieldName'];
54
55 if (!is_array($GLOBALS['TCA'][$table]['columns'][$fieldName])) {
56 return $resultArray;
57 }
58
59 $parameterArray = array();
60 $parameterArray['fieldConf'] = $GLOBALS['TCA'][$table]['columns'][$fieldName];
61
62 // Overlay fieldConf with possible defined columnsOverrides of given record type
63 $recordTypeValue = $this->globalOptions['recordTypeValue'];
64 // Hint: 0 is a valid $recordTypeValue, !empty() does not work here
65 if (trim($recordTypeValue) !== '' && is_array($GLOBALS['TCA'][$table]['types'][$recordTypeValue]['columnsOverrides'][$fieldName])) {
66 // Merge columnsOverrides of this field over existing field configuration
67 ArrayUtility::mergeRecursiveWithOverrule(
68 $parameterArray['fieldConf'],
69 $GLOBALS['TCA'][$table]['types'][$recordTypeValue]['columnsOverrides'][$fieldName]
70 );
71 }
72
73 // A couple of early returns in case the field should not be rendered
74 // Check if this field is configured and editable according to exclude fields and other configuration
75 if (
76 $parameterArray['fieldConf']['exclude'] && !$backendUser->check('non_exclude_fields', $table . ':' . $fieldName)
77 || $parameterArray['fieldConf']['config']['type'] === 'passthrough'
78 || !$backendUser->isRTE() && $parameterArray['fieldConf']['config']['showIfRTE']
79 || $GLOBALS['TCA'][$table]['ctrl']['languageField'] && !$parameterArray['fieldConf']['l10n_display'] && $parameterArray['fieldConf']['l10n_mode'] === 'exclude' && ($row[$GLOBALS['TCA'][$table]['ctrl']['languageField']] > 0)
80 || $GLOBALS['TCA'][$table]['ctrl']['languageField'] && $this->globalOptions['localizationMode'] && $this->globalOptions['localizationMode'] !== $parameterArray['fieldConf']['l10n_cat']
81 || $this->inlineFieldShouldBeSkipped()
82 ) {
83 return $resultArray;
84 }
85 // Evaluate display condition
86 if ($parameterArray['fieldConf']['displayCond'] && is_array($row)) {
87 // @todo: isn't $row = array() safe somewhere above already?
88 /** @var $elementConditionMatcher ElementConditionMatcher */
89 $elementConditionMatcher = GeneralUtility::makeInstance(ElementConditionMatcher::class);
90 if (!$elementConditionMatcher->match($parameterArray['fieldConf']['displayCond'], $row)) {
91 return $resultArray;
92 }
93 }
94 // Fetching the TSconfig for the current table/field. This includes the $row which means that
95 $parameterArray['fieldTSConfig'] = FormEngineUtility::getTSconfigForTableRow($table, $row, $fieldName);
96 if ($parameterArray['fieldTSConfig']['disabled']) {
97 return $resultArray;
98 }
99
100 // Override fieldConf by fieldTSconfig:
101 $parameterArray['fieldConf']['config'] = FormEngineUtility::overrideFieldConf($parameterArray['fieldConf']['config'], $parameterArray['fieldTSConfig']);
102 $parameterArray['itemFormElName'] = 'data[' . $table . '][' . $row['uid'] . '][' . $fieldName . ']';
103 $parameterArray['itemFormElID'] = 'data_' . $table . '_' . $row['uid'] . '_' . $fieldName;
104 $newElementBaseName = $this->globalOptions['elementBaseName'] . '[' . $table . '][' . $row['uid'] . '][' . $fieldName . ']';
105
106 // The value to show in the form field.
107 $parameterArray['itemFormElValue'] = $row[$fieldName];
108 // Set field to read-only if configured for translated records to show default language content as readonly
109 if ($parameterArray['fieldConf']['l10n_display']
110 && GeneralUtility::inList($parameterArray['fieldConf']['l10n_display'], 'defaultAsReadonly')
111 && $row[$GLOBALS['TCA'][$table]['ctrl']['languageField']] > 0
112 ) {
113 $parameterArray['fieldConf']['config']['readOnly'] = TRUE;
114 $parameterArray['itemFormElValue'] = $this->globalOptions['defaultLanguageData'][$table . ':' . $row['uid']][$fieldName];
115 }
116
117 if (strpos($GLOBALS['TCA'][$table]['ctrl']['type'], ':') === FALSE) {
118 $typeField = $GLOBALS['TCA'][$table]['ctrl']['type'];
119 } else {
120 $typeField = substr($GLOBALS['TCA'][$table]['ctrl']['type'], 0, strpos($GLOBALS['TCA'][$table]['ctrl']['type'], ':'));
121 }
122 // Create a JavaScript code line which will ask the user to save/update the form due to changing the element.
123 // This is used for eg. "type" fields and others configured with "requestUpdate"
124 if (
125 !empty($GLOBALS['TCA'][$table]['ctrl']['type'])
126 && $fieldName === $typeField
127 || !empty($GLOBALS['TCA'][$table]['ctrl']['requestUpdate'])
128 && GeneralUtility::inList(str_replace(' ', '', $GLOBALS['TCA'][$table]['ctrl']['requestUpdate']), $fieldName)
129 ) {
130 if ($backendUser->jsConfirmation(1)) {
131 $alertMsgOnChange = 'if (confirm(TBE_EDITOR.labels.onChangeAlert) && TBE_EDITOR.checkSubmit(-1)){ TBE_EDITOR.submitForm() };';
132 } else {
133 $alertMsgOnChange = 'if (TBE_EDITOR.checkSubmit(-1)){ TBE_EDITOR.submitForm() };';
134 }
135 } else {
136 $alertMsgOnChange = '';
137 }
138
139
140 if (in_array($fieldName, $this->globalOptions['hiddenFieldListArray'], TRUE)) {
141 // Render as a hidden field if this field had a forced value in overrideVals
142 // @todo: This is an ugly concept ... search for overrideVals and defVals for a full picture of this madness
143 $resultArray = $this->initializeResultArray();
144 // This hidden field can not just be returned as casual html since upper containers will then render a label and wrapping stuff - this is not wanted here
145 $resultArray['additionalHiddenFields'][] = '<input type="hidden" name="' . $parameterArray['itemFormElName'] . '" value="' . htmlspecialchars($parameterArray['itemFormElValue']) . '" />';
146 } else {
147 // JavaScript code for event handlers:
148 $parameterArray['fieldChangeFunc'] = array();
149 $parameterArray['fieldChangeFunc']['TBE_EDITOR_fieldChanged'] = 'TBE_EDITOR.fieldChanged(' . GeneralUtility::quoteJSvalue($table) . ',' . GeneralUtility::quoteJSvalue($row['uid']) . ',' . GeneralUtility::quoteJSvalue($fieldName) . ',' . GeneralUtility::quoteJSvalue($parameterArray['itemFormElName']) . ');';
150 $parameterArray['fieldChangeFunc']['alert'] = $alertMsgOnChange;
151
152 // If this is the child of an inline type and it is the field creating the label
153 if ($this->isInlineChildAndLabelField($table, $fieldName)) {
154 /** @var InlineStackProcessor $inlineStackProcessor */
155 $inlineStackProcessor = GeneralUtility::makeInstance(InlineStackProcessor::class);
156 $inlineStackProcessor->initializeByGivenStructure($this->globalOptions['inlineStructure']);
157 $inlineDomObjectId = $inlineStackProcessor->getCurrentStructureDomObjectIdPrefix($this->globalOptions['inlineFirstPid']);
158 $inlineObjectId = implode(
159 '-',
160 array(
161 $inlineDomObjectId,
162 $table,
163 $row['uid']
164 )
165 );
166 $parameterArray['fieldChangeFunc']['inline'] = 'inline.handleChangedField(' . GeneralUtility::quoteJSvalue($parameterArray['itemFormElName']) . ',' . GeneralUtility::quoteJSvalue($inlineObjectId) . ');';
167 }
168
169 // Based on the type of the item, call a render function on a child element
170 $options = $this->globalOptions;
171 $options['parameterArray'] = $parameterArray;
172 $options['elementBaseName'] = $newElementBaseName;
173 if (!empty($parameterArray['fieldConf']['config']['renderType'])) {
174 $options['renderType'] = $parameterArray['fieldConf']['config']['renderType'];
175 } else {
176 // Fallback to type if no renderType is given
177 $options['renderType'] = $parameterArray['fieldConf']['config']['type'];
178 }
179 /** @var NodeFactory $nodeFactory */
180 $nodeFactory = $this->globalOptions['nodeFactory'];
181 $resultArray = $nodeFactory->create($options)->render();
182 $html = $resultArray['html'];
183
184 // @todo: the language handling, the null and the placeholder stuff should be embedded in the single
185 // @todo: element classes. Basically, this method should return here and have the element classes
186 // @todo: decide on language stuff and other wraps already.
187
188 // Add language + diff
189 $renderLanguageDiff = TRUE;
190 if (
191 $parameterArray['fieldConf']['l10n_display'] && (GeneralUtility::inList($parameterArray['fieldConf']['l10n_display'], 'hideDiff')
192 || GeneralUtility::inList($parameterArray['fieldConf']['l10n_display'], 'defaultAsReadonly'))
193 ) {
194 $renderLanguageDiff = FALSE;
195 }
196 if ($renderLanguageDiff) {
197 $html = $this->renderDefaultLanguageContent($table, $fieldName, $row, $html);
198 $html = $this->renderDefaultLanguageDiff($table, $fieldName, $row, $html);
199 }
200
201 $fieldItemClasses = array(
202 't3js-formengine-field-item'
203 );
204
205 // NULL value and placeholder handling
206 $nullControlNameAttribute = ' name="' . htmlspecialchars('control[active][' . $table . '][' . $row['uid'] . '][' . $fieldName . ']') . '"';
207 if (!empty($parameterArray['fieldConf']['config']['eval']) && GeneralUtility::inList($parameterArray['fieldConf']['config']['eval'], 'null')
208 && (empty($parameterArray['fieldConf']['config']['mode']) || $parameterArray['fieldConf']['config']['mode'] !== 'useOrOverridePlaceholder')
209 ) {
210 // This field has eval=null set, but has no useOverridePlaceholder defined.
211 // Goal is to have a field that can distinct between NULL and empty string in the database.
212 // A checkbox and an additional hidden field will be created, both with the same name
213 // and prefixed with "control[active]". If the checkbox is set (value 1), the value from the casual
214 // input field will be written to the database. If the checkbox is not set, the hidden field
215 // transfers value=0 to DataHandler, the value of the input field will then be reset to NULL by the
216 // DataHandler at an early point in processing, so NULL will be written to DB as field value.
217
218 // If the value of the field *is* NULL at the moment, an additional class is set
219 // @todo: This does not work well at the moment, but is kept for now. see input_14 of ext:styleguide as example
220 $checked = ' checked="checked"';
221 if ($this->globalOptions['databaseRow'][$fieldName] === NULL) {
222 $fieldItemClasses[] = 'disabled';
223 $checked = '';
224 }
225
226 $formElementName = 'data[' . $table . '][' . $row['uid'] . '][' . $fieldName . ']';
227 $onChange = htmlspecialchars(
228 'typo3form.fieldSetNull(' . GeneralUtility::quoteJSvalue($formElementName) . ', !this.checked)'
229 );
230
231 $nullValueWrap = array();
232 $nullValueWrap[] = '<div class="' . implode(' ', $fieldItemClasses) . '">';
233 $nullValueWrap[] = '<div class="t3-form-field-disable"></div>';
234 $nullValueWrap[] = '<div class="checkbox">';
235 $nullValueWrap[] = '<label>';
236 $nullValueWrap[] = '<input type="hidden"' . $nullControlNameAttribute . ' value="0" />';
237 $nullValueWrap[] = '<input type="checkbox"' . $nullControlNameAttribute . ' value="1" onchange="' . $onChange . '"' . $checked . ' /> &nbsp;';
238 $nullValueWrap[] = '</label>';
239 $nullValueWrap[] = $html;
240 $nullValueWrap[] = '</div>';
241 $nullValueWrap[] = '</div>';
242
243 $html = implode(LF, $nullValueWrap);
244 } elseif (isset($parameterArray['fieldConf']['config']['mode']) && $parameterArray['fieldConf']['config']['mode'] === 'useOrOverridePlaceholder') {
245 // This field has useOverridePlaceholder set.
246 // Here, a value from a deeper DB structure can be "fetched up" as value, and can also be overridden by a
247 // local value. This is used in FAL, where eg. the "title" field can have the default value from sys_file_metadata,
248 // the title field of sys_file_reference is then set to NULL. Or the "override" checkbox is set, and a string
249 // or an empty string is then written to the field of sys_file_reference.
250 // The situation is similar to the NULL handling above, but additionally a "default" value should be shown.
251 // To achieve this, again a hidden control[hidden] field is added together with a checkbox with the same name
252 // to transfer the information whether the default value should be used or not: Checkbox checked transfers 1 as
253 // value in control[active], meaning the overridden value should be used.
254 // Additionally to the casual input field, a second field is added containing the "placeholder" value. This
255 // field has no name attribute and is not transferred at all. Those two are then hidden / shown depending
256 // on the state of the above checkbox in via JS.
257
258 $placeholder = $this->getPlaceholderValue($table, $parameterArray['fieldConf']['config'], $row);
259 $onChange = 'typo3form.fieldTogglePlaceholder(' . GeneralUtility::quoteJSvalue($parameterArray['itemFormElName']) . ', !this.checked)';
260 $checked = $parameterArray['itemFormElValue'] === NULL ? '' : ' checked="checked"';
261
262 $resultArray['additionalJavaScriptPost'][] = 'typo3form.fieldTogglePlaceholder('
263 . GeneralUtility::quoteJSvalue($parameterArray['itemFormElName']) . ', ' . ($checked ? 'false' : 'true') . ');';
264
265 // Renders a input or textarea field depending on type of "parent"
266 $options = array();
267 $options['databaseRow'] = array();
268 $options['table'] = '';
269 $options['parameterArray'] = $parameterArray;
270 $options['parameterArray']['itemFormElValue'] = GeneralUtility::fixed_lgd_cs($placeholder, 30);
271 $options['renderType'] = 'none';
272 /** @var NodeFactory $nodeFactory */
273 $nodeFactory = $this->globalOptions['nodeFactory'];
274 $noneElementResult = $nodeFactory->create($options)->render();
275 $noneElementHtml = $noneElementResult['html'];
276
277 $placeholderWrap = array();
278 $placeholderWrap[] = '<div class="' . implode(' ', $fieldItemClasses) . '">';
279 $placeholderWrap[] = '<div class="t3-form-field-disable"></div>';
280 $placeholderWrap[] = '<div class="checkbox">';
281 $placeholderWrap[] = '<label>';
282 $placeholderWrap[] = '<input type="hidden"' . $nullControlNameAttribute . ' value="0" />';
283 $placeholderWrap[] = '<input type="checkbox"' . $nullControlNameAttribute . ' value="1" id="tce-forms-textfield-use-override-' . $fieldName . '-' . $row['uid'] . '" onchange="' . htmlspecialchars($onChange) . '"' . $checked . ' />';
284 $placeholderWrap[] = sprintf($languageService->sL('LLL:EXT:lang/locallang_core.xlf:labels.placeholder.override'), BackendUtility::getRecordTitlePrep($placeholder, 20));
285 $placeholderWrap[] = '</label>';
286 $placeholderWrap[] = '</div>';
287 $placeholderWrap[] = '<div class="t3js-formengine-placeholder-placeholder">';
288 $placeholderWrap[] = $noneElementHtml;
289 $placeholderWrap[] = '</div>';
290 $placeholderWrap[] = '<div class="t3js-formengine-placeholder-formfield">';
291 $placeholderWrap[] = $html;
292 $placeholderWrap[] = '</div>';
293 $placeholderWrap[] = '</div>';
294
295 $html = implode(LF, $placeholderWrap);
296 } elseif ($parameterArray['fieldConf']['config']['type'] !== 'user' || empty($parameterArray['fieldConf']['config']['noTableWrapping'])) {
297 // Add a casual wrap if the field is not of type user with no wrap requested.
298 $standardWrap = array();
299 $standardWrap[] = '<div class="' . implode(' ', $fieldItemClasses) . '">';
300 $standardWrap[] = '<div class="t3-form-field-disable"></div>';
301 $standardWrap[] = $html;
302 $standardWrap[] = '</div>';
303
304 $html = implode(LF, $standardWrap);
305 }
306
307 $resultArray['html'] = $html;
308 }
309 return $resultArray;
310 }
311
312 /**
313 * Renders the display of default language record content around current field.
314 * Will render content if any is found in the internal array, $this->defaultLanguageData,
315 * depending on registerDefaultLanguageData() being called prior to this.
316 *
317 * @param string $table Table name of the record being edited
318 * @param string $field Field name represented by $item
319 * @param array $row Record array of the record being edited
320 * @param string $item HTML of the form field. This is what we add the content to.
321 * @return string Item string returned again, possibly with the original value added to.
322 */
323 protected function renderDefaultLanguageContent($table, $field, $row, $item) {
324 if (is_array($this->globalOptions['defaultLanguageData'][$table . ':' . $row['uid']])) {
325 $defaultLanguageValue = BackendUtility::getProcessedValue(
326 $table,
327 $field,
328 $this->globalOptions['defaultLanguageData'][$table . ':' . $row['uid']][$field],
329 0,
330 1,
331 FALSE,
332 $this->globalOptions['defaultLanguageData'][$table . ':' . $row['uid']]['uid']
333 );
334 $fieldConfig = $GLOBALS['TCA'][$table]['columns'][$field];
335 // Don't show content if it's for IRRE child records:
336 if ($fieldConfig['config']['type'] !== 'inline') {
337 if ($defaultLanguageValue !== '') {
338 $item .= '<div class="t3-form-original-language">' . FormEngineUtility::getLanguageIcon($table, $row, 0)
339 . $this->getMergeBehaviourIcon($fieldConfig['l10n_mode'])
340 . $this->previewFieldValue($defaultLanguageValue, $fieldConfig, $field) . '</div>';
341 }
342 $additionalPreviewLanguages = $this->globalOptions['additionalPreviewLanguages'];
343 foreach ($additionalPreviewLanguages as $previewLanguage) {
344 $defaultLanguageValue = BackendUtility::getProcessedValue(
345 $table,
346 $field,
347 $this->globalOptions['additionalPreviewLanguageData'][$table . ':' . $row['uid']][$previewLanguage['uid']][$field],
348 0,
349 1
350 );
351 if ($defaultLanguageValue !== '') {
352 $item .= '<div class="t3-form-original-language">'
353 . FormEngineUtility::getLanguageIcon($table, $row, ('v' . $previewLanguage['ISOcode']))
354 . $this->getMergeBehaviourIcon($fieldConfig['l10n_mode'])
355 . $this->previewFieldValue($defaultLanguageValue, $fieldConfig, $field) . '</div>';
356 }
357 }
358 }
359 }
360 return $item;
361 }
362
363 /**
364 * Renders an icon to indicate the way the translation and the original is merged (if this is relevant).
365 *
366 * If a field is defined as 'mergeIfNotBlank' this is useful information for an editor. He/she can leave the field blank and
367 * the original value will be used. Without this hint editors are likely to copy the contents even if it is not necessary.
368 *
369 * @param string $l10nMode Localization mode from TCA
370 * @return string
371 */
372 protected function getMergeBehaviourIcon($l10nMode) {
373 $icon = '';
374 if ($l10nMode === 'mergeIfNotBlank') {
375 $icon = IconUtility::getSpriteIcon(
376 'actions-edit-merge-localization',
377 array('title' => $this->getLanguageService()->sL('LLL:EXT:lang/locallang_misc.xlf:localizeMergeIfNotBlank'))
378 );
379 }
380 return $icon;
381 }
382
383 /**
384 * Renders the diff-view of default language record content compared with what the record was originally translated from.
385 * Will render content if any is found in the internal array, $this->defaultLanguageData,
386 * depending on registerDefaultLanguageData() being called prior to this.
387 *
388 * @param string $table Table name of the record being edited
389 * @param string $field Field name represented by $item
390 * @param array $row Record array of the record being edited
391 * @param string $item HTML of the form field. This is what we add the content to.
392 * @return string Item string returned again, possibly with the original value added to.
393 */
394 protected function renderDefaultLanguageDiff($table, $field, $row, $item) {
395 if (is_array($this->globalOptions['defaultLanguageDataDiff'][$table . ':' . $row['uid']])) {
396 // Initialize:
397 $dLVal = array(
398 'old' => $this->globalOptions['defaultLanguageDataDiff'][$table . ':' . $row['uid']],
399 'new' => $this->globalOptions['defaultLanguageData'][$table . ':' . $row['uid']]
400 );
401 // There must be diff-data:
402 if (isset($dLVal['old'][$field])) {
403 if ((string)$dLVal['old'][$field] !== (string)$dLVal['new'][$field]) {
404 // Create diff-result:
405 $diffUtility = GeneralUtility::makeInstance(DiffUtility::class);
406 $diffres = $diffUtility->makeDiffDisplay(
407 BackendUtility::getProcessedValue($table, $field, $dLVal['old'][$field], 0, 1),
408 BackendUtility::getProcessedValue($table, $field, $dLVal['new'][$field], 0, 1)
409 );
410 $item .= '<div class="t3-form-original-language-diff">
411 <div class="t3-form-original-language-diffheader">' .
412 htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:labels.changeInOrig')) .
413 '</div>
414 <div class="t3-form-original-language-diffcontent">' . $diffres . '</div>
415 </div>';
416 }
417 }
418 }
419 return $item;
420 }
421
422 /**
423 * Checks if the $table is the child of a inline type AND the $field is the label field of this table.
424 * This function is used to dynamically update the label while editing. This has no effect on labels,
425 * that were processed by a FormEngine-hook on saving.
426 *
427 * @param string $table The table to check
428 * @param string $field The field on this table to check
429 * @return bool Is inline child and field is responsible for the label
430 */
431 protected function isInlineChildAndLabelField($table, $field) {
432 /** @var InlineStackProcessor $inlineStackProcessor */
433 $inlineStackProcessor = GeneralUtility::makeInstance(InlineStackProcessor::class);
434 $inlineStackProcessor->initializeByGivenStructure($this->globalOptions['inlineStructure']);
435 $level = $inlineStackProcessor->getStructureLevel(-1);
436 if ($level['config']['foreign_label']) {
437 $label = $level['config']['foreign_label'];
438 } else {
439 $label = $GLOBALS['TCA'][$table]['ctrl']['label'];
440 }
441 return $level['config']['foreign_table'] === $table && $label == $field ? TRUE : FALSE;
442 }
443
444 /**
445 * Rendering of inline fields should be skipped under certain circumstances
446 *
447 * @return boolean TRUE if field should be skipped based on inline configuration
448 */
449 protected function inlineFieldShouldBeSkipped() {
450 $table = $this->globalOptions['table'];
451 $row = $this->globalOptions['databaseRow'];
452 $fieldName = $this->globalOptions['fieldName'];
453 $fieldConfig = $GLOBALS['TCA'][$table]['columns'][$fieldName]['config'];
454
455 /** @var InlineStackProcessor $inlineStackProcessor */
456 $inlineStackProcessor = GeneralUtility::makeInstance(InlineStackProcessor::class);
457 $inlineStackProcessor->initializeByGivenStructure($this->globalOptions['inlineStructure']);
458 $structureDepth = $inlineStackProcessor->getStructureDepth();
459
460 $skipThisField = FALSE;
461 if ($structureDepth > 0) {
462 $searchArray = array(
463 '%OR' => array(
464 'config' => array(
465 0 => array(
466 '%AND' => array(
467 'foreign_table' => $table,
468 '%OR' => array(
469 '%AND' => array(
470 'appearance' => array('useCombination' => TRUE),
471 'foreign_selector' => $fieldName
472 ),
473 'MM' => $fieldConfig['MM']
474 )
475 )
476 ),
477 1 => array(
478 '%AND' => array(
479 'foreign_table' => $fieldConfig['foreign_table'],
480 'foreign_selector' => $fieldConfig['foreign_field']
481 )
482 )
483 )
484 )
485 );
486 // Get the parent record from structure stack
487 $level = $inlineStackProcessor->getStructureLevel(-1);
488 // If we have symmetric fields, check on which side we are and hide fields, that are set automatically:
489 if (RelationHandler::isOnSymmetricSide($level['uid'], $level['config'], $row)) {
490 $searchArray['%OR']['config'][0]['%AND']['%OR']['symmetric_field'] = $fieldName;
491 $searchArray['%OR']['config'][0]['%AND']['%OR']['symmetric_sortby'] = $fieldName;
492 } else {
493 $searchArray['%OR']['config'][0]['%AND']['%OR']['foreign_field'] = $fieldName;
494 $searchArray['%OR']['config'][0]['%AND']['%OR']['foreign_sortby'] = $fieldName;
495 }
496 $skipThisField = $this->arrayCompareComplex($level, $searchArray);
497 }
498 return $skipThisField;
499 }
500
501 /**
502 * Handles complex comparison requests on an array.
503 * A request could look like the following:
504 *
505 * $searchArray = array(
506 * '%AND' => array(
507 * 'key1' => 'value1',
508 * 'key2' => 'value2',
509 * '%OR' => array(
510 * 'subarray' => array(
511 * 'subkey' => 'subvalue'
512 * ),
513 * 'key3' => 'value3',
514 * 'key4' => 'value4'
515 * )
516 * )
517 * );
518 *
519 * It is possible to use the array keys '%AND.1', '%AND.2', etc. to prevent
520 * overwriting the sub-array. It could be necessary, if you use complex comparisons.
521 *
522 * The example above means, key1 *AND* key2 (and their values) have to match with
523 * the $subjectArray and additional one *OR* key3 or key4 have to meet the same
524 * condition.
525 * It is also possible to compare parts of a sub-array (e.g. "subarray"), so this
526 * function recurses down one level in that sub-array.
527 *
528 * @param array $subjectArray The array to search in
529 * @param array $searchArray The array with keys and values to search for
530 * @param string $type Use '%AND' or '%OR' for comparison
531 * @return bool The result of the comparison
532 */
533 protected function arrayCompareComplex($subjectArray, $searchArray, $type = '') {
534 $localMatches = 0;
535 $localEntries = 0;
536 if (is_array($searchArray) && count($searchArray)) {
537 // If no type was passed, try to determine
538 if (!$type) {
539 reset($searchArray);
540 $type = key($searchArray);
541 $searchArray = current($searchArray);
542 }
543 // We use '%AND' and '%OR' in uppercase
544 $type = strtoupper($type);
545 // Split regular elements from sub elements
546 foreach ($searchArray as $key => $value) {
547 $localEntries++;
548 // Process a sub-group of OR-conditions
549 if ($key === '%OR') {
550 $localMatches += $this->arrayCompareComplex($subjectArray, $value, '%OR') ? 1 : 0;
551 } elseif ($key === '%AND') {
552 $localMatches += $this->arrayCompareComplex($subjectArray, $value, '%AND') ? 1 : 0;
553 } elseif (is_array($value) && $this->isAssociativeArray($searchArray)) {
554 $localMatches += $this->arrayCompareComplex($subjectArray[$key], $value, $type) ? 1 : 0;
555 } elseif (is_array($value)) {
556 $localMatches += $this->arrayCompareComplex($subjectArray, $value, $type) ? 1 : 0;
557 } else {
558 if (isset($subjectArray[$key]) && isset($value)) {
559 // Boolean match:
560 if (is_bool($value)) {
561 $localMatches += !($subjectArray[$key] xor $value) ? 1 : 0;
562 } elseif (is_numeric($subjectArray[$key]) && is_numeric($value)) {
563 $localMatches += $subjectArray[$key] == $value ? 1 : 0;
564 } else {
565 $localMatches += $subjectArray[$key] === $value ? 1 : 0;
566 }
567 }
568 }
569 // If one or more matches are required ('OR'), return TRUE after the first successful match
570 if ($type === '%OR' && $localMatches > 0) {
571 return TRUE;
572 }
573 // If all matches are required ('AND') and we have no result after the first run, return FALSE
574 if ($type === '%AND' && $localMatches == 0) {
575 return FALSE;
576 }
577 }
578 }
579 // Return the result for '%AND' (if nothing was checked, TRUE is returned)
580 return $localEntries == $localMatches ? TRUE : FALSE;
581 }
582
583 /**
584 * Checks whether an object is an associative array.
585 *
586 * @param mixed $object The object to be checked
587 * @return bool Returns TRUE, if the object is an associative array
588 */
589 protected function isAssociativeArray($object) {
590 return is_array($object) && count($object) && array_keys($object) !== range(0, sizeof($object) - 1) ? TRUE : FALSE;
591 }
592
593 /**
594 * @return BackendUserAuthentication
595 */
596 protected function getBackendUserAuthentication() {
597 return $GLOBALS['BE_USER'];
598 }
599
600 /**
601 * @return LanguageService
602 */
603 protected function getLanguageService() {
604 return $GLOBALS['LANG'];
605 }
606
607 }