Fixed bug #10581: Use $this->fixedL for list title length
[Packages/TYPO3.CMS.git] / typo3 / class.filelistfoldertree.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2008 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 require_once (PATH_t3lib.'class.t3lib_foldertree.php');
55
56
57
58 /**
59 * Extension class for the t3lib_filetree class, needed for drag and drop and ajax functionality
60 *
61 * @author Sebastian Kurfuerst <sebastian@garbage-group.de>
62 * @author Benjamin Mack <bmack@xnos.org>
63 * @package TYPO3
64 * @subpackage core
65 * @see class t3lib_browseTree
66 */
67 class filelistFolderTree extends t3lib_folderTree {
68
69 var $ext_IconMode;
70 var $ajaxStatus = false; // Indicates, whether the ajax call was successful, i.e. the requested page has been found
71
72 /**
73 * Calls init functions
74 *
75 * @return void
76 */
77 function filelistFolderTree() {
78 parent::t3lib_folderTree();
79 }
80
81 /**
82 * Wrapping icon in browse tree
83 *
84 * @param string Icon IMG code
85 * @param array Data row for element.
86 * @return string Page icon
87 */
88 function wrapIcon($icon,&$row) {
89
90 // Add title attribute to input icon tag
91 $theFolderIcon = $this->addTagAttributes($icon,($this->titleAttrib ? $this->titleAttrib.'="'.$this->getTitleAttrib($row).'"' : ''));
92
93 // Wrap icon in click-menu link.
94 if (!$this->ext_IconMode) {
95 $theFolderIcon = $GLOBALS['TBE_TEMPLATE']->wrapClickMenuOnIcon($theFolderIcon,$row['path'],'',0);
96 } elseif (!strcmp($this->ext_IconMode,'titlelink')) {
97 $aOnClick = 'return jumpTo(\''.$this->getJumpToParam($row).'\',this,\''.$this->domIdPrefix.$this->getId($row).'\','.$this->bank.');';
98 $theFolderIcon='<a href="#" onclick="'.htmlspecialchars($aOnClick).'">'.$theFolderIcon.'</a>';
99 }
100 // Wrap icon in a drag/drop span.
101 return '<span class="dragIcon" id="dragIconID_'.$this->getJumpToParam($row).'">'.$theFolderIcon.'</span>';
102 }
103
104
105 /**
106 * Wrapping $title in a-tags.
107 *
108 * @param string Title string
109 * @param string Item record
110 * @param integer Bank pointer (which mount point number)
111 * @return string
112 * @access private
113 */
114 function wrapTitle($title,$row,$bank=0) {
115 $aOnClick = 'return jumpTo(\''.$this->getJumpToParam($row).'\',this,\''.$this->domIdPrefix.$this->getId($row).'\','.$bank.');';
116 $CSM = '';
117 if ($GLOBALS['TYPO3_CONF_VARS']['BE']['useOnContextMenuHandler']) {
118 $CSM = ' oncontextmenu="'.htmlspecialchars($GLOBALS['TBE_TEMPLATE']->wrapClickMenuOnIcon('',$row['path'],'',0,'&bank='.$this->bank,'',TRUE)).'"';
119 }
120 $theFolderTitle='<a href="#" onclick="'.htmlspecialchars($aOnClick).'"'.$CSM.'>'.$title.'</a>';
121
122 // Wrap title in a drag/drop span.
123 return '<span class="dragTitle" id="dragTitleID_'.$this->getJumpToParam($row).'">'.$theFolderTitle.'</span>';
124 }
125
126
127
128
129 /**
130 * Compiles the HTML code for displaying the structure found inside the ->tree array
131 *
132 * @param array "tree-array" - if blank string, the internal ->tree array is used.
133 * @return string The HTML code for the tree
134 */
135 function printTree($treeArr='') {
136 $titleLen = intval($this->BE_USER->uc['titleLen']);
137 if (!is_array($treeArr)) $treeArr = $this->tree;
138
139 $out = '
140 <!-- TYPO3 folder tree structure. -->
141 <ul class="tree">
142 ';
143 $titleLen=intval($this->BE_USER->uc['titleLen']);
144 if (!is_array($treeArr)) $treeArr=$this->tree;
145
146 // -- evaluate AJAX request
147 // IE takes anchor as parameter
148 $PM = t3lib_div::_GP('PM');
149 if(($PMpos = strpos($PM, '#')) !== false) { $PM = substr($PM, 0, $PMpos); }
150 $PM = explode('_', $PM);
151 if((TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_AJAX) && is_array($PM) && count($PM)==4) {
152 if($PM[1]) {
153 $expandedFolderUid = $PM[2];
154 $ajaxOutput = '';
155 $invertedDepthOfAjaxRequestedItem = 0; // We don't know yet. Will be set later.
156 $doExpand = true;
157 } else {
158 $expandedFolderUid = $PM[2];
159 $doCollapse = true;
160 }
161 }
162
163
164 // we need to count the opened <ul>'s every time we dig into another level,
165 // so we know how many we have to close when all children are done rendering
166 $closeDepth = array();
167
168 foreach($treeArr as $k => $v) {
169 $classAttr = $v['row']['_CSSCLASS'];
170 $uid = $v['row']['uid'];
171 $idAttr = htmlspecialchars($this->domIdPrefix.$this->getId($v['row']).'_'.$v['bank']);
172 $itemHTML = '';
173
174 // if this item is the start of a new level,
175 // then a new level <ul> is needed, but not in ajax mode
176 if($v['isFirst'] && !($doCollapse) && !($doExpand && $expandedFolderUid == $uid)) {
177 $itemHTML = "<ul>\n";
178 }
179
180 // add CSS classes to the list item
181 if($v['hasSub']) { $classAttr = ($classAttr) ? ' expanded': 'expanded'; }
182 if($v['isLast']) { $classAttr = ($classAttr) ? ' last' : 'last'; }
183
184 $itemHTML .='
185 <li id="'.$idAttr.'"'.($classAttr ? ' class="'.$classAttr.'"' : '').'>'.
186 $v['HTML'].
187 $this->wrapTitle($this->getTitleStr($v['row'],$titleLen),$v['row'],$v['bank']);
188
189
190 if(!$v['hasSub']) { $itemHTML .= "</li>\n"; }
191
192 // we have to remember if this is the last one
193 // on level X so the last child on level X+1 closes the <ul>-tag
194 if($v['isLast'] && !($doExpand && $expandedFolderUid == $uid)) { $closeDepth[$v['invertedDepth']] = 1; }
195
196
197 // if this is the last one and does not have subitems, we need to close
198 // the tree as long as the upper levels have last items too
199 if($v['isLast'] && !$v['hasSub'] && !$doCollapse && !($doExpand && $expandedFolderUid == $uid)) {
200 for ($i = $v['invertedDepth']; $closeDepth[$i] == 1; $i++) {
201 $closeDepth[$i] = 0;
202 $itemHTML .= "</ul></li>\n";
203 }
204 }
205
206 // ajax request: collapse
207 if($doCollapse && $expandedFolderUid == $uid) {
208 $this->ajaxStatus = true;
209 return $itemHTML;
210 }
211
212 // ajax request: expand
213 if($doExpand && $expandedFolderUid == $uid) {
214 $ajaxOutput .= $itemHTML;
215 $invertedDepthOfAjaxRequestedItem = $v['invertedDepth'];
216 } elseif($invertedDepthOfAjaxRequestedItem) {
217 if($v['invertedDepth'] < $invertedDepthOfAjaxRequestedItem) {
218 $ajaxOutput .= $itemHTML;
219 } else {
220 $this->ajaxStatus = true;
221 return $ajaxOutput;
222 }
223 }
224
225 $out .= $itemHTML;
226 }
227
228 if($ajaxOutput) {
229 $this->ajaxStatus = true;
230 return $ajaxOutput;
231 }
232
233 // finally close the first ul
234 $out .= "</ul>\n";
235 return $out;
236 }
237
238
239 /**
240 * Generate the plus/minus icon for the browsable tree.
241 *
242 * @param array record for the entry
243 * @param integer The current entry number
244 * @param integer The total number of entries. If equal to $a, a "bottom" element is returned.
245 * @param integer The number of sub-elements to the current element.
246 * @param boolean The element was expanded to render subelements if this flag is set.
247 * @return string Image tag with the plus/minus icon.
248 * @access private
249 * @see t3lib_pageTree::PMicon()
250 */
251 function PMicon($row,$a,$c,$nextCount,$exp) {
252 $PM = $nextCount ? ($exp ? 'minus' : 'plus') : 'join';
253 $BTM = ($a == $c) ? 'bottom' : '';
254 $icon = '<img'.t3lib_iconWorks::skinImg($this->backPath,'gfx/ol/'.$PM.$BTM.'.gif','width="18" height="16"').' alt="" />';
255
256 if ($nextCount) {
257 $cmd = $this->bank.'_'.($exp?'0_':'1_').$row['uid'].'_'.$this->treeName;
258 $icon = $this->PMiconATagWrap($icon,$cmd,!$exp);
259 }
260 return $icon;
261 }
262
263
264 /**
265 * Wrap the plus/minus icon in a link
266 *
267 * @param string HTML string to wrap, probably an image tag.
268 * @param string Command for 'PM' get var
269 * @return string Link-wrapped input string
270 * @access private
271 */
272 function PMiconATagWrap($icon, $cmd, $isExpand = true) {
273 if ($this->thisScript) {
274 // activate dynamic ajax-based tree
275 $js = htmlspecialchars('Tree.load(\''.$cmd.'\', '.intval($isExpand).', this);');
276 return '<a class="pm" onclick="'.$js.'">'.$icon.'</a>';
277 } else {
278 return $icon;
279 }
280 }
281
282
283
284 /**
285 * Will create and return the HTML code for a browsable tree of folders.
286 * Is based on the mounts found in the internal array ->MOUNTS (set in the constructor)
287 *
288 * @return string HTML code for the browsable tree
289 */
290 function getBrowsableTree() {
291
292 // Get stored tree structure AND updating it if needed according to incoming PM GET var.
293 $this->initializePositionSaving();
294
295 // Init done:
296 $titleLen = intval($this->BE_USER->uc['titleLen']);
297 $treeArr = array();
298
299 // Traverse mounts:
300 foreach($this->MOUNTS as $key => $val) {
301 $specUID = t3lib_div::md5int($val['path']);
302 $this->specUIDmap[$specUID] = $val['path'];
303
304 // Set first:
305 $this->bank = $val['nkey'];
306 $isOpen = $this->stored[$val['nkey']][$specUID] || $this->expandFirst;
307 $this->reset();
308
309 // Set PM icon:
310 $cmd = $this->bank.'_'.($isOpen ? '0_' : '1_').$specUID.'_'.$this->treeName;
311 $icon='<img'.t3lib_iconWorks::skinImg($this->backPath,'gfx/ol/'.($isOpen?'minus':'plus').'only.gif').' alt="" />';
312 $firstHtml= $this->PM_ATagWrap($icon,$cmd);
313
314 switch ($val['type']) {
315 case 'user':
316 $icon = 'gfx/i/_icon_ftp_user.gif';
317 break;
318 case 'group':
319 $icon = 'gfx/i/_icon_ftp_group.gif';
320 break;
321 case 'readonly':
322 $icon = 'gfx/i/_icon_ftp_readonly.gif';
323 break;
324 default:
325 $icon = 'gfx/i/_icon_ftp.gif';
326 break;
327 }
328
329 // Preparing rootRec for the mount
330 $firstHtml.=$this->wrapIcon('<img'.t3lib_iconWorks::skinImg($this->backPath,$icon,'width="18" height="16"').' alt="" />',$val);
331 $row=array();
332 $row['uid'] = $specUID;
333 $row['path'] = $val['path'];
334 $row['title'] = $val['name'];
335
336 // Add the root of the mount to ->tree
337 $this->tree[] = array('HTML' => $firstHtml, 'row' => $row, 'bank' => $this->bank);
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 = ereg_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 $icon = 'gfx/i/_icon_' .$webpath . 'folders' . ($type == 'readonly' ? '_ro' : '') . '.gif';
410 if ($val == '_temp_') {
411 $icon = 'gfx/i/sysf.gif';
412 $row['title'] = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_mod_file_list.xml:temp', true);
413 $row['_title'] = '<strong>' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_mod_file_list.xml:temp', true) . '</strong>';
414 }
415 if ($val == '_recycler_') {
416 $icon = 'gfx/i/recycler.gif';
417 $row['title'] = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_mod_file_list.xml:recycler', true);
418 $row['_title'] = '<strong>' .$GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_mod_file_list.xml:recycler', true) . '</strong>';
419 }
420 $HTML .= $this->wrapIcon('<img'.t3lib_iconWorks::skinImg($this->backPath, $icon, 'width="18" height="16"').' alt="" />',$row);
421 }
422
423 // Finally, add the row/HTML content to the ->tree array in the reserved key.
424 $this->tree[$treeKey] = Array(
425 'row' => $row,
426 'HTML' => $HTML,
427 'hasSub' => $nextCount && $this->expandNext($specUID),
428 'isFirst'=> ($a == 1),
429 'isLast' => false,
430 'invertedDepth'=> $depth,
431 'bank' => $this->bank
432 );
433 }
434
435 if($a) { $this->tree[$treeKey]['isLast'] = true; }
436 return $c;
437 }
438 }
439
440 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['typo3/class.filelistfoldertree.php']) {
441 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['typo3/class.filelistfoldertree.php']);
442 }
443
444 ?>