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