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