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