‪TYPO3CMS  11.5
ExportPageTreeView.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
22 use TYPO3\CMS\Backend\Utility\BackendUtility;
27 
34 {
39  protected ‪$expandAll = false;
40 
45  public ‪$bank = 0;
46 
51  public ‪$stored = [];
52 
53  public function ‪__construct()
54  {
55  parent::__construct();
56  $this->‪init();
57  }
58 
66  public function ‪init(‪$clause = '', ‪$orderByFields = '')
67  {
68  parent::init(' AND deleted=0 AND sys_language_uid=0 ' . ‪$clause, ‪$orderByFields ?: 'sorting');
69  }
70 
78  public function ‪getTitleAttrib($row)
79  {
80  return BackendUtility::titleAttribForPages($row, '1=1 ' . $this->clause, false);
81  }
82 
86  public function ‪PMicon($row, $a, $c, $nextCount, $isOpen)
87  {
88  return '';
89  }
90 
97  public function ‪buildTreeByLevels(int $pid, int $levels): void
98  {
99  $this->expandAll = true;
100  $checkSub = $levels > 0;
101 
102  $this->‪buildTree($pid, $levels, $checkSub);
103  }
104 
110  public function ‪buildTreeByExpandedState(int $pid): void
111  {
112  $this->‪syncPageTreeState();
113 
114  $this->expandAll = false;
115  if ($pid > 0) {
116  $checkSub = (bool)($this->stored[$this->bank][$pid] ?? false);
117  } else {
118  $checkSub = true;
119  }
120 
121  $this->‪buildTree($pid, ‪Export::LEVELS_INFINITE, $checkSub);
122  }
123 
131  protected function ‪buildTree(int $pid, int $levels, bool $checkSub): void
132  {
133  $this->‪reset();
134 
135  // Root page
136  if ($pid > 0) {
137  $rootRecord = BackendUtility::getRecordWSOL('pages', $pid);
138  $rootHtml = $this->‪getPageIcon($rootRecord);
139  } else {
140  $rootRecord = [
141  'title' => ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'],
142  'uid' => 0,
143  ];
144  $rootHtml = $this->‪getRootIcon($rootRecord);
145  }
146 
147  $this->tree[] = [
148  'HTML' => $rootHtml,
149  'row' => $rootRecord,
150  'hasSub' => $checkSub,
151  'bank' => ‪$this->bank,
152  ];
153 
154  // Subtree
155  if ($checkSub) {
156  $this->‪getTree($pid, $levels);
157  }
158 
159  $idH = [];
160  $idH[$pid]['uid'] = $pid;
161  if (!empty($this->buffer_idH)) {
162  $idH[$pid]['subrow'] = ‪$this->buffer_idH;
163  }
164  $this->buffer_idH = $idH;
165 
166  // Check if root page has subtree
167  if (empty($this->buffer_idH)) {
168  $this->tree[0]['hasSub'] = false;
169  }
170  }
171 
175  protected function ‪syncPageTreeState(): void
176  {
177  $backendUserConfiguration = GeneralUtility::makeInstance(BackendUserConfiguration::class);
178  $pageTreeState = $backendUserConfiguration->get('BackendComponents.States.Pagetree');
179  if (is_object($pageTreeState) && is_object($pageTreeState->stateHash)) {
180  $pageTreeState = (array)$pageTreeState->stateHash;
181  } else {
182  $stateHash = $pageTreeState['stateHash'] ?? [];
183  $pageTreeState = is_array($stateHash) ? $stateHash : [];
184  }
185 
186  $this->stored = [];
187  foreach ($pageTreeState as $identifier => $isExpanded) {
188  list(‪$bank, $pageId) = explode('_', $identifier);
189  $this->stored[‪$bank][$pageId] = $isExpanded;
190  }
191  }
192 
199  public function ‪printTree($treeArr = '')
200  {
201  $titleLen = (int)$this->‪getBackendUser()->uc['titleLen'];
202  if (!is_array($treeArr)) {
203  $treeArr = ‪$this->tree;
204  }
205  $out = '';
206  $closeDepth = [];
207  foreach ($treeArr as $treeItem) {
208  $classAttr = '';
209  if ($treeItem['isFirst'] ?? false) {
210  $out .= '<ul class="list-tree">';
211  }
212 
213  // Add CSS classes to the list item
214  if ($treeItem['hasSub'] ?? false) {
215  $classAttr .= ' list-tree-control-open';
216  }
217 
218  $idAttr = htmlspecialchars('pages' . $treeItem['row']['uid']);
219  $out .= '
220  <li id="' . $idAttr . '"' . ($classAttr ? ' class="' . trim($classAttr) . '"' : '') . '>
221  <span class="list-tree-group">
222  <span class="list-tree-icon">' . $treeItem['HTML'] . '</span>
223  <span class="list-tree-title">' . $this->‪getTitleStr($treeItem['row'], $titleLen) . '</span>
224  </span>';
225 
226  if (!($treeItem['hasSub'] ?? false)) {
227  $out .= '</li>';
228  }
229 
230  // We have to remember if this is the last one
231  // on level X so the last child on level X+1 closes the <ul>-tag
232  if ($treeItem['isLast'] ?? false) {
233  $closeDepth[$treeItem['invertedDepth']] = 1;
234  }
235  // If this is the last one and does not have subitems, we need to close
236  // the tree as long as the upper levels have last items too
237  if (($treeItem['isLast'] ?? false) && !($treeItem['hasSub'] ?? false)) {
238  for ($i = $treeItem['invertedDepth']; ($closeDepth[$i] ?? 0) == 1; $i++) {
239  $closeDepth[$i] = 0;
240  $out .= '</ul></li>';
241  }
242  }
243  }
244  return '<ul class="list-tree list-tree-root list-tree-root-clean">' . $out . '</ul>';
245  }
246 
257  public function ‪expandNext($id)
258  {
259  return $this->expandAll || !empty($this->stored[$this->bank][$id]);
260  }
261 
268  protected function ‪getPageIcon(array $row): string
269  {
270  $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
271  return $iconFactory->getIconForRecord($this->table, $row, ‪Icon::SIZE_SMALL)->render();
272  }
273 }
‪TYPO3\CMS\Core\Imaging\Icon\SIZE_SMALL
‪const SIZE_SMALL
Definition: Icon.php:30
‪TYPO3\CMS\Impexp\View\ExportPageTreeView\__construct
‪__construct()
Definition: ExportPageTreeView.php:50
‪TYPO3\CMS\Backend\Tree\View\AbstractTreeView\$clause
‪string $clause
Definition: AbstractTreeView.php:75
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:26
‪TYPO3\CMS\Impexp\View\ExportPageTreeView\init
‪init($clause='', $orderByFields='')
Definition: ExportPageTreeView.php:63
‪TYPO3\CMS\Impexp\View\ExportPageTreeView
Definition: ExportPageTreeView.php:34
‪TYPO3\CMS\Impexp\View\ExportPageTreeView\printTree
‪string printTree($treeArr='')
Definition: ExportPageTreeView.php:196
‪TYPO3\CMS\Backend\Tree\View\AbstractTreeView\$buffer_idH
‪array $buffer_idH
Definition: AbstractTreeView.php:145
‪TYPO3\CMS\Impexp\View\ExportPageTreeView\$expandAll
‪bool $expandAll
Definition: ExportPageTreeView.php:38
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:34
‪TYPO3\CMS\Impexp\View\ExportPageTreeView\getTitleAttrib
‪string getTitleAttrib($row)
Definition: ExportPageTreeView.php:75
‪TYPO3\CMS\Impexp\View\ExportPageTreeView\buildTree
‪buildTree(int $pid, int $levels, bool $checkSub)
Definition: ExportPageTreeView.php:128
‪TYPO3\CMS\Impexp\Export\LEVELS_INFINITE
‪const LEVELS_INFINITE
Definition: Export.php:51
‪TYPO3\CMS\Impexp\View\ExportPageTreeView\buildTreeByExpandedState
‪buildTreeByExpandedState(int $pid)
Definition: ExportPageTreeView.php:107
‪TYPO3\CMS\Backend\Tree\View\AbstractTreeView\reset
‪reset()
Definition: AbstractTreeView.php:219
‪TYPO3\CMS\Backend\Tree\View\AbstractTreeView\$tree
‪array $tree
Definition: AbstractTreeView.php:151
‪TYPO3\CMS\Impexp\View\ExportPageTreeView\$stored
‪array $stored
Definition: ExportPageTreeView.php:48
‪TYPO3\CMS\Impexp\View\ExportPageTreeView\$bank
‪int $bank
Definition: ExportPageTreeView.php:43
‪TYPO3\CMS\Backend\Configuration\BackendUserConfiguration
Definition: BackendUserConfiguration.php:30
‪TYPO3\CMS\Backend\Tree\View\AbstractTreeView\getRootIcon
‪string getRootIcon($rec)
Definition: AbstractTreeView.php:311
‪TYPO3\CMS\Backend\Tree\View\AbstractTreeView\$orderByFields
‪string $orderByFields
Definition: AbstractTreeView.php:82
‪TYPO3\CMS\Backend\Tree\View\AbstractTreeView\getBackendUser
‪BackendUserAuthentication getBackendUser()
Definition: AbstractTreeView.php:589
‪TYPO3\CMS\Impexp\View\ExportPageTreeView\getPageIcon
‪string getPageIcon(array $row)
Definition: ExportPageTreeView.php:265
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Backend\Tree\View\AbstractTreeView\getTree
‪int getTree($uid, $depth=999, $depthData='')
Definition: AbstractTreeView.php:380
‪TYPO3\CMS\Impexp\Export
Definition: Export.php:48
‪TYPO3\CMS\Impexp\View\ExportPageTreeView\expandNext
‪bool expandNext($id)
Definition: ExportPageTreeView.php:254
‪TYPO3\CMS\Impexp\View\ExportPageTreeView\buildTreeByLevels
‪buildTreeByLevels(int $pid, int $levels)
Definition: ExportPageTreeView.php:94
‪TYPO3\CMS\Impexp\View\ExportPageTreeView\syncPageTreeState
‪syncPageTreeState()
Definition: ExportPageTreeView.php:172
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Backend\Tree\View\AbstractTreeView\getTitleStr
‪string getTitleStr($row, $titleLen=30)
Definition: AbstractTreeView.php:350
‪TYPO3\CMS\Impexp\View\ExportPageTreeView\PMicon
‪PMicon($row, $a, $c, $nextCount, $isOpen)
Definition: ExportPageTreeView.php:83
‪TYPO3\CMS\Backend\Tree\View\AbstractTreeView
Definition: AbstractTreeView.php:37
‪TYPO3\CMS\Impexp\View
Definition: ExportPageTreeView.php:18