2 /***************************************************************
5 * (c) 1999-2005 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 * 82: class t3lib_folderTree extends t3lib_treeView
42 * 89: function t3lib_folderTree()
43 * 107: function wrapIcon($icon,$row)
44 * 130: function wrapTitle($title,$row,$bank=0)
45 * 145: function getId($v)
46 * 155: function getJumpToParam($v)
47 * 167: function getTitleStr($row,$titleLen=30)
48 * 177: function getBrowsableTree()
49 * 240: function getFolderTree($files_path, $depth=999, $depthData='')
50 * 320: function getCount($files_path)
51 * 336: function initializePositionSaving()
54 * (This index is automatically created/updated by the extension "extdeveval")
58 require_once (PATH_t3lib
.'class.t3lib_treeview.php');
74 * Extension class for the t3lib_treeView class, specially made for browsing folders in the File module
76 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
77 * @coauthor René Fritz <r.fritz@colorcube.de>
80 * @see class t3lib_treeView
82 class t3lib_folderTree
extends t3lib_treeView
{
85 * Constructor function of the class
89 function t3lib_folderTree() {
92 $this->MOUNTS
= $GLOBALS['FILEMOUNTS'];
94 $this->treeName
='folder';
95 $this->titleAttrib
=''; //don't apply any title
96 $this->domIdPrefix
= 'folder';
100 * Wrapping the folder icon
102 * @param string The image tag for the icon
103 * @param array The row for the current element
104 * @return string The processed icon input value.
107 function wrapIcon($icon,$row) {
108 // Add title attribute to input icon tag
109 $theFolderIcon = $this->addTagAttributes($icon,($this->titleAttrib ?
$this->titleAttrib
.'="'.$this->getTitleAttrib($row).'"' : ''));
111 // Wrap icon in click-menu link.
112 if (!$this->ext_IconMode
) {
113 $theFolderIcon = $GLOBALS['TBE_TEMPLATE']->wrapClickMenuOnIcon($theFolderIcon,$row['path'],'',0);
114 } elseif (!strcmp($this->ext_IconMode
,'titlelink')) {
115 $aOnClick = 'return jumpTo(\''.$this->getJumpToParam($row).'\',this,\''.$this->domIdPrefix
.$this->getId($row).'\','.$this->bank
.');';
116 $theFolderIcon='<a href="#" onclick="'.htmlspecialchars($aOnClick).'">'.$theFolderIcon.'</a>';
118 return $theFolderIcon;
122 * Wrapping $title in a-tags.
124 * @param string Title string
125 * @param string Item record
126 * @param integer Bank pointer (which mount point number)
130 function wrapTitle($title,$row,$bank=0) {
131 $aOnClick = 'return jumpTo(\''.$this->getJumpToParam($row).'\',this,\''.$this->domIdPrefix
.$this->getId($row).'\','.$bank.');';
133 if ($GLOBALS['TYPO3_CONF_VARS']['BE']['useOnContextMenuHandler']) {
134 $CSM = ' oncontextmenu="'.htmlspecialchars($GLOBALS['TBE_TEMPLATE']->wrapClickMenuOnIcon('',$row['path'],'',0,'','',TRUE)).'"';
136 return '<a href="#" title="'.htmlspecialchars($row['title']).'" onclick="'.htmlspecialchars($aOnClick).'"'.$CSM.'>'.$title.'</a>';
140 * Returns the id from the record - for folders, this is an md5 hash.
142 * @param array Record array
143 * @return integer The "uid" field value.
146 return t3lib_div
::md5Int($v['path']);
150 * Returns jump-url parameter value.
152 * @param array The record array.
153 * @return string The jump-url parameter.
155 function getJumpToParam($v) {
156 return rawurlencode($v['path']);
160 * Returns the title for the input record. If blank, a "no title" labele (localized) will be returned.
161 * '_title' is used for setting an alternative title for folders.
163 * @param array The input row array (where the key "_title" is used for the title)
164 * @param integer Title length (30)
165 * @return string The title.
167 function getTitleStr($row,$titleLen=30) {
168 return $row['_title'] ?
$row['_title'] : parent
::getTitleStr($row,$titleLen);
172 * Will create and return the HTML code for a browsable tree of folders.
173 * Is based on the mounts found in the internal array ->MOUNTS (set in the constructor)
175 * @return string HTML code for the browsable tree
177 function getBrowsableTree() {
179 // Get stored tree structure AND updating it if needed according to incoming PM GET var.
180 $this->initializePositionSaving();
183 $titleLen=intval($this->BE_USER
->uc
['titleLen']);
187 foreach($this->MOUNTS
as $key => $val) {
188 $md5_uid = md5($val['path']);
189 $specUID=hexdec(substr($md5_uid,0,6));
190 $this->specUIDmap
[$specUID]=$val['path'];
193 $this->bank
=$val['nkey'];
194 $isOpen = $this->stored
[$val['nkey']][$specUID] ||
$this->expandFirst
;
198 $cmd=$this->bank
.'_'.($isOpen?
'0_':'1_').$specUID.'_'.$this->treeName
;
199 $icon='<img'.t3lib_iconWorks
::skinImg($this->backPath
,'gfx/ol/'.($isOpen?
'minus':'plus').'only.gif','width="18" height="16"').' alt="" />';
200 $firstHtml= $this->PM_ATagWrap($icon,$cmd);
202 switch($val['type']) {
203 case 'user': $icon = 'gfx/i/_icon_ftp_user.gif'; break;
204 case 'group': $icon = 'gfx/i/_icon_ftp_group.gif'; break;
205 default: $icon = 'gfx/i/_icon_ftp.gif'; break;
208 // Preparing rootRec for the mount
209 $firstHtml.=$this->wrapIcon('<img'.t3lib_iconWorks
::skinImg($this->backPath
,$icon,'width="18" height="16"').' alt="" />',$val);
211 $row['path']=$val['path'];
212 $row['uid']=$specUID;
213 $row['title']=$val['name'];
215 // Add the root of the mount to ->tree
216 $this->tree
[]=array('HTML'=>$firstHtml,'row'=>$row,'bank'=>$this->bank
);
218 // If the mount is expanded, go down:
221 $depthD='<img'.t3lib_iconWorks
::skinImg($this->backPath
,'gfx/ol/blank.gif','width="18" height="16"').' alt="" />';
222 $this->getFolderTree($val['path'],999,$depthD);
226 $treeArr=array_merge($treeArr,$this->tree
);
228 return $this->printTree($treeArr);
232 * Fetches the data for the tree
234 * @param string Abs file path
235 * @param integer Max depth (recursivity limit)
236 * @param string HTML-code prefix for recursive calls.
237 * @return integer The count of items on the level
238 * @see getBrowsableTree()
240 function getFolderTree($files_path, $depth=999, $depthData='') {
242 // This generates the directory tree
243 $dirs = t3lib_div
::get_dirs($files_path);
246 if (is_array($dirs)) {
247 $depth=intval($depth);
253 foreach($dirs as $key => $val) {
255 $this->tree
[]=array(); // Reserve space.
257 $treeKey = key($this->tree
); // Get the key for this space
258 $LN = ($a==$c)?
'blank':'line';
260 $val = ereg_replace('^\./','',$val);
262 $path = $files_path.$val.'/';
263 $webpath=t3lib_BEfunc
::getPathType_web_nonweb($path);
265 $md5_uid = md5($path);
266 $specUID=hexdec(substr($md5_uid,0,6));
267 $this->specUIDmap
[$specUID]=$path;
270 $row['uid']=$specUID;
271 $row['title']=$title;
273 if ($depth>1 && $this->expandNext($specUID)) {
274 $nextCount=$this->getFolderTree(
277 $this->makeHTML ?
$depthData.'<img'.t3lib_iconWorks
::skinImg($this->backPath
,'gfx/ol/'.$LN.'.gif','width="18" height="16"').' alt="" />' : ''
279 $exp=1; // Set "did expand" flag
281 $nextCount=$this->getCount($path);
282 $exp=0; // Clear "did expand" flag
285 // Set HTML-icons, if any:
286 if ($this->makeHTML
) {
287 $HTML=$depthData.$this->PMicon($row,$a,$c,$nextCount,$exp);
289 $icon = 'gfx/i/_icon_'.$webpath.'folders.gif';
290 if ($val=='_temp_') {
291 $icon = 'gfx/i/sysf.gif';
292 $row['title']='TEMP';
293 $row['_title']='<b>TEMP</b>';
295 if ($val=='_recycler_') {
296 $icon = 'gfx/i/recycler.gif';
297 $row['title']='RECYCLER';
298 $row['_title']='<b>RECYCLER</b>';
300 $HTML.=$this->wrapIcon('<img'.t3lib_iconWorks
::skinImg($this->backPath
,$icon,'width="18" height="16"').' alt="" />',$row);
303 // Finally, add the row/HTML content to the ->tree array in the reserved key.
304 $this->tree
[$treeKey] = Array(
315 * Counts the number of directories in a file path.
317 * @param string File path.
320 function getCount($files_path) {
321 // This generates the directory tree
322 $dirs = t3lib_div
::get_dirs($files_path);
324 if (is_array($dirs)) {
331 * Get stored tree structure AND updating it if needed according to incoming PM GET var.
336 function initializePositionSaving() {
337 // Get stored tree structure:
338 $this->stored
=unserialize($this->BE_USER
->uc
['browseTrees'][$this->treeName
]);
340 // Mapping md5-hash to shorter number:
342 foreach($this->MOUNTS
as $key => $val) {
343 $nkey = hexdec(substr($key,0,4));
344 $hashMap[$nkey]=$key;
345 $this->MOUNTS
[$key]['nkey']=$nkey;
349 // (If an plus/minus icon has been clicked, the PM GET var is sent and we must update the stored positions in the tree):
350 $PM = explode('_',t3lib_div
::_GP('PM')); // 0: mount key, 1: set/clear boolean, 2: item ID (cannot contain "_"), 3: treeName
351 if (count($PM)==4 && $PM[3]==$this->treeName
) {
352 if (isset($this->MOUNTS
[$hashMap[$PM[0]]])) {
354 $this->stored
[$PM[0]][$PM[2]]=1;
355 $this->savePosition($this->treeName
);
357 unset($this->stored
[$PM[0]][$PM[2]]);
358 $this->savePosition($this->treeName
);
365 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE
]['XCLASS']['t3lib/class.t3lib_foldertree.php']) {
366 include_once($TYPO3_CONF_VARS[TYPO3_MODE
]['XCLASS']['t3lib/class.t3lib_foldertree.php']);