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