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