Passed
Push — master ( cf235e...a68ace )
by
unknown
16:24
created

ExportPageTreeView::PMicon()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 5
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\AbstractTreeView;
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 AbstractTreeView
30
{
31
    /**
32
     * Points to the current mountpoint key
33
     * @var int
34
     */
35
    public $bank = 0;
36
37
    /**
38
     * Holds (session stored) information about which items in the tree are unfolded and which are not.
39
     * @var array
40
     */
41
    public $stored = [];
42
43
    public function __construct()
44
    {
45
        parent::__construct();
46
        $this->init();
47
    }
48
49
    /**
50
     * Init function
51
     * REMEMBER to feed a $clause which will filter out non-readable pages!
52
     *
53
     * @param string $clause Part of where query which will filter out non-readable pages.
54
     * @param string $orderByFields Record ORDER BY field
55
     */
56
    public function init($clause = '', $orderByFields = '')
57
    {
58
        parent::init(' AND deleted=0 AND sys_language_uid=0 ' . $clause, $orderByFields ?: 'sorting');
59
    }
60
61
    /**
62
     * Creates title attribute content for pages.
63
     * Uses API function in \TYPO3\CMS\Backend\Utility\BackendUtility which will retrieve lots of useful information for pages.
64
     *
65
     * @param array $row The table row.
66
     * @return string
67
     */
68
    public function getTitleAttrib($row)
69
    {
70
        return BackendUtility::titleAttribForPages($row, '1=1 ' . $this->clause, false);
71
    }
72
73
    /**
74
     * Wrapping Plus/Minus icon, unused in Export Page Tree
75
     */
76
    public function PMicon($row, $a, $c, $nextCount, $isOpen)
77
    {
78
        return '';
79
    }
80
81
    /**
82
     * Tree rendering
83
     *
84
     * @param int $pid PID value
85
     * @param string $clause Additional where clause
86
     * @return array Array of tree elements
87
     */
88
    public function ext_tree($pid, $clause = '')
89
    {
90
        // Initialize:
91
        $this->init(' AND ' . $this->getBackendUser()->getPagePermsClause(Permission::PAGE_SHOW) . $clause);
92
        // Get stored tree structure:
93
        $this->stored = json_decode($this->getBackendUser()->uc['browseTrees']['browsePages'], true);
94
        $treeArr = [];
95
        $idx = 0;
96
        // Set first:
97
        $this->bank = $idx;
98
        $isOpen = $this->stored[$idx][$pid];
99
        // save ids
100
        $curIds = $this->ids;
101
        $this->reset();
102
        $this->ids = $curIds;
103
        if ($pid > 0) {
104
            $rootRec = BackendUtility::getRecordWSOL('pages', $pid);
105
            $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
106
            $firstHtml = $iconFactory->getIconForRecord('pages', $rootRec, Icon::SIZE_SMALL)->render();
107
        } else {
108
            $rootRec = [
109
                'title' => $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'],
110
                'uid' => 0
111
            ];
112
            $firstHtml = $this->getRootIcon($rootRec);
113
        }
114
        $this->tree[] = ['HTML' => $firstHtml, 'row' => $rootRec, 'hasSub' => $isOpen];
115
        if ($isOpen) {
116
            $this->getTree($pid);
117
            $idH = [];
118
            $idH[$pid]['uid'] = $pid;
119
            if (!empty($this->buffer_idH)) {
120
                $idH[$pid]['subrow'] = $this->buffer_idH;
121
            }
122
            $this->buffer_idH = $idH;
123
        }
124
        // Add tree:
125
        return array_merge($treeArr, $this->tree);
126
    }
127
128
    /**
129
     * Compiles the HTML code for displaying the structure found inside the ->tree array
130
     *
131
     * @param array|string $treeArr "tree-array" - if blank string, the internal ->tree array is used.
132
     * @return string The HTML code for the tree
133
     */
134
    public function printTree($treeArr = '')
135
    {
136
        $titleLen = (int)$this->getBackendUser()->uc['titleLen'];
137
        if (!is_array($treeArr)) {
138
            $treeArr = $this->tree;
139
        }
140
        $out = '';
141
        $closeDepth = [];
142
        foreach ($treeArr as $treeItem) {
143
            $classAttr = '';
144
            if ($treeItem['isFirst']) {
145
                $out .= '<ul class="list-tree">';
146
            }
147
148
            // Add CSS classes to the list item
149
            if ($treeItem['hasSub']) {
150
                $classAttr .= ' list-tree-control-open';
151
            }
152
153
            $idAttr = htmlspecialchars('pages' . $treeItem['row']['uid']);
154
            $out .= '
155
				<li id="' . $idAttr . '"' . ($classAttr ? ' class="' . trim($classAttr) . '"' : '') . '>
156
					<span class="list-tree-group">
157
						<span class="list-tree-icon">' . $treeItem['HTML'] . '</span>
158
						<span class="list-tree-title">' . $this->getTitleStr($treeItem['row'], $titleLen) . '</span>
159
					</span>';
160
161
            if (!$treeItem['hasSub']) {
162
                $out .= '</li>';
163
            }
164
165
            // We have to remember if this is the last one
166
            // on level X so the last child on level X+1 closes the <ul>-tag
167
            if ($treeItem['isLast']) {
168
                $closeDepth[$treeItem['invertedDepth']] = 1;
169
            }
170
            // If this is the last one and does not have subitems, we need to close
171
            // the tree as long as the upper levels have last items too
172
            if ($treeItem['isLast'] && !$treeItem['hasSub']) {
173
                for ($i = $treeItem['invertedDepth']; $closeDepth[$i] == 1; $i++) {
174
                    $closeDepth[$i] = 0;
175
                    $out .= '</ul></li>';
176
                }
177
            }
178
        }
179
        return '<ul class="list-tree list-tree-root list-tree-root-clean">' . $out . '</ul>';
180
    }
181
182
    /**
183
     * Returns TRUE/FALSE if the next level for $id should be expanded - based on
184
     * data in $this->stored[][] and ->expandAll flag.
185
     * Extending parent function
186
     *
187
     * @param int $id Record id/key
188
     * @return bool
189
     * @internal
190
     * @see \TYPO3\CMS\Backend\Tree\View\PageTreeView::expandNext()
191
     */
192
    public function expandNext($id)
193
    {
194
        return !empty($this->stored[$this->bank][$id]);
195
    }
196
}
197