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