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