6a9ee0676d08317d80dbc4f76b95f7570cd58186
[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\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Lang\LanguageService;
19 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
20 use TYPO3\CMS\Core\Type\Bitmask\JsConfirmation;
21 use TYPO3\CMS\Backend\Utility\BackendUtility;
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 $table = $this->data['tableName'];
40 $flexFormDataStructureArray = $this->data['flexFormDataStructureArray'];
41 $flexFormRowData = $this->data['flexFormRowData'];
42 $flexFormFormPrefix = $this->data['flexFormFormPrefix'];
43 $parameterArray = $this->data['parameterArray'];
44
45 $languageService = $this->getLanguageService();
46 $resultArray = $this->initializeResultArray();
47 foreach ($flexFormDataStructureArray as $flexFormFieldName => $flexFormFieldArray) {
48 if (
49 // No item array found at all
50 !is_array($flexFormFieldArray)
51 // Not a section or container and not a list of single items
52 || (!isset($flexFormFieldArray['type']) && !is_array($flexFormFieldArray['config']))
53 ) {
54 continue;
55 }
56
57 if ($flexFormFieldArray['type'] === 'array') {
58 // Section
59 if (empty($flexFormFieldArray['section'])) {
60 $resultArray['html'] = LF . 'Section expected at ' . $flexFormFieldName . ' but not found';
61 continue;
62 }
63
64 $sectionTitle = '';
65 if (!empty($flexFormFieldArray['title'])) {
66 $sectionTitle = $languageService->sL($flexFormFieldArray['title']);
67 }
68
69 $options = $this->data;
70 $options['flexFormDataStructureArray'] = $flexFormFieldArray['el'];
71 $options['flexFormRowData'] = isset($flexFormRowData[$flexFormFieldName]['el']) ? $flexFormRowData[$flexFormFieldName]['el'] : [];
72 $options['flexFormSectionType'] = $flexFormFieldName;
73 $options['flexFormSectionTitle'] = $sectionTitle;
74 $options['renderType'] = 'flexFormSectionContainer';
75 $sectionContainerResult = $this->nodeFactory->create($options)->render();
76 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $sectionContainerResult);
77 } else {
78 // Set up options for single element
79 $fakeParameterArray = [
80 'fieldConf' => [
81 'label' => $languageService->sL(trim($flexFormFieldArray['label'])),
82 'config' => $flexFormFieldArray['config'],
83 'defaultExtras' => $flexFormFieldArray['defaultExtras'],
84 'onChange' => $flexFormFieldArray['onChange'],
85 ],
86 'fieldChangeFunc' => $parameterArray['fieldChangeFunc'],
87 'onFocus' => $parameterArray['onFocus'],
88 'label' => $parameterArray['label'],
89 ];
90
91 $alertMsgOnChange = '';
92 if (
93 $fakeParameterArray['fieldConf']['onChange'] === 'reload'
94 || !empty($GLOBALS['TCA'][$table]['ctrl']['type']) && $GLOBALS['TCA'][$table]['ctrl']['type'] === $flexFormFieldName
95 || !empty($GLOBALS['TCA'][$table]['ctrl']['requestUpdate']) && GeneralUtility::inList($GLOBALS['TCA'][$table]['ctrl']['requestUpdate'], $flexFormFieldName)
96 ) {
97 if ($this->getBackendUserAuthentication()->jsConfirmation(JsConfirmation::TYPE_CHANGE)) {
98 $alertMsgOnChange = 'top.TYPO3.Modal.confirm(TBE_EDITOR.labels.refreshRequired.title, TBE_EDITOR.labels.refreshRequired.content).on("button.clicked", function(e) { if (e.target.name == "ok" && TBE_EDITOR.checkSubmit(-1)) { TBE_EDITOR.submitForm() } top.TYPO3.Modal.dismiss(); });';
99 } else {
100 $alertMsgOnChange = 'if (TBE_EDITOR.checkSubmit(-1)){ TBE_EDITOR.submitForm();}';
101 }
102 }
103 if ($alertMsgOnChange) {
104 $fakeParameterArray['fieldChangeFunc']['alert'] = $alertMsgOnChange;
105 }
106
107 $fakeParameterArray['itemFormElName'] = $parameterArray['itemFormElName'] . $flexFormFormPrefix . '[' . $flexFormFieldName . '][vDEF]';
108 $fakeParameterArray['itemFormElID'] = $fakeParameterArray['itemFormElName'];
109 if (isset($flexFormRowData[$flexFormFieldName]['vDEF'])) {
110 $fakeParameterArray['itemFormElValue'] = $flexFormRowData[$flexFormFieldName]['vDEF'];
111 } else {
112 $fakeParameterArray['itemFormElValue'] = $fakeParameterArray['fieldConf']['config']['default'];
113 }
114
115 $options = $this->data;
116 $options['parameterArray'] = $fakeParameterArray;
117 $options['elementBaseName'] = $this->data['elementBaseName'] . $flexFormFormPrefix . '[' . $flexFormFieldName . '][vDEF]';
118
119 if (!empty($flexFormFieldArray['config']['renderType'])) {
120 $options['renderType'] = $flexFormFieldArray['config']['renderType'];
121 } else {
122 // Fallback to type if no renderType is given
123 $options['renderType'] = $flexFormFieldArray['config']['type'];
124 }
125 $childResult = $this->nodeFactory->create($options)->render();
126
127 // Possible line breaks in the label through xml: \n => <br/>, usage of nl2br() not possible, so it's done through str_replace (?!)
128 $processedTitle = str_replace('\\n', '<br />', htmlspecialchars($fakeParameterArray['fieldConf']['label']));
129 $html = [];
130 $html[] = '<div class="form-section">';
131 $html[] = '<div class="form-group t3js-formengine-palette-field t3js-formengine-validation-marker">';
132 $html[] = '<label class="t3js-formengine-label">';
133 $html[] = BackendUtility::wrapInHelp($parameterArray['_cshKey'], $flexFormFieldName, $processedTitle);
134 $html[] = '</label>';
135 $html[] = '<div class="t3js-formengine-field-item">';
136 $html[] = $childResult['html'];
137 $html[] = '</div>';
138 $html[] = '</div>';
139 $html[] = '</div>';
140
141 $resultArray['html'] .= implode(LF, $html);
142 $childResult['html'] = '';
143 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $childResult);
144 }
145 }
146
147 return $resultArray;
148 }
149
150 /**
151 * @return LanguageService
152 */
153 protected function getLanguageService() {
154 return $GLOBALS['LANG'];
155 }
156
157 /**
158 * @return BackendUserAuthentication
159 */
160 protected function getBackendUserAuthentication() {
161 return $GLOBALS['BE_USER'];
162 }
163
164 }