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