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