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