1ad748b7ee1fdd0457dd04ddb1b911e9f5e39cdc
[Packages/TYPO3.CMS.git] / typo3 / sysext / recordlist / Classes / RecordList / AbstractDatabaseRecordList.php
1 <?php
2 namespace TYPO3\CMS\Recordlist\RecordList;
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\AbstractRecordList;
18 use TYPO3\CMS\Backend\Routing\Router;
19 use TYPO3\CMS\Backend\Routing\UriBuilder;
20 use TYPO3\CMS\Backend\Tree\View\PageTreeView;
21 use TYPO3\CMS\Backend\Utility\BackendUtility;
22 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
23 use TYPO3\CMS\Core\Cache\CacheManager;
24 use TYPO3\CMS\Core\Database\ConnectionPool;
25 use TYPO3\CMS\Core\Database\Query\QueryBuilder;
26 use TYPO3\CMS\Core\Database\Query\QueryHelper;
27 use TYPO3\CMS\Core\Database\Query\Restriction\BackendWorkspaceRestriction;
28 use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
29 use TYPO3\CMS\Core\Imaging\Icon;
30 use TYPO3\CMS\Core\Imaging\IconFactory;
31 use TYPO3\CMS\Core\Service\DependencyOrderingService;
32 use TYPO3\CMS\Core\Type\Bitmask\Permission;
33 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
34 use TYPO3\CMS\Core\Utility\GeneralUtility;
35 use TYPO3\CMS\Core\Utility\HttpUtility;
36 use TYPO3\CMS\Core\Utility\MathUtility;
37
38 /**
39 * Child class for rendering of Web > List (not the final class)
40 * Shared between Web>List and Web>Page
41 * @see \TYPO3\CMS\Recordlist\RecordList\DatabaseRecordList
42 */
43 class AbstractDatabaseRecordList extends AbstractRecordList
44 {
45 /**
46 * Specify a list of tables which are the only ones allowed to be displayed.
47 *
48 * @var string
49 */
50 public $tableList = '';
51
52 /**
53 * Return URL
54 *
55 * @var string
56 */
57 public $returnUrl = '';
58
59 /**
60 * Thumbnails on records containing files (pictures)
61 *
62 * @var bool
63 */
64 public $thumbs = 0;
65
66 /**
67 * default Max items shown per table in "multi-table mode", may be overridden by tables.php
68 *
69 * @var int
70 */
71 public $itemsLimitPerTable = 20;
72
73 /**
74 * default Max items shown per table in "single-table mode", may be overridden by tables.php
75 *
76 * @var int
77 */
78 public $itemsLimitSingleTable = 100;
79
80 /**
81 * Current script name
82 *
83 * @var string
84 */
85 public $script = 'index.php';
86
87 /**
88 * Indicates if all available fields for a user should be selected or not.
89 *
90 * @var int
91 */
92 public $allFields = 0;
93
94 /**
95 * Whether to show localization view or not
96 *
97 * @var bool
98 */
99 public $localizationView = false;
100
101 /**
102 * If set, csvList is outputted.
103 *
104 * @var bool
105 */
106 public $csvOutput = false;
107
108 /**
109 * Field, to sort list by
110 *
111 * @var string
112 */
113 public $sortField;
114
115 /**
116 * Field, indicating to sort in reverse order.
117 *
118 * @var bool
119 */
120 public $sortRev;
121
122 /**
123 * Containing which fields to display in extended mode
124 *
125 * @var string[]
126 */
127 public $displayFields;
128
129 /**
130 * String, can contain the field name from a table which must have duplicate values marked.
131 *
132 * @var string
133 */
134 public $duplicateField;
135
136 /**
137 * Page id
138 *
139 * @var int
140 */
141 public $id;
142
143 /**
144 * Tablename if single-table mode
145 *
146 * @var string
147 */
148 public $table = '';
149
150 /**
151 * If TRUE, records are listed only if a specific table is selected.
152 *
153 * @var bool
154 */
155 public $listOnlyInSingleTableMode = false;
156
157 /**
158 * Pointer for browsing list
159 *
160 * @var int
161 */
162 public $firstElementNumber = 0;
163
164 /**
165 * Search string
166 *
167 * @var string
168 */
169 public $searchString = '';
170
171 /**
172 * Levels to search down.
173 *
174 * @var int
175 */
176 public $searchLevels = '';
177
178 /**
179 * Number of records to show
180 *
181 * @var int
182 */
183 public $showLimit = 0;
184
185 /**
186 * Page select permissions
187 *
188 * @var string
189 */
190 public $perms_clause = '';
191
192 /**
193 * Some permissions...
194 *
195 * @var int
196 */
197 public $calcPerms = 0;
198
199 /**
200 * Mode for what happens when a user clicks the title of a record.
201 *
202 * @var string
203 */
204 public $clickTitleMode = '';
205
206 /**
207 * Shared module configuration, used by localization features
208 *
209 * @var array
210 */
211 public $modSharedTSconfig = [];
212
213 /**
214 * Loaded with page record with version overlay if any.
215 *
216 * @var string[]
217 */
218 public $pageRecord = [];
219
220 /**
221 * Tables which should not get listed
222 *
223 * @var string
224 */
225 public $hideTables = '';
226
227 /**
228 * Tables which should not list their translations
229 *
230 * @var string
231 */
232 public $hideTranslations = '';
233
234 /**
235 * TSconfig which overwrites TCA-Settings
236 *
237 * @var mixed[][]
238 */
239 public $tableTSconfigOverTCA = [];
240
241 /**
242 * Array of collapsed / uncollapsed tables in multi table view
243 *
244 * @var int[][]
245 */
246 public $tablesCollapsed = [];
247
248 /**
249 * JavaScript code accumulation
250 *
251 * @var string
252 */
253 public $JScode = '';
254
255 /**
256 * HTML output
257 *
258 * @var string
259 */
260 public $HTMLcode = '';
261
262 /**
263 * "LIMIT " in SQL...
264 *
265 * @var int
266 */
267 public $iLimit = 0;
268
269 /**
270 * Counting the elements no matter what...
271 *
272 * @var int
273 */
274 public $eCounter = 0;
275
276 /**
277 * Set to the total number of items for a table when selecting.
278 *
279 * @var string
280 */
281 public $totalItems = '';
282
283 /**
284 * Cache for record path
285 *
286 * @var mixed[]
287 */
288 public $recPath_cache = [];
289
290 /**
291 * Fields to display for the current table
292 *
293 * @var string[]
294 */
295 public $setFields = [];
296
297 /**
298 * Used for tracking next/prev uids
299 *
300 * @var int[][]
301 */
302 public $currentTable = [];
303
304 /**
305 * Used for tracking duplicate values of fields
306 *
307 * @var string[]
308 */
309 public $duplicateStack = [];
310
311 /**
312 * @var array[] Module configuration
313 */
314 public $modTSconfig;
315
316 /**
317 * Override/add urlparameters in listUrl() method
318 * @var string[]
319 */
320 protected $overrideUrlParameters = [];
321
322 /**
323 * Override the page ids taken into account by getPageIdConstraint()
324 *
325 * @var array
326 */
327 protected $overridePageIdList = [];
328
329 /**
330 * Array with before/after setting for tables
331 * Structure:
332 * 'tableName' => [
333 * 'before' => ['A', ...]
334 * 'after' => []
335 * ]
336 * @var array[]
337 */
338 protected $tableDisplayOrder = [];
339
340 /**
341 * Initializes the list generation
342 *
343 * @param int $id Page id for which the list is rendered. Must be >= 0
344 * @param string $table Tablename - if extended mode where only one table is listed at a time.
345 * @param int $pointer Browsing pointer.
346 * @param string $search Search word, if any
347 * @param int $levels Number of levels to search down the page tree
348 * @param int $showLimit Limit of records to be listed.
349 */
350 public function start($id, $table, $pointer, $search = '', $levels = 0, $showLimit = 0)
351 {
352 $backendUser = $this->getBackendUserAuthentication();
353 // Setting internal variables:
354 // sets the parent id
355 $this->id = (int)$id;
356 if ($GLOBALS['TCA'][$table]) {
357 // Setting single table mode, if table exists:
358 $this->table = $table;
359 }
360 $this->firstElementNumber = $pointer;
361 $this->searchString = trim($search);
362 $this->searchLevels = (int)$levels;
363 $this->showLimit = MathUtility::forceIntegerInRange($showLimit, 0, 10000);
364 // Setting GPvars:
365 $this->csvOutput = (bool)GeneralUtility::_GP('csv');
366 $this->sortField = GeneralUtility::_GP('sortField');
367 $this->sortRev = GeneralUtility::_GP('sortRev');
368 $this->displayFields = GeneralUtility::_GP('displayFields');
369 $this->duplicateField = GeneralUtility::_GP('duplicateField');
370 if (GeneralUtility::_GP('justLocalized')) {
371 $this->localizationRedirect(GeneralUtility::_GP('justLocalized'));
372 }
373 // Init dynamic vars:
374 $this->counter = 0;
375 $this->JScode = '';
376 $this->HTMLcode = '';
377 // Limits
378 if (isset($this->modTSconfig['properties']['itemsLimitPerTable'])) {
379 $this->itemsLimitPerTable = MathUtility::forceIntegerInRange(
380 (int)$this->modTSconfig['properties']['itemsLimitPerTable'],
381 1,
382 10000
383 );
384 }
385 if (isset($this->modTSconfig['properties']['itemsLimitSingleTable'])) {
386 $this->itemsLimitSingleTable = MathUtility::forceIntegerInRange(
387 (int)$this->modTSconfig['properties']['itemsLimitSingleTable'],
388 1,
389 10000
390 );
391 }
392
393 // $table might be NULL at this point in the code. As the expressionBuilder
394 // is used to limit returned records based on the page permissions and the
395 // uid field of the pages it can hardcoded to work on the pages table.
396 $expressionBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
397 ->getQueryBuilderForTable('pages')
398 ->expr();
399 $permsClause = $expressionBuilder->andX($backendUser->getPagePermsClause(1));
400 // This will hide records from display - it has nothing to do with user rights!!
401 if ($pidList = $backendUser->getTSConfigVal('options.hideRecords.pages')) {
402 $pidList = GeneralUtility::intExplode(',', $pidList, true);
403 if (!empty($pidList)) {
404 $permsClause->add($expressionBuilder->notIn('pages.uid', $pidList));
405 }
406 }
407 $this->perms_clause = (string)$permsClause;
408
409 // Get configuration of collapsed tables from user uc and merge with sanitized GP vars
410 $this->tablesCollapsed = is_array($backendUser->uc['moduleData']['list'])
411 ? $backendUser->uc['moduleData']['list']
412 : [];
413 $collapseOverride = GeneralUtility::_GP('collapse');
414 if (is_array($collapseOverride)) {
415 foreach ($collapseOverride as $collapseTable => $collapseValue) {
416 if (is_array($GLOBALS['TCA'][$collapseTable]) && ($collapseValue == 0 || $collapseValue == 1)) {
417 $this->tablesCollapsed[$collapseTable] = $collapseValue;
418 }
419 }
420 // Save modified user uc
421 $backendUser->uc['moduleData']['list'] = $this->tablesCollapsed;
422 $backendUser->writeUC($backendUser->uc);
423 $returnUrl = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
424 if ($returnUrl !== '') {
425 HttpUtility::redirect($returnUrl);
426 }
427 }
428
429 // Initialize languages:
430 if ($this->localizationView) {
431 $this->initializeLanguages();
432 }
433 }
434
435 /**
436 * Traverses the table(s) to be listed and renders the output code for each:
437 * The HTML is accumulated in $this->HTMLcode
438 * Finishes off with a stopper-gif
439 */
440 public function generateList()
441 {
442 // Set page record in header
443 $this->pageRecord = BackendUtility::getRecordWSOL('pages', $this->id);
444 $hideTablesArray = GeneralUtility::trimExplode(',', $this->hideTables);
445
446 $backendUser = $this->getBackendUserAuthentication();
447
448 // pre-process tables and add sorting instructions
449 $tableNames = array_flip(array_keys($GLOBALS['TCA']));
450 foreach ($tableNames as $tableName => &$config) {
451 $hideTable = false;
452
453 // Checking if the table should be rendered:
454 // Checks that we see only permitted/requested tables:
455 if ($this->table && $tableName !== $this->table
456 || $this->tableList && !GeneralUtility::inList($this->tableList, $tableName)
457 || !$backendUser->check('tables_select', $tableName)
458 ) {
459 $hideTable = true;
460 }
461
462 if (!$hideTable) {
463 // Don't show table if hidden by TCA ctrl section
464 // Don't show table if hidden by pageTSconfig mod.web_list.hideTables
465 $hideTable = $hideTable
466 || !empty($GLOBALS['TCA'][$tableName]['ctrl']['hideTable'])
467 || in_array($tableName, $hideTablesArray, true)
468 || in_array('*', $hideTablesArray, true);
469 // Override previous selection if table is enabled or hidden by TSconfig TCA override mod.web_list.table
470 if (isset($this->tableTSconfigOverTCA[$tableName . '.']['hideTable'])) {
471 $hideTable = (bool)$this->tableTSconfigOverTCA[$tableName . '.']['hideTable'];
472 }
473 }
474 if ($hideTable) {
475 unset($tableNames[$tableName]);
476 } else {
477 if (isset($this->tableDisplayOrder[$tableName])) {
478 // Copy display order information
479 $tableNames[$tableName] = $this->tableDisplayOrder[$tableName];
480 } else {
481 $tableNames[$tableName] = [];
482 }
483 }
484 }
485 unset($config);
486
487 $orderedTableNames = GeneralUtility::makeInstance(DependencyOrderingService::class)
488 ->orderByDependencies($tableNames);
489
490 foreach ($orderedTableNames as $tableName => $_) {
491 // check if we are in single- or multi-table mode
492 if ($this->table) {
493 $this->iLimit = isset($GLOBALS['TCA'][$tableName]['interface']['maxSingleDBListItems'])
494 ? (int)$GLOBALS['TCA'][$tableName]['interface']['maxSingleDBListItems']
495 : $this->itemsLimitSingleTable;
496 } else {
497 // if there are no records in table continue current foreach
498 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
499 ->getQueryBuilderForTable($tableName);
500 $queryBuilder->getRestrictions()
501 ->removeAll()
502 ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
503 ->add(GeneralUtility::makeInstance(BackendWorkspaceRestriction::class));
504 $firstRow = $queryBuilder->select('uid')
505 ->from($tableName)
506 ->where($this->getPageIdConstraint($tableName))
507 ->execute()
508 ->fetch();
509 if (!is_array($firstRow)) {
510 continue;
511 }
512 $this->iLimit = isset($GLOBALS['TCA'][$tableName]['interface']['maxDBListItems'])
513 ? (int)$GLOBALS['TCA'][$tableName]['interface']['maxDBListItems']
514 : $this->itemsLimitPerTable;
515 }
516 if ($this->showLimit) {
517 $this->iLimit = $this->showLimit;
518 }
519 // Setting fields to select:
520 if ($this->allFields) {
521 $fields = $this->makeFieldList($tableName);
522 $fields[] = 'tstamp';
523 $fields[] = 'crdate';
524 $fields[] = '_PATH_';
525 $fields[] = '_CONTROL_';
526 if (is_array($this->setFields[$tableName])) {
527 $fields = array_intersect($fields, $this->setFields[$tableName]);
528 } else {
529 $fields = [];
530 }
531 } else {
532 $fields = [];
533 }
534
535 // Finally, render the list:
536 $this->HTMLcode .= $this->getTable($tableName, $this->id, implode(',', $fields));
537 }
538 }
539
540 /**
541 * To be implemented in extending classes.
542 *
543 * @param string $tableName
544 * @param int $id
545 * @param string $fields List of fields to show in the listing. Pseudo fields will be added including the record header.
546 * @return string HTML code
547 */
548 public function getTable($tableName, $id, $fields = '')
549 {
550 return '';
551 }
552
553 /**
554 * Creates the search box
555 *
556 * @param bool $formFields If TRUE, the search box is wrapped in its own form-tags
557 * @return string HTML for the search box
558 */
559 public function getSearchBox($formFields = true)
560 {
561 /** @var $iconFactory IconFactory */
562 $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
563 $lang = $this->getLanguageService();
564 // Setting form-elements, if applicable:
565 $formElements = ['', ''];
566 if ($formFields) {
567 $formElements = ['<form action="' . htmlspecialchars($this->listURL('', '-1', 'firstElementNumber,search_field')) . '" method="post">', '</form>'];
568 }
569 // Make level selector:
570 $opt = [];
571
572 // "New" generation of search levels ... based on TS config
573 $config = BackendUtility::getPagesTSconfig($this->id);
574 $searchLevelsFromTSconfig = $config['mod.']['web_list.']['searchLevel.']['items.'];
575 $searchLevelItems = [];
576
577 // get translated labels for search levels from pagets
578 foreach ($searchLevelsFromTSconfig as $keySearchLevel => $labelConfigured) {
579 $label = $lang->sL('LLL:' . $labelConfigured, false);
580 if ($label === '') {
581 $label = $labelConfigured;
582 }
583 $searchLevelItems[$keySearchLevel] = $label;
584 }
585
586 foreach ($searchLevelItems as $kv => $label) {
587 $opt[] = '<option value="' . $kv . '"' . ($kv === $this->searchLevels ? ' selected="selected"' : '') . '>' . htmlspecialchars($label) . '</option>';
588 }
589 $lMenu = '<select class="form-control" name="search_levels" title="' . htmlspecialchars($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.title.search_levels')) . '" id="search_levels">' . implode('', $opt) . '</select>';
590 // Table with the search box:
591 $content = '<div class="db_list-searchbox-form db_list-searchbox-toolbar module-docheader-bar module-docheader-bar-search t3js-module-docheader-bar t3js-module-docheader-bar-search" id="db_list-searchbox-toolbar" style="display: ' . ($this->searchString == '' ? 'none' : 'block') . ';">
592 ' . $formElements[0] . '
593 <div id="typo3-dblist-search">
594 <div class="panel panel-default">
595 <div class="panel-body">
596 <div class="row">
597 <div class="form-group col-xs-12">
598 <label for="search_field">' . htmlspecialchars($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.label.searchString')) . ': </label>
599 <input class="form-control" type="search" placeholder="' . htmlspecialchars($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.enterSearchString')) . '" title="' . htmlspecialchars($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.title.searchString')) . '" name="search_field" id="search_field" value="' . htmlspecialchars($this->searchString) . '" />
600 </div>
601 <div class="form-group col-xs-12 col-sm-6">
602 <label for="search_levels">' . htmlspecialchars($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.label.search_levels')) . ': </label>
603 ' . $lMenu . '
604 </div>
605 <div class="form-group col-xs-12 col-sm-6">
606 <label for="showLimit">' . htmlspecialchars($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.label.limit')) . ': </label>
607 <input class="form-control" type="number" min="0" max="10000" placeholder="10" title="' . htmlspecialchars($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.title.limit')) . '" name="showLimit" id="showLimit" value="' . htmlspecialchars(($this->showLimit ? $this->showLimit : '')) . '" />
608 </div>
609 <div class="form-group col-xs-12">
610 <div class="form-control-wrap">
611 <button type="submit" class="btn btn-default" name="search" title="' . htmlspecialchars($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.title.search')) . '">
612 ' . $iconFactory->getIcon('actions-search', Icon::SIZE_SMALL)->render() . ' ' . htmlspecialchars($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.search')) . '
613 </button>
614 </div>
615 </div>
616 </div>
617 </div>
618 </div>
619 </div>
620 ' . $formElements[1] . '</div>';
621 return $content;
622 }
623
624 /******************************
625 *
626 * Various helper functions
627 *
628 ******************************/
629 /**
630 * Setting the field names to display in extended list.
631 * Sets the internal variable $this->setFields
632 */
633 public function setDispFields()
634 {
635 $backendUser = $this->getBackendUserAuthentication();
636 // Getting from session:
637 $dispFields = $backendUser->getModuleData('list/displayFields');
638 // If fields has been inputted, then set those as the value and push it to session variable:
639 if (is_array($this->displayFields)) {
640 reset($this->displayFields);
641 $tKey = key($this->displayFields);
642 $dispFields[$tKey] = $this->displayFields[$tKey];
643 $backendUser->pushModuleData('list/displayFields', $dispFields);
644 }
645 // Setting result:
646 $this->setFields = $dispFields;
647 }
648
649 /**
650 * Create thumbnail code for record/field
651 *
652 * @param mixed[] $row Record array
653 * @param string $table Table (record is from)
654 * @param string $field Field name for which thumbnail are to be rendered.
655 * @return string HTML for thumbnails, if any.
656 */
657 public function thumbCode($row, $table, $field)
658 {
659 return BackendUtility::thumbCode($row, $table, $field);
660 }
661
662 /**
663 * Returns the SQL-query array to select the records from a table $table with pid = $id
664 *
665 * @param string $table Table name
666 * @param int $id Page id (NOT USED! $this->pidSelect is used instead)
667 * @param string $addWhere Additional part for where clause
668 * @param string $fieldList Field list to select, * for all (for "SELECT [fieldlist] FROM ...")
669 * @return string[] Returns query array
670 *
671 * @deprecated since TYPO3 v8, will be removed in TYPO3 v9. Please use getQueryBuilder()
672 */
673 public function makeQueryArray($table, $id, $addWhere = '', $fieldList = '*')
674 {
675 GeneralUtility::logDeprecatedFunction();
676 $hookObjectsArr = [];
677 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/class.db_list.inc']['makeQueryArray'])) {
678 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/class.db_list.inc']['makeQueryArray'] as $classRef) {
679 $hookObjectsArr[] = GeneralUtility::getUserObj($classRef);
680 }
681 }
682 // Set ORDER BY:
683 $orderBy = $GLOBALS['TCA'][$table]['ctrl']['sortby'] ? 'ORDER BY ' . $GLOBALS['TCA'][$table]['ctrl']['sortby'] : $GLOBALS['TCA'][$table]['ctrl']['default_sortby'];
684 if ($this->sortField) {
685 if (in_array($this->sortField, $this->makeFieldList($table, 1))) {
686 $orderBy = 'ORDER BY ' . $this->sortField;
687 if ($this->sortRev) {
688 $orderBy .= ' DESC';
689 }
690 }
691 }
692 // Set LIMIT:
693 $limit = $this->iLimit ? ($this->firstElementNumber ? $this->firstElementNumber . ',' : '') . $this->iLimit : '';
694 // Filtering on displayable pages (permissions):
695 $pC = $table === 'pages' && $this->perms_clause ? ' AND ' . $this->perms_clause : '';
696 // Adding search constraints:
697 $search = $this->makeSearchString($table, $id);
698 // Compiling query array:
699 $queryParts = [
700 'SELECT' => $fieldList,
701 'FROM' => $table,
702 'WHERE' => $this->getPageIdConstraint($table) . ' ' . $pC . BackendUtility::deleteClause($table) . BackendUtility::versioningPlaceholderClause($table) . ' ' . $addWhere . ' ' . $search,
703 'GROUPBY' => '',
704 'LIMIT' => $limit
705 ];
706 $tempOrderBy = [];
707 foreach (QueryHelper::parseOrderBy($orderBy) as $orderPair) {
708 list($fieldName, $order) = $orderPair;
709 if ($order !== null) {
710 $tempOrderBy[] = implode(' ', $orderPair);
711 } else {
712 $tempOrderBy[] = $fieldName;
713 }
714 }
715 $queryParts['ORDERBY'] = implode(',', $tempOrderBy);
716 // Filter out records that are translated, if TSconfig mod.web_list.hideTranslations is set
717 if ((in_array($table, GeneralUtility::trimExplode(',', $this->hideTranslations)) || $this->hideTranslations === '*') && !empty($GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']) && $table !== 'pages_language_overlay') {
718 $queryParts['WHERE'] .= ' AND ' . $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'] . '=0 ';
719 }
720 // Apply hook as requested in http://forge.typo3.org/issues/16634
721 foreach ($hookObjectsArr as $hookObj) {
722 if (method_exists($hookObj, 'makeQueryArray_post')) {
723 $_params = [
724 'orderBy' => $orderBy,
725 'limit' => $limit,
726 'pC' => $pC,
727 'search' => $search
728 ];
729 $hookObj->makeQueryArray_post($queryParts, $this, $table, $id, $addWhere, $fieldList, $_params);
730 }
731 }
732 // Return query:
733 return $queryParts;
734 }
735
736 /**
737 * Returns a QueryBuilder configured to select $fields from $table where the pid is restricted
738 * depending on the current searchlevel setting.
739 *
740 * @param string $table Table name
741 * @param int $pageId Page id Only used to build the search constraints, getPageIdConstraint() used for restrictions
742 * @param string[] $additionalConstraints Additional part for where clause
743 * @param string[] $fields Field list to select, * for all
744 * @return \TYPO3\CMS\Core\Database\Query\QueryBuilder
745 */
746 public function getQueryBuilder(
747 string $table,
748 int $pageId,
749 array $additionalConstraints = [],
750 array $fields = ['*']
751 ): QueryBuilder {
752 $queryParameters = $this->buildQueryParameters($table, $pageId, $fields, $additionalConstraints);
753
754 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
755 ->getQueryBuilderForTable($queryParameters['table']);
756 $queryBuilder->getRestrictions()
757 ->removeAll()
758 ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
759 ->add(GeneralUtility::makeInstance(BackendWorkspaceRestriction::class));
760 $queryBuilder
761 ->select(...$queryParameters['fields'])
762 ->from($queryParameters['table'])
763 ->where(...$queryParameters['where']);
764
765 if (!empty($queryParameters['orderBy'])) {
766 foreach ($queryParameters['orderBy'] as $fieldNameAndSorting) {
767 list($fieldName, $sorting) = $fieldNameAndSorting;
768 $queryBuilder->addOrderBy($fieldName, $sorting);
769 }
770 }
771
772 if (!empty($queryParameters['firstResult'])) {
773 $queryBuilder->setFirstResult((int)$queryParameters['firstResult']);
774 }
775
776 if (!empty($queryParameters['maxResults'])) {
777 $queryBuilder->setMaxResults((int)$queryParameters['maxResults']);
778 }
779
780 if (!empty($queryParameters['groupBy'])) {
781 $queryBuilder->groupBy($queryParameters['groupBy']);
782 }
783
784 return $queryBuilder;
785 }
786
787 /**
788 * Return the query parameters to select the records from a table $table with pid = $this->pidList
789 *
790 * @param string $table Table name
791 * @param int $pageId Page id Only used to build the search constraints, $this->pidList is used for restrictions
792 * @param string[] $fieldList List of fields to select from the table
793 * @param string[] $additionalConstraints Additional part for where clause
794 * @param bool $addSorting Add sorting fields to query
795 * @return array
796 */
797 protected function buildQueryParameters(
798 string $table,
799 int $pageId,
800 array $fieldList = ['*'],
801 array $additionalConstraints = [],
802 bool $addSorting = true
803 ): array {
804 $expressionBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
805 ->getQueryBuilderForTable($table)
806 ->expr();
807
808 $parameters = [
809 'table' => $table,
810 'fields' => $fieldList,
811 'groupBy' => null,
812 'orderBy' => null,
813 'firstResult' => $this->firstElementNumber ?: null,
814 'maxResults' => $this->iLimit ? $this->iLimit : null,
815 ];
816
817 if ($addSorting === true) {
818 if ($this->sortField && in_array($this->sortField, $this->makeFieldList($table, 1))) {
819 $parameters['orderBy'][] = $this->sortRev ? [$this->sortField, 'DESC'] : [$this->sortField, 'ASC'];
820 } else {
821 $orderBy = $GLOBALS['TCA'][$table]['ctrl']['sortby'] ?: $GLOBALS['TCA'][$table]['ctrl']['default_sortby'];
822 $parameters['orderBy'] = QueryHelper::parseOrderBy((string)$orderBy);
823 }
824 }
825
826 // Build the query constraints
827 $constraints = [
828 'pidSelect' => $this->getPageIdConstraint($table),
829 'search' => $this->makeSearchString($table, $pageId)
830 ];
831
832 // Filtering on displayable pages (permissions):
833 if ($table === 'pages' && $this->perms_clause) {
834 $constraints['pagePermsClause'] = $this->perms_clause;
835 }
836
837 // Filter out records that are translated, if TSconfig mod.web_list.hideTranslations is set
838 if ((GeneralUtility::inList($this->hideTranslations, $table) || $this->hideTranslations === '*')
839 && !empty($GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'])
840 && $table !== 'pages_language_overlay'
841 ) {
842 $constraints['transOrigPointerField'] = $expressionBuilder->eq(
843 $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'],
844 0
845 );
846 }
847
848 $parameters['where'] = array_merge($constraints, $additionalConstraints);
849
850 $hookName = DatabaseRecordList::class;
851 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'][$hookName]['buildQueryParameters'])) {
852 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'][$hookName]['buildQueryParameters'] as $classRef) {
853 $hookObject = GeneralUtility::getUserObj($classRef);
854 if (method_exists($hookObject, 'buildQueryParametersPostProcess')) {
855 $hookObject->buildQueryParametersPostProcess(
856 $parameters,
857 $table,
858 $pageId,
859 $additionalConstraints,
860 $fieldList,
861 $this
862 );
863 }
864 }
865 }
866
867 // array_unique / array_filter used to eliminate empty and duplicate constraints
868 // the array keys are eliminated by this as well to facilitate argument unpacking
869 // when used with the querybuilder.
870 $parameters['where'] = array_unique(array_filter(array_values($parameters['where'])));
871
872 return $parameters;
873 }
874
875 /**
876 * Set the total items for the record list
877 *
878 * @param string $table Table name
879 * @param int $pageId Only used to build the search constraints, $this->pidList is used for restrictions
880 * @param array $constraints Additional constraints for where clause
881 */
882 public function setTotalItems(string $table, int $pageId, array $constraints)
883 {
884 $queryParameters = $this->buildQueryParameters($table, $pageId, ['*'], $constraints, false);
885 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
886 ->getQueryBuilderForTable($queryParameters['table']);
887 $queryBuilder->getRestrictions()
888 ->removeAll()
889 ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
890 ->add(GeneralUtility::makeInstance(BackendWorkspaceRestriction::class));
891 $queryBuilder
892 ->from($queryParameters['table'])
893 ->where(...$queryParameters['where']);
894
895 $this->totalItems = (int)$queryBuilder->count('*')
896 ->execute()
897 ->fetchColumn();
898 }
899
900 /**
901 * Creates part of query for searching after a word ($this->searchString)
902 * fields in input table.
903 *
904 * @param string $table Table, in which the fields are being searched.
905 * @param int $currentPid Page id for the possible search limit. -1 only if called from an old XCLASS.
906 * @return string Returns part of WHERE-clause for searching, if applicable.
907 */
908 public function makeSearchString($table, $currentPid = -1)
909 {
910 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
911 $expressionBuilder = $queryBuilder->expr();
912 $constraints = [];
913 $currentPid = (int)$currentPid;
914 $tablePidField = $table === 'pages' ? 'uid' : 'pid';
915 // Make query only if table is valid and a search string is actually defined
916 if (empty($this->searchString)) {
917 return '';
918 }
919
920 $searchableFields = $this->getSearchFields($table);
921 if (MathUtility::canBeInterpretedAsInteger($this->searchString)) {
922 $constraints[] = $expressionBuilder->eq('uid', (int)$this->searchString);
923 foreach ($searchableFields as $fieldName) {
924 if (!isset($GLOBALS['TCA'][$table]['columns'][$fieldName])) {
925 continue;
926 }
927 $fieldConfig = $GLOBALS['TCA'][$table]['columns'][$fieldName]['config'];
928 $fieldType = $fieldConfig['type'];
929 $evalRules = $fieldConfig['eval'] ?: '';
930 if ($fieldType === 'input' && $evalRules && GeneralUtility::inList($evalRules, 'int')) {
931 if (is_array($fieldConfig['search'])
932 && in_array('pidonly', $fieldConfig['search'], true)
933 && $currentPid > 0
934 ) {
935 $constraints[] = $expressionBuilder->andX(
936 $expressionBuilder->eq($fieldName, (int)$this->searchString),
937 $expressionBuilder->eq($tablePidField, (int)$currentPid)
938 );
939 }
940 } elseif ($fieldType === 'text'
941 || $fieldType === 'flex'
942 || ($fieldType === 'input' && (!$evalRules || !preg_match('/date|time|int/', $evalRules)))
943 ) {
944 $constraints[] = $expressionBuilder->like(
945 $fieldName,
946 $queryBuilder->quote('%' . (int)$this->searchString . '%')
947 );
948 }
949 }
950 } elseif (!empty($searchableFields)) {
951 $like = $queryBuilder->quote('%' . $queryBuilder->escapeLikeWildcards($this->searchString) . '%');
952 foreach ($searchableFields as $fieldName) {
953 if (!isset($GLOBALS['TCA'][$table]['columns'][$fieldName])) {
954 continue;
955 }
956 $fieldConfig = $GLOBALS['TCA'][$table]['columns'][$fieldName]['config'];
957 $fieldType = $fieldConfig['type'];
958 $evalRules = $fieldConfig['eval'] ?: '';
959 $searchConstraint = $expressionBuilder->andX(
960 $expressionBuilder->comparison(
961 'LOWER(' . $queryBuilder->quoteIdentifier($fieldName) . ')',
962 'LIKE',
963 'LOWER(' . $like . ')'
964 )
965 );
966 if (is_array($fieldConfig['search'])) {
967 $searchConfig = $fieldConfig['search'];
968 if (in_array('case', $searchConfig)) {
969 // Replace case insensitive default constraint
970 $searchConstraint = $expressionBuilder->andX($expressionBuilder->like($fieldName, $like));
971 }
972 if (in_array('pidonly', $searchConfig) && $currentPid > 0) {
973 $searchConstraint->add($expressionBuilder->eq($tablePidField, (int)$currentPid));
974 }
975 if ($searchConfig['andWhere']) {
976 $searchConstraint->add(
977 QueryHelper::stripLogicalOperatorPrefix($fieldConfig['search']['andWhere'])
978 );
979 }
980 }
981 if ($fieldType === 'text'
982 || $fieldType === 'flex'
983 || $fieldType === 'input' && (!$evalRules || !preg_match('/date|time|int/', $evalRules))
984 ) {
985 if ($searchConstraint->count() !== 0) {
986 $constraints[] = $searchConstraint;
987 }
988 }
989 }
990 }
991 // If no search field conditions have been built ensure no results are returned
992 if (empty($constraints)) {
993 return '0=1';
994 }
995
996 return $expressionBuilder->orX(...$constraints);
997 }
998
999 /**
1000 * Fetches a list of fields to use in the Backend search for the given table.
1001 *
1002 * @param string $tableName
1003 * @return string[]
1004 */
1005 protected function getSearchFields($tableName)
1006 {
1007 $fieldArray = [];
1008 $fieldListWasSet = false;
1009 // Get fields from ctrl section of TCA first
1010 if (isset($GLOBALS['TCA'][$tableName]['ctrl']['searchFields'])) {
1011 $fieldArray = GeneralUtility::trimExplode(',', $GLOBALS['TCA'][$tableName]['ctrl']['searchFields'], true);
1012 $fieldListWasSet = true;
1013 }
1014 // Call hook to add or change the list
1015 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['mod_list']['getSearchFieldList'])) {
1016 $hookParameters = [
1017 'tableHasSearchConfiguration' => $fieldListWasSet,
1018 'tableName' => $tableName,
1019 'searchFields' => &$fieldArray,
1020 'searchString' => $this->searchString
1021 ];
1022 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['mod_list']['getSearchFieldList'] as $hookFunction) {
1023 GeneralUtility::callUserFunction($hookFunction, $hookParameters, $this);
1024 }
1025 }
1026 return $fieldArray;
1027 }
1028
1029 /**
1030 * Returns the title (based on $code) of a table ($table) with the proper link around. For headers over tables.
1031 * The link will cause the display of all extended mode or not for the table.
1032 *
1033 * @param string $table Table name
1034 * @param string $code Table label
1035 * @return string The linked table label
1036 */
1037 public function linkWrapTable($table, $code)
1038 {
1039 if ($this->table !== $table) {
1040 return '<a href="' . htmlspecialchars($this->listURL('', $table, 'firstElementNumber')) . '">' . $code . '</a>';
1041 }
1042 return '<a href="' . htmlspecialchars($this->listURL('', '', 'sortField,sortRev,table,firstElementNumber')) . '">' . $code . '</a>';
1043 }
1044
1045 /**
1046 * Returns the title (based on $code) of a record (from table $table) with the proper link around (that is for 'pages'-records a link to the level of that record...)
1047 *
1048 * @param string $table Table name
1049 * @param int $uid Item uid
1050 * @param string $code Item title (not htmlspecialchars()'ed yet)
1051 * @param mixed[] $row Item row
1052 * @return string The item title. Ready for HTML output (is htmlspecialchars()'ed)
1053 */
1054 public function linkWrapItems($table, $uid, $code, $row)
1055 {
1056 $lang = $this->getLanguageService();
1057 $origCode = $code;
1058 // If the title is blank, make a "no title" label:
1059 if ((string)$code === '') {
1060 $code = '<i>[' . htmlspecialchars($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.no_title')) . ']</i> - '
1061 . htmlspecialchars(BackendUtility::getRecordTitle($table, $row));
1062 } else {
1063 $code = htmlspecialchars($code, ENT_QUOTES, 'UTF-8', false);
1064 if ($code != htmlspecialchars($origCode)) {
1065 $code = '<span title="' . htmlspecialchars($origCode, ENT_QUOTES, 'UTF-8', false) . '">' . $code . '</span>';
1066 }
1067 }
1068 switch ((string)$this->clickTitleMode) {
1069 case 'edit':
1070 // If the listed table is 'pages' we have to request the permission settings for each page:
1071 if ($table === 'pages') {
1072 $localCalcPerms = $this->getBackendUserAuthentication()->calcPerms(BackendUtility::getRecord('pages', $row['uid']));
1073 $permsEdit = $localCalcPerms & Permission::PAGE_EDIT;
1074 } else {
1075 $permsEdit = $this->calcPerms & Permission::CONTENT_EDIT;
1076 }
1077 // "Edit" link: ( Only if permissions to edit the page-record of the content of the parent page ($this->id)
1078 if ($permsEdit) {
1079 $params = '&edit[' . $table . '][' . $row['uid'] . ']=edit';
1080 $code = '<a href="#" onclick="' . htmlspecialchars(BackendUtility::editOnClick($params, '', -1)) . '" title="' . htmlspecialchars($lang->getLL('edit')) . '">' . $code . '</a>';
1081 }
1082 break;
1083 case 'show':
1084 // "Show" link (only pages and tt_content elements)
1085 if ($table === 'pages' || $table === 'tt_content') {
1086 $code = '<a href="#" onclick="' . htmlspecialchars(
1087 BackendUtility::viewOnClick(($table === 'tt_content' ? $this->id . '#' . $row['uid'] : $row['uid']))
1088 ) . '" title="' . htmlspecialchars($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.showPage')) . '">' . $code . '</a>';
1089 }
1090 break;
1091 case 'info':
1092 // "Info": (All records)
1093 $code = '<a href="#" onclick="' . htmlspecialchars(('top.launchView(\'' . $table . '\', \'' . $row['uid'] . '\'); return false;')) . '" title="' . htmlspecialchars($lang->getLL('showInfo')) . '">' . $code . '</a>';
1094 break;
1095 default:
1096 // Output the label now:
1097 if ($table === 'pages') {
1098 $code = '<a href="' . htmlspecialchars($this->listURL($uid, '', 'firstElementNumber')) . '" onclick="setHighlight(' . $uid . ')">' . $code . '</a>';
1099 } else {
1100 $code = $this->linkUrlMail($code, $origCode);
1101 }
1102 }
1103 return $code;
1104 }
1105
1106 /**
1107 * Wrapping input code in link to URL or email if $testString is either.
1108 *
1109 * @param string $code code to wrap
1110 * @param string $testString String which is tested for being a URL or email and which will be used for the link if so.
1111 * @return string Link-Wrapped $code value, if $testString was URL or email.
1112 */
1113 public function linkUrlMail($code, $testString)
1114 {
1115 // Check for URL:
1116 $scheme = parse_url($testString, PHP_URL_SCHEME);
1117 if ($scheme === 'http' || $scheme === 'https' || $scheme === 'ftp') {
1118 return '<a href="' . htmlspecialchars($testString) . '" target="_blank">' . $code . '</a>';
1119 }
1120 // Check for email:
1121 if (GeneralUtility::validEmail($testString)) {
1122 return '<a href="mailto:' . htmlspecialchars($testString) . '" target="_blank">' . $code . '</a>';
1123 }
1124 // Return if nothing else...
1125 return $code;
1126 }
1127
1128 /**
1129 * Creates the URL to this script, including all relevant GPvars
1130 * Fixed GPvars are id, table, imagemode, returnUrl, search_field, search_levels and showLimit
1131 * The GPvars "sortField" and "sortRev" are also included UNLESS they are found in the $exclList variable.
1132 *
1133 * @param string $altId Alternative id value. Enter blank string for the current id ($this->id)
1134 * @param string $table Table name to display. Enter "-1" for the current table.
1135 * @param string $exclList Comma separated list of fields NOT to include ("sortField", "sortRev" or "firstElementNumber")
1136 * @return string URL
1137 */
1138 public function listURL($altId = '', $table = '-1', $exclList = '')
1139 {
1140 $urlParameters = [];
1141 if ((string)$altId !== '') {
1142 $urlParameters['id'] = $altId;
1143 } else {
1144 $urlParameters['id'] = $this->id;
1145 }
1146 if ($table === '-1') {
1147 $urlParameters['table'] = $this->table;
1148 } else {
1149 $urlParameters['table'] = $table;
1150 }
1151 if ($this->thumbs) {
1152 $urlParameters['imagemode'] = $this->thumbs;
1153 }
1154 if ($this->returnUrl) {
1155 $urlParameters['returnUrl'] = $this->returnUrl;
1156 }
1157 if ((!$exclList || !GeneralUtility::inList($exclList, 'search_field')) && $this->searchString) {
1158 $urlParameters['search_field'] = $this->searchString;
1159 }
1160 if ($this->searchLevels) {
1161 $urlParameters['search_levels'] = $this->searchLevels;
1162 }
1163 if ($this->showLimit) {
1164 $urlParameters['showLimit'] = $this->showLimit;
1165 }
1166 if ((!$exclList || !GeneralUtility::inList($exclList, 'firstElementNumber')) && $this->firstElementNumber) {
1167 $urlParameters['pointer'] = $this->firstElementNumber;
1168 }
1169 if ((!$exclList || !GeneralUtility::inList($exclList, 'sortField')) && $this->sortField) {
1170 $urlParameters['sortField'] = $this->sortField;
1171 }
1172 if ((!$exclList || !GeneralUtility::inList($exclList, 'sortRev')) && $this->sortRev) {
1173 $urlParameters['sortRev'] = $this->sortRev;
1174 }
1175
1176 $urlParameters = array_merge_recursive($urlParameters, $this->overrideUrlParameters);
1177
1178 if ($routePath = GeneralUtility::_GP('route')) {
1179 $router = GeneralUtility::makeInstance(Router::class);
1180 $route = $router->match($routePath);
1181 $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
1182 $url = (string)$uriBuilder->buildUriFromRoute($route->getOption('_identifier'), $urlParameters);
1183 } elseif ($moduleName = GeneralUtility::_GP('M')) {
1184 $url = BackendUtility::getModuleUrl($moduleName, $urlParameters);
1185 } else {
1186 $url = GeneralUtility::getIndpEnv('SCRIPT_NAME') . '?' . ltrim(GeneralUtility::implodeArrayForUrl('', $urlParameters), '&');
1187 }
1188 return $url;
1189 }
1190
1191 /**
1192 * Returns "requestUri" - which is basically listURL
1193 *
1194 * @return string Content of ->listURL()
1195 */
1196 public function requestUri()
1197 {
1198 return $this->listURL();
1199 }
1200
1201 /**
1202 * Makes the list of fields to select for a table
1203 *
1204 * @param string $table Table name
1205 * @param bool $dontCheckUser If set, users access to the field (non-exclude-fields) is NOT checked.
1206 * @param bool $addDateFields If set, also adds crdate and tstamp fields (note: they will also be added if user is admin or dontCheckUser is set)
1207 * @return string[] Array, where values are fieldnames to include in query
1208 */
1209 public function makeFieldList($table, $dontCheckUser = false, $addDateFields = false)
1210 {
1211 $backendUser = $this->getBackendUserAuthentication();
1212 // Init fieldlist array:
1213 $fieldListArr = [];
1214 // Check table:
1215 if (is_array($GLOBALS['TCA'][$table]) && isset($GLOBALS['TCA'][$table]['columns']) && is_array($GLOBALS['TCA'][$table]['columns'])) {
1216 if (isset($GLOBALS['TCA'][$table]['columns']) && is_array($GLOBALS['TCA'][$table]['columns'])) {
1217 // Traverse configured columns and add them to field array, if available for user.
1218 foreach ($GLOBALS['TCA'][$table]['columns'] as $fN => $fieldValue) {
1219 if ($dontCheckUser || (!$fieldValue['exclude'] || $backendUser->check('non_exclude_fields', $table . ':' . $fN)) && $fieldValue['config']['type'] !== 'passthrough') {
1220 $fieldListArr[] = $fN;
1221 }
1222 }
1223
1224 $fieldListArr[] = 'uid';
1225 $fieldListArr[] = 'pid';
1226
1227 // Add date fields
1228 if ($dontCheckUser || $backendUser->isAdmin() || $addDateFields) {
1229 if ($GLOBALS['TCA'][$table]['ctrl']['tstamp']) {
1230 $fieldListArr[] = $GLOBALS['TCA'][$table]['ctrl']['tstamp'];
1231 }
1232 if ($GLOBALS['TCA'][$table]['ctrl']['crdate']) {
1233 $fieldListArr[] = $GLOBALS['TCA'][$table]['ctrl']['crdate'];
1234 }
1235 }
1236 // Add more special fields:
1237 if ($dontCheckUser || $backendUser->isAdmin()) {
1238 if ($GLOBALS['TCA'][$table]['ctrl']['cruser_id']) {
1239 $fieldListArr[] = $GLOBALS['TCA'][$table]['ctrl']['cruser_id'];
1240 }
1241 if ($GLOBALS['TCA'][$table]['ctrl']['sortby']) {
1242 $fieldListArr[] = $GLOBALS['TCA'][$table]['ctrl']['sortby'];
1243 }
1244 if (ExtensionManagementUtility::isLoaded('version') && $GLOBALS['TCA'][$table]['ctrl']['versioningWS']) {
1245 $fieldListArr[] = 't3ver_id';
1246 $fieldListArr[] = 't3ver_state';
1247 $fieldListArr[] = 't3ver_wsid';
1248 }
1249 }
1250 } else {
1251 GeneralUtility::sysLog(sprintf('$TCA is broken for the table "%s": no required "columns" entry in $TCA.', $table), 'core', GeneralUtility::SYSLOG_SEVERITY_ERROR);
1252 }
1253 }
1254 return $fieldListArr;
1255 }
1256
1257 /**
1258 * Get all allowed mount pages to be searched in.
1259 *
1260 * @param int $id Page id
1261 * @param int $depth Depth to go down
1262 * @param string $perms_clause select clause
1263 * @return int[]
1264 */
1265 protected function getSearchableWebmounts($id, $depth, $perms_clause)
1266 {
1267 $runtimeCache = GeneralUtility::makeInstance(CacheManager::class)->getCache('cache_runtime');
1268 $cacheIdentifier = md5('pidList_' . $id . '_' . $depth . '_' . $perms_clause);
1269 $idList = $runtimeCache->get($cacheIdentifier);
1270 if ($idList) {
1271 return $idList;
1272 }
1273
1274 $backendUser = $this->getBackendUserAuthentication();
1275 /** @var PageTreeView $tree */
1276 $tree = GeneralUtility::makeInstance(PageTreeView::class);
1277 $tree->init('AND ' . $perms_clause);
1278 $tree->makeHTML = 0;
1279 $tree->fieldArray = ['uid', 'php_tree_stop'];
1280 $idList = [];
1281
1282 $allowedMounts = !$backendUser->isAdmin() && $id === 0
1283 ? $backendUser->returnWebmounts()
1284 : [$id];
1285
1286 foreach ($allowedMounts as $allowedMount) {
1287 $idList[] = $allowedMount;
1288 if ($depth) {
1289 $tree->getTree($allowedMount, $depth, '');
1290 }
1291 $idList = array_merge($idList, $tree->ids);
1292 }
1293 $runtimeCache->set($cacheIdentifier, $idList);
1294 return $idList;
1295 }
1296
1297 /**
1298 * Redirects to FormEngine if a record is just localized.
1299 *
1300 * @param string $justLocalized String with table, orig uid and language separated by ":
1301 */
1302 public function localizationRedirect($justLocalized)
1303 {
1304 list($table, $orig_uid, $language) = explode(':', $justLocalized);
1305 if ($GLOBALS['TCA'][$table]
1306 && $GLOBALS['TCA'][$table]['ctrl']['languageField']
1307 && $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']
1308 ) {
1309 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
1310 $queryBuilder->getRestrictions()
1311 ->removeAll()
1312 ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
1313 ->add(GeneralUtility::makeInstance(BackendWorkspaceRestriction::class));
1314
1315 $localizedRecordUid = $queryBuilder->select('uid')
1316 ->from($table)
1317 ->where(
1318 $queryBuilder->expr()->eq(
1319 $GLOBALS['TCA'][$table]['ctrl']['languageField'],
1320 $queryBuilder->createNamedParameter($language, \PDO::PARAM_INT)
1321 ),
1322 $queryBuilder->expr()->eq(
1323 $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'],
1324 $queryBuilder->createNamedParameter($orig_uid, \PDO::PARAM_INT)
1325 )
1326 )
1327 ->setMaxResults(1)
1328 ->execute()
1329 ->fetchColumn();
1330
1331 if ($localizedRecordUid !== false) {
1332 // Create parameters and finally run the classic page module for creating a new page translation
1333 $url = $this->listURL();
1334 $editUserAccountUrl = BackendUtility::getModuleUrl(
1335 'record_edit',
1336 [
1337 'edit[' . $table . '][' . $localizedRecordUid . ']' => 'edit',
1338 'returnUrl' => $url
1339 ]
1340 );
1341 HttpUtility::redirect($editUserAccountUrl);
1342 }
1343 }
1344 }
1345
1346 /**
1347 * Set URL parameters to override or add in the listUrl() method.
1348 *
1349 * @param string[] $urlParameters
1350 */
1351 public function setOverrideUrlParameters(array $urlParameters)
1352 {
1353 $this->overrideUrlParameters = $urlParameters;
1354 }
1355
1356 /**
1357 * Set table display order information
1358 *
1359 * Structure of $orderInformation:
1360 * 'tableName' => [
1361 * 'before' => // comma-separated string list or array of table names
1362 * 'after' => // comma-separated string list or array of table names
1363 * ]
1364 *
1365 * @param array $orderInformation
1366 * @throws \UnexpectedValueException
1367 */
1368 public function setTableDisplayOrder(array $orderInformation)
1369 {
1370 foreach ($orderInformation as $tableName => &$configuration) {
1371 if (isset($configuration['before'])) {
1372 if (is_string($configuration['before'])) {
1373 $configuration['before'] = GeneralUtility::trimExplode(',', $configuration['before'], true);
1374 } elseif (!is_array($configuration['before'])) {
1375 throw new \UnexpectedValueException('The specified "before" order configuration for table "' . $tableName . '" is invalid.', 1436195933);
1376 }
1377 }
1378 if (isset($configuration['after'])) {
1379 if (is_string($configuration['after'])) {
1380 $configuration['after'] = GeneralUtility::trimExplode(',', $configuration['after'], true);
1381 } elseif (!is_array($configuration['after'])) {
1382 throw new \UnexpectedValueException('The specified "after" order configuration for table "' . $tableName . '" is invalid.', 1436195934);
1383 }
1384 }
1385 }
1386 $this->tableDisplayOrder = $orderInformation;
1387 }
1388
1389 /**
1390 * @return array
1391 */
1392 public function getOverridePageIdList(): array
1393 {
1394 return $this->overridePageIdList;
1395 }
1396
1397 /**
1398 * @param int[]|array $overridePageIdList
1399 */
1400 public function setOverridePageIdList(array $overridePageIdList)
1401 {
1402 $this->overridePageIdList = array_map('intval', $overridePageIdList);
1403 }
1404
1405 /**
1406 * Build SQL fragment to limit a query to a list of page IDs based on
1407 * the current search level setting.
1408 *
1409 * @param string $tableName
1410 * @return string
1411 */
1412 protected function getPageIdConstraint(string $tableName): string
1413 {
1414 // Set search levels:
1415 $searchLevels = $this->searchLevels;
1416
1417 // Set search levels to 999 instead of -1 as the following methods
1418 // do not support -1 as valid value for infinite search.
1419 if ($searchLevels === -1) {
1420 $searchLevels = 999;
1421 }
1422
1423 // Default is to search everywhere
1424 $constraint = '1=1';
1425
1426 $expressionBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
1427 ->getConnectionForTable($tableName)
1428 ->getExpressionBuilder();
1429
1430 if ($searchLevels === 0) {
1431 $constraint = $expressionBuilder->eq($tableName . '.pid', (int)$this->id);
1432 } elseif ($searchLevels > 0) {
1433 $allowedPidList = $this->getSearchableWebmounts($this->id, $searchLevels, $this->perms_clause);
1434 $constraint = $expressionBuilder->in($tableName . '.pid', array_map('intval', $allowedPidList));
1435 }
1436
1437 if (!empty($this->getOverridePageIdList())) {
1438 $constraint = $expressionBuilder->in(
1439 $tableName . '.pid',
1440 $this->getOverridePageIdList()
1441 );
1442 }
1443
1444 return (string)$constraint;
1445 }
1446
1447 /**
1448 * @return BackendUserAuthentication
1449 */
1450 protected function getBackendUserAuthentication()
1451 {
1452 return $GLOBALS['BE_USER'];
1453 }
1454 }