[TASK] Use (int) instead of intval() or (integer)
[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 = '<img' . IconUtility::skinImg($this->backPath, ('gfx/ol/' . ($isOpen ? 'minus' : 'plus') . 'only.gif'), 'width="18" height="16"') . ' alt="" />';
414 $firstHtml = $this->PM_ATagWrap($icon, $cmd);
415 // Preparing rootRec for the mount
416 if ($uid) {
417 $rootRec = $this->getRecord($uid);
418 $firstHtml .= $this->getIcon($rootRec);
419 } else {
420 // Artificial record for the tree root, id=0
421 $rootRec = $this->getRootRecord($uid);
422 $firstHtml .= $this->getRootIcon($rootRec);
423 }
424 if (is_array($rootRec)) {
425 // In case it was swapped inside getRecord due to workspaces.
426 $uid = $rootRec['uid'];
427 // Add the root of the mount to ->tree
428 $this->tree[] = array('HTML' => $firstHtml, 'row' => $rootRec, 'bank' => $this->bank);
429 // If the mount is expanded, go down:
430 if ($isOpen) {
431 // Set depth:
432 $depthD = '<img' . IconUtility::skinImg($this->backPath, 'gfx/ol/blank.gif', 'width="18" height="16"') . ' alt="" />';
433 if ($this->addSelfId) {
434 $this->ids[] = $uid;
435 }
436 $this->getTree($uid, 999, $depthD, '', $rootRec['_SUBCSSCLASS']);
437 }
438 // Add tree:
439 $treeArr = array_merge($treeArr, $this->tree);
440 }
441 }
442 return $this->printTree($treeArr);
443 }
444
445 /**
446 * Compiles the HTML code for displaying the structure found inside the ->tree array
447 *
448 * @param array $treeArr "tree-array" - if blank string, the internal ->tree array is used.
449 * @return string The HTML code for the tree
450 * @todo Define visibility
451 */
452 public function printTree($treeArr = '') {
453 $titleLen = (int)$this->BE_USER->uc['titleLen'];
454 if (!is_array($treeArr)) {
455 $treeArr = $this->tree;
456 }
457 $out = '';
458 // put a table around it with IDs to access the rows from JS
459 // not a problem if you don't need it
460 // In XHTML there is no "name" attribute of <td> elements -
461 // but Mozilla will not be able to highlight rows if the name
462 // attribute is NOT there.
463 $out .= '
464
465 <!--
466 TYPO3 tree structure.
467 -->
468 <table cellpadding="0" cellspacing="0" border="0" id="typo3-tree">';
469 foreach ($treeArr as $k => $v) {
470 $idAttr = htmlspecialchars($this->domIdPrefix . $this->getId($v['row']) . '_' . $v['bank']);
471 $out .= '
472 <tr>
473 <td id="' . $idAttr . '"' . ($v['row']['_CSSCLASS'] ? ' class="' . $v['row']['_CSSCLASS'] . '"' : '') . '>' . $v['HTML'] . $this->wrapTitle($this->getTitleStr($v['row'], $titleLen), $v['row'], $v['bank']) . '</td>
474 </tr>
475 ';
476 }
477 $out .= '
478 </table>';
479 return $out;
480 }
481
482 /*******************************************
483 *
484 * rendering parts
485 *
486 *******************************************/
487 /**
488 * Generate the plus/minus icon for the browsable tree.
489 *
490 * @param array $row Record for the entry
491 * @param integer $a The current entry number
492 * @param integer $c The total number of entries. If equal to $a, a "bottom" element is returned.
493 * @param integer $nextCount The number of sub-elements to the current element.
494 * @param boolean $exp The element was expanded to render subelements if this flag is set.
495 * @return string Image tag with the plus/minus icon.
496 * @access private
497 * @see \TYPO3\CMS\Backend\Tree\View\PageTreeView::PMicon()
498 * @todo Define visibility
499 */
500 public function PMicon($row, $a, $c, $nextCount, $exp) {
501 $PM = $nextCount ? ($exp ? 'minus' : 'plus') : 'join';
502 $BTM = $a == $c ? 'bottom' : '';
503 $icon = '<img' . IconUtility::skinImg($this->backPath, ('gfx/ol/' . $PM . $BTM . '.gif'), 'width="18" height="16"') . ' alt="" />';
504 if ($nextCount) {
505 $cmd = $this->bank . '_' . ($exp ? '0_' : '1_') . $row['uid'] . '_' . $this->treeName;
506 $bMark = $this->bank . '_' . $row['uid'];
507 $icon = $this->PM_ATagWrap($icon, $cmd, $bMark);
508 }
509 return $icon;
510 }
511
512 /**
513 * Wrap the plus/minus icon in a link
514 *
515 * @param string $icon HTML string to wrap, probably an image tag.
516 * @param string $cmd Command for 'PM' get var
517 * @param boolean $bMark If set, the link will have a anchor point (=$bMark) and a name attribute (=$bMark)
518 * @return string Link-wrapped input string
519 * @access private
520 * @todo Define visibility
521 */
522 public function PM_ATagWrap($icon, $cmd, $bMark = '') {
523 if ($this->thisScript) {
524 if ($bMark) {
525 $anchor = '#' . $bMark;
526 $name = ' name="' . $bMark . '"';
527 }
528 $aUrl = $this->thisScript . '?PM=' . $cmd . $anchor;
529 return '<a href="' . htmlspecialchars($aUrl) . '"' . $name . '>' . $icon . '</a>';
530 } else {
531 return $icon;
532 }
533 }
534
535 /**
536 * Wrapping $title in a-tags.
537 *
538 * @param string $title Title string
539 * @param string $row Item record
540 * @param integer $bank Bank pointer (which mount point number)
541 * @return string
542 * @access private
543 * @todo Define visibility
544 */
545 public function wrapTitle($title, $row, $bank = 0) {
546 $aOnClick = 'return jumpTo(\'' . $this->getJumpToParam($row) . '\',this,\'' . $this->domIdPrefix . $this->getId($row) . '\',' . $bank . ');';
547 return '<a href="#" onclick="' . htmlspecialchars($aOnClick) . '">' . $title . '</a>';
548 }
549
550 /**
551 * Wrapping the image tag, $icon, for the row, $row (except for mount points)
552 *
553 * @param string $icon The image tag for the icon
554 * @param array $row The row for the current element
555 * @return string The processed icon input value.
556 * @access private
557 * @todo Define visibility
558 */
559 public function wrapIcon($icon, $row) {
560 return $icon;
561 }
562
563 /**
564 * Adds attributes to image tag.
565 *
566 * @param string $icon Icon image tag
567 * @param string $attr Attributes to add, eg. ' border="0"'
568 * @return string Image tag, modified with $attr attributes added.
569 * @todo Define visibility
570 */
571 public function addTagAttributes($icon, $attr) {
572 return preg_replace('/ ?\\/?>$/', '', $icon) . ' ' . $attr . ' />';
573 }
574
575 /**
576 * Adds a red "+" to the input string, $str, if the field "php_tree_stop" in the $row (pages) is set
577 *
578 * @param string $str Input string, like a page title for the tree
579 * @param array $row record row with "php_tree_stop" field
580 * @return string Modified string
581 * @access private
582 * @todo Define visibility
583 */
584 public function wrapStop($str, $row) {
585 if ($row['php_tree_stop']) {
586 $str .= '<span class="typo3-red"><a href="' . htmlspecialchars(GeneralUtility::linkThisScript(array('setTempDBmount' => $row['uid']))) . '" class="typo3-red">+</a> </span>';
587 }
588 return $str;
589 }
590
591 /*******************************************
592 *
593 * tree handling
594 *
595 *******************************************/
596 /**
597 * Returns TRUE/FALSE if the next level for $id should be expanded - based on
598 * data in $this->stored[][] and ->expandAll flag.
599 * Extending parent function
600 *
601 * @param integer $id Record id/key
602 * @return boolean
603 * @access private
604 * @see \TYPO3\CMS\Backend\Tree\View\PageTreeView::expandNext()
605 * @todo Define visibility
606 */
607 public function expandNext($id) {
608 return $this->stored[$this->bank][$id] || $this->expandAll ? 1 : 0;
609 }
610
611 /**
612 * Get stored tree structure AND updating it if needed according to incoming PM GET var.
613 *
614 * @return void
615 * @access private
616 * @todo Define visibility
617 */
618 public function initializePositionSaving() {
619 // Get stored tree structure:
620 $this->stored = unserialize($this->BE_USER->uc['browseTrees'][$this->treeName]);
621 // PM action
622 // (If an plus/minus icon has been clicked, the PM GET var is sent and we
623 // must update the stored positions in the tree):
624 // 0: mount key, 1: set/clear boolean, 2: item ID (cannot contain "_"), 3: treeName
625 $PM = explode('_', GeneralUtility::_GP('PM'));
626 if (count($PM) == 4 && $PM[3] == $this->treeName) {
627 if (isset($this->MOUNTS[$PM[0]])) {
628 // set
629 if ($PM[1]) {
630 $this->stored[$PM[0]][$PM[2]] = 1;
631 $this->savePosition();
632 } else {
633 unset($this->stored[$PM[0]][$PM[2]]);
634 $this->savePosition();
635 }
636 }
637 }
638 }
639
640 /**
641 * Saves the content of ->stored (keeps track of expanded positions in the tree)
642 * $this->treeName will be used as key for BE_USER->uc[] to store it in
643 *
644 * @return void
645 * @access private
646 * @todo Define visibility
647 */
648 public function savePosition() {
649 $this->BE_USER->uc['browseTrees'][$this->treeName] = serialize($this->stored);
650 $this->BE_USER->writeUC();
651 }
652
653 /******************************
654 *
655 * Functions that might be overwritten by extended classes
656 *
657 ********************************/
658 /**
659 * Returns the root icon for a tree/mountpoint (defaults to the globe)
660 *
661 * @param array $rec Record for root.
662 * @return string Icon image tag.
663 * @todo Define visibility
664 */
665 public function getRootIcon($rec) {
666 return $this->wrapIcon(IconUtility::getSpriteIcon('apps-pagetree-root'), $rec);
667 }
668
669 /**
670 * Get icon for the row.
671 * If $this->iconPath and $this->iconName is set, try to get icon based on those values.
672 *
673 * @param array $row Item row.
674 * @return string Image tag.
675 * @todo Define visibility
676 */
677 public function getIcon($row) {
678 if ($this->iconPath && $this->iconName) {
679 $icon = '<img' . IconUtility::skinImg('', ($this->iconPath . $this->iconName), 'width="18" height="16"') . ' alt=""' . ($this->showDefaultTitleAttribute ? ' title="UID: ' . $row['uid'] . '"' : '') . ' />';
680 } else {
681 $icon = IconUtility::getSpriteIconForRecord($this->table, $row, array(
682 'title' => $this->showDefaultTitleAttribute ? 'UID: ' . $row['uid'] : $this->getTitleAttrib($row),
683 'class' => 'c-recIcon'
684 ));
685 }
686 return $this->wrapIcon($icon, $row);
687 }
688
689 /**
690 * Returns the title for the input record. If blank, a "no title" label (localized) will be returned.
691 * Do NOT htmlspecialchar the string from this function - has already been done.
692 *
693 * @param array $row The input row array (where the key "title" is used for the title)
694 * @param integer $titleLen Title length (30)
695 * @return string The title.
696 * @todo Define visibility
697 */
698 public function getTitleStr($row, $titleLen = 30) {
699 if ($this->ext_showNavTitle && strlen(trim($row['nav_title'])) > 0) {
700 $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>';
701 } else {
702 $title = htmlspecialchars(GeneralUtility::fixed_lgd_cs($row['title'], $titleLen));
703 if (strlen(trim($row['nav_title'])) > 0) {
704 $title = '<span title="' . $GLOBALS['LANG']->sL('LLL:EXT:cms/locallang_tca.xlf:pages.nav_title', TRUE) . ' ' . htmlspecialchars(trim($row['nav_title'])) . '">' . $title . '</span>';
705 }
706 $title = strlen(trim($row['title'])) == 0 ? '<em>[' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.no_title', TRUE) . ']</em>' : $title;
707 }
708 return $title;
709 }
710
711 /**
712 * Returns the value for the image "title" attribute
713 *
714 * @param array $row The input row array (where the key "title" is used for the title)
715 * @return string The attribute value (is htmlspecialchared() already)
716 * @see wrapIcon()
717 * @todo Define visibility
718 */
719 public function getTitleAttrib($row) {
720 return htmlspecialchars($row['title']);
721 }
722
723 /**
724 * Returns the id from the record (typ. uid)
725 *
726 * @param array $row Record array
727 * @return integer The "uid" field value.
728 * @todo Define visibility
729 */
730 public function getId($row) {
731 return $row['uid'];
732 }
733
734 /**
735 * Returns jump-url parameter value.
736 *
737 * @param array $row The record array.
738 * @return string The jump-url parameter.
739 * @todo Define visibility
740 */
741 public function getJumpToParam($row) {
742 return $this->getId($row);
743 }
744
745 /********************************
746 *
747 * tree data buidling
748 *
749 ********************************/
750 /**
751 * Fetches the data for the tree
752 *
753 * @param integer $uid item id for which to select subitems (parent id)
754 * @param integer $depth Max depth (recursivity limit)
755 * @param string $depthData HTML-code prefix for recursive calls.
756 * @param string $blankLineCode ? (internal)
757 * @param string $subCSSclass CSS class to use for <td> sub-elements
758 * @return integer The count of items on the level
759 * @todo Define visibility
760 */
761 public function getTree($uid, $depth = 999, $depthData = '', $blankLineCode = '', $subCSSclass = '') {
762 // Buffer for id hierarchy is reset:
763 $this->buffer_idH = array();
764 // Init vars
765 $depth = (int)$depth;
766 $HTML = '';
767 $a = 0;
768 $res = $this->getDataInit($uid, $subCSSclass);
769 $c = $this->getDataCount($res);
770 $crazyRecursionLimiter = 999;
771 $idH = array();
772 // Traverse the records:
773 while ($crazyRecursionLimiter > 0 && ($row = $this->getDataNext($res, $subCSSclass))) {
774 $a++;
775 $crazyRecursionLimiter--;
776 $newID = $row['uid'];
777 if ($newID == 0) {
778 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);
779 }
780 // Reserve space.
781 $this->tree[] = array();
782 end($this->tree);
783 // Get the key for this space
784 $treeKey = key($this->tree);
785 $LN = $a == $c ? 'blank' : 'line';
786 // If records should be accumulated, do so
787 if ($this->setRecs) {
788 $this->recs[$row['uid']] = $row;
789 }
790 // Accumulate the id of the element in the internal arrays
791 $this->ids[] = ($idH[$row['uid']]['uid'] = $row['uid']);
792 $this->ids_hierarchy[$depth][] = $row['uid'];
793 $this->orig_ids_hierarchy[$depth][] = $row['_ORIG_uid'] ?: $row['uid'];
794 // Make a recursive call to the next level
795 $HTML_depthData = $depthData . '<img' . IconUtility::skinImg($this->backPath, ('gfx/ol/' . $LN . '.gif'), 'width="18" height="16"') . ' alt="" />';
796 if ($depth > 1 && $this->expandNext($newID) && !$row['php_tree_stop']) {
797 $nextCount = $this->getTree($newID, $depth - 1, $this->makeHTML ? $HTML_depthData : '', $blankLineCode . ',' . $LN, $row['_SUBCSSCLASS']);
798 if (count($this->buffer_idH)) {
799 $idH[$row['uid']]['subrow'] = $this->buffer_idH;
800 }
801 // Set "did expand" flag
802 $exp = 1;
803 } else {
804 $nextCount = $this->getCount($newID);
805 // Clear "did expand" flag
806 $exp = 0;
807 }
808 // Set HTML-icons, if any:
809 if ($this->makeHTML) {
810 $HTML = $depthData . $this->PMicon($row, $a, $c, $nextCount, $exp);
811 $HTML .= $this->wrapStop($this->getIcon($row), $row);
812 }
813 // Finally, add the row/HTML content to the ->tree array in the reserved key.
814 $this->tree[$treeKey] = array(
815 'row' => $row,
816 'HTML' => $HTML,
817 'HTML_depthData' => $this->makeHTML == 2 ? $HTML_depthData : '',
818 'invertedDepth' => $depth,
819 'blankLineCode' => $blankLineCode,
820 'bank' => $this->bank
821 );
822 }
823 $this->getDataFree($res);
824 $this->buffer_idH = $idH;
825 return $c;
826 }
827
828 /********************************
829 *
830 * Data handling
831 * Works with records and arrays
832 *
833 ********************************/
834 /**
835 * Returns the number of records having the parent id, $uid
836 *
837 * @param integer $uid Id to count subitems for
838 * @return integer
839 * @access private
840 * @todo Define visibility
841 */
842 public function getCount($uid) {
843 if (is_array($this->data)) {
844 $res = $this->getDataInit($uid);
845 return $this->getDataCount($res);
846 } else {
847 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);
848 }
849 }
850
851 /**
852 * Returns root record for uid (<=0)
853 *
854 * @param integer $uid uid, <= 0 (normally, this does not matter)
855 * @return array Array with title/uid keys with values of $this->title/0 (zero)
856 * @todo Define visibility
857 */
858 public function getRootRecord($uid) {
859 return array('title' => $this->title, 'uid' => 0);
860 }
861
862 /**
863 * Returns the record for a uid.
864 * For tables: Looks up the record in the database.
865 * For arrays: Returns the fake record for uid id.
866 *
867 * @param integer $uid UID to look up
868 * @return array The record
869 * @todo Define visibility
870 */
871 public function getRecord($uid) {
872 if (is_array($this->data)) {
873 return $this->dataLookup[$uid];
874 } else {
875 return BackendUtility::getRecordWSOL($this->table, $uid);
876 }
877 }
878
879 /**
880 * Getting the tree data: Selecting/Initializing data pointer to items for a certain parent id.
881 * For tables: This will make a database query to select all children to "parent"
882 * For arrays: This will return key to the ->dataLookup array
883 *
884 * @param integer $parentId parent item id
885 * @param string $subCSSclass Class for sub-elements.
886 * @return mixed Data handle (Tables: An sql-resource, arrays: A parentId integer. -1 is returned if there were NO subLevel.)
887 * @access private
888 * @todo Define visibility
889 */
890 public function getDataInit($parentId, $subCSSclass = '') {
891 if (is_array($this->data)) {
892 if (!is_array($this->dataLookup[$parentId][$this->subLevelID])) {
893 $parentId = -1;
894 } else {
895 reset($this->dataLookup[$parentId][$this->subLevelID]);
896 }
897 return $parentId;
898 } else {
899 $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);
900 return $res;
901 }
902 }
903
904 /**
905 * Getting the tree data: Counting elements in resource
906 *
907 * @param mixed $res Data handle
908 * @return integer number of items
909 * @access private
910 * @see getDataInit()
911 * @todo Define visibility
912 */
913 public function getDataCount(&$res) {
914 if (is_array($this->data)) {
915 return count($this->dataLookup[$res][$this->subLevelID]);
916 } else {
917 $c = $GLOBALS['TYPO3_DB']->sql_num_rows($res);
918 return $c;
919 }
920 }
921
922 /**
923 * Getting the tree data: next entry
924 *
925 * @param mixed $res Data handle
926 * @param string $subCSSclass CSS class for sub elements (workspace related)
927 * @return array item data array OR FALSE if end of elements.
928 * @access private
929 * @see getDataInit()
930 * @todo Define visibility
931 */
932 public function getDataNext(&$res, $subCSSclass = '') {
933 if (is_array($this->data)) {
934 if ($res < 0) {
935 $row = FALSE;
936 } else {
937 list(, $row) = each($this->dataLookup[$res][$this->subLevelID]);
938 // Passing on default <td> class for subelements:
939 if (is_array($row) && $subCSSclass !== '') {
940 $row['_CSSCLASS'] = ($row['_SUBCSSCLASS'] = $subCSSclass);
941 }
942 }
943 return $row;
944 } else {
945 while ($row = @$GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
946 BackendUtility::workspaceOL($this->table, $row, $this->BE_USER->workspace, TRUE);
947 if (is_array($row)) {
948 break;
949 }
950 }
951 // Passing on default <td> class for subelements:
952 if (is_array($row) && $subCSSclass !== '') {
953 if ($this->table === 'pages' && $this->highlightPagesWithVersions && !isset($row['_CSSCLASS']) && count(BackendUtility::countVersionsOfRecordsOnPage($this->BE_USER->workspace, $row['uid']))) {
954 $row['_CSSCLASS'] = 'ver-versions';
955 }
956 if (!isset($row['_CSSCLASS'])) {
957 $row['_CSSCLASS'] = $subCSSclass;
958 }
959 if (!isset($row['_SUBCSSCLASS'])) {
960 $row['_SUBCSSCLASS'] = $subCSSclass;
961 }
962 }
963 return $row;
964 }
965 }
966
967 /**
968 * Getting the tree data: frees data handle
969 *
970 * @param mixed $res Data handle
971 * @return void
972 * @access private
973 * @todo Define visibility
974 */
975 public function getDataFree(&$res) {
976 if (!is_array($this->data)) {
977 $GLOBALS['TYPO3_DB']->sql_free_result($res);
978 }
979 }
980
981 /**
982 * Used to initialize class with an array to browse.
983 * The array inputted will be traversed and an internal index for lookup is created.
984 * The keys of the input array are perceived as "uid"s of records which means that keys GLOBALLY must be unique like uids are.
985 * "uid" and "pid" "fakefields" are also set in each record.
986 * All other fields are optional.
987 *
988 * @param array $dataArr The input array, see examples below in this script.
989 * @param boolean $traverse Internal, for recursion.
990 * @param integer $pid Internal, for recursion.
991 * @return void
992 * @todo Define visibility
993 */
994 public function setDataFromArray(&$dataArr, $traverse = FALSE, $pid = 0) {
995 if (!$traverse) {
996 $this->data = &$dataArr;
997 $this->dataLookup = array();
998 // Add root
999 $this->dataLookup[0][$this->subLevelID] = &$dataArr;
1000 }
1001 foreach ($dataArr as $uid => $val) {
1002 $dataArr[$uid]['uid'] = $uid;
1003 $dataArr[$uid]['pid'] = $pid;
1004 // Gives quick access to id's
1005 $this->dataLookup[$uid] = &$dataArr[$uid];
1006 if (is_array($val[$this->subLevelID])) {
1007 $this->setDataFromArray($dataArr[$uid][$this->subLevelID], TRUE, $uid);
1008 }
1009 }
1010 }
1011
1012 /**
1013 * Sets the internal data arrays
1014 *
1015 * @param array $treeArr Content for $this->data
1016 * @param array $treeLookupArr Content for $this->dataLookup
1017 * @return void
1018 * @todo Define visibility
1019 */
1020 public function setDataFromTreeArray(&$treeArr, &$treeLookupArr) {
1021 $this->data = &$treeArr;
1022 $this->dataLookup = &$treeLookupArr;
1023 }
1024
1025 }