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