2 /***************************************************************
5 * (c) 2010-2011 Steffen Ritter <info@steffen-ritter.net>
6 * (c) 2010-2011 Steffen Kamper <steffen@typo3.org>
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.
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.
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.
26 * This copyright notice MUST APPEAR in all copies of the script!
27 ***************************************************************/
29 * TCEforms wizard for rendering an AJAX selector for records
31 * @author Steffen Ritter <info@steffen-ritter.net>
32 * @author Steffen Kamper <steffen@typo3.org>
35 class t3lib_TCEforms_Tree
{
37 * Stores a reference to the original tceForms object
41 protected $tceForms = NULL;
44 * Constructor which sets the tceForms.
46 * @param t3lib_TCEforms $tceForms
49 public function __construct(t3lib_TCEforms
&$tceForms) {
50 $this->tceForms
= $tceForms;
54 * renders the tree as replacement for the selector
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
65 public function renderField($table, $field, $row, &$PA, $config, $possibleSelectboxItems, $noMatchLabel) {
66 $valueArray = array();
67 $selectedNodes = array();
69 if($PA['itemFormElValue'] != 0) {
70 $valueArray = explode(',', $PA['itemFormElValue']);
73 if (count($valueArray)) {
74 foreach ($valueArray as $selectedValue) {
75 $temp = explode('|', $selectedValue);
76 $selectedNodes[] = $temp[0];
79 $allowedUids = array();
80 foreach ($possibleSelectboxItems as $item) {
81 if (intval($item[1]) > 0) {
82 $allowedUids[] = $item[1];
85 $treeDataProvider = t3lib_tree_Tca_DataProviderFactory
::getDataProvider(
91 $treeDataProvider->setSelectedList(implode(',', $selectedNodes));
92 $treeDataProvider->setItemWhiteList($allowedUids);
93 $treeDataProvider->initializeTreeData();
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);
100 $treeData = $tree->render();
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;
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]);
118 $itemArray[] = $item;
122 $itemArray[] = $treeData;
123 $treeData = json_encode($itemArray);
125 $id = md5($PA['itemFormElName']);
127 if (isset($PA['fieldConf']['config']['size']) && intval($PA['fieldConf']['config']['size']) > 0) {
128 $height = intval($PA['fieldConf']['config']['size']) * 20;
132 if (isset($PA['fieldConf']['config']['autoSizeMax']) && intval($PA['fieldConf']['config']['autoSizeMax']) > 0) {
133 $autoSizeMax = intval($PA['fieldConf']['config']['autoSizeMax']) * 20;
139 $appearance = $PA['fieldConf']['config']['treeConfig']['appearance'];
140 if (is_array($appearance)) {
141 $header = $appearance['showHeader'] ?
TRUE : FALSE;
142 $expanded = ($appearance['expandAll'] === TRUE);
146 if ($PA['fieldChangeFunc']['TBE_EDITOR_fieldChanged']) {
147 $onChange = substr($PA['fieldChangeFunc']['TBE_EDITOR_fieldChanged'], 0, -1);
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->addExtOnReadyCode('
155 TYPO3.Components.Tree.StandardTreeItemData["' . $id . '"] = ' . $treeData . ';
156 var tree' . $id . ' = new TYPO3.Components.Tree.StandardTree({
158 showHeader: ' . intval($header) . ',
159 onChange: "' . $onChange . '",
160 countSelectedNodes: ' . count ($selectedNodes) . ',
162 click: function(node, event) {
163 if (typeof(node.attributes.checked) == "boolean") {
164 node.attributes.checked = ! node.attributes.checked;
165 node.getUI().toggleCheck(node.attributes.checked);
168 dblclick: function(node, event) {
169 if (typeof(node.attributes.checked) == "boolean") {
170 node.attributes.checked = ! node.attributes.checked;
171 node.getUI().toggleCheck(node.attributes.checked);
174 checkchange: TYPO3.Components.Tree.TcaCheckChangeHandler,
175 collapsenode: function(node) {
176 top.TYPO3.BackendUserSettings.ExtDirect.removeFromList("tcaTrees." + this.ucId, node.attributes.uid);
178 expandnode: function(node) {
179 top.TYPO3.BackendUserSettings.ExtDirect.addToList("tcaTrees." + this.ucId, node.attributes.uid);
182 tcaMaxItems: ' . ($PA['fieldConf']['config']['maxitems'] ?
intval($PA['fieldConf']['config']['maxitems']) : 99999) . ',
183 tcaExclusiveKeys: "' . (
184 $PA['fieldConf']['config']['exclusiveKeys']
185 ?
$PA['fieldConf']['config']['exclusiveKeys'] : '') . '",
186 ucId: "' . md5($table . '|' . $field) . '",
187 selModel: TYPO3.Components.Tree.EmptySelectionModel
190 ?
'tree' . $id . '.bodyStyle = "max-height: ' . $autoSizeMax . 'px;min-height: ' . $height . 'px;";'
191 : 'tree' . $id . '.height = ' . $height . ';'
193 'tree' . $id . '.render("tree_' . $id . '");' .
194 ($expanded ?
'tree' . $id . '.expandAll();' : '') . '
198 <div class="typo3-tceforms-tree">
199 <input type="hidden" name="' . htmlspecialchars($PA['itemFormElName']) . '" id="treeinput' . $id . '" value="' . htmlspecialchars($PA['itemFormElValue']) . '" />
201 <div id="tree_' . $id . '">
210 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE
]['XCLASS']['classes/t3lib/tceforms/class.t3lib_tceforms_tree.php'])) {
211 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE
]['XCLASS']['classes/t3lib/tceforms/class.t3lib_tceforms_tree.php']);