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