7e9fcff06f6457df4601b6b467db790be9252541
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / Element / TreeElement.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\Utility\GeneralUtility;
18
19 /**
20 * TCEforms wizard for rendering an AJAX selector for records
21 *
22 * @author Steffen Ritter <info@steffen-ritter.net>
23 * @author Steffen Kamper <steffen@typo3.org>
24 */
25 class TreeElement extends AbstractFormElement {
26
27 /**
28 * renders the tree as replacement for the selector
29 *
30 * @param string $table The table name of the record
31 * @param string $field The field name which this element is supposed to edit
32 * @param array $row The record data array where the value(s) for the field can be found
33 * @param array $PA An array with additional configuration options.
34 * @param array $config (Redundant) content of $PA['fieldConf']['config'] (for convenience)
35 * @param array $possibleSelectboxItems Items available for selection
36 * @param string $noMatchLabel Label for no-matching-value
37 * @return string The HTML code for the TCEform field
38 */
39 public function renderField($table, $field, $row, &$PA, $config, $possibleSelectboxItems, $noMatchLabel) {
40 $valueArray = array();
41 $selectedNodes = array();
42 if (!empty($PA['itemFormElValue'])) {
43 $valueArray = explode(',', $PA['itemFormElValue']);
44 }
45 if (count($valueArray)) {
46 foreach ($valueArray as $selectedValue) {
47 $temp = explode('|', $selectedValue);
48 $selectedNodes[] = $temp[0];
49 }
50 }
51 $allowedUids = array();
52 foreach ($possibleSelectboxItems as $item) {
53 if ((int)$item[1] > 0) {
54 $allowedUids[] = $item[1];
55 }
56 }
57 $treeDataProvider = \TYPO3\CMS\Core\Tree\TableConfiguration\TreeDataProviderFactory::getDataProvider($config, $table, $field, $row);
58 $treeDataProvider->setSelectedList(implode(',', $selectedNodes));
59 $treeDataProvider->setItemWhiteList($allowedUids);
60 $treeDataProvider->initializeTreeData();
61 $treeRenderer = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Tree\TableConfiguration\ExtJsArrayTreeRenderer::class);
62 $tree = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Tree\TableConfiguration\TableConfigurationTree::class);
63 $tree->setDataProvider($treeDataProvider);
64 $tree->setNodeRenderer($treeRenderer);
65 $treeData = $tree->render();
66 $itemArray = array();
67 if (is_array($PA['fieldConf']['config']['items'])) {
68 foreach ($PA['fieldConf']['config']['items'] as $additionalItem) {
69 if ($additionalItem[1] !== '--div--') {
70 $item = new \stdClass();
71 $item->uid = $additionalItem[1];
72 $item->text = $this->getLanguageService()->sL($additionalItem[0]);
73 $item->selectable = TRUE;
74 $item->leaf = TRUE;
75 $item->checked = in_array($additionalItem[1], $selectedNodes);
76 if (file_exists(PATH_typo3 . $additionalItem[3])) {
77 $item->icon = $additionalItem[3];
78 } elseif (strlen(trim($additionalItem[3]))) {
79 $item->iconCls = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconClasses($additionalItem[3]);
80 }
81 $itemArray[] = $item;
82 }
83 }
84 }
85 $itemArray[] = $treeData;
86 $treeData = json_encode($itemArray);
87 $id = md5($PA['itemFormElName']);
88 if (isset($PA['fieldConf']['config']['size']) && (int)$PA['fieldConf']['config']['size'] > 0) {
89 $height = (int)$PA['fieldConf']['config']['size'] * 20;
90 } else {
91 $height = 280;
92 }
93 if (isset($PA['fieldConf']['config']['autoSizeMax']) && (int)$PA['fieldConf']['config']['autoSizeMax'] > 0) {
94 $autoSizeMax = (int)$PA['fieldConf']['config']['autoSizeMax'] * 20;
95 }
96 $header = FALSE;
97 $expanded = FALSE;
98 $width = 280;
99 $appearance = $PA['fieldConf']['config']['treeConfig']['appearance'];
100 if (is_array($appearance)) {
101 $header = $appearance['showHeader'] ? TRUE : FALSE;
102 $expanded = $appearance['expandAll'] === TRUE;
103 if (isset($appearance['width'])) {
104 $width = (int)$appearance['width'];
105 }
106 }
107 $onChange = '';
108 if ($PA['fieldChangeFunc']['TBE_EDITOR_fieldChanged']) {
109 $onChange = $PA['fieldChangeFunc']['TBE_EDITOR_fieldChanged'];
110 }
111 // Create a JavaScript code line which will ask the user to save/update the form due to changing the element.
112 // This is used for eg. "type" fields and others configured with "requestUpdate"
113 if (
114 !empty($GLOBALS['TCA'][$table]['ctrl']['type'])
115 && $field === $GLOBALS['TCA'][$table]['ctrl']['type']
116 || !empty($GLOBALS['TCA'][$table]['ctrl']['requestUpdate'])
117 && GeneralUtility::inList(str_replace(' ', '', $GLOBALS['TCA'][$table]['ctrl']['requestUpdate']), $field)
118 ) {
119 if ($GLOBALS['BE_USER']->jsConfirmation(1)) {
120 $onChange .= 'if (confirm(TBE_EDITOR.labels.onChangeAlert) && ' . 'TBE_EDITOR.checkSubmit(-1)){ TBE_EDITOR.submitForm() };';
121 } else {
122 $onChange .= 'if (TBE_EDITOR.checkSubmit(-1)){ TBE_EDITOR.submitForm() };';
123 }
124 }
125 /** @var $pageRenderer \TYPO3\CMS\Core\Page\PageRenderer */
126 $pageRenderer = $GLOBALS['SOBE']->doc->getPageRenderer();
127 $pageRenderer->loadExtJs();
128 $pageRenderer->addJsFile('sysext/backend/Resources/Public/JavaScript/tree.js');
129 $pageRenderer->addInlineLanguageLabelFile(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('lang') . 'locallang_csh_corebe.xlf', 'tcatree');
130 $pageRenderer->addExtOnReadyCode('
131 TYPO3.Components.Tree.StandardTreeItemData["' . $id . '"] = ' . $treeData . ';
132 var tree' . $id . ' = new TYPO3.Components.Tree.StandardTree({
133 id: "' . $id . '",
134 showHeader: ' . (int)$header . ',
135 onChange: "' . $onChange . '",
136 countSelectedNodes: ' . count($selectedNodes) . ',
137 width: ' . $width . ',
138 listeners: {
139 click: function(node, event) {
140 if (typeof(node.attributes.checked) == "boolean") {
141 node.attributes.checked = ! node.attributes.checked;
142 node.getUI().toggleCheck(node.attributes.checked);
143 }
144 },
145 dblclick: function(node, event) {
146 if (typeof(node.attributes.checked) == "boolean") {
147 node.attributes.checked = ! node.attributes.checked;
148 node.getUI().toggleCheck(node.attributes.checked);
149 }
150 },
151 checkchange: TYPO3.Components.Tree.TcaCheckChangeHandler,
152 collapsenode: function(node) {
153 if (node.id !== "root") {
154 top.TYPO3.BackendUserSettings.ExtDirect.removeFromList("tcaTrees." + this.ucId, node.attributes.uid);
155 }
156 },
157 expandnode: function(node) {
158 if (node.id !== "root") {
159 top.TYPO3.BackendUserSettings.ExtDirect.addToList("tcaTrees." + this.ucId, node.attributes.uid);
160 }
161 },
162 beforerender: function(treeCmp) {
163 // Check if that tree element is already rendered. It is appended on the first tceforms_inline call.
164 if (Ext.fly(treeCmp.getId())) {
165 return false;
166 }
167 }' . ($expanded ? ',
168 afterrender: function(treeCmp) {
169 treeCmp.expandAll();
170 }' : '') . '
171 },
172 tcaMaxItems: ' . ($PA['fieldConf']['config']['maxitems'] ? (int)$PA['fieldConf']['config']['maxitems'] : 99999) . ',
173 tcaSelectRecursiveAllowed: ' . ($appearance['allowRecursiveMode'] ? 'true' : 'false') . ',
174 tcaSelectRecursive: false,
175 tcaExclusiveKeys: "' . ($PA['fieldConf']['config']['exclusiveKeys'] ? $PA['fieldConf']['config']['exclusiveKeys'] : '') . '",
176 ucId: "' . md5(($table . '|' . $field)) . '",
177 selModel: TYPO3.Components.Tree.EmptySelectionModel,
178 disabled: ' . ($PA['fieldConf']['config']['readOnly'] || $this->isRenderReadonly() ? 'true' : 'false') . '
179 });' . LF .
180 ($autoSizeMax
181 ? 'tree' . $id . '.bodyStyle = "max-height: ' . $autoSizeMax . 'px;min-height: ' . $height . 'px;";'
182 : 'tree' . $id . '.height = ' . $height . ';'
183 ) . LF .
184 '(function() {
185 tree' . $id . '.render("tree_' . $id . '");
186 }).defer(20);
187 ');
188 $formField = '
189 <div class="typo3-tceforms-tree">
190 <input class="treeRecord" type="hidden" name="' . htmlspecialchars($PA['itemFormElName']) . '" id="treeinput' . $id . '" value="' . htmlspecialchars($PA['itemFormElValue']) . '" />
191 </div>
192 <div id="tree_' . $id . '">
193
194 </div>';
195 return $formField;
196 }
197
198 /**
199 * Dummy handler
200 *
201 * @param string $table The table name of the record
202 * @param string $field The field name which this element is supposed to edit
203 * @param array $row The record data array where the value(s) for the field can be found
204 * @param array $additionalInformation An array with additional configuration options.
205 * @return string The HTML code for the TCEform field
206 */
207 public function render($table, $field, $row, &$additionalInformation) {
208 // deliberately empty as this class is not used the same way
209 return '';
210 }
211
212 }