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