Fixed bug #9320: Adding a new mount-type "readonly" for temporary filemounts (thanks...
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_foldertree.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2008 Kasper Skaarhoj (kasperYYYY@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 <kasperYYYY@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 * 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()
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 <kasperYYYY@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->getJumpToParam($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 * Wrapping $title in a-tags.
123 *
124 * @param string Title string
125 * @param string Item record
126 * @param integer Bank pointer (which mount point number)
127 * @return string
128 * @access private
129 */
130 function wrapTitle($title,$row,$bank=0) {
131 $aOnClick = 'return jumpTo(\''.$this->getJumpToParam($row).'\',this,\''.$this->domIdPrefix.$this->getId($row).'\','.$bank.');';
132 $CSM = '';
133 if ($GLOBALS['TYPO3_CONF_VARS']['BE']['useOnContextMenuHandler']) {
134 $CSM = ' oncontextmenu="'.htmlspecialchars($GLOBALS['TBE_TEMPLATE']->wrapClickMenuOnIcon('',$row['path'],'',0,'','',TRUE)).'"';
135 }
136 return '<a href="#" title="'.htmlspecialchars($row['title']).'" onclick="'.htmlspecialchars($aOnClick).'"'.$CSM.'>'.$title.'</a>';
137 }
138
139 /**
140 * Returns the id from the record - for folders, this is an md5 hash.
141 *
142 * @param array Record array
143 * @return integer The "uid" field value.
144 */
145 function getId($v) {
146 return t3lib_div::md5Int($v['path']);
147 }
148
149 /**
150 * Returns jump-url parameter value.
151 *
152 * @param array The record array.
153 * @return string The jump-url parameter.
154 */
155 function getJumpToParam($v) {
156 return rawurlencode($v['path']);
157 }
158
159 /**
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.
162 *
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.
166 */
167 function getTitleStr($row,$titleLen=30) {
168 return $row['_title'] ? $row['_title'] : parent::getTitleStr($row,$titleLen);
169 }
170
171 /**
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)
174 *
175 * @return string HTML code for the browsable tree
176 */
177 function getBrowsableTree() {
178
179 // Get stored tree structure AND updating it if needed according to incoming PM GET var.
180 $this->initializePositionSaving();
181
182 // Init done:
183 $titleLen=intval($this->BE_USER->uc['titleLen']);
184 $treeArr=array();
185
186 // Traverse mounts:
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'];
191
192 // Set first:
193 $this->bank=$val['nkey'];
194 $isOpen = $this->stored[$val['nkey']][$specUID] || $this->expandFirst;
195 $this->reset();
196
197 // Set PM icon:
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);
201
202 switch ($val['type']) {
203 case 'user':
204 $icon = 'gfx/i/_icon_ftp_user.gif';
205 break;
206 case 'group':
207 $icon = 'gfx/i/_icon_ftp_group.gif';
208 break;
209 case 'readonly':
210 $icon = 'gfx/i/_icon_ftp_readonly.gif';
211 break;
212 default:
213 $icon = 'gfx/i/_icon_ftp.gif';
214 break;
215 }
216
217 // Preparing rootRec for the mount
218 $firstHtml.=$this->wrapIcon('<img'.t3lib_iconWorks::skinImg($this->backPath,$icon,'width="18" height="16"').' alt="" />',$val);
219 $row=array();
220 $row['path']=$val['path'];
221 $row['uid']=$specUID;
222 $row['title']=$val['name'];
223
224 // Add the root of the mount to ->tree
225 $this->tree[]=array('HTML'=>$firstHtml,'row'=>$row,'bank'=>$this->bank);
226
227 // If the mount is expanded, go down:
228 if ($isOpen) {
229 // Set depth:
230 $depthD='<img'.t3lib_iconWorks::skinImg($this->backPath,'gfx/ol/blank.gif','width="18" height="16"').' alt="" />';
231 $this->getFolderTree($val['path'], 999, $depthD, $val['type']);
232 }
233
234 // Add tree:
235 $treeArr=array_merge($treeArr,$this->tree);
236 }
237 return $this->printTree($treeArr);
238 }
239
240 /**
241 * Fetches the data for the tree
242 *
243 * @param string Abs file path
244 * @param integer Max depth (recursivity limit)
245 * @param string HTML-code prefix for recursive calls.
246 * @return integer The count of items on the level
247 * @see getBrowsableTree()
248 */
249 function getFolderTree($files_path, $depth=999, $depthData='', $type='') {
250
251 // This generates the directory tree
252 $dirs = t3lib_div::get_dirs($files_path);
253
254 $c=0;
255 if (is_array($dirs)) {
256 $depth=intval($depth);
257 $HTML='';
258 $a=0;
259 $c=count($dirs);
260 sort($dirs);
261
262 foreach($dirs as $key => $val) {
263 $a++;
264 $this->tree[]=array(); // Reserve space.
265 end($this->tree);
266 $treeKey = key($this->tree); // Get the key for this space
267 $LN = ($a==$c)?'blank':'line';
268
269 $val = ereg_replace('^\./','',$val);
270 $title = $val;
271 $path = $files_path.$val.'/';
272 $webpath=t3lib_BEfunc::getPathType_web_nonweb($path);
273
274 $md5_uid = md5($path);
275 $specUID=hexdec(substr($md5_uid,0,6));
276 $this->specUIDmap[$specUID]=$path;
277 $row=array();
278 $row['path']=$path;
279 $row['uid']=$specUID;
280 $row['title']=$title;
281
282 if ($depth>1 && $this->expandNext($specUID)) {
283 $nextCount=$this->getFolderTree(
284 $path,
285 $depth-1,
286 $this->makeHTML ? $depthData.'<img'.t3lib_iconWorks::skinImg($this->backPath,'gfx/ol/'.$LN.'.gif','width="18" height="16"').' alt="" />' : '',
287 $type
288 );
289 $exp=1; // Set "did expand" flag
290 } else {
291 $nextCount=$this->getCount($path);
292 $exp=0; // Clear "did expand" flag
293 }
294
295 // Set HTML-icons, if any:
296 if ($this->makeHTML) {
297 $HTML=$depthData.$this->PMicon($row,$a,$c,$nextCount,$exp);
298
299 $icon = 'gfx/i/_icon_' .$webpath . 'folders' . ($type == 'readonly' ? '_ro' : '') . '.gif';
300 if ($val=='_temp_') {
301 $icon = 'gfx/i/sysf.gif';
302 $row['title']='TEMP';
303 $row['_title']='<b>TEMP</b>';
304 }
305 if ($val=='_recycler_') {
306 $icon = 'gfx/i/recycler.gif';
307 $row['title']='RECYCLER';
308 $row['_title']='<b>RECYCLER</b>';
309 }
310 $HTML.=$this->wrapIcon('<img'.t3lib_iconWorks::skinImg($this->backPath,$icon,'width="18" height="16"').' alt="" />',$row);
311 }
312
313 // Finally, add the row/HTML content to the ->tree array in the reserved key.
314 $this->tree[$treeKey] = Array(
315 'row'=>$row,
316 'HTML'=>$HTML,
317 'bank'=>$this->bank
318 );
319 }
320 }
321 return $c;
322 }
323
324 /**
325 * Counts the number of directories in a file path.
326 *
327 * @param string File path.
328 * @return integer
329 */
330 function getCount($files_path) {
331 // This generates the directory tree
332 $dirs = t3lib_div::get_dirs($files_path);
333 $c=0;
334 if (is_array($dirs)) {
335 $c=count($dirs);
336 }
337 return $c;
338 }
339
340 /**
341 * Get stored tree structure AND updating it if needed according to incoming PM GET var.
342 *
343 * @return void
344 * @access private
345 */
346 function initializePositionSaving() {
347 // Get stored tree structure:
348 $this->stored=unserialize($this->BE_USER->uc['browseTrees'][$this->treeName]);
349
350 // Mapping md5-hash to shorter number:
351 $hashMap=array();
352 foreach($this->MOUNTS as $key => $val) {
353 $nkey = hexdec(substr($key,0,4));
354 $hashMap[$nkey]=$key;
355 $this->MOUNTS[$key]['nkey']=$nkey;
356 }
357
358 // PM action:
359 // (If an plus/minus icon has been clicked, the PM GET var is sent and we must update the stored positions in the tree):
360 $PM = explode('_',t3lib_div::_GP('PM')); // 0: mount key, 1: set/clear boolean, 2: item ID (cannot contain "_"), 3: treeName
361 if (count($PM)==4 && $PM[3]==$this->treeName) {
362 if (isset($this->MOUNTS[$hashMap[$PM[0]]])) {
363 if ($PM[1]) { // set
364 $this->stored[$PM[0]][$PM[2]]=1;
365 $this->savePosition($this->treeName);
366 } else { // clear
367 unset($this->stored[$PM[0]][$PM[2]]);
368 $this->savePosition($this->treeName);
369 }
370 }
371 }
372 }
373 }
374
375 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_foldertree.php']) {
376 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_foldertree.php']);
377 }
378 ?>