(feature) Added #7096: Consistent interface for AJAX calls in the 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 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': $icon = 'gfx/i/_icon_ftp_user.gif'; break;
316 case 'group': $icon = 'gfx/i/_icon_ftp_group.gif'; break;
317 default: $icon = 'gfx/i/_icon_ftp.gif'; break;
318 }
319
320 // Preparing rootRec for the mount
321 $firstHtml.=$this->wrapIcon('<img'.t3lib_iconWorks::skinImg($this->backPath,$icon,'width="18" height="16"').' alt="" />',$val);
322 $row=array();
323 $row['uid'] = $specUID;
324 $row['path'] = $val['path'];
325 $row['title'] = $val['name'];
326
327 // Add the root of the mount to ->tree
328 $this->tree[] = array('HTML' => $firstHtml, 'row' => $row, 'bank' => $this->bank);
329
330 // If the mount is expanded, go down:
331 if ($isOpen)
332 $this->getFolderTree($val['path'], 999);
333
334 // Add tree:
335 $treeArr = array_merge($treeArr, $this->tree);
336 }
337 return $this->printTree($treeArr);
338 }
339
340
341
342 /**
343 * Fetches the data for the tree
344 *
345 * @param string Abs file path
346 * @param integer Max depth (recursivity limit)
347 * @return integer The count of items on the level
348 * @see getBrowsableTree()
349 */
350 function getFolderTree($files_path, $depth=999) {
351
352 // This generates the directory tree
353 $dirs = t3lib_div::get_dirs($files_path);
354 if (!is_array($dirs)) return 0;
355
356 sort($dirs);
357 $c = count($dirs);
358
359 $depth = intval($depth);
360 $HTML = '';
361 $a = 0;
362
363 foreach($dirs as $key => $val) {
364 $a++;
365 $this->tree[] = array(); // Reserve space.
366 end($this->tree);
367 $treeKey = key($this->tree); // Get the key for this space
368
369 $val = ereg_replace('^\./','',$val);
370 $title = $val;
371 $path = $files_path.$val.'/';
372
373 $specUID = t3lib_div::md5int($path);
374 $this->specUIDmap[$specUID] = $path;
375
376 $row = array();
377 $row['path'] = $path;
378 $row['uid'] = $specUID;
379 $row['title'] = $title;
380
381 // Make a recursive call to the next level
382 if ($depth > 1 && $this->expandNext($specUID)) {
383 $nextCount = $this->getFolderTree(
384 $path,
385 $depth-1,
386 $this->makeHTML ? '<img'.t3lib_iconWorks::skinImg($this->backPath,'gfx/ol/'.($a == $c ? 'blank' : 'line').'.gif','width="18" height="16"').' alt="" />' : ''
387 );
388 $exp = 1; // Set "did expand" flag
389 } else {
390 $nextCount = $this->getCount($path);
391 $exp = 0; // Clear "did expand" flag
392 }
393
394 // Set HTML-icons, if any:
395 if ($this->makeHTML) {
396 $HTML = $this->PMicon($row,$a,$c,$nextCount,$exp);
397
398 $icon = 'gfx/i/_icon_'.t3lib_BEfunc::getPathType_web_nonweb($path).'folders.gif';
399 if ($val == '_temp_') {
400 $icon = 'gfx/i/sysf.gif';
401 $row['title']='TEMP';
402 $row['_title']='<b>TEMP</b>';
403 }
404 if ($val == '_recycler_') {
405 $icon = 'gfx/i/recycler.gif';
406 $row['title']='RECYCLER';
407 $row['_title']='<b>RECYCLER</b>';
408 }
409 $HTML .= $this->wrapIcon('<img'.t3lib_iconWorks::skinImg($this->backPath, $icon, 'width="18" height="16"').' alt="" />',$row);
410 }
411
412 // Finally, add the row/HTML content to the ->tree array in the reserved key.
413 $this->tree[$treeKey] = Array(
414 'row' => $row,
415 'HTML' => $HTML,
416 'hasSub' => $nextCount && $this->expandNext($specUID),
417 'isFirst'=> ($a == 1),
418 'isLast' => false,
419 'invertedDepth'=> $depth,
420 'bank' => $this->bank
421 );
422 }
423
424 if($a) { $this->tree[$treeKey]['isLast'] = true; }
425 return $c;
426 }
427 }
428
429 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['typo3/class.filelistfoldertree.php']) {
430 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['typo3/class.filelistfoldertree.php']);
431 }
432 ?>