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