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