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