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