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

ElementBrowserPageTreeView::wrapIcon()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
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\Backend\Tree\View;
17
18
use TYPO3\CMS\Core\Domain\Repository\PageRepository;
19
use TYPO3\CMS\Core\LinkHandling\LinkService;
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
use TYPO3\CMS\Core\Utility\HttpUtility;
22
use TYPO3\CMS\Recordlist\Tree\View\LinkParameterProviderInterface;
23
24
/**
25
 * Class which generates the selectable page tree
26
 *
27
 * Browsable tree, used in PagePositionMaps (move elements), the Link Wizard and the Database Browser (for which it will be extended)
28
 */
29
class ElementBrowserPageTreeView extends BrowseTreeView
30
{
31
    /**
32
     * @var LinkParameterProviderInterface
33
     */
34
    protected $linkParameterProvider;
35
36
    /**
37
     * Constructor. Just calling init()
38
     */
39
    public function __construct()
40
    {
41
        parent::__construct();
42
        $this->init();
43
        $this->clause = ' AND doktype <> ' . PageRepository::DOKTYPE_RECYCLER . $this->clause;
44
    }
45
46
    /**
47
     * @param LinkParameterProviderInterface $linkParameterProvider
48
     */
49
    public function setLinkParameterProvider(LinkParameterProviderInterface $linkParameterProvider)
50
    {
51
        $this->linkParameterProvider = $linkParameterProvider;
52
        $this->thisScript = $linkParameterProvider->getScriptUrl();
53
    }
54
55
    /**
56
     * Wrapping the title in a link, if applicable.
57
     *
58
     * @param string $title Title, (must be ready for output, that means it must be htmlspecialchars()'ed).
59
     * @param array $v The record
60
     * @return string Wrapping title string.
61
     */
62
    public function wrapTitle($title, $v)
63
    {
64
        if ($this->ext_isLinkable($v['doktype'], $v['uid'])) {
65
            $url = GeneralUtility::makeInstance(LinkService::class)->asString(['type' => LinkService::TYPE_PAGE, 'pageuid' => (int)$v['uid']]);
66
            return '<span class="list-tree-title"><a href="' . htmlspecialchars($url) . '" class="t3js-pageLink">' . $title . '</a></span>';
67
        }
68
        return '<span class="list-tree-title text-muted">' . $title . '</span>';
69
    }
70
71
    /**
72
     * Create the page navigation tree in HTML
73
     *
74
     * @param array|string $treeArr Tree array
75
     * @return string HTML output.
76
     */
77
    public function printTree($treeArr = '')
78
    {
79
        $titleLen = (int)$this->getBackendUser()->uc['titleLen'];
80
        if (!is_array($treeArr)) {
81
            $treeArr = $this->tree;
82
        }
83
        $out = '';
84
        // We need to count the opened <ul>'s every time we dig into another level,
85
        // so we know how many we have to close when all children are done rendering
86
        $closeDepth = [];
87
        foreach ($treeArr as $treeItem) {
88
            $classAttr = $treeItem['row']['_CSSCLASS'];
89
            if ($treeItem['isFirst']) {
90
                $out .= '<ul class="list-tree">';
91
            }
92
93
            // Add CSS classes to the list item
94
            if ($treeItem['hasSub']) {
95
                $classAttr .= ' list-tree-control-open';
96
            }
97
98
            $selected = '';
99
            if ($this->linkParameterProvider->isCurrentlySelectedItem(['pid' => (int)$treeItem['row']['uid']])) {
100
                $selected = ' bg-success';
101
                $classAttr .= ' active';
102
            }
103
            $urlParameters = $this->linkParameterProvider->getUrlParameters(['pid' => (int)$treeItem['row']['uid']]);
104
            $cEbullet = $this->ext_isLinkable($treeItem['row']['doktype'], $treeItem['row']['uid'])
105
                ? '<a href="' . htmlspecialchars($this->getThisScript() . HttpUtility::buildQueryString($urlParameters)) . '" class="list-tree-show"><i class="fa fa-caret-square-o-right"></i></a>'
106
                : '';
107
            $out .= '
108
				<li' . ($classAttr ? ' class="' . trim($classAttr) . '"' : '') . '>
109
					<span class="list-tree-group' . $selected . '">
110
						' . $cEbullet . $treeItem['HTML'] . $this->wrapTitle($this->getTitleStr($treeItem['row'], $titleLen), $treeItem['row']) . '
111
					</span>
112
				';
113
            if (!$treeItem['hasSub']) {
114
                $out .= '</li>';
115
            }
116
117
            // We have to remember if this is the last one
118
            // on level X so the last child on level X+1 closes the <ul>-tag
119
            if ($treeItem['isLast']) {
120
                $closeDepth[$treeItem['invertedDepth']] = 1;
121
            }
122
            // If this is the last one and does not have subitems, we need to close
123
            // the tree as long as the upper levels have last items too
124
            if ($treeItem['isLast'] && !$treeItem['hasSub']) {
125
                for ($i = $treeItem['invertedDepth']; $closeDepth[$i] == 1; $i++) {
126
                    $closeDepth[$i] = 0;
127
                    $out .= '</ul></li>';
128
                }
129
            }
130
        }
131
        return '<ul class="list-tree list-tree-root">' . $out . '</ul>';
132
    }
133
134
    /**
135
     * Returns TRUE if a doktype can be linked.
136
     *
137
     * @param int $doktype Doktype value to test
138
     * @param int $uid uid to test.
139
     * @return bool
140
     */
141
    public function ext_isLinkable($doktype, $uid)
142
    {
143
        return $uid && $doktype < PageRepository::DOKTYPE_SPACER;
144
    }
145
146
    /**
147
     * Wrap the plus/minus icon in a link
148
     *
149
     * @param string $bMark If set, the link will have a name attribute (=$bMark)
150
     * @param bool $isOpen
151
     * @return string Link-wrapped input string
152
     */
153
    public function PM_ATagWrap($bMark = '', $isOpen = false)
154
    {
155
        $name = $bMark ? ' name=' . $bMark : '';
156
        $urlParameters = $this->linkParameterProvider->getUrlParameters([]);
157
        return '<a class="list-tree-control ' . ($isOpen ? 'list-tree-control-open' : 'list-tree-control-closed')
158
            . '" href="' . htmlspecialchars($this->getThisScript() . HttpUtility::buildQueryString($urlParameters)) . '"' . htmlspecialchars($name) . '><i class="fa"></i></a>';
159
    }
160
}
161