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