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