[TASK] Streamline tbe_editor code
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / Container / FlexFormElementContainer.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\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
19 use TYPO3\CMS\Core\Type\Bitmask\JsConfirmation;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Lang\LanguageService;
22
23 /**
24 * The container handles single elements.
25 *
26 * This one is called by FlexFormTabsContainer, FlexFormNoTabsContainer or FlexFormContainerContainer.
27 * For single fields, the code is similar to SingleFieldContainer, processing will end up in single
28 * element classes depending on specific type of an element. Additionally, it determines if a
29 * section is handled and hands over to FlexFormSectionContainer in this case.
30 */
31 class FlexFormElementContainer extends AbstractContainer
32 {
33 /**
34 * Entry method
35 *
36 * @return array As defined in initializeResultArray() of AbstractNode
37 */
38 public function render()
39 {
40 $table = $this->data['tableName'];
41 $flexFormDataStructureArray = $this->data['flexFormDataStructureArray'];
42 $flexFormRowData = $this->data['flexFormRowData'];
43 $flexFormFormPrefix = $this->data['flexFormFormPrefix'];
44 $parameterArray = $this->data['parameterArray'];
45
46 $languageService = $this->getLanguageService();
47 $resultArray = $this->initializeResultArray();
48 foreach ($flexFormDataStructureArray as $flexFormFieldName => $flexFormFieldArray) {
49 if (
50 // No item array found at all
51 !is_array($flexFormFieldArray)
52 // Not a section or container and not a list of single items
53 || (!isset($flexFormFieldArray['type']) && !is_array($flexFormFieldArray['config']))
54 ) {
55 continue;
56 }
57
58 if ($flexFormFieldArray['type'] === 'array') {
59 // Section
60 if (empty($flexFormFieldArray['section'])) {
61 $resultArray['html'] = LF . 'Section expected at ' . $flexFormFieldName . ' but not found';
62 continue;
63 }
64
65 $sectionTitle = '';
66 if (!empty(trim($flexFormFieldArray['title']))) {
67 $sectionTitle = $languageService->sL(trim($flexFormFieldArray['title']));
68 }
69
70 $options = $this->data;
71 $options['flexFormDataStructureArray'] = $flexFormFieldArray['el'];
72 $options['flexFormRowData'] = isset($flexFormRowData[$flexFormFieldName]['el']) ? $flexFormRowData[$flexFormFieldName]['el'] : [];
73 $options['flexFormSectionType'] = $flexFormFieldName;
74 $options['flexFormSectionTitle'] = $sectionTitle;
75 $options['renderType'] = 'flexFormSectionContainer';
76 $sectionContainerResult = $this->nodeFactory->create($options)->render();
77 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $sectionContainerResult);
78 } else {
79 // Set up options for single element
80 $fakeParameterArray = [
81 'fieldConf' => [
82 // @todo review this field during flex refactoring
83 'flexFormFieldName' => $flexFormFieldName,
84 'label' => $languageService->sL(trim($flexFormFieldArray['label'])),
85 'config' => $flexFormFieldArray['config'],
86 'children' => $flexFormFieldArray['children'],
87 'defaultExtras' => $flexFormFieldArray['defaultExtras'],
88 'onChange' => $flexFormFieldArray['onChange'],
89 ],
90 'fieldChangeFunc' => $parameterArray['fieldChangeFunc'],
91 'label' => $parameterArray['label'],
92 ];
93
94 $alertMsgOnChange = '';
95 if (
96 $fakeParameterArray['fieldConf']['onChange'] === 'reload'
97 || !empty($GLOBALS['TCA'][$table]['ctrl']['type']) && $GLOBALS['TCA'][$table]['ctrl']['type'] === $flexFormFieldName
98 || !empty($GLOBALS['TCA'][$table]['ctrl']['requestUpdate']) && GeneralUtility::inList($GLOBALS['TCA'][$table]['ctrl']['requestUpdate'], $flexFormFieldName)
99 ) {
100 if ($this->getBackendUserAuthentication()->jsConfirmation(JsConfirmation::TYPE_CHANGE)) {
101 $alertMsgOnChange = 'top.TYPO3.Modal.confirm(TYPO3.lang["FormEngine.refreshRequiredTitle"], TYPO3.lang["FormEngine.refreshRequiredContent"]).on("button.clicked", function(e) { if (e.target.name == "ok" && TBE_EDITOR.checkSubmit(-1)) { TBE_EDITOR.submitForm() } top.TYPO3.Modal.dismiss(); });';
102 } else {
103 $alertMsgOnChange = 'if (TBE_EDITOR.checkSubmit(-1)){ TBE_EDITOR.submitForm();}';
104 }
105 }
106 if ($alertMsgOnChange) {
107 $fakeParameterArray['fieldChangeFunc']['alert'] = $alertMsgOnChange;
108 }
109
110 $originalFieldName = $parameterArray['itemFormElName'];
111 $fakeParameterArray['itemFormElName'] = $parameterArray['itemFormElName'] . $flexFormFormPrefix . '[' . $flexFormFieldName . '][vDEF]';
112 if ($fakeParameterArray['itemFormElName'] !== $originalFieldName) {
113 // If calculated itemFormElName is different from originalFieldName
114 // change the originalFieldName in TBE_EDITOR_fieldChanged. This is
115 // especially relevant for wizards writing their content back to hidden fields
116 if (!empty($fakeParameterArray['fieldChangeFunc']['TBE_EDITOR_fieldChanged'])) {
117 $fakeParameterArray['fieldChangeFunc']['TBE_EDITOR_fieldChanged'] = str_replace($originalFieldName, $fakeParameterArray['itemFormElName'], $fakeParameterArray['fieldChangeFunc']['TBE_EDITOR_fieldChanged']);
118 }
119 }
120 $fakeParameterArray['itemFormElID'] = $fakeParameterArray['itemFormElName'];
121 if (isset($flexFormRowData[$flexFormFieldName]['vDEF'])) {
122 $fakeParameterArray['itemFormElValue'] = $flexFormRowData[$flexFormFieldName]['vDEF'];
123 } else {
124 $fakeParameterArray['itemFormElValue'] = $fakeParameterArray['fieldConf']['config']['default'];
125 }
126
127 $options = $this->data;
128 $options['parameterArray'] = $fakeParameterArray;
129 $options['elementBaseName'] = $this->data['elementBaseName'] . $flexFormFormPrefix . '[' . $flexFormFieldName . '][vDEF]';
130
131 if (!empty($flexFormFieldArray['config']['renderType'])) {
132 $options['renderType'] = $flexFormFieldArray['config']['renderType'];
133 } else {
134 // Fallback to type if no renderType is given
135 $options['renderType'] = $flexFormFieldArray['config']['type'];
136 }
137 $childResult = $this->nodeFactory->create($options)->render();
138
139 // Possible line breaks in the label through xml: \n => <br/>, usage of nl2br() not possible, so it's done through str_replace (?!)
140 $processedTitle = str_replace('\\n', '<br />', htmlspecialchars($fakeParameterArray['fieldConf']['label']));
141 $html = [];
142 $html[] = '<div class="form-section">';
143 $html[] = '<div class="form-group t3js-formengine-palette-field t3js-formengine-validation-marker">';
144 $html[] = '<label class="t3js-formengine-label">';
145 $html[] = BackendUtility::wrapInHelp($parameterArray['_cshKey'], $flexFormFieldName, $processedTitle);
146 $html[] = '</label>';
147 $html[] = '<div class="t3js-formengine-field-item">';
148 $html[] = $childResult['html'];
149 $html[] = '</div>';
150 $html[] = '</div>';
151 $html[] = '</div>';
152
153 $resultArray['html'] .= implode(LF, $html);
154 $childResult['html'] = '';
155 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $childResult);
156 }
157 }
158
159 return $resultArray;
160 }
161
162 /**
163 * @return LanguageService
164 */
165 protected function getLanguageService()
166 {
167 return $GLOBALS['LANG'];
168 }
169
170 /**
171 * @return BackendUserAuthentication
172 */
173 protected function getBackendUserAuthentication()
174 {
175 return $GLOBALS['BE_USER'];
176 }
177 }