[API][!!!] Fix API compatibility break
[Packages/TYPO3.CMS.git] / typo3 / class.webpagetree.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2011 Kasper Skårhøj (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 Skårhøj
31 * XHTML compliant
32 *
33 * @author Kasper Skårhøj <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 /**
60 * Extension class for the t3lib_browsetree class, specially made
61 * for browsing pages in the Web module
62 *
63 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
64 * @author Benjamin Mack <bmack@xnos.org>
65 * @package TYPO3
66 * @subpackage core
67 * @see class t3lib_browseTree
68 */
69 class webPageTree extends t3lib_browseTree {
70
71 var $ext_showPageId;
72 var $ext_IconMode;
73 var $ext_separateNotinmenuPages;
74 var $ext_alphasortNotinmenuPages;
75 var $ajaxStatus = FALSE; // Indicates, whether the ajax call was successful, i.e. the requested page has been found
76
77 /**
78 * Calls init functions
79 *
80 * @return void
81 */
82 function __construct() {
83 $this->init();
84 }
85
86 /**
87 * Compatibility constructor.
88 *
89 * @deprecated since TYPO3 4.6 and will be removed in TYPO3 4.8. Use __construct() instead.
90 */
91 public function webPageTree() {
92 t3lib_div::logDeprecatedFunction();
93 // Note: we cannot call $this->__construct() here because it would call the derived class constructor and cause recursion
94 // This code uses official PHP behavior (http://www.php.net/manual/en/language.oop5.basic.php) when $this in the
95 // statically called non-static method inherits $this from the caller's scope.
96 webPageTree::__construct();
97 }
98
99 /**
100 * Wrapping icon in browse tree
101 *
102 * @param string Icon IMG code
103 * @param array Data row for element.
104 * @return string Page icon
105 */
106 function wrapIcon($thePageIcon, &$row) {
107 // If the record is locked, present a warning sign.
108 if ($lockInfo=t3lib_BEfunc::isRecordLocked('pages',$row['uid'])) {
109 $aOnClick = 'alert('.$GLOBALS['LANG']->JScharCode($lockInfo['msg']).');return false;';
110 $lockIcon='<a href="#" onclick="'.htmlspecialchars($aOnClick).'">'.
111 t3lib_iconWorks::getSpriteIcon('status-warning-in-use',array('title'=>htmlspecialchars($lockInfo['msg']))).
112 '</a>';
113 } else $lockIcon = '';
114
115 // Wrap icon in click-menu link.
116 if (!$this->ext_IconMode) {
117 $thePageIcon = $GLOBALS['TBE_TEMPLATE']->wrapClickMenuOnIcon($thePageIcon,'pages',$row['uid'],0,'&bank='.$this->bank);
118 } elseif (!strcmp($this->ext_IconMode,'titlelink')) {
119 $aOnClick = 'return jumpTo(\''.$this->getJumpToParam($row).'\',this,\''.$this->treeName.'\');';
120 $thePageIcon='<a href="#" onclick="'.htmlspecialchars($aOnClick).'">'.$thePageIcon.'</a>';
121 }
122
123 // Wrap icon in a drag/drop span.
124 $dragDropIcon = '<span class="dragIcon" id="dragIconID_'.$row['uid'].'">'.$thePageIcon.'</span>';
125
126 // Add Page ID:
127 $pageIdStr = '';
128 if ($this->ext_showPageId) {
129 $pageIdStr = '<span class="dragId">[' . $row['uid'] . ']</span> ';
130 }
131
132 // Call stats information hook
133 $stat = '';
134 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['recStatInfoHooks'])) {
135 $_params = array('pages',$row['uid']);
136 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['recStatInfoHooks'] as $_funcRef) {
137 $stat.=t3lib_div::callUserFunction($_funcRef,$_params,$this);
138 }
139 }
140
141 return $dragDropIcon.$lockIcon.$pageIdStr.$stat;
142 }
143
144 /**
145 * Adds a red "+" to the input string, $str, if the field "php_tree_stop" in the $row (pages) is set
146 *
147 * @param string Input string, like a page title for the tree
148 * @param array record row with "php_tree_stop" field
149 * @return string Modified string
150 * @access private
151 */
152 function wrapStop($str,$row) {
153 if ($row['php_tree_stop']) {
154 $str.='<a href="'.htmlspecialchars(t3lib_div::linkThisScript(array('setTempDBmount' => $row['uid']))).'" class="typo3-red">+</a> ';
155 }
156 return $str;
157 }
158
159 /**
160 * Wrapping $title in a-tags.
161 *
162 * @param string Title string
163 * @param string Item record
164 * @param integer Bank pointer (which mount point number)
165 * @return string
166 * @access private
167 */
168 function wrapTitle($title,$row,$bank=0) {
169 // Hook for overriding the page title
170 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/class.webpagetree.php']['pageTitleOverlay'])) {
171 $_params = array('title' => &$title, 'row' => &$row);
172 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/class.webpagetree.php']['pageTitleOverlay'] as $_funcRef) {
173 t3lib_div::callUserFunction($_funcRef, $_params, $this);
174 }
175 unset($_params);
176 }
177
178 $aOnClick = 'return jumpTo(\''.$this->getJumpToParam($row).'\',this,\''.$this->domIdPrefix.$this->getId($row).'\','.$bank.');';
179 $CSM = '';
180 if ($GLOBALS['TYPO3_CONF_VARS']['BE']['useOnContextMenuHandler']) {
181 $CSM = ' oncontextmenu="'.htmlspecialchars($GLOBALS['TBE_TEMPLATE']->wrapClickMenuOnIcon('','pages',$row['uid'],0,'&bank='.$this->bank,'',TRUE)).';"';
182 }
183 $thePageTitle='<a href="#" onclick="'.htmlspecialchars($aOnClick).'"'.$CSM.'>'.$title.'</a>';
184
185 // Wrap title in a drag/drop span.
186 return '<span class="dragTitle" id="dragTitleID_'.$row['uid'].'">'.$thePageTitle.'</span>';
187 }
188
189
190 /**
191 * Compiles the HTML code for displaying the structure found inside the ->tree array
192 *
193 * @param array "tree-array" - if blank string, the internal ->tree array is used.
194 * @return string The HTML code for the tree
195 */
196 function printTree($treeArr = '') {
197 $titleLen = intval($this->BE_USER->uc['titleLen']);
198 if (!is_array($treeArr)) {
199 $treeArr = $this->tree;
200 }
201
202 $out = '
203 <!-- TYPO3 tree structure. -->
204 <ul class="tree" id="treeRoot">
205 ';
206
207 // -- evaluate AJAX request
208 // IE takes anchor as parameter
209 $PM = t3lib_div::_GP('PM');
210 if(($PMpos = strpos($PM, '#')) !== FALSE) { $PM = substr($PM, 0, $PMpos); }
211 $PM = explode('_', $PM);
212 if ((TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_AJAX) && is_array($PM) && count($PM) == 4 && $PM[2] != 0) {
213 if($PM[1]) {
214 $expandedPageUid = $PM[2];
215 $ajaxOutput = '';
216 $invertedDepthOfAjaxRequestedItem = 0; // We don't know yet. Will be set later.
217 $doExpand = TRUE;
218 } else {
219 $collapsedPageUid = $PM[2];
220 $doCollapse = TRUE;
221 }
222 }
223
224 // we need to count the opened <ul>'s every time we dig into another level,
225 // so we know how many we have to close when all children are done rendering
226 $closeDepth = array();
227
228 foreach($treeArr as $k => $v) {
229 $classAttr = $v['row']['_CSSCLASS'];
230 $uid = $v['row']['uid'];
231 $idAttr = htmlspecialchars($this->domIdPrefix.$this->getId($v['row']).'_'.$v['bank']);
232 $itemHTML = '';
233
234 // if this item is the start of a new level,
235 // then a new level <ul> is needed, but not in ajax mode
236 if($v['isFirst'] && !($doCollapse) && !($doExpand && $expandedPageUid == $uid)) {
237 $itemHTML = '<ul>';
238 }
239
240 // add CSS classes to the list item
241 if($v['hasSub']) { $classAttr .= ($classAttr) ? ' expanded': 'expanded'; }
242 if($v['isLast']) { $classAttr .= ($classAttr) ? ' last' : 'last'; }
243
244 $itemHTML .='
245 <li id="'.$idAttr.'"'.($classAttr ? ' class="'.$classAttr.'"' : '').'><div class="treeLinkItem">'.
246 $v['HTML'].
247 $this->wrapTitle($this->getTitleStr($v['row'],$titleLen),$v['row'],$v['bank'])."</div>\n";
248
249
250 if(!$v['hasSub']) { $itemHTML .= '</li>'; }
251
252 // we have to remember if this is the last one
253 // on level X so the last child on level X+1 closes the <ul>-tag
254 if($v['isLast'] && !($doExpand && $expandedPageUid == $uid)) { $closeDepth[$v['invertedDepth']] = 1; }
255
256
257 // if this is the last one and does not have subitems, we need to close
258 // the tree as long as the upper levels have last items too
259 if($v['isLast'] && !$v['hasSub'] && !$doCollapse && !($doExpand && $expandedPageUid == $uid)) {
260 for ($i = $v['invertedDepth']; $closeDepth[$i] == 1; $i++) {
261 $closeDepth[$i] = 0;
262 $itemHTML .= '</ul></li>';
263 }
264 }
265
266 // ajax request: collapse
267 if($doCollapse && $collapsedPageUid == $uid) {
268 $this->ajaxStatus = TRUE;
269 return $itemHTML;
270 }
271
272 // ajax request: expand
273 if($doExpand && $expandedPageUid == $uid) {
274 $ajaxOutput .= $itemHTML;
275 $invertedDepthOfAjaxRequestedItem = $v['invertedDepth'];
276 } elseif($invertedDepthOfAjaxRequestedItem) {
277 if($v['invertedDepth'] < $invertedDepthOfAjaxRequestedItem) {
278 $ajaxOutput .= $itemHTML;
279 } else {
280 $this->ajaxStatus = TRUE;
281 return $ajaxOutput;
282 }
283 }
284
285 $out .= $itemHTML;
286 }
287
288 if($ajaxOutput) {
289 $this->ajaxStatus = TRUE;
290 return $ajaxOutput;
291 }
292
293 // finally close the first ul
294 $out .= '</ul>';
295 return $out;
296 }
297
298
299 /**
300 * Generate the plus/minus icon for the browsable tree.
301 *
302 * @param array record for the entry
303 * @param integer The current entry number
304 * @param integer The total number of entries. If equal to $a, a "bottom" element is returned.
305 * @param integer The number of sub-elements to the current element.
306 * @param boolean The element was expanded to render subelements if this flag is set.
307 * @return string Image tag with the plus/minus icon.
308 * @access private
309 * @see t3lib_pageTree::PMicon()
310 */
311 function PMicon($row,$a,$c,$nextCount,$exp) {
312 $PM = $nextCount ? ($exp ? 'minus' : 'plus') : 'join';
313 $BTM = ($a == $c) ? 'bottom' : '';
314 $icon = '<img'.t3lib_iconWorks::skinImg($this->backPath,'gfx/ol/'.$PM.$BTM.'.gif','width="18" height="16"').' alt="" />';
315
316 if ($nextCount) {
317 $cmd = $this->bank.'_'.($exp?'0_':'1_').$row['uid'].'_'.$this->treeName;
318 $icon = $this->PMiconATagWrap($icon,$cmd,!$exp);
319 }
320 return $icon;
321 }
322
323
324 /**
325 * Wrap the plus/minus icon in a link
326 *
327 * @param string HTML string to wrap, probably an image tag.
328 * @param string Command for 'PM' get var
329 * @return string Link-wrapped input string
330 * @access private
331 */
332 function PMiconATagWrap($icon, $cmd, $isExpand = TRUE) {
333 if ($this->thisScript) {
334 // activate dynamic ajax-based tree
335 $js = htmlspecialchars('Tree.load(\''.$cmd.'\', '.intval($isExpand).', this);');
336 return '<a class="pm" onclick="'.$js.'">'.$icon.'</a>';
337 } else {
338 return $icon;
339 }
340 }
341
342
343 /**
344 * Will create and return the HTML code for a browsable tree
345 * Is based on the mounts found in the internal array ->MOUNTS (set in the constructor)
346 *
347 * @return string HTML code for the browsable tree
348 */
349 function getBrowsableTree() {
350
351 // Get stored tree structure AND updating it if needed according to incoming PM GET var.
352 $this->initializePositionSaving();
353
354 // Init done:
355 $titleLen = intval($this->BE_USER->uc['titleLen']);
356 $treeArr = array();
357
358 // Traverse mounts:
359 foreach($this->MOUNTS as $idx => $uid) {
360
361 // Set first:
362 $this->bank = $idx;
363 $isOpen = $this->stored[$idx][$uid] || $this->expandFirst || $uid === '0';
364
365 // Save ids while resetting everything else.
366 $curIds = $this->ids;
367 $this->reset();
368 $this->ids = $curIds;
369
370 // Set PM icon for root of mount:
371 $cmd = $this->bank.'_'.($isOpen? "0_" : "1_").$uid.'_'.$this->treeName;
372 // only, if not for uid 0
373 if ($uid) {
374 $icon = '<img' . t3lib_iconWorks::skinImg($this->backPath,'gfx/ol/' . ($isOpen ? 'minus' :'plus' ) . 'only.gif') . ' alt="" />';
375 $firstHtml = $this->PMiconATagWrap($icon, $cmd, !$isOpen);
376 }
377
378 // Preparing rootRec for the mount
379 if ($uid) {
380 $rootRec = $this->getRecord($uid);
381 $firstHtml.=$this->getIcon($rootRec);
382 } else {
383 // Artificial record for the tree root, id=0
384 $rootRec = $this->getRootRecord($uid);
385 $firstHtml.=$this->getRootIcon($rootRec);
386 }
387
388 if (is_array($rootRec)) {
389 // In case it was swapped inside getRecord due to workspaces.
390 $uid = $rootRec['uid'];
391
392 // Add the root of the mount to ->tree
393 $this->tree[] = array('HTML'=>$firstHtml, 'row'=>$rootRec, 'bank'=>$this->bank, 'hasSub'=>TRUE, 'invertedDepth'=>1000);
394
395 // If the mount is expanded, go down:
396 if ($isOpen) {
397 // Set depth:
398 if ($this->addSelfId) { $this->ids[] = $uid; }
399 $this->getTree($uid, 999, '', $rootRec['_SUBCSSCLASS']);
400 }
401 // Add tree:
402 $treeArr=array_merge($treeArr,$this->tree);
403 }
404 }
405 return $this->printTree($treeArr);
406 }
407
408
409 /**
410 * Fetches the data for the tree
411 *
412 * @param integer item id for which to select subitems (parent id)
413 * @param integer Max depth (recursivity limit)
414 * @param string ? (internal)
415 * @return integer The count of items on the level
416 */
417 function getTree($uid, $depth=999, $blankLineCode='', $subCSSclass='') {
418
419 // Buffer for id hierarchy is reset:
420 $this->buffer_idH = array();
421
422 // Init vars
423 $depth = intval($depth);
424 $HTML = '';
425 $a = 0;
426
427 $res = $this->getDataInit($uid, $subCSSclass);
428 $c = $this->getDataCount($res);
429 $crazyRecursionLimiter = 999;
430
431 $inMenuPages = array();
432 $outOfMenuPages = array();
433 $outOfMenuPagesTextIndex = array();
434 while ($crazyRecursionLimiter > 0 && $row = $this->getDataNext($res,$subCSSclass)) {
435 $crazyRecursionLimiter--;
436
437 // Not in menu:
438 // @TODO: RFC #7370: doktype 2&5 are deprecated since TYPO3 4.2-beta1
439 if ($this->ext_separateNotinmenuPages &&
440 ($row['doktype'] == t3lib_pageSelect::DOKTYPE_HIDE_IN_MENU || $row['doktype'] == t3lib_pageSelect::DOKTYPE_BE_USER_SECTION ||
441 $row['doktype'] >= 200 || $row['nav_hide'])) {
442 $outOfMenuPages[] = $row;
443 $outOfMenuPagesTextIndex[] = ($row['doktype']>=200 ? 'zzz'.$row['doktype'].'_' : '').$row['title'];
444 } else {
445 $inMenuPages[] = $row;
446 }
447 }
448
449 $label_shownAlphabetically = "";
450 if (count($outOfMenuPages)) {
451 // Sort out-of-menu pages:
452 $outOfMenuPages_alphabetic = array();
453 if ($this->ext_alphasortNotinmenuPages) {
454 asort($outOfMenuPagesTextIndex);
455 $label_shownAlphabetically = " (alphabetic)";
456 }
457 foreach($outOfMenuPagesTextIndex as $idx => $txt) {
458 $outOfMenuPages_alphabetic[] = $outOfMenuPages[$idx];
459 }
460
461 // Merge:
462 $outOfMenuPages_alphabetic[0]['_FIRST_NOT_IN_MENU']=TRUE;
463 $allRows = array_merge($inMenuPages,$outOfMenuPages_alphabetic);
464 } else {
465 $allRows = $inMenuPages;
466 }
467
468 // Traverse the records:
469 foreach ($allRows as $row) {
470 $a++;
471
472 $newID = $row['uid'];
473 $this->tree[]=array(); // Reserve space.
474 end($this->tree);
475 $treeKey = key($this->tree); // Get the key for this space
476 $LN = ($a==$c) ? 'blank' : 'line';
477
478 // If records should be accumulated, do so
479 if ($this->setRecs) { $this->recs[$row['uid']] = $row; }
480
481 // Accumulate the id of the element in the internal arrays
482 $this->ids[]=$idH[$row['uid']]['uid'] = $row['uid'];
483 $this->ids_hierarchy[$depth][] = $row['uid'];
484
485 // Make a recursive call to the next level
486 if ($depth > 1 && $this->expandNext($newID) && !$row['php_tree_stop']) {
487 $nextCount=$this->getTree(
488 $newID,
489 $depth-1,
490 $blankLineCode.','.$LN,
491 $row['_SUBCSSCLASS']
492 );
493 if (count($this->buffer_idH)) { $idH[$row['uid']]['subrow']=$this->buffer_idH; }
494 $exp = 1; // Set "did expand" flag
495 } else {
496 $nextCount = $this->getCount($newID);
497 $exp = 0; // Clear "did expand" flag
498 }
499
500 // Set HTML-icons, if any:
501 if ($this->makeHTML) {
502 if ($row['_FIRST_NOT_IN_MENU']) {
503 $HTML = '<img'.t3lib_iconWorks::skinImg($this->backPath,'gfx/ol/line.gif').' alt="" /><br/><img'.t3lib_iconWorks::skinImg($this->backPath,'gfx/ol/line.gif').' alt="" /><i>Not shown in menu'.$label_shownAlphabetically.':</i><br>';
504 } else {
505 $HTML = '';
506 }
507
508 $HTML.= $this->PMicon($row,$a,$c,$nextCount,$exp);
509 $HTML.= $this->wrapStop($this->getIcon($row),$row);
510 }
511
512 // Finally, add the row/HTML content to the ->tree array in the reserved key.
513 $this->tree[$treeKey] = array(
514 'row' => $row,
515 'HTML' => $HTML,
516 'hasSub' => $nextCount&&$this->expandNext($newID),
517 'isFirst'=> $a==1,
518 'isLast' => FALSE,
519 'invertedDepth'=> $depth,
520 'blankLineCode'=> $blankLineCode,
521 'bank' => $this->bank
522 );
523 }
524
525 if($a) { $this->tree[$treeKey]['isLast'] = TRUE; }
526
527 $this->getDataFree($res);
528 $this->buffer_idH = $idH;
529 return $c;
530 }
531 }
532
533 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['typo3/class.webpagetree.php'])) {
534 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['typo3/class.webpagetree.php']);
535 }
536
537 ?>