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