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