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