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