[TASK] Use non-extjs labels for select tree
[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\Core\Authentication\BackendUserAuthentication;
18 use TYPO3\CMS\Core\Type\Bitmask\JsConfirmation;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Render data as a tree.
23 *
24 * Typically rendered for config type=select, renderType=selectTree
25 */
26 class SelectTreeElement extends AbstractFormElement
27 {
28 /**
29 * Default height of the tree in pixels.
30 *
31 * @const
32 */
33 const DEFAULT_HEIGHT = 280;
34
35 /**
36 * Render tree widget
37 *
38 * @return array As defined in initializeResultArray() of AbstractNode
39 * @see AbstractNode::initializeResultArray()
40 */
41 public function render()
42 {
43 $resultArray = $this->initializeResultArray();
44 $parameterArray = $this->data['parameterArray'];
45 $formElementId = md5($parameterArray['itemFormElName']);
46
47 // Field configuration from TCA:
48 $config = $parameterArray['fieldConf']['config'];
49 $readOnly = !empty($config['readOnly']) ? 'true' : 'false';
50 $exclusiveKeys = !empty($config['exclusiveKeys']) ? $config['exclusiveKeys'] : '';
51 $exclusiveKeys = $exclusiveKeys . ',';
52 $appearance = !empty($config['treeConfig']['appearance']) ? $config['treeConfig']['appearance'] : [];
53 $expanded = !empty($appearance['expandAll']);
54 $showHeader = !empty($appearance['showHeader']);
55 if (isset($config['size']) && (int)$config['size'] > 0) {
56 $height = min(count($config['items']), (int)$config['size']) * 20;
57 } else {
58 $height = static::DEFAULT_HEIGHT;
59 }
60
61 $treeWrapperId = 'tree_' . $formElementId;
62
63 $html = [];
64 $html[] = '<div class="typo3-tceforms-tree">';
65 $html[] = ' <input class="treeRecord" type="hidden"';
66 $html[] = ' ' . $this->getValidationDataAsDataAttribute($parameterArray['fieldConf']['config']);
67 $html[] = ' data-formengine-input-name="' . htmlspecialchars($parameterArray['itemFormElName']) . '"';
68 $html[] = ' data-relatedfieldname="' . htmlspecialchars($parameterArray['itemFormElName']) . '"';
69 $html[] = ' data-table="' . htmlspecialchars($this->data['tableName']) . '"';
70 $html[] = ' data-field="' . htmlspecialchars($this->data['fieldName']) . '"';
71 $html[] = ' data-uid="' . (int)$this->data['vanillaUid'] . '"';
72 $html[] = ' data-read-only="' . $readOnly . '"';
73 $html[] = ' data-tree-exclusive-keys="' . htmlspecialchars($exclusiveKeys) . '"';
74 $html[] = ' data-tree-expand-up-to-level="' . ($expanded ? '999' : '1') . '"';
75 $html[] = ' data-tree-show-toolbar="' . $showHeader . '"';
76 $html[] = ' name="' . htmlspecialchars($parameterArray['itemFormElName']) . '"';
77 $html[] = ' id="treeinput' . $formElementId . '"';
78 $html[] = ' value="' . htmlspecialchars(implode(',', $config['treeData']['selectedNodes'])) . '"';
79 $html[] = ' />';
80 $html[] = '</div>';
81 $html[] = '<div id="' . $treeWrapperId . '" class="svg-tree-wrapper" style="height: ' . $height . 'px;"></div>';
82 $html[] = '<script type="text/javascript">var ' . $treeWrapperId . ' = ' . $this->getTreeOnChangeJs() . '</script>';
83
84 $resultArray['html'] = implode(LF, $html);
85
86 // add necessary labels for tree header
87 if ($showHeader) {
88 $resultArray['additionalInlineLanguageLabelFiles'][] = 'EXT:lang/locallang_csh_corebe.xlf';
89 }
90 $resultArray['requireJsModules']['selectTreeElement'] = 'TYPO3/CMS/Backend/FormEngine/Element/SelectTreeElement';
91
92 return $resultArray;
93 }
94
95 /**
96 * Generates JS code triggered on change of the tree
97 *
98 * @return string
99 */
100 protected function getTreeOnChangeJs()
101 {
102 $table = $this->data['tableName'];
103 $field = $this->data['fieldName'];
104 $parameterArray = $this->data['parameterArray'];
105 $onChange = !empty($parameterArray['fieldChangeFunc']['TBE_EDITOR_fieldChanged']) ? $parameterArray['fieldChangeFunc']['TBE_EDITOR_fieldChanged'] : '';
106 $onChange .= !empty($parameterArray['fieldChangeFunc']['alert']) ? $parameterArray['fieldChangeFunc']['alert'] : '';
107
108 // Create a JavaScript code line which will ask the user to save/update the form due to changing the element.
109 // This is used for eg. "type" fields and others configured with "requestUpdate"
110 if (
111 !empty($GLOBALS['TCA'][$table]['ctrl']['type'])
112 && $field === $GLOBALS['TCA'][$table]['ctrl']['type']
113 || !empty($GLOBALS['TCA'][$table]['ctrl']['requestUpdate'])
114 && GeneralUtility::inList(str_replace(' ', '', $GLOBALS['TCA'][$table]['ctrl']['requestUpdate']), $field)
115 ) {
116 if ($this->getBackendUserAuthentication()->jsConfirmation(JsConfirmation::TYPE_CHANGE)) {
117 $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(); });';
118 } else {
119 $onChange .= 'if (TBE_EDITOR.checkSubmit(-1)){ TBE_EDITOR.submitForm() };';
120 }
121 }
122 return 'function () {' . $onChange . '}';
123 }
124
125 /**
126 * @return BackendUserAuthentication
127 */
128 protected function getBackendUserAuthentication()
129 {
130 return $GLOBALS['BE_USER'];
131 }
132 }