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