Added $Id$ keywords, cleaned up comment tags
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_foldertree.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2002 Kasper Skaarhoj (kasper@typo3.com)
6 * All rights reserved
7 *
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.
13 *
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.
18 *
19 *
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.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Generate a folder tree
29 *
30 * $Id$
31 * Revised for TYPO3 3.6 August/2003 by Kasper Skaarhoj
32 * Maintained by René Fritz
33 *
34 * @author Kasper Skaarhoj <kasper@typo3.com>
35 * @coauthor René Fritz <r.fritz@colorcube.de>
36 */
37 /**
38 * [CLASS/FUNCTION INDEX of SCRIPT]
39 *
40 *
41 *
42 * 82: class t3lib_folderTree extends t3lib_treeView
43 * 87: function t3lib_folderTree()
44 * 107: function wrapTitle($title,&$row)
45 * 119: function wrapIcon($icon,&$row)
46 * 139: function getId(&$v)
47 * 149: function getJumpToParm(&$v)
48 * 159: function getTitleStr(&$row)
49 * 168: function getBrowsableTree()
50 * 249: function getFolderTree($files_path, $depth=999, $depthData='')
51 * 318: function getCount($files_path)
52 *
53 * TOTAL FUNCTIONS: 9
54 * (This index is automatically created/updated by the extension "extdeveval")
55 *
56 */
57
58 require_once (PATH_t3lib.'class.t3lib_treeview.php');
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 /**
74 * Extension class for the t3lib_browsetree class, specially made for browsing folders in the File module
75 *
76 * @author Kasper Skaarhoj <kasper@typo3.com>
77 * @coauthor René Fritz <r.fritz@colorcube.de>
78 * @package TYPO3
79 * @subpackage t3lib
80 * @see class t3lib_treeView
81 */
82 class t3lib_folderTree extends t3lib_treeView {
83
84 /**
85 * @return [type] ...
86 */
87 function t3lib_folderTree() {
88 $this->BE_USER = $GLOBALS['BE_USER'];
89 $this->titleAttrib = t3lib_BEfunc::titleAttrib();
90 $this->backPath = $GLOBALS['BACK_PATH'];
91
92 $this->MOUNTS = $GLOBALS['FILEMOUNTS'];
93 $this->treeName='folder';
94 $this->titleAttrib=''; //don't apply any title
95 $this->domIdPrefix = 'folder';
96 // unsused $this->iconName = 'folder';
97 }
98
99 /**
100 * Wrapping $title in a-tags.
101 * $row is the array with path and other info.
102 *
103 * @param [type] $title: ...
104 * @param [type] $row: ...
105 * @return [type] ...
106 */
107 function wrapTitle($title,&$row) {
108 $aOnClick = 'return jumpTo('.$this->getJumpToParm($row).',this,'.$this->getId($row).');';
109 return '<a href="#" onclick="'.htmlspecialchars($aOnClick).'">'.$title.'</a>';
110 }
111
112 /**
113 * Wrapping the folder icon
114 *
115 * @param [type] $icon: ...
116 * @param [type] $row: ...
117 * @return [type] ...
118 */
119 function wrapIcon($icon,&$row) {
120 // Add border attribute...
121 $theFolderIcon = substr($icon,0,-1).' border="0" />';
122
123 // Wrap icon in click-menu link.
124 if (!$this->ext_IconMode) {
125 $theFolderIcon = $GLOBALS['TBE_TEMPLATE']->wrapClickMenuOnIcon($theFolderIcon,$row['path'],'',0);
126 } elseif (!strcmp($this->ext_IconMode,'titlelink')) {
127 $aOnClick = 'return jumpTo('.$this->getJumpToParm($row).'\',this,'.$this->getId($row).');';
128 $theFolderIcon='<a href="#" onclick="'.htmlspecialchars($aOnClick).'">'.$theFolderIcon.'</a>';
129 }
130 return $theFolderIcon;
131 }
132
133 /**
134 * [Describe function...]
135 *
136 * @param [type] $$v: ...
137 * @return [type] ...
138 */
139 function getId(&$v) {
140 return t3lib_div::md5Int($v['path']);
141 }
142
143 /**
144 * [Describe function...]
145 *
146 * @param [type] $$v: ...
147 * @return [type] ...
148 */
149 function getJumpToParm(&$v) {
150 return "'".rawurlencode($v['path'])."'";
151 }
152
153 /**
154 * [Describe function...]
155 *
156 * @param [type] $$row: ...
157 * @return [type] ...
158 */
159 function getTitleStr(&$row) {
160 return $row['_title'] ? $row['_title'] : parent::getTitleStr($row);
161 }
162
163 /**
164 * [Describe function...]
165 *
166 * @return [type] ...
167 */
168 function getBrowsableTree() {
169 $this->init($addClause);
170
171 // Get stored tree structure:
172 $this->stored=unserialize($this->BE_USER->uc[$this->treeName]);
173
174 // Mapping md5-hash to shorter number:
175 $hashMap=array();
176 reset($this->MOUNTS);
177 while (list($key,$val) = each($this->MOUNTS)) {
178 $nkey = hexdec(substr($key,0,4));
179 $hashMap[$nkey]=$key;
180 $this->MOUNTS[$key]['nkey']=$nkey;
181 }
182
183 // PM action:
184 $PM = explode('_',t3lib_div::GPvar('PM'));
185 if (count($PM)==4 && $PM[3]==$this->treeName) {
186 if (isset($this->MOUNTS[$hashMap[$PM[0]]])) {
187 if ($PM[1]) { // set
188 $this->stored[$PM[0]][$PM[2]]=1;
189 $this->savePosition($this->treeName);
190 } else { // clear
191 unset($this->stored[$PM[0]][$PM[2]]);
192 $this->savePosition($this->treeName);
193 }
194 }
195 }
196
197
198 // traverse mounts:
199 $titleLen=intval($this->BE_USER->uc['titleLen']);
200 $treeArr=array();
201 reset($this->MOUNTS);
202 while (list($key,$val) = each($this->MOUNTS)) {
203 $md5_uid = md5($val['path']);
204 $specUID=hexdec(substr($md5_uid,0,6));
205 $this->specUIDmap[$specUID]=$val['path'];
206
207 // Set first:
208 $this->bank=$val['nkey'];
209 $isOpen = $this->stored[$val['nkey']][$specUID] || $this->expandFirst;
210 $this->reset();
211
212 // Set PM icon:
213 $cmd=$this->bank.'_'.($isOpen?'0_':'1_').$specUID.'_'.$this->treeName;
214 $icon='<img src="'.$this->backPath.'t3lib/gfx/ol/'.($isOpen?'minus':'plus').'only.gif" width="18" height="16" align="top" border="0" alt="" \></a>';
215 $firstHtml= $this->PM_ATagWrap($icon,$cmd);
216
217 switch($val['type']) {
218 case 'user': $icon = 'gfx/i/_icon_ftp_user.gif'; break;
219 case 'group': $icon = 'gfx/i/_icon_ftp_group.gif'; break;
220 default: $icon = 'gfx/i/_icon_ftp.gif'; break;
221 }
222
223 $firstHtml.=$this->wrapIcon('<img src="'.$this->backPath.$icon.'" width="18" height="16" align="top" alt="" \>',$val);
224 $row=array();
225 $row['path']=$val['path'];
226 $row['uid']=$specUID;
227 $row['title']=$val['name'];
228 $this->tree[]=array('HTML'=>$firstHtml,'row'=>$row);
229
230 if ($isOpen) {
231 // Set depth:
232 $depthD='<img src="'.$this->backPath.'t3lib/gfx/ol/blank.gif" width="18" height="16" align="top" alt="" \>';
233 $this->getFolderTree($val['path'],999,$depthD);
234 }
235 // Add tree:
236 $treeArr=array_merge($treeArr,$this->tree);
237 }
238 return $this->printTree($treeArr);
239 }
240
241 /**
242 * [Describe function...]
243 *
244 * @param [type] $files_path: ...
245 * @param [type] $depth: ...
246 * @param [type] $depthData: ...
247 * @return [type] ...
248 */
249 function getFolderTree($files_path, $depth=999, $depthData='') {
250 // This generates the directory tree
251 $dirs = t3lib_div::get_dirs($files_path);
252 // debug($dirs);
253 $c=0;
254 if (is_array($dirs)) {
255 $depth=intval($depth);
256 $HTML='';
257 $a=0;
258 $c=count($dirs);
259 sort($dirs);
260
261 while (list($key,$val)= each($dirs)) {
262 $a++;
263 $this->tree[]=array(); // Reserve space.
264 end($this->tree);
265 $treeKey = key($this->tree); // Get the key for this space
266 $LN = ($a==$c)?'blank':'line';
267
268 $val = ereg_replace('^\./','',$val);
269 $title = $val;
270 $path = $files_path.$val.'/';
271 $webpath=t3lib_BEfunc::getPathType_web_nonweb($path);
272
273 $md5_uid = md5($path);
274 $specUID=hexdec(substr($md5_uid,0,6));
275 $this->specUIDmap[$specUID]=$path;
276 $row=array();
277 $row['path']=$path;
278 $row['uid']=$specUID;
279 $row['title']=$title;
280
281 if ($depth>1 && $this->expandNext($specUID)) {
282 $nextCount=$this->getFolderTree($path, $depth-1, $this->makeHTML?$depthData.'<img src="'.$this->backPath.'t3lib/gfx/ol/'.$LN.'.gif" width="18" height="16" align="top" alt="" \>':'');
283 $exp=1;
284 } else {
285 $nextCount=$this->getCount($path);
286 $exp=0;
287 }
288
289 // Set HTML-icons, if any:
290 if ($this->makeHTML) {
291 $HTML=$depthData.$this->PMicon($row,$a,$c,$nextCount,$exp);
292
293 $icon = 'gfx/i/_icon_'.$webpath.'folders.gif';
294 if ($val=='_temp_') {
295 $icon = 'gfx/i/sysf.gif';
296 $row['title']='TEMP';
297 $row['_title']='<b>TEMP</b>';
298 }
299 if ($val=='_recycler_') {
300 $icon = 'gfx/i/recycler.gif';
301 $row['title']='RECYCLER';
302 $row['_title']='<b>RECYCLER</b>';
303 }
304 $HTML.=$this->wrapIcon('<img src="'.$this->backPath.$icon.'" width="18" height="16" align="top" alt="" \>',$row);
305 }
306 $this->tree[$treeKey] = Array('row'=>$row, 'HTML'=>$HTML);
307 }
308 }
309 return $c;
310 }
311
312 /**
313 * [Describe function...]
314 *
315 * @param [type] $files_path: ...
316 * @return [type] ...
317 */
318 function getCount($files_path) {
319 // This generates the directory tree
320 $dirs = t3lib_div::get_dirs($files_path);
321 $c=0;
322 if (is_array($dirs)) {
323 $c=count($dirs);
324 }
325 return $c;
326 }
327 }
328
329 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_foldertree.php']) {
330 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_foldertree.php']);
331 }
332 ?>