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