[TASK] Clean up phpDoc comments in typo3/
[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 /**
29 * Page navigation tree for the Web module
30 *
31 * Revised for TYPO3 3.6 2/2003 by Kasper Skårhøj
32 * XHTML compliant
33 *
34 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
35 * @author Benjamin Mack <bmack@xnos.org>
36 *
37 * [CLASS/FUNCTION INDEX of SCRIPT]
38 *
39 * 71: class webPageTree extends t3lib_browseTree
40 * 81: function webPageTree()
41 * 92: function wrapIcon($icon, &$row)
42 * 130: function wrapStop($str, $row)
43 * 146: function wrapTitle($title, $row, $bank=0)
44 * 165: function printTree($treeArr = '')
45 * 271: function PMicon($row, $a, $c, $nextCount, $exp)
46 * 292: function PMiconATagWrap($icon, $cmd, $isExpand = TRUE)
47 * 309: function getBrowsableTree()
48 * 377: function getTree($uid, $depth=999, $depthData='', $blankLineCode='', $subCSSclass='')
49 *
50 *
51 * TOTAL FUNCTIONS: 9
52 * (This index is automatically created/updated by the extension "extdeveval")
53 */
54
55 /**
56 * Extension class for the t3lib_browsetree class, specially made
57 * for browsing pages in the Web module
58 *
59 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
60 * @author Benjamin Mack <bmack@xnos.org>
61 * @package TYPO3
62 * @subpackage core
63 * @see class t3lib_browseTree
64 */
65 class webPageTree extends t3lib_browseTree {
66
67 var $ext_showPageId;
68 var $ext_IconMode;
69 var $ext_separateNotinmenuPages;
70 var $ext_alphasortNotinmenuPages;
71 // Indicates, whether the ajax call was successful, i.e. the requested page has been found
72 var $ajaxStatus = FALSE;
73
74 /**
75 * Calls init functions
76 */
77 function __construct() {
78 $this->init();
79 }
80
81 /**
82 * Wrapping icon in browse tree
83 *
84 * @param string $thePageIcon Icon IMG code
85 * @param array $row Data row for element.
86 * @return string Page icon
87 */
88 function wrapIcon($thePageIcon, &$row) {
89 // If the record is locked, present a warning sign.
90 if ($lockInfo=t3lib_BEfunc::isRecordLocked('pages', $row['uid'])) {
91 $aOnClick = 'alert('.$GLOBALS['LANG']->JScharCode($lockInfo['msg']).');return false;';
92 $lockIcon='<a href="#" onclick="'.htmlspecialchars($aOnClick).'">'.
93 t3lib_iconWorks::getSpriteIcon('status-warning-in-use', array('title'=>htmlspecialchars($lockInfo['msg']))).
94 '</a>';
95 } else {
96 $lockIcon = '';
97 }
98
99 // Wrap icon in click-menu link.
100 if (!$this->ext_IconMode) {
101 $thePageIcon = $GLOBALS['TBE_TEMPLATE']->wrapClickMenuOnIcon($thePageIcon, 'pages', $row['uid'], 0, '&bank='.$this->bank);
102 } elseif (!strcmp($this->ext_IconMode, 'titlelink')) {
103 $aOnClick = 'return jumpTo(\''.$this->getJumpToParam($row).'\',this,\''.$this->treeName.'\');';
104 $thePageIcon='<a href="#" onclick="'.htmlspecialchars($aOnClick).'">'.$thePageIcon.'</a>';
105 }
106
107 // Wrap icon in a drag/drop span.
108 $dragDropIcon = '<span class="dragIcon" id="dragIconID_'.$row['uid'].'">'.$thePageIcon.'</span>';
109
110 // Add Page ID:
111 $pageIdStr = '';
112 if ($this->ext_showPageId) {
113 $pageIdStr = '<span class="dragId">[' . $row['uid'] . ']</span> ';
114 }
115
116 // Call stats information hook
117 $stat = '';
118 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['recStatInfoHooks'])) {
119 $_params = array('pages', $row['uid']);
120 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['recStatInfoHooks'] as $_funcRef) {
121 $stat.=t3lib_div::callUserFunction($_funcRef, $_params, $this);
122 }
123 }
124
125 return $dragDropIcon.$lockIcon.$pageIdStr.$stat;
126 }
127
128 /**
129 * Adds a red "+" to the input string, $str, if the field "php_tree_stop" in the $row (pages) is set
130 *
131 * @param string $str Input string, like a page title for the tree
132 * @param array $row Record row with "php_tree_stop" field
133 * @return string Modified string
134 * @access private
135 */
136 function wrapStop($str, $row) {
137 if ($row['php_tree_stop']) {
138 $str .= '<a href="'.htmlspecialchars(t3lib_div::linkThisScript(array('setTempDBmount' => $row['uid']))).'" class="typo3-red">+</a> ';
139 }
140 return $str;
141 }
142
143 /**
144 * Wrapping $title in a-tags.
145 *
146 * @param string $title Title string
147 * @param string $row Item record
148 * @param integer $bank Bank pointer (which mount point number)
149 * @return string
150 * @access private
151 */
152 function wrapTitle($title, $row, $bank = 0) {
153 // Hook for overriding the page title
154 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/class.webpagetree.php']['pageTitleOverlay'])) {
155 $_params = array('title' => &$title, 'row' => &$row);
156 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/class.webpagetree.php']['pageTitleOverlay'] as $_funcRef) {
157 t3lib_div::callUserFunction($_funcRef, $_params, $this);
158 }
159 unset($_params);
160 }
161
162 $aOnClick = 'return jumpTo(\''.$this->getJumpToParam($row).'\',this,\''.$this->domIdPrefix.$this->getId($row).'\','.$bank.');';
163 $CSM = ' oncontextmenu="'.htmlspecialchars($GLOBALS['TBE_TEMPLATE']->wrapClickMenuOnIcon('', 'pages', $row['uid'], 0, '&bank='.$this->bank, '', TRUE)).';"';
164 $thePageTitle='<a href="#" onclick="'.htmlspecialchars($aOnClick).'"'.$CSM.'>'.$title.'</a>';
165
166 // Wrap title in a drag/drop span.
167 return '<span class="dragTitle" id="dragTitleID_'.$row['uid'].'">'.$thePageTitle.'</span>';
168 }
169
170 /**
171 * Compiles the HTML code for displaying the structure found inside the ->tree array
172 *
173 * @param array $treeArr "tree-array" - if blank string, the internal ->tree array is used.
174 * @return string The HTML code for the tree
175 */
176 function printTree($treeArr = '') {
177 $titleLen = intval($this->BE_USER->uc['titleLen']);
178 if (!is_array($treeArr)) {
179 $treeArr = $this->tree;
180 }
181
182 $out = '
183 <!-- TYPO3 tree structure. -->
184 <ul class="tree" id="treeRoot">
185 ';
186
187 // -- evaluate AJAX request
188 // IE takes anchor as parameter
189 $PM = t3lib_div::_GP('PM');
190 if (($PMpos = strpos($PM, '#')) !== FALSE) {
191 $PM = substr($PM, 0, $PMpos);
192 }
193 $PM = explode('_', $PM);
194 if ((TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_AJAX) && is_array($PM) && count($PM) == 4 && $PM[2] != 0) {
195 if ($PM[1]) {
196 $expandedPageUid = $PM[2];
197 $ajaxOutput = '';
198 // We don't know yet. Will be set later.
199 $invertedDepthOfAjaxRequestedItem = 0;
200 $doExpand = TRUE;
201 } else {
202 $collapsedPageUid = $PM[2];
203 $doCollapse = TRUE;
204 }
205 }
206
207 // We need to count the opened <ul>'s every time we dig into another level,
208 // so we know how many we have to close when all children are done rendering
209 $closeDepth = array();
210
211 foreach ($treeArr as $k => $v) {
212 $classAttr = $v['row']['_CSSCLASS'];
213 $uid = $v['row']['uid'];
214 $idAttr = htmlspecialchars($this->domIdPrefix.$this->getId($v['row']).'_'.$v['bank']);
215 $itemHTML = '';
216
217 // If this item is the start of a new level,
218 // then a new level <ul> is needed, but not in ajax mode
219 if($v['isFirst'] && !($doCollapse) && !($doExpand && $expandedPageUid == $uid)) {
220 $itemHTML = '<ul>';
221 }
222
223 // Add CSS classes to the list item
224 if ($v['hasSub']) {
225 $classAttr .= ($classAttr) ? ' expanded' : 'expanded';
226 }
227 if ($v['isLast']) {
228 $classAttr .= ($classAttr) ? ' last' : 'last';
229 }
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']) {
238 $itemHTML .= '</li>';
239 }
240
241 // We have to remember if this is the last one
242 // on level X so the last child on level X+1 closes the <ul>-tag
243 if ($v['isLast'] && !($doExpand && $expandedPageUid == $uid)) {
244 $closeDepth[$v['invertedDepth']] = 1;
245 }
246
247 // If this is the last one and does not have subitems, we need to close
248 // the tree as long as the upper levels have last items too
249 if ($v['isLast'] && !$v['hasSub'] && !$doCollapse && !($doExpand && $expandedPageUid == $uid)) {
250 for ($i = $v['invertedDepth']; $closeDepth[$i] == 1; $i++) {
251 $closeDepth[$i] = 0;
252 $itemHTML .= '</ul></li>';
253 }
254 }
255
256 // Ajax request: collapse
257 if ($doCollapse && $collapsedPageUid == $uid) {
258 $this->ajaxStatus = TRUE;
259 return $itemHTML;
260 }
261
262 // ajax request: expand
263 if ($doExpand && $expandedPageUid == $uid) {
264 $ajaxOutput .= $itemHTML;
265 $invertedDepthOfAjaxRequestedItem = $v['invertedDepth'];
266 } elseif ($invertedDepthOfAjaxRequestedItem) {
267 if ($v['invertedDepth'] < $invertedDepthOfAjaxRequestedItem) {
268 $ajaxOutput .= $itemHTML;
269 } else {
270 $this->ajaxStatus = TRUE;
271 return $ajaxOutput;
272 }
273 }
274
275 $out .= $itemHTML;
276 }
277
278 if ($ajaxOutput) {
279 $this->ajaxStatus = TRUE;
280 return $ajaxOutput;
281 }
282
283 // Finally close the first ul
284 $out .= '</ul>';
285 return $out;
286 }
287
288 /**
289 * Generate the plus/minus icon for the browsable tree.
290 *
291 * @param array $row Record for the entry
292 * @param integer $a The current entry number
293 * @param integer $c The total number of entries. If equal to $a, a "bottom" element is returned.
294 * @param integer $nextCount The number of sub-elements to the current element.
295 * @param boolean $exp The element was expanded to render subelements if this flag is set.
296 * @return string Image tag with the plus/minus icon.
297 * @access private
298 * @see t3lib_pageTree::PMicon()
299 */
300 function PMicon($row, $a, $c, $nextCount, $exp) {
301 $PM = $nextCount ? ($exp ? 'minus' : 'plus') : 'join';
302 $BTM = ($a == $c) ? 'bottom' : '';
303 $icon = '<img'.t3lib_iconWorks::skinImg($this->backPath, 'gfx/ol/'.$PM.$BTM.'.gif', 'width="18" height="16"').' alt="" />';
304
305 if ($nextCount) {
306 $cmd = $this->bank.'_'.($exp?'0_':'1_').$row['uid'].'_'.$this->treeName;
307 $icon = $this->PMiconATagWrap($icon, $cmd, !$exp);
308 }
309 return $icon;
310 }
311
312 /**
313 * Wrap the plus/minus icon in a link
314 *
315 * @param string $icon HTML string to wrap, probably an image tag.
316 * @param string $cmd Command for 'PM' get var
317 * @return boolean $isExpand Link-wrapped input string
318 * @access private
319 */
320 function PMiconATagWrap($icon, $cmd, $isExpand = TRUE) {
321 if ($this->thisScript) {
322 // Activate dynamic ajax-based tree
323 $js = htmlspecialchars('Tree.load(\''.$cmd.'\', '.intval($isExpand).', this);');
324 return '<a class="pm" onclick="'.$js.'">'.$icon.'</a>';
325 } else {
326 return $icon;
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) {
386 $this->ids[] = $uid;
387 }
388 $this->getTree($uid, 999, '', $rootRec['_SUBCSSCLASS']);
389 }
390 // Add tree:
391 $treeArr = array_merge($treeArr, $this->tree);
392 }
393 }
394 return $this->printTree($treeArr);
395 }
396
397 /**
398 * Fetches the data for the tree
399 *
400 * @param integer $uid Item id for which to select subitems (parent id)
401 * @param integer $depth Max depth (recursivity limit)
402 * @param string $blankLineCode ? (internal)
403 * @param string $subCSSclass
404 * @return integer The count of items on the level
405 */
406 function getTree($uid, $depth = 999, $blankLineCode = '', $subCSSclass = '') {
407
408 // Buffer for id hierarchy is reset:
409 $this->buffer_idH = array();
410
411 // Init vars
412 $depth = intval($depth);
413 $HTML = '';
414 $a = 0;
415
416 $res = $this->getDataInit($uid, $subCSSclass);
417 $c = $this->getDataCount($res);
418 $crazyRecursionLimiter = 999;
419
420 $inMenuPages = array();
421 $outOfMenuPages = array();
422 $outOfMenuPagesTextIndex = array();
423 while ($crazyRecursionLimiter > 0 && $row = $this->getDataNext($res, $subCSSclass)) {
424 $crazyRecursionLimiter--;
425
426 // Not in menu:
427 if ($this->ext_separateNotinmenuPages &&
428 ($row['doktype'] == t3lib_pageSelect::DOKTYPE_BE_USER_SECTION ||
429 $row['doktype'] >= 200 || $row['nav_hide'])) {
430 $outOfMenuPages[] = $row;
431 $outOfMenuPagesTextIndex[] = ($row['doktype']>=200 ? 'zzz'.$row['doktype'].'_' : '').$row['title'];
432 } else {
433 $inMenuPages[] = $row;
434 }
435 }
436
437 $label_shownAlphabetically = '';
438 if (count($outOfMenuPages)) {
439 // Sort out-of-menu pages:
440 $outOfMenuPages_alphabetic = array();
441 if ($this->ext_alphasortNotinmenuPages) {
442 asort($outOfMenuPagesTextIndex);
443 $label_shownAlphabetically = ' (alphabetic)';
444 }
445 foreach ($outOfMenuPagesTextIndex as $idx => $txt) {
446 $outOfMenuPages_alphabetic[] = $outOfMenuPages[$idx];
447 }
448
449 // Merge:
450 $outOfMenuPages_alphabetic[0]['_FIRST_NOT_IN_MENU'] = TRUE;
451 $allRows = array_merge($inMenuPages, $outOfMenuPages_alphabetic);
452 } else {
453 $allRows = $inMenuPages;
454 }
455
456 // Traverse the records:
457 foreach ($allRows as $row) {
458 $a++;
459
460 $newID = $row['uid'];
461 // Reserve space.
462 $this->tree[] = array();
463 end($this->tree);
464 // Get the key for this space
465 $treeKey = key($this->tree);
466 $LN = ($a == $c) ? 'blank' : 'line';
467
468 // If records should be accumulated, do so
469 if ($this->setRecs) {
470 $this->recs[$row['uid']] = $row;
471 }
472
473 // Accumulate the id of the element in the internal arrays
474 $this->ids[] = $idH[$row['uid']]['uid'] = $row['uid'];
475 $this->ids_hierarchy[$depth][] = $row['uid'];
476
477 // Make a recursive call to the next level
478 if ($depth > 1 && $this->expandNext($newID) && !$row['php_tree_stop']) {
479 $nextCount=$this->getTree(
480 $newID,
481 $depth-1,
482 $blankLineCode.','.$LN,
483 $row['_SUBCSSCLASS']
484 );
485 if (count($this->buffer_idH)) {
486 $idH[$row['uid']]['subrow']=$this->buffer_idH;
487 }
488 // Set "did expand" flag
489 $exp = 1;
490 } else {
491 $nextCount = $this->getCount($newID);
492 // Clear "did expand" flag
493 $exp = 0;
494 }
495
496 // Set HTML-icons, if any:
497 if ($this->makeHTML) {
498 if ($row['_FIRST_NOT_IN_MENU']) {
499 $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>';
500 } else {
501 $HTML = '';
502 }
503
504 $HTML .= $this->PMicon($row, $a, $c, $nextCount, $exp);
505 $HTML .= $this->wrapStop($this->getIcon($row), $row);
506 }
507
508 // Finally, add the row/HTML content to the ->tree array in the reserved key.
509 $this->tree[$treeKey] = array(
510 'row' => $row,
511 'HTML' => $HTML,
512 'hasSub' => $nextCount && $this->expandNext($newID),
513 'isFirst'=> $a == 1,
514 'isLast' => FALSE,
515 'invertedDepth'=> $depth,
516 'blankLineCode'=> $blankLineCode,
517 'bank' => $this->bank
518 );
519 }
520
521 if ($a) {
522 $this->tree[$treeKey]['isLast'] = TRUE;
523 }
524
525 $this->getDataFree($res);
526 $this->buffer_idH = $idH;
527 return $c;
528 }
529 }
530 ?>