[BUGFIX] TCA tree fail to load with IRRE
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / Element / TreeElement.php
1 <?php
2 namespace TYPO3\CMS\Backend\Form\Element;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2013 Steffen Ritter <info@steffen-ritter.net>
8 * (c) 2010-2013 Steffen Kamper <steffen@typo3.org>
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 * A copy is found in the text file GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30
31 use TYPO3\CMS\Core\Utility\GeneralUtility;
32
33 /**
34 * TCEforms wizard for rendering an AJAX selector for records
35 *
36 * @author Steffen Ritter <info@steffen-ritter.net>
37 * @author Steffen Kamper <steffen@typo3.org>
38 */
39 class TreeElement {
40
41 /**
42 * Stores a reference to the original tceForms object
43 *
44 * @var \TYPO3\CMS\Backend\Form\FormEngine
45 */
46 protected $tceForms = NULL;
47
48 /**
49 * Constructor which sets the tceForms.
50 *
51 * @param \TYPO3\CMS\Backend\Form\FormEngine $tceForms
52 */
53 public function __construct(\TYPO3\CMS\Backend\Form\FormEngine &$tceForms) {
54 $this->tceForms = $tceForms;
55 }
56
57 /**
58 * renders the tree as replacement for the selector
59 *
60 * @param string $table The table name of the record
61 * @param string $field The field name which this element is supposed to edit
62 * @param array $row The record data array where the value(s) for the field can be found
63 * @param array $PA An array with additional configuration options.
64 * @param array $config (Redundant) content of $PA['fieldConf']['config'] (for convenience)
65 * @param array $possibleSelectboxItems Items available for selection
66 * @param string $noMatchLabel Label for no-matching-value
67 * @return string The HTML code for the TCEform field
68 */
69 public function renderField($table, $field, $row, &$PA, $config, $possibleSelectboxItems, $noMatchLabel) {
70 $valueArray = array();
71 $selectedNodes = array();
72 if (!empty($PA['itemFormElValue'])) {
73 $valueArray = explode(',', $PA['itemFormElValue']);
74 }
75 if (count($valueArray)) {
76 foreach ($valueArray as $selectedValue) {
77 $temp = explode('|', $selectedValue);
78 $selectedNodes[] = $temp[0];
79 }
80 }
81 $allowedUids = array();
82 foreach ($possibleSelectboxItems as $item) {
83 if ((int)$item[1] > 0) {
84 $allowedUids[] = $item[1];
85 }
86 }
87 $treeDataProvider = \TYPO3\CMS\Core\Tree\TableConfiguration\TreeDataProviderFactory::getDataProvider($config, $table, $field, $row);
88 $treeDataProvider->setSelectedList(implode(',', $selectedNodes));
89 $treeDataProvider->setItemWhiteList($allowedUids);
90 $treeDataProvider->initializeTreeData();
91 $treeRenderer = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Tree\\TableConfiguration\\ExtJsArrayTreeRenderer');
92 $tree = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Tree\\TableConfiguration\\TableConfigurationTree');
93 $tree->setDataProvider($treeDataProvider);
94 $tree->setNodeRenderer($treeRenderer);
95 $treeData = $tree->render();
96 $itemArray = array();
97 if (is_array($PA['fieldConf']['config']['items'])) {
98 foreach ($PA['fieldConf']['config']['items'] as $additionalItem) {
99 if ($additionalItem[1] !== '--div--') {
100 $item = new \stdClass();
101 $item->uid = $additionalItem[1];
102 $item->text = $GLOBALS['LANG']->sL($additionalItem[0]);
103 $item->selectable = TRUE;
104 $item->leaf = TRUE;
105 $item->checked = in_array($additionalItem[1], $selectedNodes);
106 if (file_exists(PATH_typo3 . $additionalItem[3])) {
107 $item->icon = $additionalItem[3];
108 } elseif (strlen(trim($additionalItem[3]))) {
109 $item->iconCls = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconClasses($additionalItem[3]);
110 }
111 $itemArray[] = $item;
112 }
113 }
114 }
115 $itemArray[] = $treeData;
116 $treeData = json_encode($itemArray);
117 $id = md5($PA['itemFormElName']);
118 if (isset($PA['fieldConf']['config']['size']) && (int)$PA['fieldConf']['config']['size'] > 0) {
119 $height = (int)$PA['fieldConf']['config']['size'] * 20;
120 } else {
121 $height = 280;
122 }
123 if (isset($PA['fieldConf']['config']['autoSizeMax']) && (int)$PA['fieldConf']['config']['autoSizeMax'] > 0) {
124 $autoSizeMax = (int)$PA['fieldConf']['config']['autoSizeMax'] * 20;
125 }
126 $header = FALSE;
127 $expanded = FALSE;
128 $width = 280;
129 $appearance = $PA['fieldConf']['config']['treeConfig']['appearance'];
130 if (is_array($appearance)) {
131 $header = $appearance['showHeader'] ? TRUE : FALSE;
132 $expanded = $appearance['expandAll'] === TRUE;
133 if (isset($appearance['width'])) {
134 $width = (int)$appearance['width'];
135 }
136 }
137 $onChange = '';
138 if ($PA['fieldChangeFunc']['TBE_EDITOR_fieldChanged']) {
139 $onChange = $PA['fieldChangeFunc']['TBE_EDITOR_fieldChanged'];
140 }
141 // Create a JavaScript code line which will ask the user to save/update the form due to changing the element.
142 // This is used for eg. "type" fields and others configured with "requestUpdate"
143 if (
144 !empty($GLOBALS['TCA'][$table]['ctrl']['type'])
145 && $field === $GLOBALS['TCA'][$table]['ctrl']['type']
146 || !empty($GLOBALS['TCA'][$table]['ctrl']['requestUpdate'])
147 && GeneralUtility::inList(str_replace(' ', '', $GLOBALS['TCA'][$table]['ctrl']['requestUpdate']), $field)
148 ) {
149 if ($GLOBALS['BE_USER']->jsConfirmation(1)) {
150 $onChange .= 'if (confirm(TBE_EDITOR.labels.onChangeAlert) && ' . 'TBE_EDITOR.checkSubmit(-1)){ TBE_EDITOR.submitForm() };';
151 } else {
152 $onChange .= 'if (TBE_EDITOR.checkSubmit(-1)){ TBE_EDITOR.submitForm() };';
153 }
154 }
155 /** @var $pageRenderer \TYPO3\CMS\Core\Page\PageRenderer */
156 $pageRenderer = $GLOBALS['SOBE']->doc->getPageRenderer();
157 $pageRenderer->loadExtJs();
158 $pageRenderer->addJsFile('sysext/backend/Resources/Public/JavaScript/tree.js');
159 $pageRenderer->addInlineLanguageLabelFile(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('lang') . 'locallang_csh_corebe.xlf', 'tcatree');
160 $pageRenderer->addExtOnReadyCode('
161 TYPO3.Components.Tree.StandardTreeItemData["' . $id . '"] = ' . $treeData . ';
162 var tree' . $id . ' = new TYPO3.Components.Tree.StandardTree({
163 id: "' . $id . '",
164 showHeader: ' . (int)$header . ',
165 onChange: "' . $onChange . '",
166 countSelectedNodes: ' . count($selectedNodes) . ',
167 width: ' . $width . ',
168 listeners: {
169 click: function(node, event) {
170 if (typeof(node.attributes.checked) == "boolean") {
171 node.attributes.checked = ! node.attributes.checked;
172 node.getUI().toggleCheck(node.attributes.checked);
173 }
174 },
175 dblclick: function(node, event) {
176 if (typeof(node.attributes.checked) == "boolean") {
177 node.attributes.checked = ! node.attributes.checked;
178 node.getUI().toggleCheck(node.attributes.checked);
179 }
180 },
181 checkchange: TYPO3.Components.Tree.TcaCheckChangeHandler,
182 collapsenode: function(node) {
183 if (node.id !== "root") {
184 top.TYPO3.BackendUserSettings.ExtDirect.removeFromList("tcaTrees." + this.ucId, node.attributes.uid);
185 }
186 },
187 expandnode: function(node) {
188 if (node.id !== "root") {
189 top.TYPO3.BackendUserSettings.ExtDirect.addToList("tcaTrees." + this.ucId, node.attributes.uid);
190 }
191 },
192 beforerender: function(treeCmp) {
193 // Check if that tree element is already rendered. It is appended on the first tceforms_inline call.
194 if (Ext.fly(treeCmp.getId())) {
195 return false;
196 }
197 }' . ($expanded ? ',
198 afterrender: function(treeCmp) {
199 treeCmp.expandAll();
200 }' : '') . '
201 },
202 tcaMaxItems: ' . ($PA['fieldConf']['config']['maxitems'] ? (int)$PA['fieldConf']['config']['maxitems'] : 99999) . ',
203 tcaSelectRecursiveAllowed: ' . ($appearance['allowRecursiveMode'] ? 'true' : 'false') . ',
204 tcaSelectRecursive: false,
205 tcaExclusiveKeys: "' . ($PA['fieldConf']['config']['exclusiveKeys'] ? $PA['fieldConf']['config']['exclusiveKeys'] : '') . '",
206 ucId: "' . md5(($table . '|' . $field)) . '",
207 selModel: TYPO3.Components.Tree.EmptySelectionModel,
208 disabled: ' . ($PA['fieldConf']['config']['readOnly'] ? 'true' : 'false') . '
209 });' . LF .
210 ($autoSizeMax
211 ? 'tree' . $id . '.bodyStyle = "max-height: ' . $autoSizeMax . 'px;min-height: ' . $height . 'px;";'
212 : 'tree' . $id . '.height = ' . $height . ';'
213 ) . LF .
214 '(function() {
215 tree' . $id . '.render("tree_' . $id . '");
216 }).defer(20);
217 ');
218 $formField = '
219 <div class="typo3-tceforms-tree">
220 <input class="treeRecord" type="hidden" name="' . htmlspecialchars($PA['itemFormElName']) . '" id="treeinput' . $id . '" value="' . htmlspecialchars($PA['itemFormElValue']) . '" />
221 </div>
222 <div id="tree_' . $id . '">
223
224 </div>';
225 return $formField;
226 }
227
228 }