Fixed bug #14050: CleanUp - CGL format of t3lib files - t3lib_scbase
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_treeview.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2010 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 * Contains base class for creating a browsable array/page/folder tree in HTML
29 *
30 * $Id$
31 * Revised for TYPO3 3.6 November/2003 by Kasper Skårhøj
32 *
33 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
34 * @coauthor René Fritz <r.fritz@colorcube.de>
35 */
36 /**
37 * [CLASS/FUNCTION INDEX of SCRIPT]
38 *
39 *
40 *
41 * 115: class t3lib_treeView
42 * 270: function init($clause='', $orderByFields='')
43 * 301: function setTreeName($treeName='')
44 * 315: function addField($field,$noCheck=0)
45 * 329: function reset()
46 *
47 * SECTION: output
48 * 349: function getBrowsableTree()
49 * 412: function printTree($treeArr='')
50 *
51 * SECTION: rendering parts
52 * 467: function PMicon($row,$a,$c,$nextCount,$exp)
53 * 489: function PM_ATagWrap($icon,$cmd,$bMark='')
54 * 511: function wrapTitle($title,$row,$bank=0)
55 * 524: function wrapIcon($icon,$row)
56 * 535: function addTagAttributes($icon,$attr)
57 * 547: function wrapStop($str,$row)
58 *
59 * SECTION: tree handling
60 * 575: function expandNext($id)
61 * 585: function initializePositionSaving()
62 * 612: function savePosition()
63 *
64 * SECTION: Functions that might be overwritten by extended classes
65 * 641: function getRootIcon($rec)
66 * 654: function getIcon($row)
67 * 673: function getTitleStr($row,$titleLen=30)
68 * 685: function getTitleAttrib($row)
69 * 695: function getId($row)
70 * 705: function getJumpToParam($row)
71 *
72 * SECTION: tree data buidling
73 * 739: function getTree($uid, $depth=999, $depthData='',$blankLineCode='',$subCSSclass='')
74 *
75 * SECTION: Data handling
76 * 839: function getCount($uid)
77 * 865: function getRootRecord($uid)
78 * 878: function getRecord($uid)
79 * 898: function getDataInit($parentId,$subCSSclass='')
80 * 929: function getDataCount(&$res)
81 * 947: function getDataNext(&$res,$subCSSclass='')
82 * 986: function getDataFree(&$res)
83 * 1006: function setDataFromArray(&$dataArr,$traverse=FALSE,$pid=0)
84 * 1035: function setDataFromTreeArray(&$treeArr, &$treeLookupArr)
85 *
86 * TOTAL FUNCTIONS: 31
87 * (This index is automatically created/updated by the extension "extdeveval")
88 *
89 */
90
91
92
93
94
95
96
97
98
99
100
101
102
103 /**
104 * Base class for creating a browsable array/page/folder tree in HTML
105 *
106 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
107 * @coauthor René Fritz <r.fritz@colorcube.de>
108 * @package TYPO3
109 * @subpackage t3lib
110 * @see t3lib_browsetree, t3lib_pagetree, t3lib_foldertree
111 */
112 class t3lib_treeView {
113
114 // EXTERNAL, static:
115 var $expandFirst=0; // If set, the first element in the tree is always expanded.
116 var $expandAll=0; // If set, then ALL items will be expanded, regardless of stored settings.
117 var $thisScript=''; // Holds the current script to reload to.
118 var $titleAttrib = 'title'; // Which HTML attribute to use: alt/title. See init().
119 var $ext_IconMode = false; // If true, no context menu is rendered on icons. If set to "titlelink" the icon is linked as the title is.
120 var $addSelfId = 0; // If set, the id of the mounts will be added to the internal ids array
121 var $title='no title'; // Used if the tree is made of records (not folders for ex.)
122 var $showDefaultTitleAttribute = FALSE; // If true, a default title attribute showing the UID of the record is shown. This cannot be enabled by default because it will destroy many applications where another title attribute is in fact applied later.
123 var $highlightPagesWithVersions = TRUE; // If true, pages containing child records which has versions will be highlighted in yellow. This might be too expensive in terms of processing power.
124
125 /**
126 * Needs to be initialized with $GLOBALS['BE_USER']
127 * Done by default in init()
128 *
129 * @var t3lib_beUserAuth
130 */
131 var $BE_USER='';
132
133 /**
134 * Needs to be initialized with e.g. $GLOBALS['WEBMOUNTS']
135 * Default setting in init() is 0 => 0
136 * The keys are mount-ids (can be anything basically) and the values are the ID of the root element (COULD be zero or anything else. For pages that would be the uid of the page, zero for the pagetree root.)
137 */
138 var $MOUNTS='';
139
140
141
142 /**
143 * Database table to get the tree data from.
144 * Leave blank if data comes from an array.
145 */
146 var $table='';
147
148 /**
149 * Defines the field of $table which is the parent id field (like pid for table pages).
150 */
151 var $parentField='pid';
152
153 /**
154 * WHERE clause used for selecting records for the tree. Is set by function init.
155 * Only makes sense when $this->table is set.
156 * @see init()
157 */
158 var $clause='';
159
160 /**
161 * Field for ORDER BY. Is set by function init.
162 * Only makes sense when $this->table is set.
163 * @see init()
164 */
165 var $orderByFields='';
166
167 /**
168 * Default set of fields selected from the tree table.
169 * Make SURE that these fields names listed herein are actually possible to select from $this->table (if that variable is set to a TCA table name)
170 * @see addField()
171 */
172 var $fieldArray = Array('uid','title');
173
174 /**
175 * List of other fields which are ALLOWED to set (here, based on the "pages" table!)
176 * @see addField()
177 */
178 var $defaultList = 'uid,pid,tstamp,sorting,deleted,perms_userid,perms_groupid,perms_user,perms_group,perms_everybody,crdate,cruser_id';
179
180
181 /**
182 * Unique name for the tree.
183 * Used as key for storing the tree into the BE users settings.
184 * Used as key to pass parameters in links.
185 * MUST NOT contain underscore chars.
186 * etc.
187 */
188 var $treeName = '';
189
190 /**
191 * A prefix for table cell id's which will be wrapped around an item.
192 * Can be used for highlighting by JavaScript.
193 * Needs to be unique if multiple trees are on one HTML page.
194 * @see printTree()
195 */
196 var $domIdPrefix = 'row';
197
198 /**
199 * Back path for icons
200 */
201 var $backPath;
202
203 /**
204 * Icon file path.
205 */
206 var $iconPath = '';
207
208
209 /**
210 * Icon file name for item icons.
211 */
212 var $iconName = 'default.gif';
213
214 /**
215 * If true, HTML code is also accumulated in ->tree array during rendering of the tree.
216 * If 2, then also the icon prefix code (depthData) is stored
217 */
218 var $makeHTML=1;
219
220 /**
221 * If true, records as selected will be stored internally in the ->recs array
222 */
223 var $setRecs = 0;
224
225 /**
226 * Sets the associative array key which identifies a new sublevel if arrays are used for trees.
227 * This value has formerly been "subLevel" and "--sublevel--"
228 */
229 var $subLevelID = '_SUB_LEVEL';
230
231
232
233
234 // *********
235 // Internal
236 // *********
237 // For record trees:
238 var $ids = Array(); // one-dim array of the uid's selected.
239 var $ids_hierarchy = array(); // The hierarchy of element uids
240 var $orig_ids_hierarchy = array(); // The hierarchy of versioned element uids
241 var $buffer_idH = array(); // Temporary, internal array
242
243 // For FOLDER trees:
244 var $specUIDmap=array(); // Special UIDs for folders (integer-hashes of paths)
245
246 // For arrays:
247 var $data = false; // Holds the input data array
248 var $dataLookup = false; // Holds an index with references to the data array.
249
250 // For both types
251 var $tree = Array(); // Tree is accumulated in this variable
252 var $stored = array(); // Holds (session stored) information about which items in the tree are unfolded and which are not.
253 var $bank=0; // Points to the current mountpoint key
254 var $recs = array(); // Accumulates the displayed records.
255
256
257
258
259
260
261
262 /**
263 * Initialize the tree class. Needs to be overwritten
264 * Will set ->fieldsArray, ->backPath and ->clause
265 *
266 * @param string record WHERE clause
267 * @param string record ORDER BY field
268 * @return void
269 */
270 function init($clause='', $orderByFields='') {
271 $this->BE_USER = $GLOBALS['BE_USER']; // Setting BE_USER by default
272 $this->titleAttrib = 'title'; // Setting title attribute to use.
273 $this->backPath = $GLOBALS['BACK_PATH']; // Setting backpath.
274
275 if ($clause) $this->clause = $clause; // Setting clause
276 if ($orderByFields) $this->orderByFields = $orderByFields;
277
278 if (!is_array($this->MOUNTS)) {
279 $this->MOUNTS = array(0 => 0); // dummy
280 }
281
282 $this->setTreeName();
283
284 if($this->table) {
285 t3lib_div::loadTCA($this->table);
286 }
287
288 // setting this to false disables the use of array-trees by default
289 $this->data = false;
290 $this->dataLookup = false;
291 }
292
293
294 /**
295 * Sets the tree name which is used to identify the tree
296 * Used for JavaScript and other things
297 *
298 * @param string Default is the table name. Underscores are stripped.
299 * @return void
300 */
301 function setTreeName($treeName='') {
302 $this->treeName = $treeName ? $treeName : $this->treeName;
303 $this->treeName = $this->treeName ? $this->treeName : $this->table;
304 $this->treeName = str_replace('_','',$this->treeName);
305 }
306
307
308 /**
309 * Adds a fieldname to the internal array ->fieldArray
310 *
311 * @param string Field name to
312 * @param boolean If set, the fieldname will be set no matter what. Otherwise the field name must either be found as key in $TCA[$table]['columns'] or in the list ->defaultList
313 * @return void
314 */
315 function addField($field,$noCheck=0) {
316 global $TCA;
317 if ($noCheck || is_array($TCA[$this->table]['columns'][$field]) || t3lib_div::inList($this->defaultList,$field)) {
318 $this->fieldArray[]=$field;
319 }
320 }
321
322
323
324 /**
325 * Resets the tree, recs, ids, ids_hierarchy and orig_ids_hierarchy internal variables. Use it if you need it.
326 *
327 * @return void
328 */
329 function reset() {
330 $this->tree = array();
331 $this->recs = array();
332 $this->ids = array();
333 $this->ids_hierarchy = array();
334 $this->orig_ids_hierarchy = array();
335 }
336
337
338 /*******************************************
339 *
340 * output
341 *
342 *******************************************/
343
344 /**
345 * Will create and return the HTML code for a browsable tree
346 * Is based on the mounts found in the internal array ->MOUNTS (set in the constructor)
347 *
348 * @return string HTML code for the browsable tree
349 */
350 function getBrowsableTree() {
351
352 // Get stored tree structure AND updating it if needed according to incoming PM GET var.
353 $this->initializePositionSaving();
354
355 // Init done:
356 $titleLen=intval($this->BE_USER->uc['titleLen']);
357 $treeArr=array();
358
359 // Traverse mounts:
360 foreach($this->MOUNTS as $idx => $uid) {
361
362 // Set first:
363 $this->bank=$idx;
364 $isOpen = $this->stored[$idx][$uid] || $this->expandFirst;
365
366 // Save ids while resetting everything else.
367 $curIds = $this->ids;
368 $this->reset();
369 $this->ids = $curIds;
370
371 // Set PM icon for root of mount:
372 $cmd=$this->bank.'_'.($isOpen?"0_":"1_").$uid.'_'.$this->treeName;
373 $icon='<img'.t3lib_iconWorks::skinImg($this->backPath,'gfx/ol/'.($isOpen?'minus':'plus').'only.gif','width="18" height="16"').' alt="" />';
374 $firstHtml= $this->PM_ATagWrap($icon,$cmd);
375
376 // Preparing rootRec for the mount
377 if ($uid) {
378 $rootRec = $this->getRecord($uid);
379 $firstHtml.=$this->getIcon($rootRec);
380 } else {
381 // Artificial record for the tree root, id=0
382 $rootRec = $this->getRootRecord($uid);
383 $firstHtml.=$this->getRootIcon($rootRec);
384 }
385
386 if (is_array($rootRec)) {
387 $uid = $rootRec['uid']; // In case it was swapped inside getRecord due to workspaces.
388
389 // Add the root of the mount to ->tree
390 $this->tree[]=array('HTML'=>$firstHtml, 'row'=>$rootRec, 'bank'=>$this->bank);
391
392 // If the mount is expanded, go down:
393 if ($isOpen) {
394 // Set depth:
395 $depthD='<img'.t3lib_iconWorks::skinImg($this->backPath,'gfx/ol/blank.gif','width="18" height="16"').' alt="" />';
396 if ($this->addSelfId) $this->ids[] = $uid;
397 $this->getTree($uid,999,$depthD,'',$rootRec['_SUBCSSCLASS']);
398 }
399
400 // Add tree:
401 $treeArr=array_merge($treeArr,$this->tree);
402 }
403 }
404 return $this->printTree($treeArr);
405 }
406
407 /**
408 * Compiles the HTML code for displaying the structure found inside the ->tree array
409 *
410 * @param array "tree-array" - if blank string, the internal ->tree array is used.
411 * @return string The HTML code for the tree
412 */
413 function printTree($treeArr='') {
414 $titleLen=intval($this->BE_USER->uc['titleLen']);
415 if (!is_array($treeArr)) $treeArr=$this->tree;
416 $out='';
417
418 // put a table around it with IDs to access the rows from JS
419 // not a problem if you don't need it
420 // In XHTML there is no "name" attribute of <td> elements - but Mozilla will not be able to highlight rows if the name attribute is NOT there.
421 $out .= '
422
423 <!--
424 TYPO3 tree structure.
425 -->
426 <table cellpadding="0" cellspacing="0" border="0" id="typo3-tree">';
427
428 foreach($treeArr as $k => $v) {
429 $idAttr = htmlspecialchars($this->domIdPrefix.$this->getId($v['row']).'_'.$v['bank']);
430
431 $out.='
432 <tr>
433 <td id="'.$idAttr.'"'.
434 ($v['row']['_CSSCLASS'] ? ' class="'.$v['row']['_CSSCLASS'].'"' : '').'>'.
435 $v['HTML'].
436 $this->wrapTitle($this->getTitleStr($v['row'],$titleLen),$v['row'],$v['bank']).
437 '</td>
438 </tr>
439 ';
440 }
441 $out .= '
442 </table>';
443 return $out;
444 }
445
446
447
448 /*******************************************
449 *
450 * rendering parts
451 *
452 *******************************************/
453
454
455
456 /**
457 * Generate the plus/minus icon for the browsable tree.
458 *
459 * @param array record for the entry
460 * @param integer The current entry number
461 * @param integer The total number of entries. If equal to $a, a "bottom" element is returned.
462 * @param integer The number of sub-elements to the current element.
463 * @param boolean The element was expanded to render subelements if this flag is set.
464 * @return string Image tag with the plus/minus icon.
465 * @access private
466 * @see t3lib_pageTree::PMicon()
467 */
468 function PMicon($row,$a,$c,$nextCount,$exp) {
469 $PM = $nextCount ? ($exp?'minus':'plus') : 'join';
470 $BTM = ($a==$c)?'bottom':'';
471 $icon = '<img'.t3lib_iconWorks::skinImg($this->backPath,'gfx/ol/'.$PM.$BTM.'.gif','width="18" height="16"').' alt="" />';
472
473 if ($nextCount) {
474 $cmd=$this->bank.'_'.($exp?'0_':'1_').$row['uid'].'_'.$this->treeName;
475 $bMark=($this->bank.'_'.$row['uid']);
476 $icon = $this->PM_ATagWrap($icon,$cmd,$bMark);
477 }
478 return $icon;
479 }
480
481 /**
482 * Wrap the plus/minus icon in a link
483 *
484 * @param string HTML string to wrap, probably an image tag.
485 * @param string Command for 'PM' get var
486 * @param boolean If set, the link will have a anchor point (=$bMark) and a name attribute (=$bMark)
487 * @return string Link-wrapped input string
488 * @access private
489 */
490 function PM_ATagWrap($icon,$cmd,$bMark='') {
491 if ($this->thisScript) {
492 if ($bMark) {
493 $anchor = '#'.$bMark;
494 $name=' name="'.$bMark.'"';
495 }
496 $aUrl = $this->thisScript.'?PM='.$cmd.$anchor;
497 return '<a href="'.htmlspecialchars($aUrl).'"'.$name.'>'.$icon.'</a>';
498 } else {
499 return $icon;
500 }
501 }
502
503 /**
504 * Wrapping $title in a-tags.
505 *
506 * @param string Title string
507 * @param string Item record
508 * @param integer Bank pointer (which mount point number)
509 * @return string
510 * @access private
511 */
512 function wrapTitle($title,$row,$bank=0) {
513 $aOnClick = 'return jumpTo(\''.$this->getJumpToParam($row).'\',this,\''.$this->domIdPrefix.$this->getId($row).'\','.$bank.');';
514 return '<a href="#" onclick="'.htmlspecialchars($aOnClick).'">'.$title.'</a>';
515 }
516
517 /**
518 * Wrapping the image tag, $icon, for the row, $row (except for mount points)
519 *
520 * @param string The image tag for the icon
521 * @param array The row for the current element
522 * @return string The processed icon input value.
523 * @access private
524 */
525 function wrapIcon($icon,$row) {
526 return $icon;
527 }
528
529 /**
530 * Adds attributes to image tag.
531 *
532 * @param string Icon image tag
533 * @param string Attributes to add, eg. ' border="0"'
534 * @return string Image tag, modified with $attr attributes added.
535 */
536 function addTagAttributes($icon,$attr) {
537 return preg_replace('/ ?\/?>$/','',$icon).' '.$attr.' />';
538 }
539
540 /**
541 * Adds a red "+" to the input string, $str, if the field "php_tree_stop" in the $row (pages) is set
542 *
543 * @param string Input string, like a page title for the tree
544 * @param array record row with "php_tree_stop" field
545 * @return string Modified string
546 * @access private
547 */
548 function wrapStop($str,$row) {
549 if ($row['php_tree_stop']) {
550 $str .= '<span class="typo3-red"><a href="' . htmlspecialchars(t3lib_div::linkThisScript(array('setTempDBmount' => $row['uid']))) . '" class="typo3-red">+</a> </span>';
551 }
552 return $str;
553 }
554
555
556
557
558
559
560 /*******************************************
561 *
562 * tree handling
563 *
564 *******************************************/
565
566
567 /**
568 * Returns true/false if the next level for $id should be expanded - based on data in $this->stored[][] and ->expandAll flag.
569 * Extending parent function
570 *
571 * @param integer record id/key
572 * @return boolean
573 * @access private
574 * @see t3lib_pageTree::expandNext()
575 */
576 function expandNext($id) {
577 return ($this->stored[$this->bank][$id] || $this->expandAll)? 1 : 0;
578 }
579
580 /**
581 * Get stored tree structure AND updating it if needed according to incoming PM GET var.
582 *
583 * @return void
584 * @access private
585 */
586 function initializePositionSaving() {
587 // Get stored tree structure:
588 $this->stored=unserialize($this->BE_USER->uc['browseTrees'][$this->treeName]);
589
590 // PM action
591 // (If an plus/minus icon has been clicked, the PM GET var is sent and we must update the stored positions in the tree):
592 $PM = explode('_',t3lib_div::_GP('PM')); // 0: mount key, 1: set/clear boolean, 2: item ID (cannot contain "_"), 3: treeName
593 if (count($PM)==4 && $PM[3]==$this->treeName) {
594 if (isset($this->MOUNTS[$PM[0]])) {
595 if ($PM[1]) { // set
596 $this->stored[$PM[0]][$PM[2]]=1;
597 $this->savePosition();
598 } else { // clear
599 unset($this->stored[$PM[0]][$PM[2]]);
600 $this->savePosition();
601 }
602 }
603 }
604 }
605
606 /**
607 * Saves the content of ->stored (keeps track of expanded positions in the tree)
608 * $this->treeName will be used as key for BE_USER->uc[] to store it in
609 *
610 * @return void
611 * @access private
612 */
613 function savePosition() {
614 $this->BE_USER->uc['browseTrees'][$this->treeName] = serialize($this->stored);
615 $this->BE_USER->writeUC();
616 }
617
618
619
620
621
622
623
624
625
626
627
628
629
630 /******************************
631 *
632 * Functions that might be overwritten by extended classes
633 *
634 ********************************/
635
636 /**
637 * Returns the root icon for a tree/mountpoint (defaults to the globe)
638 *
639 * @param array Record for root.
640 * @return string Icon image tag.
641 */
642 function getRootIcon($rec) {
643 return $this->wrapIcon(t3lib_iconWorks::getSpriteIcon('apps-pagetree-root'),$rec);
644 }
645
646
647
648 /**
649 * Get icon for the row.
650 * If $this->iconPath and $this->iconName is set, try to get icon based on those values.
651 *
652 * @param array Item row.
653 * @return string Image tag.
654 */
655 function getIcon($row) {
656 if ($this->iconPath && $this->iconName) {
657 $icon = '<img'.t3lib_iconWorks::skinImg('',$this->iconPath.$this->iconName,'width="18" height="16"').' alt=""'.($this->showDefaultTitleAttribute ? ' title="UID: '.$row['uid'].'"':'').' />';
658 } else {
659
660 $icon = t3lib_iconWorks::getSpriteIconForRecord($this->table, $row, array(
661 'title' => ($this->showDefaultTitleAttribute ? 'UID: ' . $row['uid'] : $this->getTitleAttrib($row)),
662 'class' => 'c-recIcon'
663 ));
664
665 }
666
667 return $this->wrapIcon($icon,$row);
668 }
669
670
671 /**
672 * Returns the title for the input record. If blank, a "no title" label (localized) will be returned.
673 * Do NOT htmlspecialchar the string from this function - has already been done.
674 *
675 * @param array The input row array (where the key "title" is used for the title)
676 * @param integer Title length (30)
677 * @return string The title.
678 */
679 function getTitleStr($row,$titleLen=30) {
680 if ($this->ext_showNavTitle && strlen(trim($row['nav_title'])) > 0) {
681 $title = '<span title="'.$GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_tca.xml:title',1).' '.htmlspecialchars(trim($row['title'])).'">'.htmlspecialchars(t3lib_div::fixed_lgd_cs($row['nav_title'],$titleLen)).'</span>';
682 } else {
683 $title = htmlspecialchars(t3lib_div::fixed_lgd_cs($row['title'],$titleLen));
684 if (strlen(trim($row['nav_title'])) > 0) {
685 $title = '<span title="'.$GLOBALS['LANG']->sL('LLL:EXT:cms/locallang_tca.xml:pages.nav_title',1).' '.htmlspecialchars(trim($row['nav_title'])).'">'.$title.'</span>';
686 }
687 $title = (strlen(trim($row['title'])) == 0) ? '<em>['.$GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.php:labels.no_title',1).']</em>' : $title;
688 }
689
690 return $title;
691 }
692
693 /**
694 * Returns the value for the image "title" attribute
695 *
696 * @param array The input row array (where the key "title" is used for the title)
697 * @return string The attribute value (is htmlspecialchared() already)
698 * @see wrapIcon()
699 */
700 function getTitleAttrib($row) {
701 return htmlspecialchars($row['title']);
702 }
703
704 /**
705 * Returns the id from the record (typ. uid)
706 *
707 * @param array Record array
708 * @return integer The "uid" field value.
709 */
710 function getId($row) {
711 return $row['uid'];
712 }
713
714 /**
715 * Returns jump-url parameter value.
716 *
717 * @param array The record array.
718 * @return string The jump-url parameter.
719 */
720 function getJumpToParam($row) {
721 return $this->getId($row);
722 }
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738 /********************************
739 *
740 * tree data buidling
741 *
742 ********************************/
743
744 /**
745 * Fetches the data for the tree
746 *
747 * @param integer item id for which to select subitems (parent id)
748 * @param integer Max depth (recursivity limit)
749 * @param string HTML-code prefix for recursive calls.
750 * @param string ? (internal)
751 * @param string CSS class to use for <td> sub-elements
752 * @return integer The count of items on the level
753 */
754 function getTree($uid, $depth=999, $depthData='',$blankLineCode='',$subCSSclass='') {
755
756 // Buffer for id hierarchy is reset:
757 $this->buffer_idH=array();
758
759 // Init vars
760 $depth=intval($depth);
761 $HTML='';
762 $a=0;
763
764 $res = $this->getDataInit($uid,$subCSSclass);
765 $c = $this->getDataCount($res);
766 $crazyRecursionLimiter = 999;
767
768 $idH = array();
769
770 // Traverse the records:
771 while ($crazyRecursionLimiter>0 && $row = $this->getDataNext($res,$subCSSclass)) {
772 $a++;
773 $crazyRecursionLimiter--;
774
775 $newID = $row['uid'];
776
777 if ($newID == 0) {
778 throw new RuntimeException('Endless recursion detected: TYPO3 has detected an error in the database. Please fix it manually (e.g. using phpMyAdmin) and change the UID of '.$this->table.':0 to a new value.<br /><br />See <a href="http://bugs.typo3.org/view.php?id=3495" target="_blank">bugs.typo3.org/view.php?id=3495</a> to get more information about a possible cause.');
779 }
780
781 $this->tree[]=array(); // Reserve space.
782 end($this->tree);
783 $treeKey = key($this->tree); // Get the key for this space
784 $LN = ($a==$c)?'blank':'line';
785
786 // If records should be accumulated, do so
787 if ($this->setRecs) {
788 $this->recs[$row['uid']] = $row;
789 }
790
791 // Accumulate the id of the element in the internal arrays
792 $this->ids[] = $idH[$row['uid']]['uid'] = $row['uid'];
793 $this->ids_hierarchy[$depth][] = $row['uid'];
794 $this->orig_ids_hierarchy[$depth][] = $row['_ORIG_uid'] ? $row['_ORIG_uid'] : $row['uid'];
795
796 // Make a recursive call to the next level
797 $HTML_depthData = $depthData.'<img'.t3lib_iconWorks::skinImg($this->backPath,'gfx/ol/'.$LN.'.gif','width="18" height="16"').' alt="" />';
798 if ($depth>1 && $this->expandNext($newID) && !$row['php_tree_stop']) {
799 $nextCount=$this->getTree(
800 $newID,
801 $depth-1,
802 $this->makeHTML ? $HTML_depthData : '',
803 $blankLineCode.','.$LN,
804 $row['_SUBCSSCLASS']
805 );
806 if (count($this->buffer_idH)) $idH[$row['uid']]['subrow']=$this->buffer_idH;
807 $exp=1; // Set "did expand" flag
808 } else {
809 $nextCount=$this->getCount($newID);
810 $exp=0; // Clear "did expand" flag
811 }
812
813 // Set HTML-icons, if any:
814 if ($this->makeHTML) {
815 $HTML = $depthData.$this->PMicon($row,$a,$c,$nextCount,$exp);
816 $HTML.=$this->wrapStop($this->getIcon($row),$row);
817 # $HTML.=$this->wrapStop($this->wrapIcon($this->getIcon($row),$row),$row);
818 }
819
820 // Finally, add the row/HTML content to the ->tree array in the reserved key.
821 $this->tree[$treeKey] = Array(
822 'row'=>$row,
823 'HTML'=>$HTML,
824 'HTML_depthData' => $this->makeHTML==2 ? $HTML_depthData : '',
825 'invertedDepth'=>$depth,
826 'blankLineCode'=>$blankLineCode,
827 'bank' => $this->bank
828 );
829 }
830
831 $this->getDataFree($res);
832 $this->buffer_idH=$idH;
833 return $c;
834 }
835
836
837
838
839
840
841
842
843
844
845
846
847 /********************************
848 *
849 * Data handling
850 * Works with records and arrays
851 *
852 ********************************/
853
854 /**
855 * Returns the number of records having the parent id, $uid
856 *
857 * @param integer id to count subitems for
858 * @return integer
859 * @access private
860 */
861 function getCount($uid) {
862 if (is_array($this->data)) {
863 $res = $this->getDataInit($uid);
864 return $this->getDataCount($res);
865 } else {
866 return $GLOBALS['TYPO3_DB']->exec_SELECTcountRows(
867 'uid',
868 $this->table,
869 $this->parentField . '=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($uid, $this->table) .
870 t3lib_BEfunc::deleteClause($this->table) .
871 t3lib_BEfunc::versioningPlaceholderClause($this->table) .
872 $this->clause // whereClauseMightContainGroupOrderBy
873 );
874 }
875 }
876
877
878
879 /**
880 * Returns root record for uid (<=0)
881 *
882 * @param integer uid, <= 0 (normally, this does not matter)
883 * @return array Array with title/uid keys with values of $this->title/0 (zero)
884 */
885 function getRootRecord($uid) {
886 return array('title' => $this->title, 'uid' => 0);
887 }
888
889
890 /**
891 * Returns the record for a uid.
892 * For tables: Looks up the record in the database.
893 * For arrays: Returns the fake record for uid id.
894 *
895 * @param integer UID to look up
896 * @return array The record
897 */
898 function getRecord($uid) {
899 if (is_array($this->data)) {
900 return $this->dataLookup[$uid];
901 } else {
902 return t3lib_BEfunc::getRecordWSOL($this->table, $uid);
903 }
904 }
905
906 /**
907 * Getting the tree data: Selecting/Initializing data pointer to items for a certain parent id.
908 * For tables: This will make a database query to select all children to "parent"
909 * For arrays: This will return key to the ->dataLookup array
910 *
911 * @param integer parent item id
912 * @param string Class for sub-elements.
913 * @return mixed data handle (Tables: An sql-resource, arrays: A parentId integer. -1 is returned if there were NO subLevel.)
914 * @access private
915 */
916 function getDataInit($parentId,$subCSSclass='') {
917 if (is_array($this->data)) {
918 if (!is_array($this->dataLookup[$parentId][$this->subLevelID])) {
919 $parentId = -1;
920 } else {
921 reset($this->dataLookup[$parentId][$this->subLevelID]);
922 }
923 return $parentId;
924 } else {
925 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
926 implode(',',$this->fieldArray),
927 $this->table,
928 $this->parentField.'='.$GLOBALS['TYPO3_DB']->fullQuoteStr($parentId, $this->table).
929 t3lib_BEfunc::deleteClause($this->table).
930 t3lib_BEfunc::versioningPlaceholderClause($this->table).
931 $this->clause, // whereClauseMightContainGroupOrderBy
932 '',
933 $this->orderByFields
934 );
935 return $res;
936 }
937 }
938
939 /**
940 * Getting the tree data: Counting elements in resource
941 *
942 * @param mixed data handle
943 * @return integer number of items
944 * @access private
945 * @see getDataInit()
946 */
947 function getDataCount(&$res) {
948 if (is_array($this->data)) {
949 return count($this->dataLookup[$res][$this->subLevelID]);
950 } else {
951 $c = $GLOBALS['TYPO3_DB']->sql_num_rows($res);
952 return $c;
953 }
954 }
955
956 /**
957 * Getting the tree data: next entry
958 *
959 * @param mixed data handle
960 * @param string CSS class for sub elements (workspace related)
961 * @return array item data array OR FALSE if end of elements.
962 * @access private
963 * @see getDataInit()
964 */
965 function getDataNext(&$res,$subCSSclass='') {
966 if (is_array($this->data)) {
967 if ($res<0) {
968 $row=FALSE;
969 } else {
970 list(,$row) = each($this->dataLookup[$res][$this->subLevelID]);
971
972 // Passing on default <td> class for subelements:
973 if (is_array($row) && $subCSSclass!=='') {
974 $row['_CSSCLASS'] = $row['_SUBCSSCLASS'] = $subCSSclass;
975 }
976 }
977 return $row;
978 } else {
979 while($row = @$GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
980 t3lib_BEfunc::workspaceOL($this->table, $row, $this->BE_USER->workspace, TRUE);
981 if (is_array($row)) break;
982 }
983
984 // Passing on default <td> class for subelements:
985 if (is_array($row) && $subCSSclass!=='') {
986
987 if ($this->table==='pages' && $this->highlightPagesWithVersions && !isset($row['_CSSCLASS']) && count(t3lib_BEfunc::countVersionsOfRecordsOnPage($this->BE_USER->workspace, $row['uid'], TRUE))) {
988 $row['_CSSCLASS'] = 'ver-versions';
989 }
990
991 if (!isset($row['_CSSCLASS'])) $row['_CSSCLASS'] = $subCSSclass;
992 if (!isset($row['_SUBCSSCLASS'])) $row['_SUBCSSCLASS'] = $subCSSclass;
993 }
994
995 return $row;
996 }
997 }
998
999 /**
1000 * Getting the tree data: frees data handle
1001 *
1002 * @param mixed data handle
1003 * @return void
1004 * @access private
1005 */
1006 function getDataFree(&$res) {
1007 if (!is_array($this->data)) {
1008 $GLOBALS['TYPO3_DB']->sql_free_result($res);
1009 }
1010 }
1011
1012 /**
1013 * Used to initialize class with an array to browse.
1014 * The array inputted will be traversed and an internal index for lookup is created.
1015 * The keys of the input array are perceived as "uid"s of records which means that keys GLOBALLY must be unique like uids are.
1016 * "uid" and "pid" "fakefields" are also set in each record.
1017 * All other fields are optional.
1018 *
1019 * @param array The input array, see examples below in this script.
1020 * @param boolean Internal, for recursion.
1021 * @param integer Internal, for recursion.
1022 * @return void
1023 */
1024 function setDataFromArray(&$dataArr,$traverse=FALSE,$pid=0) {
1025 if (!$traverse) {
1026 $this->data = &$dataArr;
1027 $this->dataLookup=array();
1028 // add root
1029 $this->dataLookup[0][$this->subLevelID]=&$dataArr;
1030 }
1031
1032 foreach($dataArr as $uid => $val) {
1033
1034 $dataArr[$uid]['uid']=$uid;
1035 $dataArr[$uid]['pid']=$pid;
1036
1037 // gives quick access to id's
1038 $this->dataLookup[$uid] = &$dataArr[$uid];
1039
1040 if (is_array($val[$this->subLevelID])) {
1041 $this->setDataFromArray($dataArr[$uid][$this->subLevelID],TRUE,$uid);
1042 }
1043 }
1044 }
1045
1046 /**
1047 * Sets the internal data arrays
1048 *
1049 * @param array Content for $this->data
1050 * @param array Content for $this->dataLookup
1051 * @return void
1052 */
1053 function setDataFromTreeArray(&$treeArr, &$treeLookupArr) {
1054 $this->data = &$treeArr;
1055 $this->dataLookup=&$treeLookupArr;
1056 }
1057
1058
1059 /*
1060 array(
1061 [id1] => array(
1062 'title'=>'title...',
1063 'id' => 'id1',
1064 'icon' => 'icon ref, relative to typo3/ folder...'
1065 ),
1066 [id2] => array(
1067 'title'=>'title...',
1068 'id' => 'id2',
1069 'icon' => 'icon ref, relative to typo3/ folder...'
1070 ),
1071 [id3] => array(
1072 'title'=>'title...',
1073 'id' => 'id3',
1074 'icon' => 'icon ref, relative to typo3/ folder...'
1075 $this->subLevelID => array(
1076 [id3_asdf#1] => array(
1077 'title'=>'title...',
1078 'id' => 'asdf#1',
1079 'icon' => 'icon ref, relative to typo3/ folder...'
1080 ),
1081 [5] => array(
1082 'title'=>'title...',
1083 'id' => 'id...',
1084 'icon' => 'icon ref, relative to typo3/ folder...'
1085 ),
1086 [6] => array(
1087 'title'=>'title...',
1088 'id' => 'id...',
1089 'icon' => 'icon ref, relative to typo3/ folder...'
1090 ),
1091 )
1092 ),
1093 )
1094 */
1095 }
1096
1097
1098 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_treeview.php']) {
1099 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_treeview.php']);
1100 }
1101
1102 ?>