[BUGFIX] Show folder icons in ElementBrowser file tree
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Tree / View / ElementBrowserFolderTreeView.php
1 <?php
2 namespace TYPO3\CMS\Backend\Tree\View;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Resource\Folder;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Recordlist\Tree\View\LinkParameterProviderInterface;
20
21 /**
22 * Base extension class which generates the folder tree.
23 * Used directly by the RTE.
24 * also used for the linkpicker on files
25 *
26 * Browsable folder tree, used in Element Browser and RTE (for which it will be extended)
27 * previously located inside typo3/class.browse_links.php
28 */
29 class ElementBrowserFolderTreeView extends FolderTreeView
30 {
31 /**
32 * @var string
33 */
34 public $ext_IconMode = 'titlelink';
35
36 /**
37 * @var LinkParameterProviderInterface
38 */
39 protected $linkParameterProvider;
40
41 /**
42 * @param LinkParameterProviderInterface $linkParameterProvider
43 *
44 * @return void
45 */
46 public function setLinkParameterProvider(LinkParameterProviderInterface $linkParameterProvider)
47 {
48 $this->linkParameterProvider = $linkParameterProvider;
49 $this->thisScript = $linkParameterProvider->getScriptUrl();
50 }
51
52 /**
53 * Wrapping the folder icon
54 *
55 * @param string $icon The image tag for the icon
56 * @param Folder $folderObject The row for the current element
57 *
58 * @return string The processed icon input value.
59 * @internal
60 */
61 public function wrapIcon($icon, $folderObject)
62 {
63 // Add title attribute to input icon tag
64 $theFolderIcon = '';
65
66 // Wrap icon in link (in ElementBrowser only the "titlelink" is used).
67 if ($this->ext_IconMode === 'titlelink' && $this->ext_isLinkable($folderObject)) {
68 $parameters = GeneralUtility::implodeArrayForUrl('', $this->linkParameterProvider->getUrlParameters(['identifier' => $folderObject->getCombinedIdentifier()]));
69 $aOnClick = 'return jumpToUrl(' . GeneralUtility::quoteJSvalue($this->getThisScript() . ltrim($parameters, '&')) . ');';
70 $theFolderIcon = '<a href="#" onclick="' . htmlspecialchars($aOnClick) . '">' . $icon . '</a>';
71 }
72
73 return $theFolderIcon;
74 }
75
76 /**
77 * Wrapping the title in a link, if applicable.
78 *
79 * @param string $title Title, ready for output.
80 * @param Folder $folderObject The record
81 * @param int $bank Bank pointer (which mount point number)
82 * @return string Wrapping title string.
83 */
84 public function wrapTitle($title, $folderObject, $bank = 0)
85 {
86 if ($this->ext_isLinkable($folderObject)) {
87 $parameters = GeneralUtility::implodeArrayForUrl('', $this->linkParameterProvider->getUrlParameters(['identifier' => $folderObject->getCombinedIdentifier()]));
88 return '<a href="#" onclick="return jumpToUrl(' . htmlspecialchars(GeneralUtility::quoteJSvalue($this->getThisScript() . ltrim($parameters, '&'))) . ');">' . $title . '</a>';
89 } else {
90 return '<span class="text-muted">' . $title . '</span>';
91 }
92 }
93
94 /**
95 * Returns TRUE if the input "record" contains a folder which can be linked.
96 *
97 * @param Folder $folderObject Object with information about the folder element. Contains keys like title, uid, path, _title
98 * @return bool TRUE is returned if the path is found in the web-part of the server and is NOT a recycler or temp folder AND if ->ext_noTempRecyclerDirs is not set.
99 */
100 public function ext_isLinkable(Folder $folderObject)
101 {
102 $identifier = $folderObject->getIdentifier();
103 return !$this->ext_noTempRecyclerDirs || substr($identifier, -7) !== '_temp_/' && substr($identifier, -11) !== '_recycler_/';
104 }
105
106 /**
107 * @param string $cmd
108 * @param bool $isOpen
109 * @return string
110 */
111 protected function renderPMIconAndLink($cmd, $isOpen)
112 {
113 if (get_class($this) === __CLASS__) {
114 return $this->PMiconATagWrap('', $cmd, !$isOpen);
115 }
116 return parent::renderPMIconAndLink($cmd, $isOpen);
117 }
118
119 /**
120 * Wrap the plus/minus icon in a link
121 *
122 * @param string $icon HTML string to wrap, probably an image tag.
123 * @param string $cmd Command for 'PM' get var
124 * @param bool|string $bMark If set, the link will have an anchor point (=$bMark) and a name attribute (=$bMark)
125 * @param bool $isOpen check if the item has children
126 * @return string Link-wrapped input string
127 * @access private
128 */
129 public function PM_ATagWrap($icon, $cmd, $bMark = '', $isOpen = false)
130 {
131 $anchor = $bMark ? '#' . $bMark : '';
132 $name = $bMark ? ' name=' . $bMark : '';
133 $urlParameters = $this->linkParameterProvider->getUrlParameters([]);
134 $urlParameters['PM'] = $cmd;
135 $aOnClick = 'return jumpToUrl(' . GeneralUtility::quoteJSvalue($this->getThisScript() . ltrim(GeneralUtility::implodeArrayForUrl('', $urlParameters), '&')) . ',' . GeneralUtility::quoteJSvalue($anchor) . ');';
136 return '<a href="#"' . htmlspecialchars($name) . ' onclick="' . htmlspecialchars($aOnClick) . '">' . $icon . '</a>';
137 }
138
139 /**
140 * Wrap the plus/minus icon in a link
141 *
142 * @param string $icon HTML string to wrap, probably an image tag.
143 * @param string $cmd Command for 'PM' get var
144 * @param bool $isExpand Whether to be expanded
145 * @return string Link-wrapped input string
146 * @internal
147 */
148 public function PMiconATagWrap($icon, $cmd, $isExpand = true)
149 {
150 if (empty($this->scope)) {
151 $this->scope = array(
152 'class' => get_class($this),
153 'script' => $this->thisScript,
154 'ext_noTempRecyclerDirs' => $this->ext_noTempRecyclerDirs,
155 'browser' => $this->linkParameterProvider->getUrlParameters([]),
156 );
157 }
158
159 return parent::PMiconATagWrap($icon, $cmd, $isExpand);
160 }
161 }