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