Fixed bug: AJAX navtree erroneously showed whole tree inside of the expanded branch...
[Packages/TYPO3.CMS.git] / typo3 / class.webpagetree.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 * Page navigation tree for the Web module
29 *
30 * Revised for TYPO3 3.6 2/2003 by Kasper Skaarhoj
31 * XHTML compliant
32 *
33 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
34 * @author Benjamin Mack <bmack@xnos.org>
35 *
36 *
37 * [CLASS/FUNCTION INDEX of SCRIPT]
38 *
39 *
40 *
41 * 71: class webPageTree extends t3lib_browseTree
42 * 81: function webPageTree()
43 * 92: function wrapIcon($icon,&$row)
44 * 130: function wrapStop($str,$row)
45 * 146: function wrapTitle($title,$row,$bank=0)
46 * 165: function printTree($treeArr = '')
47 * 271: function PMicon($row,$a,$c,$nextCount,$exp)
48 * 292: function PMiconATagWrap($icon, $cmd, $isExpand = true)
49 * 309: function getBrowsableTree()
50 * 377: function getTree($uid, $depth=999, $depthData='',$blankLineCode='',$subCSSclass='')
51 *
52 *
53 * TOTAL FUNCTIONS: 9
54 * (This index is automatically created/updated by the extension "extdeveval")
55 *
56 */
57
58
59 require_once(PATH_t3lib.'class.t3lib_browsetree.php');
60
61
62 /**
63 * Extension class for the t3lib_browsetree class, specially made
64 * for browsing pages in the Web module
65 *
66 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
67 * @author Benjamin Mack <bmack@xnos.org>
68 * @package TYPO3
69 * @subpackage core
70 * @see class t3lib_browseTree
71 */
72 class webPageTree extends t3lib_browseTree {
73
74 var $ext_showPageId;
75 var $ext_IconMode;
76 var $ajaxStatus = false; // Indicates, whether the ajax call was successful, i.e. the requested page has been found
77
78 /**
79 * Calls init functions
80 *
81 * @return void
82 */
83 function webPageTree() {
84 $this->init();
85 }
86
87 /**
88 * Wrapping icon in browse tree
89 *
90 * @param string Icon IMG code
91 * @param array Data row for element.
92 * @return string Page icon
93 */
94 function wrapIcon($icon,&$row) {
95 // If the record is locked, present a warning sign.
96 if ($lockInfo=t3lib_BEfunc::isRecordLocked('pages',$row['uid'])) {
97 $aOnClick = 'alert('.$GLOBALS['LANG']->JScharCode($lockInfo['msg']).');return false;';
98 $lockIcon='<a href="#" onclick="'.htmlspecialchars($aOnClick).'">'.
99 '<img'.t3lib_iconWorks::skinImg('','gfx/recordlock_warning3.gif','width="17" height="12"').' title="'.htmlspecialchars($lockInfo['msg']).'" alt="" />'.
100 '</a>';
101 } else $lockIcon = '';
102
103 // Add title attribute to input icon tag
104 $thePageIcon = $this->addTagAttributes($icon, $this->titleAttrib.'="'.$this->getTitleAttrib($row).'"');
105
106 // Wrap icon in click-menu link.
107 if (!$this->ext_IconMode) {
108 $thePageIcon = $GLOBALS['TBE_TEMPLATE']->wrapClickMenuOnIcon($thePageIcon,'pages',$row['uid'],0,'&bank='.$this->bank);
109 } elseif (!strcmp($this->ext_IconMode,'titlelink')) {
110 $aOnClick = 'return jumpTo(\''.$this->getJumpToParam($row).'\',this,\''.$this->treeName.'\');';
111 $thePageIcon='<a href="#" onclick="'.htmlspecialchars($aOnClick).'">'.$thePageIcon.'</a>';
112 }
113
114 // Wrap icon in a drag/drop span.
115 $dragDropIcon = '<span class="dragIcon" id="dragIconID_'.$row['uid'].'">'.$thePageIcon.'</span>';
116
117 // Add Page ID:
118 $pageIdStr = '';
119 if ($this->ext_showPageId) { $pageIdStr = '['.$row['uid'].']&nbsp;'; }
120
121 return $dragDropIcon.$lockIcon.$pageIdStr;
122 }
123
124 /**
125 * Adds a red "+" to the input string, $str, if the field "php_tree_stop" in the $row (pages) is set
126 *
127 * @param string Input string, like a page title for the tree
128 * @param array record row with "php_tree_stop" field
129 * @return string Modified string
130 * @access private
131 */
132 function wrapStop($str,$row) {
133 if ($row['php_tree_stop']) {
134 $str.='<a href="'.htmlspecialchars(t3lib_div::linkThisScript(array('setTempDBmount' => $row['uid']))).'" class="typo3-red">+</a> ';
135 }
136 return $str;
137 }
138
139 /**
140 * Wrapping $title in a-tags.
141 *
142 * @param string Title string
143 * @param string Item record
144 * @param integer Bank pointer (which mount point number)
145 * @return string
146 * @access private
147 */
148 function wrapTitle($title,$row,$bank=0) {
149 $aOnClick = 'return jumpTo(\''.$this->getJumpToParam($row).'\',this,\''.$this->domIdPrefix.$this->getId($row).'\','.$bank.');';
150 $CSM = '';
151 if ($GLOBALS['TYPO3_CONF_VARS']['BE']['useOnContextMenuHandler']) {
152 $CSM = ' oncontextmenu="'.htmlspecialchars($GLOBALS['TBE_TEMPLATE']->wrapClickMenuOnIcon('','pages',$row['uid'],0,'&bank='.$this->bank,'',TRUE)).';"';
153 }
154 $thePageTitle='<a href="#" onclick="'.htmlspecialchars($aOnClick).'"'.$CSM.'>'.$title.'</a>';
155
156 // Wrap title in a drag/drop span.
157 return '<span class="dragTitle" id="dragTitleID_'.$row['uid'].'">'.$thePageTitle.'</span>';
158 }
159
160
161 /**
162 * Compiles the HTML code for displaying the structure found inside the ->tree array
163 *
164 * @param array "tree-array" - if blank string, the internal ->tree array is used.
165 * @return string The HTML code for the tree
166 */
167 function printTree($treeArr = '') {
168 $titleLen = intval($this->BE_USER->uc['titleLen']);
169 if (!is_array($treeArr)) $treeArr = $this->tree;
170
171 $out = '
172 <!-- TYPO3 tree structure. -->
173 <ul class="tree">
174 ';
175
176 // -- evaluate AJAX request
177 // IE takes anchor as parameter
178 $PM = t3lib_div::_GP('PM');
179 if(($PMpos = strpos($PM, '#')) !== false) { $PM = substr($PM, 0, $PMpos); }
180 $PM = explode('_', $PM);
181 if(($isAjaxCall = t3lib_div::_GP('ajax')) && is_array($PM) && count($PM)==4) {
182 if($PM[1]) {
183 $expandedPageUid = $PM[2];
184 $ajaxOutput = '';
185 $invertedDepthOfAjaxRequestedItem = 0; // We don't know yet. Will be set later.
186 $doExpand = true;
187 } else {
188 $collapsedPageUid = $PM[2];
189 $doCollapse = true;
190 }
191 }
192
193 // we need to count the opened <ul>'s every time we dig into another level,
194 // so we know how many we have to close when all children are done rendering
195 $closeDepth = array();
196
197 foreach($treeArr as $k => $v) {
198 $classAttr = $v['row']['_CSSCLASS'];
199 $uid = $v['row']['uid'];
200 $idAttr = htmlspecialchars($this->domIdPrefix.$this->getId($v['row']).'_'.$v['bank']);
201 $itemHTML = '';
202
203 // if this item is the start of a new level,
204 // then a new level <ul> is needed, but not in ajax mode
205 if($v['isFirst'] && !($doCollapse) && !($doExpand && $expandedPageUid == $uid)) {
206 $itemHTML = '<ul>';
207 }
208
209 // add CSS classes to the list item
210 if($v['hasSub']) { $classAttr .= ($classAttr) ? ' expanded': 'expanded'; }
211 if($v['isLast']) { $classAttr .= ($classAttr) ? ' last' : 'last'; }
212
213 $itemHTML .='
214 <li id="'.$idAttr.'"'.($classAttr ? ' class="'.$classAttr.'"' : '').'>'.
215 $v['HTML'].
216 $this->wrapTitle($this->getTitleStr($v['row'],$titleLen),$v['row'],$v['bank'])."\n";
217
218
219 if(!$v['hasSub']) { $itemHTML .= '</li>'; }
220
221 // we have to remember if this is the last one
222 // on level X so the last child on level X+1 closes the <ul>-tag
223 if($v['isLast'] && !($doExpand && $expandedPageUid == $uid)) { $closeDepth[$v['invertedDepth']] = 1; }
224
225
226 // if this is the last one and does not have subitems, we need to close
227 // the tree as long as the upper levels have last items too
228 if($v['isLast'] && !$v['hasSub'] && !$doCollapse && !($doExpand && $expandedPageUid == $uid)) {
229 for ($i = $v['invertedDepth']; $closeDepth[$i] == 1; $i++) {
230 $closeDepth[$i] = 0;
231 $itemHTML .= '</ul></li>';
232 }
233 }
234
235 // ajax request: collapse
236 if($doCollapse && $collapsedPageUid == $uid) {
237 $this->ajaxStatus = true;
238 return $itemHTML;
239 }
240
241 // ajax request: expand
242 if($doExpand && $expandedPageUid == $uid) {
243 $ajaxOutput .= $itemHTML;
244 $invertedDepthOfAjaxRequestedItem = $v['invertedDepth'];
245 } elseif($invertedDepthOfAjaxRequestedItem) {
246 if($v['invertedDepth'] < $invertedDepthOfAjaxRequestedItem) {
247 $ajaxOutput .= $itemHTML;
248 } else {
249 $this->ajaxStatus = true;
250 return $ajaxOutput;
251 }
252 }
253
254 $out .= $itemHTML;
255 }
256
257 if($ajaxOutput) {
258 $this->ajaxStatus = true;
259 return $ajaxOutput;
260 }
261
262 // finally close the first ul
263 $out .= '</ul>';
264 return $out;
265 }
266
267
268 /**
269 * Generate the plus/minus icon for the browsable tree.
270 *
271 * @param array record for the entry
272 * @param integer The current entry number
273 * @param integer The total number of entries. If equal to $a, a "bottom" element is returned.
274 * @param integer The number of sub-elements to the current element.
275 * @param boolean The element was expanded to render subelements if this flag is set.
276 * @return string Image tag with the plus/minus icon.
277 * @access private
278 * @see t3lib_pageTree::PMicon()
279 */
280 function PMicon($row,$a,$c,$nextCount,$exp) {
281 $PM = $nextCount ? ($exp ? 'minus' : 'plus') : 'join';
282 $BTM = ($a == $c) ? 'bottom' : '';
283 $icon = '<img'.t3lib_iconWorks::skinImg($this->backPath,'gfx/ol/'.$PM.$BTM.'.gif','width="18" height="16"').' alt="" />';
284
285 if ($nextCount) {
286 $cmd = $this->bank.'_'.($exp?'0_':'1_').$row['uid'].'_'.$this->treeName;
287 $icon = $this->PMiconATagWrap($icon,$cmd,!$exp);
288 }
289 return $icon;
290 }
291
292
293 /**
294 * Wrap the plus/minus icon in a link
295 *
296 * @param string HTML string to wrap, probably an image tag.
297 * @param string Command for 'PM' get var
298 * @return string Link-wrapped input string
299 * @access private
300 */
301 function PMiconATagWrap($icon, $cmd, $isExpand = true) {
302 if ($this->thisScript) {
303 // activate dynamic ajax-based tree
304 $js = htmlspecialchars('Tree.load(\''.$cmd.'\', '.intval($isExpand).', this);');
305 return '<a class="pm" onclick="'.$js.'">'.$icon.'</a>';
306 } else {
307 return $icon;
308 }
309 }
310
311
312 /**
313 * Will create and return the HTML code for a browsable tree
314 * Is based on the mounts found in the internal array ->MOUNTS (set in the constructor)
315 *
316 * @return string HTML code for the browsable tree
317 */
318 function getBrowsableTree() {
319
320 // Get stored tree structure AND updating it if needed according to incoming PM GET var.
321 $this->initializePositionSaving();
322
323 // Init done:
324 $titleLen = intval($this->BE_USER->uc['titleLen']);
325 $treeArr = array();
326
327 // Traverse mounts:
328 foreach($this->MOUNTS as $idx => $uid) {
329
330 // Set first:
331 $this->bank = $idx;
332 $isOpen = $this->stored[$idx][$uid] || $this->expandFirst;
333
334 // Save ids while resetting everything else.
335 $curIds = $this->ids;
336 $this->reset();
337 $this->ids = $curIds;
338
339 // Set PM icon for root of mount:
340 $cmd = $this->bank.'_'.($isOpen? "0_" : "1_").$uid.'_'.$this->treeName;
341 $icon='<img'.t3lib_iconWorks::skinImg($this->backPath,'gfx/ol/'.($isOpen?'minus':'plus').'only.gif').' alt="" />';
342 $firstHtml = $this->PMiconATagWrap($icon,$cmd);
343
344 // Preparing rootRec for the mount
345 if ($uid) {
346 $rootRec = $this->getRecord($uid);
347 $firstHtml.=$this->getIcon($rootRec);
348 } else {
349 // Artificial record for the tree root, id=0
350 $rootRec = $this->getRootRecord($uid);
351 $firstHtml.=$this->getRootIcon($rootRec);
352 }
353
354 if (is_array($rootRec)) {
355 // In case it was swapped inside getRecord due to workspaces.
356 $uid = $rootRec['uid'];
357
358 // Add the root of the mount to ->tree
359 $this->tree[] = array('HTML'=>$firstHtml, 'row'=>$rootRec, 'bank'=>$this->bank, 'hasSub'=>true, 'invertedDepth'=>1000);
360
361 // If the mount is expanded, go down:
362 if ($isOpen) {
363 // Set depth:
364 if ($this->addSelfId) { $this->ids[] = $uid; }
365 $this->getTree($uid, 999, '', $rootRec['_SUBCSSCLASS']);
366 }
367 // Add tree:
368 $treeArr=array_merge($treeArr,$this->tree);
369 }
370 }
371 return $this->printTree($treeArr);
372 }
373
374
375 /**
376 * Fetches the data for the tree
377 *
378 * @param integer item id for which to select subitems (parent id)
379 * @param integer Max depth (recursivity limit)
380 * @param string ? (internal)
381 * @return integer The count of items on the level
382 */
383 function getTree($uid, $depth=999, $blankLineCode='', $subCSSclass='') {
384
385 // Buffer for id hierarchy is reset:
386 $this->buffer_idH = array();
387
388 // Init vars
389 $depth = intval($depth);
390 $HTML = '';
391 $a = 0;
392
393 $res = $this->getDataInit($uid, $subCSSclass);
394 $c = $this->getDataCount($res);
395 $crazyRecursionLimiter = 999;
396
397 // Traverse the records:
398 while ($crazyRecursionLimiter > 0 && $row = $this->getDataNext($res,$subCSSclass)) {
399 $a++;
400 $crazyRecursionLimiter--;
401
402 $newID = $row['uid'];
403 $this->tree[]=array(); // Reserve space.
404 end($this->tree);
405 $treeKey = key($this->tree); // Get the key for this space
406 $LN = ($a==$c) ? 'blank' : 'line';
407
408 // If records should be accumulated, do so
409 if ($this->setRecs) { $this->recs[$row['uid']] = $row; }
410
411 // Accumulate the id of the element in the internal arrays
412 $this->ids[]=$idH[$row['uid']]['uid'] = $row['uid'];
413 $this->ids_hierarchy[$depth][] = $row['uid'];
414
415 // Make a recursive call to the next level
416 if ($depth > 1 && $this->expandNext($newID) && !$row['php_tree_stop']) {
417 $nextCount=$this->getTree(
418 $newID,
419 $depth-1,
420 $blankLineCode.','.$LN,
421 $row['_SUBCSSCLASS']
422 );
423 if (count($this->buffer_idH)) { $idH[$row['uid']]['subrow']=$this->buffer_idH; }
424 $exp = 1; // Set "did expand" flag
425 } else {
426 $nextCount = $this->getCount($newID);
427 $exp = 0; // Clear "did expand" flag
428 }
429
430 // Set HTML-icons, if any:
431 if ($this->makeHTML) {
432 $HTML = $this->PMicon($row,$a,$c,$nextCount,$exp);
433 $HTML.= $this->wrapStop($this->getIcon($row),$row);
434 }
435
436 // Finally, add the row/HTML content to the ->tree array in the reserved key.
437 $this->tree[$treeKey] = array(
438 'row' => $row,
439 'HTML' => $HTML,
440 'hasSub' => $nextCount&&$this->expandNext($newID),
441 'isFirst'=> $a==1,
442 'isLast' => false,
443 'invertedDepth'=> $depth,
444 'blankLineCode'=> $blankLineCode,
445 'bank' => $this->bank
446 );
447 }
448
449 if($a) { $this->tree[$treeKey]['isLast'] = true; }
450
451 $this->getDataFree($res);
452 $this->buffer_idH = $idH;
453 return $c;
454 }
455 }
456
457 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['typo3/class.webpagetree.php']) {
458 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['typo3/class.webpagetree.php']);
459 }
460 ?>