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