bddce76bc1e5bf80cae62459d27a3e36a4b30d6d
[Packages/TYPO3.CMS.git] / t3lib / tceforms / class.t3lib_tceforms_tree.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010 Steffen Ritter <info@steffen-ritter.net>
6 * (c) 2010 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 * $Id: class.t3lib_tceforms_suggest.php 7905 2010-06-13 14:42:33Z ohader $
32 *
33 * @author Steffen Ritter <info@steffen-ritter.net>
34 * @author Steffen Kamper <steffen@typo3.org>
35 */
36
37 class t3lib_TCEforms_Tree {
38
39 /**
40 * @param string The table name of the record
41 * @param string The field name which this element is supposed to edit
42 * @param array The record data array where the value(s) for the field can be found
43 * @param array An array with additional configuration options.
44 * @return string The HTML code for the TCEform field
45 */
46 public function renderField($table, $field, $row, &$PA, &$tceForms) {
47 $valueArray = explode(',', $row[$field]);
48 $selectedNodes = array();
49 if (count($valueArray)) {
50 foreach ($valueArray as $selectedValue) {
51 $temp = explode('|', $selectedValue);
52 $selectedNodes[] = $temp[0];
53 }
54 }
55
56 $treeDataProvider = t3lib_tree_Tca_DataProviderFactory::getDataProvider(
57 $GLOBALS['TCA'][$table]['columns'][$field]['config'],
58 $table,
59 $field,
60 $row
61 );
62 $treeDataProvider->setSelectedList(implode(',', $selectedNodes));
63 $treeDataProvider->initializeTreeData();
64 $treeDataProvider->setGeneratedTSConfig($tceForms->setTSconfig($table, $row));
65
66 $treeRenderer = t3lib_div::makeInstance('t3lib_tree_Tca_ExtJsArrayRenderer');
67 $tree = t3lib_div::makeInstance('t3lib_tree_Tca_TcaTree');
68 $tree->setDataProvider($treeDataProvider);
69 $tree->setNodeRenderer($treeRenderer);
70
71 $treeData = $tree->render();
72
73 $itemArray = array();
74 if (is_array($PA['fieldConf']['config']['items'])) {
75 foreach ($PA['fieldConf']['config']['items'] as $additionalItem) {
76 if ($additionalItem[1] !== '--div--') {
77 $item = new stdClass();
78 $item->uid = $additionalItem[1];
79 $item->text = $GLOBALS['LANG']->sL($additionalItem[0]);
80 $item->selectable = TRUE;
81 $item->leaf = TRUE;
82 $item->checked = in_array($additionalItem[1], $selectedNodes);
83 if (file_exists(PATH_typo3 . $additionalItem[3])) {
84 $item->icon = $additionalItem[3];
85 } elseif (strlen(trim($additionalItem[3]))) {
86 $item->iconCls = t3lib_iconWorks::getSpriteIconClasses($additionalItem[3]);
87 }
88
89 $itemArray[] = $item;
90 }
91 }
92 }
93 $itemArray[] = $treeData;
94 $treeData = json_encode($itemArray);
95
96 $id = md5($PA['itemFormElName']);
97
98 if (isset($PA['fieldConf']['config']['size']) && intval($PA['fieldConf']['config']['size']) > 0) {
99 $height = intval($PA['fieldConf']['config']['size']) * 20;
100 } else {
101 $height = 280;
102 }
103 if (isset($PA['fieldConf']['config']['autoSizeMax']) && intval($PA['fieldConf']['config']['autoSizeMax']) > 0) {
104 $autoSizeMax = intval($PA['fieldConf']['config']['autoSizeMax']) * 20;
105 }
106
107
108 $header = FALSE;
109 $expanded = FALSE;
110 $appearance = $PA['fieldConf']['config']['treeConfig']['appearance'];
111 if (is_array($appearance)) {
112 $header = $appearance['showHeader'] ? TRUE : FALSE;
113 $expanded = ($appearance['expandAll'] === TRUE);
114 }
115
116 $onChange = '';
117 if ($PA['fieldChangeFunc']['TBE_EDITOR_fieldChanged']) {
118 $onChange = substr($PA['fieldChangeFunc']['TBE_EDITOR_fieldChanged'], 0, -1);
119 }
120
121 /** @var $pageRenderer t3lib_PageRenderer */
122 $pageRenderer = $GLOBALS['SOBE']->doc->getPageRenderer();
123 $pageRenderer->loadExtJs();
124 $pageRenderer->addJsFile('../t3lib/js/extjs/tree/tree.js');
125 $pageRenderer->addExtOnReadyCode('
126 TYPO3.Components.Tree.StandardTreeItemData["' . $id . '"] = ' . $treeData . ';
127 var tree' . $id . ' = new TYPO3.Components.Tree.StandardTree({
128 checkChangeHandler: TYPO3.Components.Tree.TcaCheckChangeHandler,
129 id: "' . $id . '",
130 showHeader: ' . intval($header) . ',
131 onChange: "' . $onChange . '",
132 tcaMaxItems: ' . ($PA['fieldConf']['config']['maxitems'] ? intval($PA['fieldConf']['config']['maxitems']) : 99999) . ',
133 tcaExclusiveKeys: "' . (
134 $PA['fieldConf']['config']['exclusiveKeys']
135 ? $PA['fieldConf']['config']['exclusiveKeys'] : '') . '",
136 ucId: "' . md5($table . '|' . $field) . '",
137 selModel: TYPO3.Components.Tree.EmptySelectionModel
138 });
139 tree' . $id . '.' . ($autoSizeMax
140 ? 'bodyStyle = "max-height: ' . $autoSizeMax . 'px;"'
141 : 'height = ' . $height
142 ) . ';
143 tree' . $id . '.render("tree_' . $id . '");' .
144 ($expanded ? 'tree' . $id . '.expandAll();' : '') . '
145 ');
146
147 $formField = '
148 <div class="typo3-tceforms-tree">
149 <input type="hidden" name="' . $PA['itemFormElName'] . '" id="treeinput' . $id . '" value="' . $row[$field] . '" />
150 </div>
151 <div id="tree_' . $id . '">
152
153 </div>';
154
155 return $formField;
156 }
157 }
158
159
160 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['classes/t3lib/tceforms/class.t3lib_tceforms_tree.php']) {
161 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['classes/t3lib/tceforms/class.t3lib_tceforms_tree.php']);
162 }
163
164 ?>