503a775149ee0bc729a3ab815ed0e9da46e0ab4e
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / Container / FlexFormSectionContainer.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\IconUtility;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
20 use TYPO3\CMS\Lang\LanguageService;
21 use TYPO3\CMS\Backend\Form\NodeFactory;
22
23 /**
24 * Handle flex form sections.
25 *
26 * This container is created by FlexFormElementContainer if a "single" element is in
27 * fact a sections. For each existing section container it creates as FlexFormContainerContainer
28 * to render its inner fields, additionally for each possible container a "template" of this
29 * container type is rendered and added - to be added by JS to DOM on click on "new xy container".
30 */
31 class FlexFormSectionContainer extends AbstractContainer {
32
33 /**
34 * Entry method
35 *
36 * @return array As defined in initializeResultArray() of AbstractNode
37 */
38 public function render() {
39 $languageService = $this->getLanguageService();
40
41 $flexFormFieldsArray = $this->globalOptions['flexFormDataStructureArray'];
42 $flexFormRowData = $this->globalOptions['flexFormRowData'];
43 $flexFormFieldIdentifierPrefix = $this->globalOptions['flexFormFieldIdentifierPrefix'];
44 $flexFormSectionType = $this->globalOptions['flexFormSectionType'];
45 $flexFormSectionTitle = $this->globalOptions['flexFormSectionTitle'];
46
47 $userHasAccessToDefaultLanguage = $this->getBackendUserAuthentication()->checkLanguageAccess(0);
48
49 $resultArray = $this->initializeResultArray();
50
51 // Creating IDs for form fields:
52 // It's important that the IDs "cascade" - otherwise we can't dynamically expand the flex form
53 // because this relies on simple string substitution of the first parts of the id values.
54 $flexFormFieldIdentifierPrefix = $flexFormFieldIdentifierPrefix . '-' . GeneralUtility::shortMd5(uniqid('id', TRUE));
55
56 // Render each existing container
57 foreach ($flexFormRowData as $flexFormContainerCounter => $existingSectionContainerData) {
58 // @todo: This relies on the fact that "_TOGGLE" is *below* the real data in the saved xml structure
59 if (is_array($existingSectionContainerData)) {
60 $existingSectionContainerDataStructureType = key($existingSectionContainerData);
61 $existingSectionContainerData = $existingSectionContainerData[$existingSectionContainerDataStructureType];
62 $containerDataStructure = $flexFormFieldsArray[$existingSectionContainerDataStructureType];
63 // There may be cases where a field is still in DB but does not exist in definition
64 if (is_array($containerDataStructure)) {
65 $sectionTitle = '';
66 if (!empty($containerDataStructure['title'])) {
67 $sectionTitle = $languageService->sL($containerDataStructure['title']);
68 }
69
70 $options = $this->globalOptions;
71 $options['flexFormRowData'] = $existingSectionContainerData['el'];
72 $options['flexFormDataStructureArray'] = $containerDataStructure['el'];
73 $options['flexFormFieldIdentifierPrefix'] = $flexFormFieldIdentifierPrefix;
74 $options['flexFormFormPrefix'] = $this->globalOptions['flexFormFormPrefix'] . '[' . $flexFormSectionType . ']' . '[el]';
75 $options['flexFormContainerName'] = $existingSectionContainerDataStructureType;
76 $options['flexFormContainerCounter'] = $flexFormContainerCounter;
77 $options['flexFormContainerTitle'] = $sectionTitle;
78 $options['flexFormContainerElementCollapsed'] = (bool)$existingSectionContainerData['el']['_TOGGLE'];
79 $options['renderType'] = 'flexFormContainerContainer';
80 /** @var NodeFactory $nodeFactory */
81 $nodeFactory = $this->globalOptions['nodeFactory'];
82 $flexFormContainerContainerResult = $nodeFactory->create($options)->render();
83 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $flexFormContainerContainerResult);
84 }
85 }
86 }
87
88 // "New container" handling: Creates a "template" of each possible container and stuffs it
89 // somewhere into DOM to be handled with JS magic.
90 // Fun part: Handle the fact that such things may be set for children
91 $containerTemplatesHtml = array();
92 foreach ($flexFormFieldsArray as $flexFormContainerName => $flexFormFieldDefinition) {
93 $containerTemplateHtml = array();
94 $sectionTitle = '';
95 if (!empty($flexFormFieldDefinition['title'])) {
96 $sectionTitle = $languageService->sL($flexFormFieldDefinition['title']);
97 }
98
99 $options = $this->globalOptions;
100 $options['flexFormRowData'] = array();
101 $options['flexFormDataStructureArray'] = $flexFormFieldDefinition['el'];
102 $options['flexFormFieldIdentifierPrefix'] = $flexFormFieldIdentifierPrefix;
103 $options['flexFormFormPrefix'] = $this->globalOptions['flexFormFormPrefix'] . '[' . $flexFormSectionType . ']' . '[el]';
104 $options['flexFormContainerName'] = $flexFormContainerName;
105 $options['flexFormContainerCounter'] = $flexFormFieldIdentifierPrefix . '-form';
106 $options['flexFormContainerTitle'] = $sectionTitle;
107 $options['flexFormContainerElementCollapsed'] = FALSE;
108 $options['renderType'] = 'flexFormContainerContainer';
109 /** @var NodeFactory $nodeFactory */
110 $nodeFactory = $this->globalOptions['nodeFactory'];
111 $flexFormContainerContainerTemplateResult = $nodeFactory->create($options)->render();
112
113 $uniqueId = str_replace('.', '', uniqid('idvar', TRUE));
114 $identifierPrefixJs = 'replace(/' . $flexFormFieldIdentifierPrefix . '-/g,"' . $flexFormFieldIdentifierPrefix . '-"+' . $uniqueId . '+"-")';
115 $identifierPrefixJs .= '.replace(/(tceforms-(datetime|date)field-)/g,"$1" + (new Date()).getTime())';
116
117 $onClickInsert = array();
118 $onClickInsert[] = 'var ' . $uniqueId . ' = "' . 'idx"+(new Date()).getTime();';
119 $onClickInsert[] = 'TYPO3.jQuery(' . json_encode($flexFormContainerContainerTemplateResult['html']) . '.' . $identifierPrefixJs . ').insertAfter(TYPO3.jQuery("#' . $flexFormFieldIdentifierPrefix . '"));';
120 $onClickInsert[] = 'TYPO3.jQuery("#' . $flexFormFieldIdentifierPrefix . '").t3FormEngineFlexFormElement();';
121 $onClickInsert[] = 'eval(unescape("' . rawurlencode(implode(';', $flexFormContainerContainerTemplateResult['additionalJavaScriptPost'])) . '").' . $identifierPrefixJs . ');';
122 $onClickInsert[] = 'TBE_EDITOR.addActionChecks("submit", unescape("' . rawurlencode(implode(';', $flexFormContainerContainerTemplateResult['additionalJavaScriptSubmit'])) . '").' . $identifierPrefixJs . ');';
123 $onClickInsert[] = 'TYPO3.FormEngine.reinitialize();';
124 $onClickInsert[] = 'return false;';
125
126 $containerTemplateHtml[] = '<a href="#" onclick="' . htmlspecialchars(implode(LF, $onClickInsert)) . '">';
127 $containerTemplateHtml[] = IconUtility::getSpriteIcon('actions-document-new');
128 $containerTemplateHtml[] = htmlspecialchars(GeneralUtility::fixed_lgd_cs($sectionTitle, 30));
129 $containerTemplateHtml[] = '</a>';
130 $containerTemplatesHtml[] = implode(LF, $containerTemplateHtml);
131
132 $flexFormContainerContainerTemplateResult['html'] = '';
133 $flexFormContainerContainerTemplateResult['additionalJavaScriptPost'] = array();
134 $flexFormContainerContainerTemplateResult['additionalJavaScriptSubmit'] = array();
135
136 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $flexFormContainerContainerTemplateResult);
137 }
138
139 // Create new elements links
140 $createElementsHtml = array();
141 if ($userHasAccessToDefaultLanguage) {
142 $createElementsHtml[] = '<div class="t3-form-field-add-flexsection">';
143 $createElementsHtml[] = '<strong>';
144 $createElementsHtml[] = $languageService->sL('LLL:EXT:lang/locallang_core.xlf:labels.addnew', TRUE) . ':';
145 $createElementsHtml[] = '</strong>';
146 $createElementsHtml[] = implode('|', $containerTemplatesHtml);
147 $createElementsHtml[] = '</div>';
148 }
149
150 // Wrap child stuff
151 $toggleAll = $languageService->sL('LLL:EXT:lang/locallang_core.xlf:labels.toggleall', TRUE);
152 $html = array();
153 $html[] = '<div class="t3-form-field-container t3-form-flex">';
154 $html[] = '<div class="t3-form-field-label-flexsection">';
155 $html[] = '<strong>';
156 $html[] = htmlspecialchars($flexFormSectionTitle);
157 $html[] = '</strong>';
158 $html[] = '</div>';
159 $html[] = '<div class="t3-form-field-toggle-flexsection t3-form-flexsection-toggle">';
160 $html[] = '<a href="#">';
161 $html[] = IconUtility::getSpriteIcon('actions-move-right', array('title' => $toggleAll)) . $toggleAll;
162 $html[] = '</a>';
163 $html[] = '</div>';
164 $html[] = '<div';
165 $html[] = 'id="' . $flexFormFieldIdentifierPrefix . '"';
166 $html[] = 'class="t3-form-field-container-flexsection t3-flex-container"';
167 $html[] = 'data-t3-flex-allow-restructure="' . ($userHasAccessToDefaultLanguage ? '1' : '0') . '"';
168 $html[] = '>';
169 $html[] = $resultArray['html'];
170 $html[] = '</div>';
171 $html[] = implode(LF, $createElementsHtml);
172 $html[] = '</div>';
173
174 $resultArray['html'] = implode(LF, $html);
175
176 return $resultArray;
177 }
178
179 /**
180 * @return BackendUserAuthentication
181 */
182 protected function getBackendUserAuthentication() {
183 return $GLOBALS['BE_USER'];
184 }
185
186 /**
187 * @return LanguageService
188 */
189 protected function getLanguageService() {
190 return $GLOBALS['LANG'];
191 }
192
193 }