2 /***************************************************************
5 * (c) 1999-2004 Kasper Skaarhoj (kasperYYYY@typo3.com)
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
28 * Generate a folder tree
31 * Revised for TYPO3 3.6 November/2003 by Kasper Skaarhoj
33 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
34 * @coauthor René Fritz <r.fritz@colorcube.de>
37 * [CLASS/FUNCTION INDEX of SCRIPT]
41 * 81: class t3lib_folderTree extends t3lib_treeView
42 * 88: function t3lib_folderTree()
43 * 106: function wrapIcon($icon,$row)
44 * 126: function getId($v)
45 * 136: function getJumpToParam($v)
46 * 148: function getTitleStr($row,$titleLen=30)
47 * 158: function getBrowsableTree()
48 * 221: function getFolderTree($files_path, $depth=999, $depthData='')
49 * 301: function getCount($files_path)
50 * 317: function initializePositionSaving()
53 * (This index is automatically created/updated by the extension "extdeveval")
57 require_once (PATH_t3lib
.'class.t3lib_treeview.php');
73 * Extension class for the t3lib_treeView class, specially made for browsing folders in the File module
75 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
76 * @coauthor René Fritz <r.fritz@colorcube.de>
79 * @see class t3lib_treeView
81 class t3lib_folderTree
extends t3lib_treeView
{
84 * Constructor function of the class
88 function t3lib_folderTree() {
91 $this->MOUNTS
= $GLOBALS['FILEMOUNTS'];
93 $this->treeName
='folder';
94 $this->titleAttrib
=''; //don't apply any title
95 $this->domIdPrefix
= 'folder';
99 * Wrapping the folder icon
101 * @param string The image tag for the icon
102 * @param array The row for the current element
103 * @return string The processed icon input value.
106 function wrapIcon($icon,$row) {
107 // Add title attribute to input icon tag
108 $theFolderIcon = $this->addTagAttributes($icon,($this->titleAttrib ?
$this->titleAttrib
.'="'.$this->getTitleAttrib($row).'"' : ''));
110 // Wrap icon in click-menu link.
111 if (!$this->ext_IconMode
) {
112 $theFolderIcon = $GLOBALS['TBE_TEMPLATE']->wrapClickMenuOnIcon($theFolderIcon,$row['path'],'',0);
113 } elseif (!strcmp($this->ext_IconMode
,'titlelink')) {
114 $aOnClick = 'return jumpTo(\''.$this->getJumpToParam($row).'\',this,\''.$this->domIdPrefix
.$this->getId($row).'_'.$this->bank
.'\');';
115 $theFolderIcon='<a href="#" onclick="'.htmlspecialchars($aOnClick).'">'.$theFolderIcon.'</a>';
117 return $theFolderIcon;
121 * Wrapping $title in a-tags.
123 * @param string Title string
124 * @param string Item record
125 * @param integer Bank pointer (which mount point number)
129 function wrapTitle($title,$row,$bank=0) {
130 $aOnClick = 'return jumpTo(\''.$this->getJumpToParam($row).'\',this,\''.$this->domIdPrefix
.$this->getId($row).'_'.$bank.'\');';
132 if ($GLOBALS['TYPO3_CONF_VARS']['BE']['useOnContextMenuHandler']) {
133 $CSM = ' oncontextmenu="'.htmlspecialchars($GLOBALS['TBE_TEMPLATE']->wrapClickMenuOnIcon('',$row['path'],'',0,'','',TRUE)).'"';
135 return '<a href="#" onclick="'.htmlspecialchars($aOnClick).'"'.$CSM.'>'.$title.'</a>';
139 * Returns the id from the record - for folders, this is an md5 hash.
141 * @param array Record array
142 * @return integer The "uid" field value.
145 return t3lib_div
::md5Int($v['path']);
149 * Returns jump-url parameter value.
151 * @param array The record array.
152 * @return string The jump-url parameter.
154 function getJumpToParam($v) {
155 return rawurlencode($v['path']);
159 * Returns the title for the input record. If blank, a "no title" labele (localized) will be returned.
160 * '_title' is used for setting an alternative title for folders.
162 * @param array The input row array (where the key "_title" is used for the title)
163 * @param integer Title length (30)
164 * @return string The title.
166 function getTitleStr($row,$titleLen=30) {
167 return $row['_title'] ?
$row['_title'] : parent
::getTitleStr($row,$titleLen);
171 * Will create and return the HTML code for a browsable tree of folders.
172 * Is based on the mounts found in the internal array ->MOUNTS (set in the constructor)
174 * @return string HTML code for the browsable tree
176 function getBrowsableTree() {
178 // Get stored tree structure AND updating it if needed according to incoming PM GET var.
179 $this->initializePositionSaving();
182 $titleLen=intval($this->BE_USER
->uc
['titleLen']);
186 foreach($this->MOUNTS
as $key => $val) {
187 $md5_uid = md5($val['path']);
188 $specUID=hexdec(substr($md5_uid,0,6));
189 $this->specUIDmap
[$specUID]=$val['path'];
192 $this->bank
=$val['nkey'];
193 $isOpen = $this->stored
[$val['nkey']][$specUID] ||
$this->expandFirst
;
197 $cmd=$this->bank
.'_'.($isOpen?
'0_':'1_').$specUID.'_'.$this->treeName
;
198 $icon='<img'.t3lib_iconWorks
::skinImg($this->backPath
,'gfx/ol/'.($isOpen?
'minus':'plus').'only.gif','width="18" height="16"').' alt="" />';
199 $firstHtml= $this->PM_ATagWrap($icon,$cmd);
201 switch($val['type']) {
202 case 'user': $icon = 'gfx/i/_icon_ftp_user.gif'; break;
203 case 'group': $icon = 'gfx/i/_icon_ftp_group.gif'; break;
204 default: $icon = 'gfx/i/_icon_ftp.gif'; break;
207 // Preparing rootRec for the mount
208 $firstHtml.=$this->wrapIcon('<img'.t3lib_iconWorks
::skinImg($this->backPath
,$icon,'width="18" height="16"').' alt="" />',$val);
210 $row['path']=$val['path'];
211 $row['uid']=$specUID;
212 $row['title']=$val['name'];
214 // Add the root of the mount to ->tree
215 $this->tree
[]=array('HTML'=>$firstHtml,'row'=>$row,'bank'=>$this->bank
);
217 // If the mount is expanded, go down:
220 $depthD='<img'.t3lib_iconWorks
::skinImg($this->backPath
,'gfx/ol/blank.gif','width="18" height="16"').' alt="" />';
221 $this->getFolderTree($val['path'],999,$depthD);
225 $treeArr=array_merge($treeArr,$this->tree
);
227 return $this->printTree($treeArr);
231 * Fetches the data for the tree
233 * @param string Abs file path
234 * @param integer Max depth (recursivity limit)
235 * @param string HTML-code prefix for recursive calls.
236 * @return integer The count of items on the level
237 * @see getBrowsableTree()
239 function getFolderTree($files_path, $depth=999, $depthData='') {
241 // This generates the directory tree
242 $dirs = t3lib_div
::get_dirs($files_path);
245 if (is_array($dirs)) {
246 $depth=intval($depth);
252 foreach($dirs as $key => $val) {
254 $this->tree
[]=array(); // Reserve space.
256 $treeKey = key($this->tree
); // Get the key for this space
257 $LN = ($a==$c)?
'blank':'line';
259 $val = ereg_replace('^\./','',$val);
261 $path = $files_path.$val.'/';
262 $webpath=t3lib_BEfunc
::getPathType_web_nonweb($path);
264 $md5_uid = md5($path);
265 $specUID=hexdec(substr($md5_uid,0,6));
266 $this->specUIDmap
[$specUID]=$path;
269 $row['uid']=$specUID;
270 $row['title']=$title;
272 if ($depth>1 && $this->expandNext($specUID)) {
273 $nextCount=$this->getFolderTree(
276 $this->makeHTML ?
$depthData.'<img'.t3lib_iconWorks
::skinImg($this->backPath
,'gfx/ol/'.$LN.'.gif','width="18" height="16"').' alt="" />' : ''
278 $exp=1; // Set "did expand" flag
280 $nextCount=$this->getCount($path);
281 $exp=0; // Clear "did expand" flag
284 // Set HTML-icons, if any:
285 if ($this->makeHTML
) {
286 $HTML=$depthData.$this->PMicon($row,$a,$c,$nextCount,$exp);
288 $icon = 'gfx/i/_icon_'.$webpath.'folders.gif';
289 if ($val=='_temp_') {
290 $icon = 'gfx/i/sysf.gif';
291 $row['title']='TEMP';
292 $row['_title']='<b>TEMP</b>';
294 if ($val=='_recycler_') {
295 $icon = 'gfx/i/recycler.gif';
296 $row['title']='RECYCLER';
297 $row['_title']='<b>RECYCLER</b>';
299 $HTML.=$this->wrapIcon('<img'.t3lib_iconWorks
::skinImg($this->backPath
,$icon,'width="18" height="16"').' alt="" />',$row);
302 // Finally, add the row/HTML content to the ->tree array in the reserved key.
303 $this->tree
[$treeKey] = Array(
314 * Counts the number of directories in a file path.
316 * @param string File path.
319 function getCount($files_path) {
320 // This generates the directory tree
321 $dirs = t3lib_div
::get_dirs($files_path);
323 if (is_array($dirs)) {
330 * Get stored tree structure AND updating it if needed according to incoming PM GET var.
335 function initializePositionSaving() {
336 // Get stored tree structure:
337 $this->stored
=unserialize($this->BE_USER
->uc
['browseTrees'][$this->treeName
]);
339 // Mapping md5-hash to shorter number:
341 foreach($this->MOUNTS
as $key => $val) {
342 $nkey = hexdec(substr($key,0,4));
343 $hashMap[$nkey]=$key;
344 $this->MOUNTS
[$key]['nkey']=$nkey;
348 // (If an plus/minus icon has been clicked, the PM GET var is sent and we must update the stored positions in the tree):
349 $PM = explode('_',t3lib_div
::_GP('PM')); // 0: mount key, 1: set/clear boolean, 2: item ID (cannot contain "_"), 3: treeName
350 if (count($PM)==4 && $PM[3]==$this->treeName
) {
351 if (isset($this->MOUNTS
[$hashMap[$PM[0]]])) {
353 $this->stored
[$PM[0]][$PM[2]]=1;
354 $this->savePosition($this->treeName
);
356 unset($this->stored
[$PM[0]][$PM[2]]);
357 $this->savePosition($this->treeName
);
364 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE
]['XCLASS']['t3lib/class.t3lib_foldertree.php']) {
365 include_once($TYPO3_CONF_VARS[TYPO3_MODE
]['XCLASS']['t3lib/class.t3lib_foldertree.php']);