[CLEANUP] Use Permission constants consistently
[Packages/TYPO3.CMS.git] / typo3 / sysext / recordlist / Classes / Browser / DatabaseBrowser.php
1 <?php
2 namespace TYPO3\CMS\Recordlist\Browser;
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\RecordList\ElementBrowserRecordList;
18 use TYPO3\CMS\Backend\Utility\BackendUtility;
19 use TYPO3\CMS\Core\Imaging\Icon;
20 use TYPO3\CMS\Core\Type\Bitmask\Permission;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Core\Utility\MathUtility;
23 use TYPO3\CMS\Recordlist\Tree\View\ElementBrowserPageTreeView;
24 use TYPO3\CMS\Recordlist\Tree\View\LinkParameterProviderInterface;
25
26 /**
27 * Showing a page tree and allows you to browse for records
28 */
29 class DatabaseBrowser extends AbstractElementBrowser implements ElementBrowserInterface, LinkParameterProviderInterface
30 {
31 /**
32 * When you click a page title/expand icon to see the content of a certain page, this
33 * value will contain the ID of the expanded page.
34 * If the value is NOT set by GET parameter, then it will be restored from the module session data.
35 *
36 * @var int|null
37 */
38 protected $expandPage;
39
40 /**
41 */
42 protected function initialize()
43 {
44 parent::initialize();
45 $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Recordlist/BrowseDatabase');
46 }
47
48 /**
49 */
50 protected function initVariables()
51 {
52 parent::initVariables();
53 $this->expandPage = GeneralUtility::_GP('expandPage');
54 }
55
56 /**
57 * Session data for this class can be set from outside with this method.
58 *
59 * @param mixed[] $data Session data array
60 * @return array[] Session data and boolean which indicates that data needs to be stored in session because it's changed
61 */
62 public function processSessionData($data)
63 {
64 if ($this->expandPage !== null) {
65 $data['expandPage'] = $this->expandPage;
66 $store = true;
67 } else {
68 $this->expandPage = (int)$data['expandPage'];
69 $store = false;
70 }
71 return [$data, $store];
72 }
73
74 /**
75 * @return string HTML content
76 */
77 public function render()
78 {
79 $this->setTemporaryDbMounts();
80
81 list(, , , $allowedTables) = explode('|', $this->bparams);
82 $backendUser = $this->getBackendUser();
83
84 // Making the browsable pagetree:
85 /** @var ElementBrowserPageTreeView $pageTree */
86 $pageTree = GeneralUtility::makeInstance(ElementBrowserPageTreeView::class);
87 $pageTree->setLinkParameterProvider($this);
88 $pageTree->ext_pArrPages = $allowedTables === 'pages';
89 $pageTree->ext_showNavTitle = (bool)$backendUser->getTSConfigVal('options.pageTree.showNavTitle');
90 $pageTree->ext_showPageId = (bool)$backendUser->getTSConfigVal('options.pageTree.showPageIdWithTitle');
91 $pageTree->ext_showPathAboveMounts = (bool)$backendUser->getTSConfigVal('options.pageTree.showPathAboveMounts');
92 $pageTree->addField('nav_title');
93 $tree = $pageTree->getBrowsableTree();
94
95 $withTree = true;
96 if ($allowedTables !== '' && $allowedTables !== '*') {
97 $tablesArr = GeneralUtility::trimExplode(',', $allowedTables, true);
98 $onlyRootLevel = true;
99 foreach ($tablesArr as $currentTable) {
100 if (isset($GLOBALS['TCA'][$currentTable])) {
101 if (!isset($GLOBALS['TCA'][$currentTable]['ctrl']['rootLevel']) || (int)$GLOBALS['TCA'][$currentTable]['ctrl']['rootLevel'] !== 1) {
102 $onlyRootLevel = false;
103 }
104 }
105 }
106 if ($onlyRootLevel) {
107 $withTree = false;
108 // page to work on is root
109 $this->expandPage = 0;
110 }
111 }
112
113 $renderedRecordList = $this->renderTableRecords($allowedTables);
114
115 $this->initDocumentTemplate();
116 $content = $this->doc->startPage(htmlspecialchars($this->getLanguageService()->getLL('recordSelector')));
117
118 // Putting the parts together, side by side:
119 $markup = [];
120 $markup[] = '<!-- Wrapper table for folder tree / filelist: -->';
121 $markup[] = '<div class="element-browser">';
122 $markup[] = ' <div class="element-browser-panel element-browser-main">';
123 if ($withTree) {
124 $markup[] = ' <div class="element-browser-main-sidebar">';
125 $markup[] = ' <div class="element-browser-body">';
126 $markup[] = ' <h3>' . htmlspecialchars($this->getLanguageService()->getLL('pageTree')) . ':</h3>';
127 $markup[] = ' ' . $this->getTemporaryTreeMountCancelNotice();
128 $markup[] = ' ' . $tree;
129 $markup[] = ' </div>';
130 $markup[] = ' </div>';
131 }
132 $markup[] = ' <div class="element-browser-main-content">';
133 $markup[] = ' <div class="element-browser-body">';
134 $markup[] = ' ' . $this->doc->getFlashMessages();
135 $markup[] = ' ' . $renderedRecordList;
136 $markup[] = ' </div>';
137 $markup[] = ' </div>';
138 $markup[] = ' </div>';
139 $markup[] = '</div>';
140 $content .= implode('', $markup);
141
142 // Ending page, returning content:
143 $content .= $this->doc->endPage();
144 return $this->doc->insertStylesAndJS($content);
145 }
146
147 /**
148 * Check if a temporary tree mount is set and return a cancel button
149 *
150 * @return string HTML code
151 */
152 protected function getTemporaryTreeMountCancelNotice()
153 {
154 if ((int)$this->getBackendUser()->getSessionData('pageTree_temporaryMountPoint') === 0) {
155 return '';
156 }
157 $link = '<p><a href="' . htmlspecialchars(GeneralUtility::linkThisScript(['setTempDBmount' => 0])) . '" class="btn btn-primary">'
158 . htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.temporaryDBmount')) . '</a></p>';
159
160 return $link;
161 }
162
163 /**
164 * If the current Backend User has set a temporary DB mount, it is stored to her/his UC.
165 */
166 protected function setTemporaryDbMounts()
167 {
168 $backendUser = $this->getBackendUser();
169
170 // Clear temporary DB mounts
171 $tmpMount = GeneralUtility::_GET('setTempDBmount');
172 if (isset($tmpMount)) {
173 $backendUser->setAndSaveSessionData('pageTree_temporaryMountPoint', (int)$tmpMount);
174 }
175 // Set temporary DB mounts
176 $alternativeWebmountPoint = (int)$backendUser->getSessionData('pageTree_temporaryMountPoint');
177 if ($alternativeWebmountPoint) {
178 $alternativeWebmountPoint = GeneralUtility::intExplode(',', $alternativeWebmountPoint);
179 $backendUser->setWebmounts($alternativeWebmountPoint);
180 } else {
181 // Setting alternative browsing mounts (ONLY local to browse_links.php this script so they stay "read-only")
182 $alternativeWebmountPoints = trim($backendUser->getTSConfigVal('options.pageTree.altElementBrowserMountPoints'));
183 $appendAlternativeWebmountPoints = $backendUser->getTSConfigVal('options.pageTree.altElementBrowserMountPoints.append');
184 if ($alternativeWebmountPoints) {
185 $alternativeWebmountPoints = GeneralUtility::intExplode(',', $alternativeWebmountPoints);
186 $this->getBackendUser()->setWebmounts($alternativeWebmountPoints, $appendAlternativeWebmountPoints);
187 }
188 }
189 }
190
191 /**
192 * This lists all content elements for the given list of tables
193 *
194 * @param string $tables Comma separated list of tables. Set to "*" if you want all tables.
195 * @return string HTML code
196 */
197 protected function renderTableRecords($tables)
198 {
199 $backendUser = $this->getBackendUser();
200 if ($this->expandPage === null || $this->expandPage < 0 || !$backendUser->isInWebMount($this->expandPage)) {
201 return '';
202 }
203 // Set array with table names to list:
204 if (trim($tables) === '*') {
205 $tablesArr = array_keys($GLOBALS['TCA']);
206 } else {
207 $tablesArr = GeneralUtility::trimExplode(',', $tables, true);
208 }
209
210 $out = '<h3>' . htmlspecialchars($this->getLanguageService()->getLL('selectRecords')) . ':</h3>';
211 // Create the header, showing the current page for which the listing is.
212 // Includes link to the page itself, if pages are amount allowed tables.
213 $titleLen = (int)$backendUser->uc['titleLen'];
214 $mainPageRecord = BackendUtility::getRecordWSOL('pages', $this->expandPage);
215 if (is_array($mainPageRecord)) {
216 $pText = htmlspecialchars(GeneralUtility::fixed_lgd_cs($mainPageRecord['title'], $titleLen));
217
218 $out .= $this->iconFactory->getIconForRecord('pages', $mainPageRecord, Icon::SIZE_SMALL)->render();
219 if (in_array('pages', $tablesArr, true)) {
220 $out .= '<span data-uid="' . htmlspecialchars($mainPageRecord['uid']) . '" data-table="pages" data-title="' . htmlspecialchars($mainPageRecord['title']) . '" data-icon="">';
221 $out .= '<a href="#" data-close="0">'
222 . $this->iconFactory->getIcon('actions-add', Icon::SIZE_SMALL)->render()
223 . '</a>'
224 . '<a href="#" data-close="1">'
225 . $pText
226 . '</a>';
227 $out .= '</span>';
228 } else {
229 $out .= $pText;
230 }
231 $out .= '<br />';
232 }
233
234 $permsClause = $backendUser->getPagePermsClause(Permission::PAGE_SHOW);
235 $pageInfo = BackendUtility::readPageAccess($this->expandPage, $permsClause);
236
237 /** @var ElementBrowserRecordList $dbList */
238 $dbList = GeneralUtility::makeInstance(ElementBrowserRecordList::class);
239 $dbList->setOverrideUrlParameters($this->getUrlParameters([]));
240 $dbList->thisScript = $this->thisScript;
241 $dbList->thumbs = false;
242 $dbList->setIsEditable(false);
243 $dbList->calcPerms = $backendUser->calcPerms($pageInfo);
244 $dbList->noControlPanels = true;
245 $dbList->clickMenuEnabled = false;
246 $dbList->tableList = implode(',', $tablesArr);
247
248 // a string like "data[pages][79][storage_pid]"
249 list($fieldPointerString) = explode('|', $this->bparams);
250 // parts like: data, pages], 79], storage_pid]
251 $fieldPointerParts = explode('[', $fieldPointerString);
252 $relatingTableName = substr($fieldPointerParts[1], 0, -1);
253 $relatingFieldName = substr($fieldPointerParts[3], 0, -1);
254 if ($relatingTableName && $relatingFieldName) {
255 $dbList->setRelatingTableAndField($relatingTableName, $relatingFieldName);
256 }
257
258 $dbList->start(
259 $this->expandPage,
260 GeneralUtility::_GP('table'),
261 MathUtility::forceIntegerInRange(GeneralUtility::_GP('pointer'), 0, 100000),
262 GeneralUtility::_GP('search_field'),
263 GeneralUtility::_GP('search_levels'),
264 GeneralUtility::_GP('showLimit')
265 );
266
267 $dbList->setDispFields();
268 $dbList->generateList();
269
270 $out .= $dbList->getSearchBox();
271
272 // Add the HTML for the record list to output variable:
273 $out .= $dbList->HTMLcode;
274
275 // Add support for fieldselectbox in singleTableMode
276 if ($dbList->table) {
277 $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Recordlist/FieldSelectBox');
278 $out .= $dbList->fieldSelectBox($dbList->table);
279 }
280
281 return $out;
282 }
283
284 /**
285 * @return string[] Array of body-tag attributes
286 */
287 protected function getBodyTagAttributes()
288 {
289 return [
290 'data-mode' => 'db'
291 ];
292 }
293
294 /**
295 * @param array $values Array of values to include into the parameters
296 * @return string[] Array of parameters which have to be added to URLs
297 */
298 public function getUrlParameters(array $values)
299 {
300 $pid = isset($values['pid']) ? $values['pid'] : $this->expandPage;
301 return [
302 'mode' => 'db',
303 'expandPage' => $pid,
304 'bparams' => $this->bparams
305 ];
306 }
307
308 /**
309 * @param array $values Values to be checked
310 * @return bool Returns TRUE if the given values match the currently selected item
311 */
312 public function isCurrentlySelectedItem(array $values)
313 {
314 return false;
315 }
316
317 /**
318 * Returns the URL of the current script
319 *
320 * @return string
321 */
322 public function getScriptUrl()
323 {
324 return $this->thisScript;
325 }
326 }