Updated copyright notices to show "2004"
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_foldertree.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2004 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 November/2003 by Kasper Skaarhoj
32 *
33 * @author Kasper Skaarhoj <kasper@typo3.com>
34 * @coauthor René Fritz <r.fritz@colorcube.de>
35 */
36 /**
37 * [CLASS/FUNCTION INDEX of SCRIPT]
38 *
39 *
40 *
41 * 82: class t3lib_folderTree extends t3lib_treeView
42 * 89: function t3lib_folderTree()
43 * 108: function wrapTitle($title,$row)
44 * 121: function wrapIcon($icon,$row)
45 * 141: function getId($v)
46 * 151: function getJumpToParm($v)
47 * 163: function getTitleStr($row,$titleLen=30)
48 * 173: function getBrowsableTree()
49 * 236: function getFolderTree($files_path, $depth=999, $depthData='')
50 * 315: function getCount($files_path)
51 * 331: function initializePositionSaving()
52 *
53 * TOTAL FUNCTIONS: 10
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_treeView 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 * Constructor function of the class
86 *
87 * @return void
88 */
89 function t3lib_folderTree() {
90 parent::init();
91
92 $this->MOUNTS = $GLOBALS['FILEMOUNTS'];
93
94 $this->treeName='folder';
95 $this->titleAttrib=''; //don't apply any title
96 $this->domIdPrefix = 'folder';
97 }
98
99 /**
100 * Wrapping the folder icon
101 *
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.
105 * @access private
106 */
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).'"' : ''));
110
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->getJumpToParm($row).'\',this,\''.$this->domIdPrefix.$this->getId($row).'_'.$this->bank.'\');';
116 $theFolderIcon='<a href="#" onclick="'.htmlspecialchars($aOnClick).'">'.$theFolderIcon.'</a>';
117 }
118 return $theFolderIcon;
119 }
120
121 /**
122 * Returns the id from the record - for folders, this is an md5 hash.
123 *
124 * @param array Record array
125 * @return integer The "uid" field value.
126 */
127 function getId($v) {
128 return t3lib_div::md5Int($v['path']);
129 }
130
131 /**
132 * Returns jump-url parameter value.
133 *
134 * @param array The record array.
135 * @return string The jump-url parameter.
136 */
137 function getJumpToParm($v) {
138 return "'".rawurlencode($v['path'])."'";
139 }
140
141 /**
142 * Returns the title for the input record. If blank, a "no title" labele (localized) will be returned.
143 * '_title' is used for setting an alternative title for folders.
144 *
145 * @param array The input row array (where the key "_title" is used for the title)
146 * @param integer Title length (30)
147 * @return string The title.
148 */
149 function getTitleStr($row,$titleLen=30) {
150 return $row['_title'] ? $row['_title'] : parent::getTitleStr($row,$titleLen);
151 }
152
153 /**
154 * Will create and return the HTML code for a browsable tree of folders.
155 * Is based on the mounts found in the internal array ->MOUNTS (set in the constructor)
156 *
157 * @return string HTML code for the browsable tree
158 */
159 function getBrowsableTree() {
160
161 // Get stored tree structure AND updating it if needed according to incoming PM GET var.
162 $this->initializePositionSaving();
163
164 // Init done:
165 $titleLen=intval($this->BE_USER->uc['titleLen']);
166 $treeArr=array();
167
168 // Traverse mounts:
169 foreach($this->MOUNTS as $key => $val) {
170 $md5_uid = md5($val['path']);
171 $specUID=hexdec(substr($md5_uid,0,6));
172 $this->specUIDmap[$specUID]=$val['path'];
173
174 // Set first:
175 $this->bank=$val['nkey'];
176 $isOpen = $this->stored[$val['nkey']][$specUID] || $this->expandFirst;
177 $this->reset();
178
179 // Set PM icon:
180 $cmd=$this->bank.'_'.($isOpen?'0_':'1_').$specUID.'_'.$this->treeName;
181 $icon='<img'.t3lib_iconWorks::skinImg($this->backPath,'gfx/ol/'.($isOpen?'minus':'plus').'only.gif','width="18" height="16"').' alt="" />';
182 $firstHtml= $this->PM_ATagWrap($icon,$cmd);
183
184 switch($val['type']) {
185 case 'user': $icon = 'gfx/i/_icon_ftp_user.gif'; break;
186 case 'group': $icon = 'gfx/i/_icon_ftp_group.gif'; break;
187 default: $icon = 'gfx/i/_icon_ftp.gif'; break;
188 }
189
190 // Preparing rootRec for the mount
191 $firstHtml.=$this->wrapIcon('<img'.t3lib_iconWorks::skinImg($this->backPath,$icon,'width="18" height="16"').' alt="" />',$val);
192 $row=array();
193 $row['path']=$val['path'];
194 $row['uid']=$specUID;
195 $row['title']=$val['name'];
196
197 // Add the root of the mount to ->tree
198 $this->tree[]=array('HTML'=>$firstHtml,'row'=>$row,'bank'=>$this->bank);
199
200 // If the mount is expanded, go down:
201 if ($isOpen) {
202 // Set depth:
203 $depthD='<img'.t3lib_iconWorks::skinImg($this->backPath,'gfx/ol/blank.gif','width="18" height="16"').' alt="" />';
204 $this->getFolderTree($val['path'],999,$depthD);
205 }
206
207 // Add tree:
208 $treeArr=array_merge($treeArr,$this->tree);
209 }
210 return $this->printTree($treeArr);
211 }
212
213 /**
214 * Fetches the data for the tree
215 *
216 * @param string Abs file path
217 * @param integer Max depth (recursivity limit)
218 * @param string HTML-code prefix for recursive calls.
219 * @return integer The count of items on the level
220 * @see getBrowsableTree()
221 */
222 function getFolderTree($files_path, $depth=999, $depthData='') {
223
224 // This generates the directory tree
225 $dirs = t3lib_div::get_dirs($files_path);
226
227 $c=0;
228 if (is_array($dirs)) {
229 $depth=intval($depth);
230 $HTML='';
231 $a=0;
232 $c=count($dirs);
233 sort($dirs);
234
235 foreach($dirs as $key => $val) {
236 $a++;
237 $this->tree[]=array(); // Reserve space.
238 end($this->tree);
239 $treeKey = key($this->tree); // Get the key for this space
240 $LN = ($a==$c)?'blank':'line';
241
242 $val = ereg_replace('^\./','',$val);
243 $title = $val;
244 $path = $files_path.$val.'/';
245 $webpath=t3lib_BEfunc::getPathType_web_nonweb($path);
246
247 $md5_uid = md5($path);
248 $specUID=hexdec(substr($md5_uid,0,6));
249 $this->specUIDmap[$specUID]=$path;
250 $row=array();
251 $row['path']=$path;
252 $row['uid']=$specUID;
253 $row['title']=$title;
254
255 if ($depth>1 && $this->expandNext($specUID)) {
256 $nextCount=$this->getFolderTree(
257 $path,
258 $depth-1,
259 $this->makeHTML ? $depthData.'<img'.t3lib_iconWorks::skinImg($this->backPath,'gfx/ol/'.$LN.'.gif','width="18" height="16"').' alt="" />' : ''
260 );
261 $exp=1; // Set "did expand" flag
262 } else {
263 $nextCount=$this->getCount($path);
264 $exp=0; // Clear "did expand" flag
265 }
266
267 // Set HTML-icons, if any:
268 if ($this->makeHTML) {
269 $HTML=$depthData.$this->PMicon($row,$a,$c,$nextCount,$exp);
270
271 $icon = 'gfx/i/_icon_'.$webpath.'folders.gif';
272 if ($val=='_temp_') {
273 $icon = 'gfx/i/sysf.gif';
274 $row['title']='TEMP';
275 $row['_title']='<b>TEMP</b>';
276 }
277 if ($val=='_recycler_') {
278 $icon = 'gfx/i/recycler.gif';
279 $row['title']='RECYCLER';
280 $row['_title']='<b>RECYCLER</b>';
281 }
282 $HTML.=$this->wrapIcon('<img'.t3lib_iconWorks::skinImg($this->backPath,$icon,'width="18" height="16"').' alt="" />',$row);
283 }
284
285 // Finally, add the row/HTML content to the ->tree array in the reserved key.
286 $this->tree[$treeKey] = Array(
287 'row'=>$row,
288 'HTML'=>$HTML,
289 'bank'=>$this->bank
290 );
291 }
292 }
293 return $c;
294 }
295
296 /**
297 * Counts the number of directories in a file path.
298 *
299 * @param string File path.
300 * @return integer
301 */
302 function getCount($files_path) {
303 // This generates the directory tree
304 $dirs = t3lib_div::get_dirs($files_path);
305 $c=0;
306 if (is_array($dirs)) {
307 $c=count($dirs);
308 }
309 return $c;
310 }
311
312 /**
313 * Get stored tree structure AND updating it if needed according to incoming PM GET var.
314 *
315 * @return void
316 * @access private
317 */
318 function initializePositionSaving() {
319 // Get stored tree structure:
320 $this->stored=unserialize($this->BE_USER->uc['browseTrees'][$this->treeName]);
321
322 // Mapping md5-hash to shorter number:
323 $hashMap=array();
324 foreach($this->MOUNTS as $key => $val) {
325 $nkey = hexdec(substr($key,0,4));
326 $hashMap[$nkey]=$key;
327 $this->MOUNTS[$key]['nkey']=$nkey;
328 }
329
330 // PM action:
331 // (If an plus/minus icon has been clicked, the PM GET var is sent and we must update the stored positions in the tree):
332 $PM = explode('_',t3lib_div::GPvar('PM')); // 0: mount key, 1: set/clear boolean, 2: item ID (cannot contain "_"), 3: treeName
333 if (count($PM)==4 && $PM[3]==$this->treeName) {
334 if (isset($this->MOUNTS[$hashMap[$PM[0]]])) {
335 if ($PM[1]) { // set
336 $this->stored[$PM[0]][$PM[2]]=1;
337 $this->savePosition($this->treeName);
338 } else { // clear
339 unset($this->stored[$PM[0]][$PM[2]]);
340 $this->savePosition($this->treeName);
341 }
342 }
343 }
344 }
345 }
346
347 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_foldertree.php']) {
348 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_foldertree.php']);
349 }
350 ?>