Passed
Push — master ( d99b2c...0c4ec8 )
by
unknown
23:31
created

ExportPageTreeView::ext_tree()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 38
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 38
rs 9.488
c 0
b 0
f 0
cc 4
nc 6
nop 2
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace TYPO3\CMS\Impexp\View;
17
18
use TYPO3\CMS\Backend\Tree\View\BrowseTreeView;
19
use TYPO3\CMS\Backend\Utility\BackendUtility;
20
use TYPO3\CMS\Core\Imaging\Icon;
21
use TYPO3\CMS\Core\Imaging\IconFactory;
22
use TYPO3\CMS\Core\Type\Bitmask\Permission;
23
use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25
/**
26
 * Extension of the page tree class. Used to get the tree of pages to export.
27
 * @internal
28
 */
29
class ExportPageTreeView extends BrowseTreeView
30
{
31
    /**
32
     * Initialization
33
     */
34
    public function __construct()
35
    {
36
        parent::__construct();
37
        $this->init();
38
    }
39
40
    /**
41
     * Wrapping title from page tree.
42
     *
43
     * @param string $title Title to wrap
44
     * @return string Wrapped title
45
     * @internal
46
     */
47
    public function wrapTitle($title)
48
    {
49
        return trim($title) === '' ? '<em>[' . htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.no_title')) . ']</em>' : htmlspecialchars($title);
50
    }
51
52
    /**
53
     * Wrapping Plus/Minus icon, unused in Export Page Tree
54
     *
55
     * @param mixed $bMark (See parent class)
56
     * @param bool $isOpen
57
     * @return string
58
     */
59
    public function PM_ATagWrap($bMark = '', $isOpen = false)
60
    {
61
        return '';
62
    }
63
64
    /**
65
     * Tree rendering
66
     *
67
     * @param int $pid PID value
68
     * @param string $clause Additional where clause
69
     * @return array Array of tree elements
70
     */
71
    public function ext_tree($pid, $clause = '')
72
    {
73
        // Initialize:
74
        $this->init(' AND ' . $this->getBackendUser()->getPagePermsClause(Permission::PAGE_SHOW) . $clause);
75
        // Get stored tree structure:
76
        $this->stored = json_decode($this->getBackendUser()->uc['browseTrees']['browsePages'], true);
77
        $treeArr = [];
78
        $idx = 0;
79
        // Set first:
80
        $this->bank = $idx;
81
        $isOpen = $this->stored[$idx][$pid];
82
        // save ids
83
        $curIds = $this->ids;
84
        $this->reset();
85
        $this->ids = $curIds;
86
        if ($pid > 0) {
87
            $rootRec = BackendUtility::getRecordWSOL('pages', $pid);
88
            $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
89
            $firstHtml = $iconFactory->getIconForRecord('pages', $rootRec, Icon::SIZE_SMALL)->render();
90
        } else {
91
            $rootRec = [
92
                'title' => $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'],
93
                'uid' => 0
94
            ];
95
            $firstHtml = $this->getRootIcon($rootRec);
96
        }
97
        $this->tree[] = ['HTML' => $firstHtml, 'row' => $rootRec, 'hasSub' => $isOpen];
98
        if ($isOpen) {
99
            $this->getTree($pid);
100
            $idH = [];
101
            $idH[$pid]['uid'] = $pid;
102
            if (!empty($this->buffer_idH)) {
103
                $idH[$pid]['subrow'] = $this->buffer_idH;
104
            }
105
            $this->buffer_idH = $idH;
106
        }
107
        // Add tree:
108
        return array_merge($treeArr, $this->tree);
109
    }
110
111
    /**
112
     * Compiles the HTML code for displaying the structure found inside the ->tree array
113
     *
114
     * @param array|string $treeArr "tree-array" - if blank string, the internal ->tree array is used.
115
     * @return string The HTML code for the tree
116
     */
117
    public function printTree($treeArr = '')
118
    {
119
        $titleLen = (int)$this->getBackendUser()->uc['titleLen'];
120
        if (!is_array($treeArr)) {
121
            $treeArr = $this->tree;
122
        }
123
        $out = '';
124
        $closeDepth = [];
125
        foreach ($treeArr as $treeItem) {
126
            $classAttr = '';
127
            if ($treeItem['isFirst']) {
128
                $out .= '<ul class="list-tree">';
129
            }
130
131
            // Add CSS classes to the list item
132
            if ($treeItem['hasSub']) {
133
                $classAttr .= ' list-tree-control-open';
134
            }
135
136
            $idAttr = htmlspecialchars('pages' . $treeItem['row']['uid'] . '_' . $treeItem['bank']);
137
            $out .= '
138
				<li id="' . $idAttr . '"' . ($classAttr ? ' class="' . trim($classAttr) . '"' : '') . '>
139
					<span class="list-tree-group">
140
						<span class="list-tree-icon">' . $treeItem['HTML'] . '</span>
141
						<span class="list-tree-title">' . $this->wrapTitle($this->getTitleStr($treeItem['row'], $titleLen)) . '</span>
142
					</span>';
143
144
            if (!$treeItem['hasSub']) {
145
                $out .= '</li>';
146
            }
147
148
            // We have to remember if this is the last one
149
            // on level X so the last child on level X+1 closes the <ul>-tag
150
            if ($treeItem['isLast']) {
151
                $closeDepth[$treeItem['invertedDepth']] = 1;
152
            }
153
            // If this is the last one and does not have subitems, we need to close
154
            // the tree as long as the upper levels have last items too
155
            if ($treeItem['isLast'] && !$treeItem['hasSub']) {
156
                for ($i = $treeItem['invertedDepth']; $closeDepth[$i] == 1; $i++) {
157
                    $closeDepth[$i] = 0;
158
                    $out .= '</ul></li>';
159
                }
160
            }
161
        }
162
        $out = '<ul class="list-tree list-tree-root list-tree-root-clean">' . $out . '</ul>';
163
        return $out;
164
    }
165
}
166