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