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