Revert "[TASK] Avoid slow array functions in loops"
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / View / PageTreeView.php
1 <?php
2 namespace TYPO3\CMS\Backend\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\Backend\Tree\View\BrowseTreeView;
18 use TYPO3\CMS\Backend\Utility\BackendUtility;
19 use TYPO3\CMS\Core\Imaging\Icon;
20 use TYPO3\CMS\Core\Imaging\IconFactory;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22
23 /**
24 * Browse pages in Web module
25 * @internal This class is a TYPO3 Backend implementation and is not considered part of the Public TYPO3 API.
26 */
27 class PageTreeView extends BrowseTreeView
28 {
29 /**
30 * @var bool
31 */
32 public $ext_showPageId = false;
33
34 /**
35 * Indicates, whether the ajax call was successful, i.e. the requested page has been found
36 *
37 * @var bool
38 */
39 public $ajaxStatus = false;
40
41 /**
42 * Calls init functions
43 */
44 public function __construct()
45 {
46 parent::__construct();
47 $this->init();
48 }
49
50 /**
51 * Wrapping icon in browse tree
52 *
53 * @param string $thePageIcon Icon IMG code
54 * @param array $row Data row for element.
55 * @return string Page icon
56 */
57 public function wrapIcon($thePageIcon, $row)
58 {
59 /** @var IconFactory $iconFactory */
60 $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
61 // If the record is locked, present a warning sign.
62 if ($lockInfo = BackendUtility::isRecordLocked('pages', $row['uid'])) {
63 $aOnClick = 'alert(' . GeneralUtility::quoteJSvalue($lockInfo['msg']) . ');return false;';
64 $lockIcon = '<a href="#" onclick="' . htmlspecialchars($aOnClick) . '">'
65 . '<span title="' . htmlspecialchars($lockInfo['msg']) . '">' . $iconFactory->getIcon('warning-in-use', Icon::SIZE_SMALL)->render() . '</span></a>';
66 } else {
67 $lockIcon = '';
68 }
69 // Wrap icon in click-menu link.
70 if (!$this->ext_IconMode) {
71 $thePageIcon = BackendUtility::wrapClickMenuOnIcon($thePageIcon, 'pages', $row['uid'], 'tree');
72 } elseif ($this->ext_IconMode === 'titlelink') {
73 $aOnClick = 'return jumpTo(' . GeneralUtility::quoteJSvalue($this->getJumpToParam($row)) . ',this,' . GeneralUtility::quoteJSvalue($this->treeName) . ');';
74 $thePageIcon = '<a href="#" onclick="' . htmlspecialchars($aOnClick) . '">' . $thePageIcon . '</a>';
75 }
76 // Wrap icon in a drag/drop span.
77 $dragDropIcon = '<span class="list-tree-icon dragIcon" id="dragIconID_' . $row['uid'] . '">' . $thePageIcon . '</span> ';
78 // Add Page ID:
79 $pageIdStr = '';
80 if ($this->ext_showPageId) {
81 $pageIdStr = '<span class="dragId">[' . $row['uid'] . ']</span> ';
82 }
83 // Call stats information hook
84 $stat = '';
85 $_params = ['pages', $row['uid']];
86 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['recStatInfoHooks'] ?? [] as $_funcRef) {
87 $stat .= GeneralUtility::callUserFunction($_funcRef, $_params, $this);
88 }
89 return $dragDropIcon . $lockIcon . $pageIdStr . $stat;
90 }
91
92 /**
93 * Wrapping $title in a-tags.
94 *
95 * @param string $title Title string
96 * @param string $row Item record
97 * @param int $bank Bank pointer (which mount point number)
98 * @return string
99 * @internal
100 */
101 public function wrapTitle($title, $row, $bank = 0)
102 {
103 // Hook for overriding the page title
104
105 $_params = ['title' => &$title, 'row' => &$row];
106 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/class.webpagetree.php']['pageTitleOverlay'] ?? [] as $_funcRef) {
107 GeneralUtility::callUserFunction($_funcRef, $_params, $this);
108 }
109
110 $aOnClick = 'return jumpTo(' . GeneralUtility::quoteJSvalue($this->getJumpToParam($row)) . ',this,' . GeneralUtility::quoteJSvalue($this->domIdPrefix . $this->getId($row)) . ',' . $bank . ');';
111 $clickMenuParts = BackendUtility::wrapClickMenuOnIcon('', 'pages', $row['uid'], 'tree', '', '', true);
112
113 $thePageTitle = '<a href="#" onclick="' . htmlspecialchars($aOnClick) . '"' . GeneralUtility::implodeAttributes($clickMenuParts) . '>' . $title . '</a>';
114 // Wrap title in a drag/drop span.
115 return '<span class="list-tree-title dragTitle" id="dragTitleID_' . $row['uid'] . '">' . $thePageTitle . '</span>';
116 }
117
118 /**
119 * Compiles the HTML code for displaying the structure found inside the ->tree array
120 *
121 * @param array|string $treeArr "tree-array" - if blank string, the internal ->tree array is used.
122 * @return string The HTML code for the tree
123 */
124 public function printTree($treeArr = '')
125 {
126 $titleLen = (int)$this->BE_USER->uc['titleLen'];
127 if (!is_array($treeArr)) {
128 $treeArr = $this->tree;
129 }
130 $out = '<ul class="list-tree list-tree-root">';
131 // -- evaluate AJAX request
132 // IE takes anchor as parameter
133 $PM = GeneralUtility::_GP('PM');
134 if (($PMpos = strpos($PM, '#')) !== false) {
135 $PM = substr($PM, 0, $PMpos);
136 }
137 $PM = explode('_', $PM);
138
139 $doCollapse = false;
140 $doExpand = false;
141 $expandedPageUid = null;
142 $collapsedPageUid = null;
143 if (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_AJAX && is_array($PM) && count($PM) === 4 && $PM[2] != 0) {
144 if ($PM[1]) {
145 $expandedPageUid = $PM[2];
146 $doExpand = true;
147 } else {
148 $collapsedPageUid = $PM[2];
149 $doCollapse = true;
150 }
151 }
152 // We need to count the opened <ul>'s every time we dig into another level,
153 // so we know how many we have to close when all children are done rendering
154 $closeDepth = [];
155 $ajaxOutput = '';
156 $invertedDepthOfAjaxRequestedItem = 0;
157 foreach ($treeArr as $k => $treeItem) {
158 $classAttr = $treeItem['row']['_CSSCLASS'];
159 $uid = $treeItem['row']['uid'];
160 $idAttr = htmlspecialchars($this->domIdPrefix . $this->getId($treeItem['row']) . '_' . $treeItem['bank']);
161 $itemHTML = '';
162 // If this item is the start of a new level,
163 // then a new level <ul> is needed, but not in ajax mode
164 if ($treeItem['isFirst'] && !$doCollapse && (!$doExpand || (int)$expandedPageUid !== (int)$uid)) {
165 $itemHTML = '<ul class="list-tree">';
166 }
167
168 // Add CSS classes to the list item
169 if ($treeItem['hasSub']) {
170 $classAttr .= ' list-tree-control-open';
171 }
172 $itemHTML .= '<li id="' . $idAttr . '" ' . ($classAttr ? ' class="' . trim($classAttr) . '"' : '')
173 . '><span class="list-tree-group">' . $treeItem['HTML']
174 . $this->wrapTitle($this->getTitleStr($treeItem['row'], $titleLen), $treeItem['row'], $treeItem['bank']) . '</span>';
175 if (!$treeItem['hasSub']) {
176 $itemHTML .= '</li>';
177 }
178
179 // We have to remember if this is the last one
180 // on level X so the last child on level X+1 closes the <ul>-tag
181 if ($treeItem['isLast'] && !($doExpand && $expandedPageUid == $uid)) {
182 $closeDepth[$treeItem['invertedDepth']] = 1;
183 }
184 // If this is the last one and does not have subitems, we need to close
185 // the tree as long as the upper levels have last items too
186 if ($treeItem['isLast'] && !$treeItem['hasSub'] && !$doCollapse && !($doExpand && $expandedPageUid == $uid)) {
187 for ($i = $treeItem['invertedDepth']; $closeDepth[$i] == 1; $i++) {
188 $closeDepth[$i] = 0;
189 $itemHTML .= '</ul></li>';
190 }
191 }
192 // Ajax request: collapse
193 if ($doCollapse && (int)$collapsedPageUid === (int)$uid) {
194 $this->ajaxStatus = true;
195 return $itemHTML;
196 }
197 // ajax request: expand
198 if ($doExpand && (int)$expandedPageUid === (int)$uid) {
199 $ajaxOutput .= $itemHTML;
200 $invertedDepthOfAjaxRequestedItem = $treeItem['invertedDepth'];
201 } elseif ($invertedDepthOfAjaxRequestedItem) {
202 if ($treeItem['invertedDepth'] < $invertedDepthOfAjaxRequestedItem) {
203 $ajaxOutput .= $itemHTML;
204 } else {
205 $this->ajaxStatus = true;
206 return $ajaxOutput;
207 }
208 }
209 $out .= $itemHTML;
210 }
211 if ($ajaxOutput) {
212 $this->ajaxStatus = true;
213 return $ajaxOutput;
214 }
215 // Finally close the first ul
216 $out .= '</ul>';
217 return $out;
218 }
219
220 /**
221 * Generate the plus/minus icon for the browsable tree.
222 *
223 * @param array $row Record for the entry
224 * @param int $a The current entry number
225 * @param int $c The total number of entries. If equal to $a, a "bottom" element is returned.
226 * @param int $nextCount The number of sub-elements to the current element.
227 * @param bool $exp The element was expanded to render subelements if this flag is set.
228 * @return string Image tag with the plus/minus icon.
229 * @internal
230 * @see \TYPO3\CMS\Backend\Tree\View\PageTreeView::PMicon()
231 */
232 public function PMicon($row, $a, $c, $nextCount, $exp)
233 {
234 $icon = '';
235 if ($nextCount) {
236 $cmd = $this->bank . '_' . ($exp ? '0_' : '1_') . $row['uid'] . '_' . $this->treeName;
237 $icon = $this->PMiconATagWrap($icon, $cmd, !$exp);
238 }
239 return $icon;
240 }
241
242 /**
243 * Wrap the plus/minus icon in a link
244 *
245 * @param string $icon HTML string to wrap, probably an image tag.
246 * @param string $cmd Command for 'PM' get var
247 * @param bool $isExpand Link-wrapped input string
248 * @return string
249 * @internal
250 */
251 public function PMiconATagWrap($icon, $cmd, $isExpand = true)
252 {
253 if ($this->thisScript) {
254 // Activate dynamic ajax-based tree
255 $js = htmlspecialchars('Tree.load(' . GeneralUtility::quoteJSvalue($cmd) . ', ' . (int)$isExpand . ', this);');
256 return '<a class="list-tree-control' . (!$isExpand ? ' list-tree-control-open' : ' list-tree-control-closed') . '" onclick="' . $js . '"><i class="fa"></i></a>';
257 }
258 return $icon;
259 }
260
261 /**
262 * Will create and return the HTML code for a browsable tree
263 * Is based on the mounts found in the internal array ->MOUNTS (set in the constructor)
264 *
265 * @return string HTML code for the browsable tree
266 */
267 public function getBrowsableTree()
268 {
269 // Get stored tree structure AND updating it if needed according to incoming PM GET var.
270 $this->initializePositionSaving();
271 // Init done:
272 $treeArr = [];
273 // Traverse mounts:
274 $firstHtml = '';
275 foreach ($this->MOUNTS as $idx => $uid) {
276 // Set first:
277 $this->bank = $idx;
278 $isOpen = $this->stored[$idx][$uid] || $this->expandFirst || $uid === '0';
279 // Save ids while resetting everything else.
280 $curIds = $this->ids;
281 $this->reset();
282 $this->ids = $curIds;
283 // Only, if not for uid 0
284 if ($uid) {
285 // Set PM icon for root of mount:
286 $cmd = $this->bank . '_' . ($isOpen ? '0_' : '1_') . $uid . '_' . $this->treeName;
287 $firstHtml = '<a class="list-tree-control list-tree-control-' . ($isOpen ? 'open' : 'closed')
288 . '" href="' . htmlspecialchars($this->getThisScript() . 'PM=' . $cmd) . '"><i class="fa"></i></a>';
289 }
290 // Preparing rootRec for the mount
291 if ($uid) {
292 $rootRec = $this->getRecord($uid);
293 $firstHtml .= $this->getIcon($rootRec);
294 } else {
295 // Artificial record for the tree root, id=0
296 $rootRec = $this->getRootRecord();
297 $firstHtml .= $this->getRootIcon($rootRec);
298 }
299 if (is_array($rootRec)) {
300 // In case it was swapped inside getRecord due to workspaces.
301 $uid = $rootRec['uid'];
302 // Add the root of the mount to ->tree
303 $this->tree[] = ['HTML' => $firstHtml, 'row' => $rootRec, 'bank' => $this->bank, 'hasSub' => true, 'invertedDepth' => 1000];
304 // If the mount is expanded, go down:
305 if ($isOpen) {
306 // Set depth:
307 if ($this->addSelfId) {
308 $this->ids[] = $uid;
309 }
310 $this->getTree($uid);
311 }
312 // Add tree:
313 $treeArr = array_merge($treeArr, $this->tree);
314 }
315 }
316 return $this->printTree($treeArr);
317 }
318 }