[TASK] FormEngine: Remove references to deprecated renderMode property
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / Element / SelectTreeElement.php
1 <?php
2 namespace TYPO3\CMS\Backend\Form\Element;
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
22 /**
23 * Render data as a tree.
24 *
25 * Typically rendered for config [type=select, renderType=selectTree
26 */
27 class SelectTreeElement extends AbstractFormElement
28 {
29
30 /**
31 * Default height of the tree in pixels.
32 *
33 * @const
34 */
35 const DEFAULT_HEIGHT = 280;
36
37 /**
38 * Default width of the tree in pixels.
39 *
40 * @const
41 */
42 const DEFAULT_WIDTH = 280;
43
44 /**
45 * Render tree widget
46 *
47 * @return array As defined in initializeResultArray() of AbstractNode
48 * @see AbstractNode::initializeResultArray()
49 */
50 public function render()
51 {
52 $resultArray = $this->initializeResultArray();
53 $parameterArray = $this->data['parameterArray'];
54 $formElementId = md5($parameterArray['itemFormElName']);
55
56 // Field configuration from TCA:
57 $config = $parameterArray['fieldConf']['config'];
58
59 $resultArray['extJSCODE'] .= LF . $this->generateJavascript($formElementId);
60
61 $html = [];
62 $html[] = '<div class="typo3-tceforms-tree">';
63 $html[] = ' <input class="treeRecord" type="hidden"';
64 $html[] = ' ' . $this->getValidationDataAsDataAttribute($parameterArray['fieldConf']['config']);
65 $html[] = ' data-formengine-input-name="' . htmlspecialchars($parameterArray['itemFormElName']) . '"';
66 $html[] = ' data-relatedfieldname="' . htmlspecialchars($parameterArray['itemFormElName']) . '"';
67 $html[] = ' name="' . htmlspecialchars($parameterArray['itemFormElName']) . '"';
68 $html[] = ' id="treeinput' . $formElementId . '"';
69 $html[] = ' value="' . htmlspecialchars(implode(',', $config['treeData']['selectedNodes'])) . '"';
70 $html[] = ' />';
71 $html[] = '</div>';
72 $html[] = '<div id="tree_' . $formElementId . '"></div>';
73
74 $resultArray['html'] = implode(LF, $html);
75
76 // Wizards:
77 if (empty($config['readOnly'])) {
78 $resultArray['html'] = $this->renderWizards(
79 [$resultArray['html']],
80 $config['wizards'],
81 $this->data['tableName'],
82 $this->data['databaseRow'],
83 $this->data['fieldName'],
84 $parameterArray,
85 $parameterArray['itemFormElName'],
86 BackendUtility::getSpecConfParts($parameterArray['fieldConf']['defaultExtras'])
87 );
88 }
89
90 return $resultArray;
91 }
92
93 /**
94 * Generates the Ext JS tree JavaScript code.
95 *
96 * @param string $formElementId The HTML element ID of the tree select field.
97 * @return string
98 */
99 protected function generateJavascript($formElementId)
100 {
101 $table = $this->data['tableName'];
102 $field = $this->data['fieldName'];
103 $parameterArray = $this->data['parameterArray'];
104 $config = $parameterArray['fieldConf']['config'];
105
106 $disabled = !empty($config['readOnly']) ? 'true' : 'false';
107 $maxItems = $config['maxitems'] ? (int)$config['maxitems'] : 99999;
108 $exclusiveKeys = !empty($config['exclusiveKeys']) ? $config['exclusiveKeys'] : '';
109
110 $appearance = !empty($config['treeConfig']['appearance']) ? $config['treeConfig']['appearance'] : [];
111 $width = isset($appearance['width']) ? (int)$appearance['width'] : static::DEFAULT_WIDTH;
112 if (isset($config['size']) && (int)$config['size'] > 0) {
113 $height = (int)$config['size'] * 20;
114 } else {
115 $height = static::DEFAULT_HEIGHT;
116 }
117 $showHeader = !empty($appearance['showHeader']);
118 $expanded = !empty($appearance['expandAll']);
119 $allowRecursiveMode = !empty($appearance['allowRecursiveMode']) ? 'true' : 'false';
120
121 $autoSizeMax = null;
122 if (isset($config['autoSizeMax']) && (int)$config['autoSizeMax'] > 0) {
123 $autoSizeMax = (int)$config['autoSizeMax'] * 20;
124 }
125
126 $onChange = !empty($parameterArray['fieldChangeFunc']['TBE_EDITOR_fieldChanged']) ? $parameterArray['fieldChangeFunc']['TBE_EDITOR_fieldChanged'] : '';
127 $onChange .= !empty($parameterArray['fieldChangeFunc']['alert']) ? $parameterArray['fieldChangeFunc']['alert'] : '';
128
129 // Create a JavaScript code line which will ask the user to save/update the form due to changing the element.
130 // This is used for eg. "type" fields and others configured with "requestUpdate"
131 if (
132 !empty($GLOBALS['TCA'][$table]['ctrl']['type'])
133 && $field === $GLOBALS['TCA'][$table]['ctrl']['type']
134 || !empty($GLOBALS['TCA'][$table]['ctrl']['requestUpdate'])
135 && GeneralUtility::inList(str_replace(' ', '', $GLOBALS['TCA'][$table]['ctrl']['requestUpdate']), $field)
136 ) {
137 if ($this->getBackendUserAuthentication()->jsConfirmation(JsConfirmation::TYPE_CHANGE)) {
138 $onChange = '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(); });';
139 } else {
140 $onChange .= 'if (TBE_EDITOR.checkSubmit(-1)){ TBE_EDITOR.submitForm() };';
141 }
142 }
143
144 $javascript = [];
145 $javascript[] = 'Ext.onReady(function() {';
146 $javascript[] = ' TYPO3.Components.Tree.StandardTreeItemData["' . $formElementId . '"] = ' . json_encode($config['treeData']['items']) . ';';
147 $javascript[] = ' var tree' . $formElementId . ' = new TYPO3.Components.Tree.StandardTree({';
148 $javascript[] = ' id: "' . $formElementId . '",';
149 $javascript[] = ' stateful: true,';
150 $javascript[] = ' stateId: "tcaTrees." + this.ucId,';
151 $javascript[] = ' stateEvents: [],';
152 $javascript[] = ' showHeader: ' . (int)$showHeader . ',';
153 $javascript[] = ' onChange: ' . GeneralUtility::quoteJSvalue($onChange) . ',';
154 $javascript[] = ' countSelectedNodes: ' . count($config['treeData']['selectedNodes']) . ',';
155 $javascript[] = ' width: ' . $width . ',';
156 $javascript[] = ' rendering: false,';
157 $javascript[] = ' listeners: {';
158 $javascript[] = ' click: function(node, event) {';
159 $javascript[] = ' if (typeof(node.attributes.checked) == "boolean") {';
160 $javascript[] = ' node.attributes.checked = ! node.attributes.checked;';
161 $javascript[] = ' node.getUI().toggleCheck(node.attributes.checked);';
162 $javascript[] = ' }';
163 $javascript[] = ' },';
164 $javascript[] = ' dblclick: function(node, event) {';
165 $javascript[] = ' if (typeof(node.attributes.checked) == "boolean") {';
166 $javascript[] = ' node.attributes.checked = ! node.attributes.checked;';
167 $javascript[] = ' node.getUI().toggleCheck(node.attributes.checked);';
168 $javascript[] = ' }';
169 $javascript[] = ' },';
170 $javascript[] = ' checkchange: TYPO3.Components.Tree.TcaCheckChangeHandler,';
171 $javascript[] = ' collapsenode: function(node) {';
172 $javascript[] = ' if (node.id !== "root" && !this.rendering) {';
173 $javascript[] = ' top.TYPO3.Storage.Persistent.removeFromList("tcaTrees." + this.ucId, node.attributes.uid);';
174 $javascript[] = ' }';
175 $javascript[] = ' },';
176 $javascript[] = ' expandnode: function(node) {';
177 $javascript[] = ' if (node.id !== "root" && !this.rendering) {';
178 $javascript[] = ' top.TYPO3.Storage.Persistent.addToList("tcaTrees." + this.ucId, node.attributes.uid);';
179 $javascript[] = ' }';
180 $javascript[] = ' },';
181 $javascript[] = ' beforerender: function(treeCmp) {';
182 $javascript[] = ' this.rendering = true';
183 $javascript[] = ' // Check if that tree element is already rendered. It is appended on the first tceforms_inline call.';
184 $javascript[] = ' if (Ext.fly(treeCmp.getId())) {';
185 $javascript[] = ' return false;';
186 $javascript[] = ' }';
187 $javascript[] = ' },';
188 $javascript[] = ' afterrender: function(treeCmp) {';
189 if ($expanded) {
190 $javascript[] = ' treeCmp.expandAll();';
191 }
192 $javascript[] = ' this.rendering = false;';
193 $javascript[] = ' }';
194 $javascript[] = ' },';
195 $javascript[] = ' tcaMaxItems: ' . $maxItems . ',';
196 $javascript[] = ' tcaSelectRecursiveAllowed: ' . $allowRecursiveMode . ',';
197 $javascript[] = ' tcaSelectRecursive: false,';
198 $javascript[] = ' tcaExclusiveKeys: "' . $exclusiveKeys . '",';
199 $javascript[] = ' ucId: "' . md5(($table . '|' . $field)) . '",';
200 $javascript[] = ' selModel: TYPO3.Components.Tree.EmptySelectionModel,';
201 $javascript[] = ' disabled: ' . $disabled;
202 $javascript[] = ' });';
203
204 if ($autoSizeMax) {
205 $javascript[] = ' tree' . $formElementId . '.bodyStyle = "max-height: ' . $autoSizeMax . 'px;min-height: ' . $height . 'px;";';
206 } else {
207 $javascript[] = ' tree' . $formElementId . '.height = ' . $height . ';';
208 }
209
210 $javascript[] = ' window.setTimeout(function() {';
211 $javascript[] = ' tree' . $formElementId . '.render("tree_' . $formElementId . '");';
212 $javascript[] = ' }, 200);';
213 $javascript[] = '});';
214
215 return implode(LF, $javascript);
216 }
217
218 /**
219 * @return BackendUserAuthentication
220 */
221 protected function getBackendUserAuthentication()
222 {
223 return $GLOBALS['BE_USER'];
224 }
225 }