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