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