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