‪TYPO3CMS  ‪main
DatabaseBrowser.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 
20 use Psr\Http\Message\ServerRequestInterface;
24 use TYPO3\CMS\Backend\Utility\BackendUtility;
26 use TYPO3\CMS\Core\Imaging\IconSize;
30 
38 {
39  protected string ‪$identifier = 'db';
40 
48  protected ‪$expandPage;
49  protected array ‪$modTSconfig = [];
50 
51  protected function ‪initialize(ServerRequestInterface ‪$request)
52  {
53  parent::initialize(‪$request);
54  $this->pageRenderer->loadJavaScriptModule('@typo3/backend/browse-database.js');
55  $this->pageRenderer->loadJavaScriptModule('@typo3/backend/tree/page-browser.js');
56  $this->pageRenderer->loadJavaScriptModule('@typo3/backend/column-selector-button.js');
57  $this->pageRenderer->loadJavaScriptModule('@typo3/backend/recordlist.js');
58  $this->pageRenderer->loadJavaScriptModule('@typo3/backend/record-search.js');
59  }
60 
61  protected function ‪initVariables(ServerRequestInterface ‪$request)
62  {
63  parent::initVariables(‪$request);
64  $this->‪expandPage = $request->getParsedBody()['expandPage'] ?? ‪$request->getQueryParams()['expandPage'] ?? null;
65  }
66 
73  public function ‪processSessionData($data)
74  {
75  if ($this->‪expandPage !== null) {
76  $data['expandPage'] = ‪$this->expandPage;
77  ‪$store = true;
78  } else {
79  $this->‪expandPage = (int)($data['expandPage'] ?? 0);
80  ‪$store = false;
81  }
82  return [$data, ‪$store];
83  }
84 
88  public function ‪render()
89  {
90  $this->‪getBackendUser()->initializeWebmountsForElementBrowser();
91  $this->modTSconfig = BackendUtility::getPagesTSconfig((int)$this->‪expandPage)['mod.']['web_list.'] ?? [];
92  [, , , $allowedTables] = explode('|', $this->bparams);
93 
94  $withTree = true;
95  if ($allowedTables !== '' && $allowedTables !== '*') {
96  $tablesArr = ‪GeneralUtility::trimExplode(',', $allowedTables, true);
97  $onlyRootLevel = true;
98  foreach ($tablesArr as $currentTable) {
99  if (isset(‪$GLOBALS['TCA'][$currentTable])) {
100  if (!isset(‪$GLOBALS['TCA'][$currentTable]['ctrl']['rootLevel']) || (int)‪$GLOBALS['TCA'][$currentTable]['ctrl']['rootLevel'] !== 1) {
101  $onlyRootLevel = false;
102  }
103  }
104  }
105  if ($onlyRootLevel) {
106  $withTree = false;
107  // page to work on is root
108  $this->expandPage = 0;
109  }
110  }
111 
112  $contentOnly = (bool)($this->‪getRequest()->getQueryParams()['contentOnly'] ?? false);
113  $renderedRecordList = $this->‪renderTableRecords($allowedTables);
114 
115  $this->pageRenderer->setTitle($this->‪getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang_browse_links.xlf:recordSelector'));
118  'treeEnabled' => $withTree,
119  'treeActions' => $allowedTables === 'pages' ? ['select'] : [],
120  'activePage' => $this->expandPage,
121  'initialNavigationWidth' => $this->‪getBackendUser()->uc['selector']['navigation']['width'] ?? 250,
122  'content' => $renderedRecordList,
123  'contentOnly' => $contentOnly,
124  ]);
125  $content = $this->view->render('ElementBrowser/Page');
126  if ($contentOnly) {
127  return $content;
128  }
129  $this->pageRenderer->setBodyContent('<body ' . $this->‪getBodyTagParameters() . '>' . $content);
130  return $this->pageRenderer->render();
131  }
132 
139  protected function ‪renderTableRecords($tables)
140  {
141  ‪$request = $this->‪getRequest();
142  $backendUser = $this->‪getBackendUser();
143  if ($this->expandPage === null || $this->expandPage < 0 || !$backendUser->isInWebMount($this->expandPage)) {
144  return '';
145  }
146  // Set array with table names to list:
147  if (trim($tables) === '*') {
148  $tablesArr = array_keys(‪$GLOBALS['TCA']);
149  } else {
150  $tablesArr = ‪GeneralUtility::trimExplode(',', $tables, true);
151  }
152 
153  $out = '';
154  // Create the header, showing the current page for which the listing is.
155  // Includes link to the page itself, if pages are amount allowed tables.
156  $titleLen = (int)$backendUser->uc['titleLen'];
157  $mainPageRecord = BackendUtility::getRecordWSOL('pages', $this->expandPage);
158  if (is_array($mainPageRecord)) {
159  $pText = htmlspecialchars(‪GeneralUtility::fixed_lgd_cs($mainPageRecord['title'], $titleLen));
160 
161  $out .= '<p>' . $this->iconFactory->getIconForRecord('pages', $mainPageRecord, IconSize::SMALL)->render() . '&nbsp;';
162  if (in_array('pages', $tablesArr, true)) {
163  $out .= '<span data-uid="' . htmlspecialchars((string)$mainPageRecord['uid']) . '" data-table="pages" data-title="' . htmlspecialchars($mainPageRecord['title']) . '">';
164  $out .= '<a href="#" data-close="0">'
165  . $this->iconFactory->getIcon('actions-plus', IconSize::SMALL)->render()
166  . '</a>'
167  . '<a href="#" data-close="1">'
168  . $pText
169  . '</a>';
170  $out .= '</span>';
171  } else {
172  $out .= $pText;
173  }
174  $out .= '</p>';
175  }
176 
177  $permsClause = $backendUser->getPagePermsClause(‪Permission::PAGE_SHOW);
178  $pageInfo = BackendUtility::readPageAccess($this->expandPage, $permsClause);
179  $existingModuleData = $backendUser->getModuleData('web_list');
180  $moduleData = new ModuleData('web_list', is_array($existingModuleData) ? $existingModuleData : []);
181 
182  $dbList = GeneralUtility::makeInstance(ElementBrowserRecordList::class);
183  $dbList->setRequest(‪$request);
184  $dbList->setModuleData($moduleData);
185  $dbList->setOverrideUrlParameters($this->‪getUrlParameters([]), ‪$request);
186  $dbList->setIsEditable(false);
187  $dbList->calcPerms = new Permission($backendUser->calcPerms($pageInfo));
188  $dbList->noControlPanels = true;
189  $dbList->clickMenuEnabled = false;
190  $dbList->displayRecordDownload = false;
191  $dbList->tableList = implode(',', $tablesArr);
192 
193  // a string like "data[pages][79][storage_pid]"
194  [$fieldPointerString] = explode('|', $this->bparams);
195  // parts like: data, pages], 79], storage_pid]
196  $fieldPointerParts = explode('[', $fieldPointerString);
197 
198  $relatingTableName = substr(($fieldPointerParts[1] ?? ''), 0, -1);
199  $relatingFieldName = substr(($fieldPointerParts[3] ?? ''), 0, -1);
200 
201  if ($relatingTableName && $relatingFieldName) {
202  $dbList->setRelatingTableAndField($relatingTableName, $relatingFieldName);
203  }
204 
205  $selectedTable = (string)(‪$request->getParsedBody()['table'] ?? ‪$request->getQueryParams()['table'] ?? '');
206  $searchWord = (string)(‪$request->getParsedBody()['searchTerm'] ?? ‪$request->getQueryParams()['searchTerm'] ?? '');
207  $searchLevels = (int)(‪$request->getParsedBody()['search_levels'] ?? ‪$request->getQueryParams()['search_levels'] ?? 0);
208  $pointer = (int)(‪$request->getParsedBody()['pointer'] ?? ‪$request->getQueryParams()['pointer'] ?? 0);
209 
210  $dbList->start(
211  $this->expandPage,
212  $selectedTable,
213  ‪MathUtility::forceIntegerInRange($pointer, 0, 100000),
214  $searchWord,
215  $searchLevels
216  );
217 
218  $tableList = $dbList->generateList();
219 
220  $out .= $this->‪renderSearchBox(‪$request, $dbList, $searchWord, $searchLevels);
221 
222  // Add the HTML for the record list to output variable:
223  $out .= $tableList;
224 
225  return $out;
226  }
227 
228  protected function ‪renderSearchBox(ServerRequestInterface ‪$request, ‪ElementBrowserRecordList $dblist, string $searchWord, int $searchLevels): string
229  {
230  return GeneralUtility::makeInstance(RecordSearchBoxComponent::class)
231  ->setAllowedSearchLevels((array)($this->modTSconfig['searchLevel.']['items.'] ?? []))
232  ->setSearchWord($searchWord)
233  ->setSearchLevel($searchLevels)
234  ->render(‪$request, $dblist->‪listURL('', '-1', 'pointer,searchTerm'));
235  }
236 
241  public function ‪getUrlParameters(array $values): array
242  {
243  $pid = $values['pid'] ?? ‪$this->expandPage;
244  return [
245  'mode' => 'db',
246  'expandPage' => $pid,
247  'bparams' => ‪$this->bparams,
248  ];
249  }
250 }
‪TYPO3\CMS\Backend\ElementBrowser\DatabaseBrowser\getUrlParameters
‪string[] getUrlParameters(array $values)
Definition: DatabaseBrowser.php:240
‪TYPO3\CMS\Backend\ElementBrowser\DatabaseBrowser\expandPage
‪array< int, function processSessionData( $data) { if( $this->expandPage !==null) { $data[ 'expandPage']=$this-> expandPage
Definition: DatabaseBrowser.php:75
‪TYPO3\CMS\Core\View\ViewInterface\assignMultiple
‪assignMultiple(array $values)
‪TYPO3\CMS\Backend\ElementBrowser\ElementBrowserInterface
Definition: ElementBrowserInterface.php:28
‪TYPO3\CMS\Core\Utility\GeneralUtility\fixed_lgd_cs
‪static string fixed_lgd_cs(string $string, int $chars, string $appendString='...')
Definition: GeneralUtility.php:92
‪TYPO3\CMS\Backend\RecordList\ElementBrowserRecordList
Definition: ElementBrowserRecordList.php:27
‪TYPO3\CMS\Backend\ElementBrowser\DatabaseBrowser\render
‪string render()
Definition: DatabaseBrowser.php:87
‪TYPO3\CMS\Backend\ElementBrowser\DatabaseBrowser\$store
‪$store
Definition: DatabaseBrowser.php:76
‪TYPO3\CMS\Backend\RecordList\DatabaseRecordList\listURL
‪string listURL($altId='', $table='-1', $exclList='')
Definition: DatabaseRecordList.php:2663
‪TYPO3\CMS\Backend\ElementBrowser\AbstractElementBrowser\$bparams
‪string $bparams
Definition: AbstractElementBrowser.php:63
‪TYPO3\CMS\Backend\ElementBrowser\DatabaseBrowser\renderTableRecords
‪string renderTableRecords($tables)
Definition: DatabaseBrowser.php:138
‪TYPO3\CMS\Backend\Module\ModuleData
Definition: ModuleData.php:30
‪TYPO3\CMS\Backend\ElementBrowser\AbstractElementBrowser
Definition: AbstractElementBrowser.php:39
‪TYPO3\CMS\Backend\ElementBrowser\DatabaseBrowser\renderSearchBox
‪renderSearchBox(ServerRequestInterface $request, ElementBrowserRecordList $dblist, string $searchWord, int $searchLevels)
Definition: DatabaseBrowser.php:227
‪TYPO3\CMS\Core\Type\Bitmask\Permission
Definition: Permission.php:26
‪TYPO3\CMS\Backend\ElementBrowser\AbstractElementBrowser\$view
‪ViewInterface $view
Definition: AbstractElementBrowser.php:66
‪TYPO3\CMS\Backend\ElementBrowser\DatabaseBrowser\$identifier
‪string $identifier
Definition: DatabaseBrowser.php:39
‪TYPO3\CMS\Backend\ElementBrowser\DatabaseBrowser\$modTSconfig
‪array $modTSconfig
Definition: DatabaseBrowser.php:48
‪TYPO3\CMS\Backend\ElementBrowser\DatabaseBrowser\$expandPage
‪int null $expandPage
Definition: DatabaseBrowser.php:47
‪TYPO3\CMS\Backend\ElementBrowser\AbstractElementBrowser\getBodyTagParameters
‪getBodyTagParameters()
Definition: AbstractElementBrowser.php:104
‪TYPO3\CMS\Backend\ElementBrowser
Definition: AbstractElementBrowser.php:18
‪TYPO3\CMS\Backend\ElementBrowser\DatabaseBrowser\initialize
‪initialize(ServerRequestInterface $request)
Definition: DatabaseBrowser.php:50
‪TYPO3\CMS\Backend\ElementBrowser\AbstractElementBrowser\getLanguageService
‪getLanguageService()
Definition: AbstractElementBrowser.php:156
‪TYPO3\CMS\Core\Type\Bitmask\Permission\PAGE_SHOW
‪const PAGE_SHOW
Definition: Permission.php:35
‪TYPO3\CMS\Backend\ElementBrowser\ElementBrowserInterface\processSessionData
‪array[] processSessionData($data)
‪TYPO3\CMS\Backend\ElementBrowser\AbstractElementBrowser\getRequest
‪getRequest()
Definition: AbstractElementBrowser.php:151
‪TYPO3\CMS\Backend\View\RecordSearchBoxComponent
Definition: RecordSearchBoxComponent.php:28
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Backend\ElementBrowser\DatabaseBrowser
Definition: DatabaseBrowser.php:38
‪TYPO3\CMS\Core\Utility\MathUtility\forceIntegerInRange
‪static int forceIntegerInRange(mixed $theInt, int $min, int $max=2000000000, int $defaultValue=0)
Definition: MathUtility.php:34
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Backend\ElementBrowser\AbstractElementBrowser\$request
‪ServerRequestInterface $request
Definition: AbstractElementBrowser.php:65
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode(string $delim, string $string, bool $removeEmptyValues=false, int $limit=0)
Definition: GeneralUtility.php:817
‪TYPO3\CMS\Backend\ElementBrowser\AbstractElementBrowser\getBackendUser
‪getBackendUser()
Definition: AbstractElementBrowser.php:161
‪TYPO3\CMS\Backend\ElementBrowser\DatabaseBrowser\initVariables
‪initVariables(ServerRequestInterface $request)
Definition: DatabaseBrowser.php:60