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