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