FlexFormSectionContainer.php 9.22 KB
Newer Older
Christian Kuhn's avatar
Christian Kuhn committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
namespace TYPO3\CMS\Backend\Form\Container;

/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

use TYPO3\CMS\Backend\Utility\IconUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Lang\LanguageService;
21
use TYPO3\CMS\Backend\Form\NodeFactory;
Christian Kuhn's avatar
Christian Kuhn committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

/**
 * Handle flex form sections.
 *
 * This container is created by FlexFormElementContainer if a "single" element is in
 * fact a sections. For each existing section container it creates as FlexFormContainerContainer
 * to render its inner fields, additionally for each possible container a "template" of this
 * container type is rendered and added - to be added by JS to DOM on click on "new xy container".
 */
class FlexFormSectionContainer extends AbstractContainer {

	/**
	 * Entry method
	 *
	 * @return array As defined in initializeResultArray() of AbstractNode
	 */
	public function render() {
		$languageService = $this->getLanguageService();

		$flexFormFieldsArray = $this->globalOptions['flexFormDataStructureArray'];
		$flexFormRowData = $this->globalOptions['flexFormRowData'];
		$flexFormFieldIdentifierPrefix = $this->globalOptions['flexFormFieldIdentifierPrefix'];
		$flexFormSectionType = $this->globalOptions['flexFormSectionType'];
		$flexFormSectionTitle = $this->globalOptions['flexFormSectionTitle'];

		$userHasAccessToDefaultLanguage = $this->getBackendUserAuthentication()->checkLanguageAccess(0);

		$resultArray = $this->initializeResultArray();

		// Creating IDs for form fields:
		// It's important that the IDs "cascade" - otherwise we can't dynamically expand the flex form
		// because this relies on simple string substitution of the first parts of the id values.
		$flexFormFieldIdentifierPrefix = $flexFormFieldIdentifierPrefix . '-' . GeneralUtility::shortMd5(uniqid('id', TRUE));

		// Render each existing container
		foreach ($flexFormRowData as $flexFormContainerCounter => $existingSectionContainerData) {
			// @todo: This relies on the fact that "_TOGGLE" is *below* the real data in the saved xml structure
			if (is_array($existingSectionContainerData)) {
				$existingSectionContainerDataStructureType = key($existingSectionContainerData);
				$existingSectionContainerData = $existingSectionContainerData[$existingSectionContainerDataStructureType];
				$containerDataStructure = $flexFormFieldsArray[$existingSectionContainerDataStructureType];
				// There may be cases where a field is still in DB but does not exist in definition
				if (is_array($containerDataStructure)) {
					$sectionTitle = '';
					if (!empty($containerDataStructure['title'])) {
						$sectionTitle = $languageService->sL($containerDataStructure['title']);
					}

					$options = $this->globalOptions;
					$options['flexFormRowData'] = $existingSectionContainerData['el'];
					$options['flexFormDataStructureArray'] = $containerDataStructure['el'];
					$options['flexFormFieldIdentifierPrefix'] = $flexFormFieldIdentifierPrefix;
					$options['flexFormFormPrefix'] = $this->globalOptions['flexFormFormPrefix'] . '[' . $flexFormSectionType . ']' . '[el]';
					$options['flexFormContainerName'] = $existingSectionContainerDataStructureType;
					$options['flexFormContainerCounter'] = $flexFormContainerCounter;
					$options['flexFormContainerTitle'] = $sectionTitle;
					$options['flexFormContainerElementCollapsed'] = (bool)$existingSectionContainerData['el']['_TOGGLE'];
79
					$options['renderType'] = 'flexFormContainerContainer';
80
81
82
					/** @var NodeFactory $nodeFactory */
					$nodeFactory = $this->globalOptions['nodeFactory'];
					$flexFormContainerContainerResult = $nodeFactory->create($options)->render();
Christian Kuhn's avatar
Christian Kuhn committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
					$resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $flexFormContainerContainerResult);
				}
			}
		}

		// "New container" handling: Creates a "template" of each possible container and stuffs it
		// somewhere into DOM to be handled with JS magic.
		// Fun part: Handle the fact that requiredElements and such may be set for children
		$containerTemplatesHtml = array();
		foreach ($flexFormFieldsArray as $flexFormContainerName => $flexFormFieldDefinition) {
			$containerTemplateHtml = array();
			$sectionTitle = '';
			if (!empty($flexFormFieldDefinition['title'])) {
				$sectionTitle = $languageService->sL($flexFormFieldDefinition['title']);
			}

			$options = $this->globalOptions;
			$options['flexFormRowData'] = array();
			$options['flexFormDataStructureArray'] = $flexFormFieldDefinition['el'];
			$options['flexFormFieldIdentifierPrefix'] = $flexFormFieldIdentifierPrefix;
103
104
105
			$options['flexFormFormPrefix'] = $this->globalOptions['flexFormFormPrefix'] . '[' . $flexFormSectionType . ']' . '[el]';
			$options['flexFormContainerName'] = $flexFormContainerName;
			$options['flexFormContainerCounter'] = $flexFormFieldIdentifierPrefix . '-form';
Christian Kuhn's avatar
Christian Kuhn committed
106
107
			$options['flexFormContainerTitle'] = $sectionTitle;
			$options['flexFormContainerElementCollapsed'] = FALSE;
108
			$options['renderType'] = 'flexFormContainerContainer';
109
110
111
			/** @var NodeFactory $nodeFactory */
			$nodeFactory = $this->globalOptions['nodeFactory'];
			$flexFormContainerContainerTemplateResult = $nodeFactory->create($options)->render();
Christian Kuhn's avatar
Christian Kuhn committed
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193

			$uniqueId = str_replace('.', '', uniqid('idvar', TRUE));
			$identifierPrefixJs = 'replace(/' . $flexFormFieldIdentifierPrefix . '-/g,"' . $flexFormFieldIdentifierPrefix . '-"+' . $uniqueId . '+"-")';
			$identifierPrefixJs .= '.replace(/(tceforms-(datetime|date)field-)/g,"$1" + (new Date()).getTime())';

			$onClickInsert = array();
			$onClickInsert[] = 'var ' . $uniqueId . ' = "' . 'idx"+(new Date()).getTime();';
			$onClickInsert[] = 'new Insertion.Bottom($("' . $flexFormFieldIdentifierPrefix . '"), ' . json_encode($flexFormContainerContainerTemplateResult['html']) . '.' . $identifierPrefixJs . ');';
			$onClickInsert[] = 'TYPO3.jQuery("#' . $flexFormFieldIdentifierPrefix . '").t3FormEngineFlexFormElement();';
			$onClickInsert[] = 'eval(unescape("' . rawurlencode(implode(';', $flexFormContainerContainerTemplateResult['additionalJavaScriptPost'])) . '").' . $identifierPrefixJs . ');';
			$onClickInsert[] = 'TBE_EDITOR.addActionChecks("submit", unescape("' . rawurlencode(implode(';', $flexFormContainerContainerTemplateResult['additionalJavaScriptSubmit'])) . '").' . $identifierPrefixJs . ');';
			$onClickInsert[] = 'TYPO3.FormEngine.reinitialize();';
			$onClickInsert[] = 'return false;';

			$containerTemplateHtml[] = '<a href="#" onclick="' . htmlspecialchars(implode(LF, $onClickInsert)) . '">';
			$containerTemplateHtml[] = 	IconUtility::getSpriteIcon('actions-document-new');
			$containerTemplateHtml[] = 	htmlspecialchars(GeneralUtility::fixed_lgd_cs($sectionTitle, 30));
			$containerTemplateHtml[] = '</a>';
			$containerTemplatesHtml[] = implode(LF, $containerTemplateHtml);

			$flexFormContainerContainerTemplateResult['html'] = '';
			$flexFormContainerContainerTemplateResult['additionalJavaScriptPost'] = array();
			$flexFormContainerContainerTemplateResult['additionalJavaScriptSubmit'] = array();

			$resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $flexFormContainerContainerTemplateResult);
		}

		// Create new elements links
		$createElementsHtml = array();
		if ($userHasAccessToDefaultLanguage) {
			$createElementsHtml[] = '<div class="t3-form-field-add-flexsection">';
			$createElementsHtml[] = 	'<strong>';
			$createElementsHtml[] = 		$languageService->sL('LLL:EXT:lang/locallang_core.xlf:labels.addnew', TRUE) . ':';
			$createElementsHtml[] = 	'</strong>';
			$createElementsHtml[] = 	implode('|', $containerTemplatesHtml);
			$createElementsHtml[] = '</div>';
		}

		// Wrap child stuff
		$toggleAll = $languageService->sL('LLL:EXT:lang/locallang_core.xlf:labels.toggleall', TRUE);
		$html = array();
		$html[] = '<div class="t3-form-field-container t3-form-flex">';
		$html[] = 	'<div class="t3-form-field-label-flexsection">';
		$html[] = 		'<strong>';
		$html[] = 			htmlspecialchars($flexFormSectionTitle);
		$html[] = 		'</strong>';
		$html[] = 	'</div>';
		$html[] = 	'<div class="t3-form-field-toggle-flexsection t3-form-flexsection-toggle">';
		$html[] = 		'<a href="#">';
		$html[] = 			IconUtility::getSpriteIcon('actions-move-right', array('title' => $toggleAll)) . $toggleAll;
		$html[] = 		'</a>';
		$html[] = 	'</div>';
		$html[] = 	'<div';
		$html[] = 		'id="' . $flexFormFieldIdentifierPrefix . '"';
		$html[] = 		'class="t3-form-field-container-flexsection t3-flex-container"';
		$html[] = 		'data-t3-flex-allow-restructure="' . ($userHasAccessToDefaultLanguage ? '1' : '0') . '"';
		$html[] = 	'>';
		$html[] = 		$resultArray['html'];
		$html[] = 	'</div>';
		$html[] = 	implode(LF, $createElementsHtml);
		$html[] = '</div>';

		$resultArray['html'] = implode(LF, $html);

		return $resultArray;
	}

	/**
	 * @return BackendUserAuthentication
	 */
	protected function getBackendUserAuthentication() {
		return $GLOBALS['BE_USER'];
	}

	/**
	 * @return LanguageService
	 */
	protected function getLanguageService() {
		return $GLOBALS['LANG'];
	}

}