[BUGFIX] Regression: filelistFolderTree constructor is recursive
[Packages/TYPO3.CMS.git] / typo3 / class.filelistfoldertree.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2011 Kasper Skårhøj (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 * Folder navigation tree for the File main module
29 *
30 * @author Benjamin Mack <bmack@xnos.org>
31 *
32 *
33 * [CLASS/FUNCTION INDEX of SCRIPT]
34 *
35 *
36 *
37 * 71: class fileListTree extends t3lib_browseTree
38 * 81: function webPageTree()
39 * 92: function wrapIcon($icon,&$row)
40 * 130: function wrapStop($str,$row)
41 * 146: function wrapTitle($title,$row,$bank=0)
42 * 165: function printTree($treeArr = '')
43 * 271: function PMicon($row,$a,$c,$nextCount,$exp)
44 * 292: function PMiconATagWrap($icon, $cmd, $isExpand = TRUE)
45 * 309: function getBrowsableTree()
46 * 377: function getTree($uid, $depth=999, $depthData='',$blankLineCode='',$subCSSclass='')
47 *
48 *
49 * TOTAL FUNCTIONS: 9
50 * (This index is automatically created/updated by the extension "extdeveval")
51 *
52 */
53 /**
54 * Extension class for the t3lib_filetree class, needed for drag and drop and ajax functionality
55 *
56 * @author Sebastian Kurfürst <sebastian@garbage-group.de>
57 * @author Benjamin Mack <bmack@xnos.org>
58 * @package TYPO3
59 * @subpackage core
60 * @see class t3lib_browseTree
61 */
62 class filelistFolderTree extends t3lib_folderTree {
63
64 var $ext_IconMode;
65 var $ajaxStatus = FALSE; // Indicates, whether the ajax call was successful, i.e. the requested page has been found
66
67 /**
68 * Init parent class
69 */
70 public function __construct() {
71 parent::__construct();
72 }
73
74 /**
75 * Compatibility constructor.
76 *
77 * @deprecated since TYPO3 4.6 and will be removed in TYPO3 4.8. Use __construct() instead.
78 */
79 public function filelistFolderTree() {
80 t3lib_div::logDeprecatedFunction();
81 // Note: we cannot call $this->__construct() here because it would call the derived class constructor and cause recursion
82 // This code uses official PHP behavior (http://www.php.net/manual/en/language.oop5.basic.php) when $this in the
83 // statically called non-static method inherits $this from the caller's scope.
84 filelistFolderTree::__construct();
85 }
86
87 /**
88 * Wrapping icon in browse tree
89 *
90 * @param string Icon IMG code
91 * @param array Data row for element.
92 * @return string Page icon
93 */
94 function wrapIcon($theFolderIcon, &$row) {
95
96 // Wrap icon in click-menu link.
97 if (!$this->ext_IconMode) {
98 $theFolderIcon = $GLOBALS['TBE_TEMPLATE']->wrapClickMenuOnIcon($theFolderIcon,$row['path'],'',0);
99 } elseif (!strcmp($this->ext_IconMode,'titlelink')) {
100 $aOnClick = 'return jumpTo(\''.$this->getJumpToParam($row).'\',this,\''.$this->domIdPrefix.$this->getId($row).'\','.$this->bank.');';
101 $theFolderIcon='<a href="#" onclick="'.htmlspecialchars($aOnClick).'">'.$theFolderIcon.'</a>';
102 }
103 // Wrap icon in a drag/drop span.
104 return '<span class="dragIcon" id="dragIconID_'.$this->getJumpToParam($row).'">'.$theFolderIcon.'</span>';
105 }
106
107
108 /**
109 * Wrapping $title in a-tags.
110 *
111 * @param string Title string
112 * @param string Item record
113 * @param integer Bank pointer (which mount point number)
114 * @return string
115 * @access private
116 */
117 function wrapTitle($title,$row,$bank=0) {
118 $aOnClick = 'return jumpTo(\''.$this->getJumpToParam($row).'\',this,\''.$this->domIdPrefix.$this->getId($row).'\','.$bank.');';
119 $CSM = '';
120 if ($GLOBALS['TYPO3_CONF_VARS']['BE']['useOnContextMenuHandler']) {
121 $CSM = ' oncontextmenu="'.htmlspecialchars($GLOBALS['TBE_TEMPLATE']->wrapClickMenuOnIcon('',$row['path'],'',0,'&bank='.$this->bank,'',TRUE)).'"';
122 }
123 $theFolderTitle='<a href="#" onclick="'.htmlspecialchars($aOnClick).'"'.$CSM.'>'.$title.'</a>';
124
125 // Wrap title in a drag/drop span.
126 return '<span class="dragTitle" id="dragTitleID_'.$this->getJumpToParam($row).'">'.$theFolderTitle.'</span>';
127 }
128
129
130
131
132 /**
133 * Compiles the HTML code for displaying the structure found inside the ->tree array
134 *
135 * @param array "tree-array" - if blank string, the internal ->tree array is used.
136 * @return string The HTML code for the tree
137 */
138 function printTree($treeArr='') {
139 $titleLen = intval($this->BE_USER->uc['titleLen']);
140 if (!is_array($treeArr)) $treeArr = $this->tree;
141
142 $out = '
143 <!-- TYPO3 folder tree structure. -->
144 <ul class="tree" id="treeRoot">
145 ';
146 $titleLen=intval($this->BE_USER->uc['titleLen']);
147 if (!is_array($treeArr)) $treeArr=$this->tree;
148
149 // -- evaluate AJAX request
150 // IE takes anchor as parameter
151 $PM = t3lib_div::_GP('PM');
152 if(($PMpos = strpos($PM, '#')) !== FALSE) { $PM = substr($PM, 0, $PMpos); }
153 $PM = explode('_', $PM);
154 if((TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_AJAX) && is_array($PM) && count($PM)==4) {
155 if($PM[1]) {
156 $expandedFolderUid = $PM[2];
157 $ajaxOutput = '';
158 $invertedDepthOfAjaxRequestedItem = 0; // We don't know yet. Will be set later.
159 $doExpand = TRUE;
160 } else {
161 $expandedFolderUid = $PM[2];
162 $doCollapse = TRUE;
163 }
164 }
165
166
167 // we need to count the opened <ul>'s every time we dig into another level,
168 // so we know how many we have to close when all children are done rendering
169 $closeDepth = array();
170
171 foreach($treeArr as $k => $v) {
172 $classAttr = $v['row']['_CSSCLASS'];
173 $uid = $v['row']['uid'];
174 $idAttr = htmlspecialchars($this->domIdPrefix.$this->getId($v['row']).'_'.$v['bank']);
175 $itemHTML = '';
176
177 // if this item is the start of a new level,
178 // then a new level <ul> is needed, but not in ajax mode
179 if($v['isFirst'] && !($doCollapse) && !($doExpand && $expandedFolderUid == $uid)) {
180 $itemHTML = "<ul>\n";
181 }
182
183 // add CSS classes to the list item
184 if($v['hasSub']) { $classAttr = ($classAttr) ? ' expanded': 'expanded'; }
185 if($v['isLast']) { $classAttr = ($classAttr) ? ' last' : 'last'; }
186
187 $itemHTML .='
188 <li id="'.$idAttr.'"'.($classAttr ? ' class="'.$classAttr.'"' : '').'><div class="treeLinkItem">'.
189 $v['HTML'].
190 $this->wrapTitle($this->getTitleStr($v['row'],$titleLen),$v['row'],$v['bank']) . '</div>';
191
192
193 if(!$v['hasSub']) { $itemHTML .= "</li>\n"; }
194
195 // we have to remember if this is the last one
196 // on level X so the last child on level X+1 closes the <ul>-tag
197 if($v['isLast'] && !($doExpand && $expandedFolderUid == $uid)) { $closeDepth[$v['invertedDepth']] = 1; }
198
199
200 // if this is the last one and does not have subitems, we need to close
201 // the tree as long as the upper levels have last items too
202 if($v['isLast'] && !$v['hasSub'] && !$doCollapse && !($doExpand && $expandedFolderUid == $uid)) {
203 for ($i = $v['invertedDepth']; $closeDepth[$i] == 1; $i++) {
204 $closeDepth[$i] = 0;
205 $itemHTML .= "</ul></li>\n";
206 }
207 }
208
209 // ajax request: collapse
210 if($doCollapse && $expandedFolderUid == $uid) {
211 $this->ajaxStatus = TRUE;
212 return $itemHTML;
213 }
214
215 // ajax request: expand
216 if($doExpand && $expandedFolderUid == $uid) {
217 $ajaxOutput .= $itemHTML;
218 $invertedDepthOfAjaxRequestedItem = $v['invertedDepth'];
219 } elseif($invertedDepthOfAjaxRequestedItem) {
220 if($v['invertedDepth'] < $invertedDepthOfAjaxRequestedItem) {
221 $ajaxOutput .= $itemHTML;
222 } else {
223 $this->ajaxStatus = TRUE;
224 return $ajaxOutput;
225 }
226 }
227
228 $out .= $itemHTML;
229 }
230
231 if($ajaxOutput) {
232 $this->ajaxStatus = TRUE;
233 return $ajaxOutput;
234 }
235
236 // finally close the first ul
237 $out .= "</ul>\n";
238 return $out;
239 }
240
241
242 /**
243 * Generate the plus/minus icon for the browsable tree.
244 *
245 * @param array record for the entry
246 * @param integer The current entry number
247 * @param integer The total number of entries. If equal to $a, a "bottom" element is returned.
248 * @param integer The number of sub-elements to the current element.
249 * @param boolean The element was expanded to render subelements if this flag is set.
250 * @return string Image tag with the plus/minus icon.
251 * @access private
252 * @see t3lib_pageTree::PMicon()
253 */
254 function PMicon($row,$a,$c,$nextCount,$exp) {
255 $PM = $nextCount ? ($exp ? 'minus' : 'plus') : 'join';
256 $BTM = ($a == $c) ? 'bottom' : '';
257 $icon = '<img'.t3lib_iconWorks::skinImg($this->backPath,'gfx/ol/'.$PM.$BTM.'.gif','width="18" height="16"').' alt="" />';
258
259 if ($nextCount) {
260 $cmd = $this->bank.'_'.($exp?'0_':'1_').$row['uid'].'_'.$this->treeName;
261 $icon = $this->PMiconATagWrap($icon,$cmd,!$exp);
262 }
263 return $icon;
264 }
265
266
267 /**
268 * Wrap the plus/minus icon in a link
269 *
270 * @param string HTML string to wrap, probably an image tag.
271 * @param string Command for 'PM' get var
272 * @return string Link-wrapped input string
273 * @access private
274 */
275 function PMiconATagWrap($icon, $cmd, $isExpand = TRUE) {
276 if ($this->thisScript) {
277 // activate dynamic ajax-based tree
278 $js = htmlspecialchars('Tree.load(\''.$cmd.'\', '.intval($isExpand).', this);');
279 return '<a class="pm" onclick="'.$js.'">'.$icon.'</a>';
280 } else {
281 return $icon;
282 }
283 }
284
285
286
287 /**
288 * Will create and return the HTML code for a browsable tree of folders.
289 * Is based on the mounts found in the internal array ->MOUNTS (set in the constructor)
290 *
291 * @return string HTML code for the browsable tree
292 */
293 function getBrowsableTree() {
294
295 // Get stored tree structure AND updating it if needed according to incoming PM GET var.
296 $this->initializePositionSaving();
297
298 // Init done:
299 $titleLen = intval($this->BE_USER->uc['titleLen']);
300 $treeArr = array();
301
302 // Traverse mounts:
303 foreach($this->MOUNTS as $key => $val) {
304 $hasSub = FALSE;
305 $specUID = t3lib_div::md5int($val['path']);
306 $this->specUIDmap[$specUID] = $val['path'];
307
308 // Set first:
309 $this->bank = $val['nkey'];
310 $isOpen = $this->stored[$val['nkey']][$specUID] || $this->expandFirst;
311 $this->reset();
312
313 // Set PM icon:
314 $cmd = $this->bank.'_'.($isOpen ? '0_' : '1_').$specUID.'_'.$this->treeName;
315 $icon='<img'.t3lib_iconWorks::skinImg($this->backPath,'gfx/ol/'.($isOpen? 'minus':'plus').'only.gif').' alt="" />';
316 $firstHtml= $this->PM_ATagWrap($icon,$cmd);
317
318 switch ($val['type']) {
319 case 'user':
320 $icon = 'apps-filetree-folder-user';
321 break;
322 case 'group':
323 $icon = 'apps-filetree-folder-user';
324 break;
325 case 'readonly':
326 $icon = 'apps-filetree-folder-locked';
327 break;
328 default:
329 $icon = 'apps-filetree-mount';
330 break;
331 }
332
333 // Preparing rootRec for the mount
334 $firstHtml.=$this->wrapIcon(t3lib_iconWorks::getSpriteIcon($icon),$val);
335 $row=array();
336 $row['uid'] = $specUID;
337 $row['path'] = $val['path'];
338 $row['title'] = $val['name'];
339
340 // hasSub is TRUE when the root of the mount is expanded
341 if ($isOpen) {
342 $hasSub = TRUE;
343 }
344 // Add the root of the mount to ->tree
345 $this->tree[] = array('HTML' => $firstHtml, 'row' => $row, 'bank' => $this->bank, 'hasSub' => $hasSub);
346
347 // If the mount is expanded, go down:
348 if ($isOpen)
349 $this->getFolderTree($val['path'], 999, $val['type']);
350
351 // Add tree:
352 $treeArr = array_merge($treeArr, $this->tree);
353
354 // if this is an AJAX call, don't run through all mounts, only
355 // show the expansion of the current one, not the rest of the mounts
356 if (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_AJAX) {
357 break;
358 }
359 }
360 return $this->printTree($treeArr);
361 }
362
363
364
365 /**
366 * Fetches the data for the tree
367 *
368 * @param string Abs file path
369 * @param integer Max depth (recursivity limit)
370 * @return integer The count of items on the level
371 * @see getBrowsableTree()
372 */
373 function getFolderTree($files_path, $depth=999, $type='') {
374
375 // This generates the directory tree
376 $dirs = t3lib_div::get_dirs($files_path);
377 if (!is_array($dirs)) return 0;
378
379 sort($dirs);
380 $c = count($dirs);
381
382 $depth = intval($depth);
383 $HTML = '';
384 $a = 0;
385
386 foreach($dirs as $key => $val) {
387 $a++;
388 $this->tree[] = array(); // Reserve space.
389 end($this->tree);
390 $treeKey = key($this->tree); // Get the key for this space
391
392 $val = preg_replace('/^\.\//','',$val);
393 $title = $val;
394 $path = $files_path.$val.'/';
395
396 $specUID = t3lib_div::md5int($path);
397 $this->specUIDmap[$specUID] = $path;
398
399 $row = array();
400 $row['path'] = $path;
401 $row['uid'] = $specUID;
402 $row['title'] = $title;
403
404 // Make a recursive call to the next level
405 if ($depth > 1 && $this->expandNext($specUID)) {
406 $nextCount = $this->getFolderTree(
407 $path,
408 $depth-1,
409 $this->makeHTML ? '<img'.t3lib_iconWorks::skinImg($this->backPath,'gfx/ol/'.($a == $c ? 'blank' : 'line').'.gif','width="18" height="16"').' alt="" />' : '',
410 $type
411 );
412 $exp = 1; // Set "did expand" flag
413 } else {
414 $nextCount = $this->getCount($path);
415 $exp = 0; // Clear "did expand" flag
416 }
417
418 // Set HTML-icons, if any:
419 if ($this->makeHTML) {
420 $HTML = $this->PMicon($row,$a,$c,$nextCount,$exp);
421
422 $webpath = t3lib_BEfunc::getPathType_web_nonweb($path);
423
424 if (is_writable($path)) {
425 $type = '';
426 $overlays = array();
427 } else {
428 $type = 'readonly';
429 $overlays= array('status-overlay-locked'=>array());
430
431 }
432
433 if ($exp) {
434 $icon = 'apps-filetree-folder-opened';
435 } else {
436 $icon = 'apps-filetree-folder-default';
437 }
438 if ($val == '_temp_') {
439 $icon = 'apps-filetree-folder-temp';
440 $row['title'] = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_mod_file_list.xml:temp', TRUE);
441 $row['_title'] = '<strong>' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_mod_file_list.xml:temp', TRUE) . '</strong>';
442 }
443 if ($val == '_recycler_') {
444 $icon = 'apps-filetree-folder-recycler';
445 $row['title'] = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_mod_file_list.xml:recycler', TRUE);
446 $row['_title'] = '<strong>' .$GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_mod_file_list.xml:recycler', TRUE) . '</strong>';
447 }
448 $HTML .= $this->wrapIcon(t3lib_iconWorks::getSpriteIcon($icon,array('title'=>$row['title']),$overlays),$row);
449 }
450
451 // Finally, add the row/HTML content to the ->tree array in the reserved key.
452 $this->tree[$treeKey] = Array(
453 'row' => $row,
454 'HTML' => $HTML,
455 'hasSub' => $nextCount && $this->expandNext($specUID),
456 'isFirst'=> ($a == 1),
457 'isLast' => FALSE,
458 'invertedDepth'=> $depth,
459 'bank' => $this->bank
460 );
461 }
462
463 if($a) { $this->tree[$treeKey]['isLast'] = TRUE; }
464 return $c;
465 }
466 }
467
468 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['typo3/class.filelistfoldertree.php'])) {
469 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['typo3/class.filelistfoldertree.php']);
470 }
471
472 ?>