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