d20b3893e8a44877146fd50eed24f6abe72b53fa
[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 $valueArray = array();
67 $selectedNodes = array();
68
69 if(!empty($PA['itemFormElValue'])) {
70 $valueArray = explode(',', $PA['itemFormElValue']);
71 }
72
73 if (count($valueArray)) {
74 foreach ($valueArray as $selectedValue) {
75 $temp = explode('|', $selectedValue);
76 $selectedNodes[] = $temp[0];
77 }
78 }
79 $allowedUids = array();
80 foreach ($possibleSelectboxItems as $item) {
81 if (intval($item[1]) > 0) {
82 $allowedUids[] = $item[1];
83 }
84 }
85 $treeDataProvider = t3lib_tree_Tca_DataProviderFactory::getDataProvider(
86 $config,
87 $table,
88 $field,
89 $row
90 );
91 $treeDataProvider->setSelectedList(implode(',', $selectedNodes));
92 $treeDataProvider->setItemWhiteList($allowedUids);
93 $treeDataProvider->initializeTreeData();
94
95 $treeRenderer = t3lib_div::makeInstance('t3lib_tree_Tca_ExtJsArrayRenderer');
96 $tree = t3lib_div::makeInstance('t3lib_tree_Tca_TcaTree');
97 $tree->setDataProvider($treeDataProvider);
98 $tree->setNodeRenderer($treeRenderer);
99
100 $treeData = $tree->render();
101
102 $itemArray = array();
103 if (is_array($PA['fieldConf']['config']['items'])) {
104 foreach ($PA['fieldConf']['config']['items'] as $additionalItem) {
105 if ($additionalItem[1] !== '--div--') {
106 $item = new stdClass();
107 $item->uid = $additionalItem[1];
108 $item->text = $GLOBALS['LANG']->sL($additionalItem[0]);
109 $item->selectable = TRUE;
110 $item->leaf = TRUE;
111 $item->checked = in_array($additionalItem[1], $selectedNodes);
112 if (file_exists(PATH_typo3 . $additionalItem[3])) {
113 $item->icon = $additionalItem[3];
114 } elseif (strlen(trim($additionalItem[3]))) {
115 $item->iconCls = t3lib_iconWorks::getSpriteIconClasses($additionalItem[3]);
116 }
117
118 $itemArray[] = $item;
119 }
120 }
121 }
122 $itemArray[] = $treeData;
123 $treeData = json_encode($itemArray);
124
125 $id = md5($PA['itemFormElName']);
126
127 if (isset($PA['fieldConf']['config']['size']) && intval($PA['fieldConf']['config']['size']) > 0) {
128 $height = intval($PA['fieldConf']['config']['size']) * 20;
129 } else {
130 $height = 280;
131 }
132 if (isset($PA['fieldConf']['config']['autoSizeMax']) && intval($PA['fieldConf']['config']['autoSizeMax']) > 0) {
133 $autoSizeMax = intval($PA['fieldConf']['config']['autoSizeMax']) * 20;
134 }
135
136
137 $header = FALSE;
138 $expanded = FALSE;
139 $appearance = $PA['fieldConf']['config']['treeConfig']['appearance'];
140 if (is_array($appearance)) {
141 $header = $appearance['showHeader'] ? TRUE : FALSE;
142 $expanded = ($appearance['expandAll'] === TRUE);
143 }
144
145 $onChange = '';
146 if ($PA['fieldChangeFunc']['TBE_EDITOR_fieldChanged']) {
147 $onChange = substr($PA['fieldChangeFunc']['TBE_EDITOR_fieldChanged'], 0, -1);
148 }
149
150 /** @var $pageRenderer t3lib_PageRenderer */
151 $pageRenderer = $GLOBALS['SOBE']->doc->getPageRenderer();
152 $pageRenderer->loadExtJs();
153 $pageRenderer->addJsFile('../t3lib/js/extjs/tree/tree.js');
154 $pageRenderer->addInlineLanguageLabelFile(t3lib_extMgm::extPath('lang') . 'locallang_csh_corebe.xml', 'tcatree');
155 $pageRenderer->addExtOnReadyCode('
156 TYPO3.Components.Tree.StandardTreeItemData["' . $id . '"] = ' . $treeData . ';
157 var tree' . $id . ' = new TYPO3.Components.Tree.StandardTree({
158 id: "' . $id . '",
159 showHeader: ' . intval($header) . ',
160 onChange: "' . $onChange . '",
161 countSelectedNodes: ' . count ($selectedNodes) . ',
162 listeners: {
163 click: function(node, event) {
164 if (typeof(node.attributes.checked) == "boolean") {
165 node.attributes.checked = ! node.attributes.checked;
166 node.getUI().toggleCheck(node.attributes.checked);
167 }
168 },
169 dblclick: 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 checkchange: TYPO3.Components.Tree.TcaCheckChangeHandler,
176 collapsenode: function(node) {
177 top.TYPO3.BackendUserSettings.ExtDirect.removeFromList("tcaTrees." + this.ucId, node.attributes.uid);
178 },
179 expandnode: function(node) {
180 top.TYPO3.BackendUserSettings.ExtDirect.addToList("tcaTrees." + this.ucId, node.attributes.uid);
181 }
182 },
183 tcaMaxItems: ' . ($PA['fieldConf']['config']['maxitems'] ? intval($PA['fieldConf']['config']['maxitems']) : 99999) . ',
184 tcaExclusiveKeys: "' . (
185 $PA['fieldConf']['config']['exclusiveKeys']
186 ? $PA['fieldConf']['config']['exclusiveKeys'] : '') . '",
187 ucId: "' . md5($table . '|' . $field) . '",
188 selModel: TYPO3.Components.Tree.EmptySelectionModel,
189 disabled: ' . ($PA['fieldConf']['config']['readOnly'] ? 'true' : 'false') . '
190 });' . LF .
191 ($autoSizeMax
192 ? 'tree' . $id . '.bodyStyle = "max-height: ' . $autoSizeMax . 'px;min-height: ' . $height . 'px;";'
193 : 'tree' . $id . '.height = ' . $height . ';'
194 ) . LF .
195 'tree' . $id . '.render("tree_' . $id . '");' .
196 ($expanded ? 'tree' . $id . '.expandAll();' : '') . '
197 ');
198
199 $formField = '
200 <div class="typo3-tceforms-tree">
201 <input type="hidden" name="' . htmlspecialchars($PA['itemFormElName']) . '" id="treeinput' . $id . '" value="' . htmlspecialchars($PA['itemFormElValue']) . '" />
202 </div>
203 <div id="tree_' . $id . '">
204
205 </div>';
206
207 return $formField;
208 }
209 }
210
211
212 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['classes/t3lib/tceforms/class.t3lib_tceforms_tree.php'])) {
213 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['classes/t3lib/tceforms/class.t3lib_tceforms_tree.php']);
214 }
215
216 ?>