[BUGFIX] Refactor record querying in deep nested structures in recycler
[Packages/TYPO3.CMS.git] / typo3 / sysext / recycler / Classes / Domain / Model / Tables.php
1 <?php
2 namespace TYPO3\CMS\Recycler\Domain\Model;
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\Core\Database\ConnectionPool;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Recycler\Utility\RecyclerUtility;
20
21 /**
22 * Model class for the 'recycler' extension.
23 */
24 class Tables
25 {
26 /**
27 * Get tables for menu example
28 *
29 * @param int $startUid UID from selected page
30 * @param int $depth How many levels recursive
31 * @return array The tables to be displayed
32 */
33 public function getTables($startUid, $depth = 0)
34 {
35 $deletedRecordsTotal = 0;
36 $lang = $this->getLanguageService();
37 $tables = [];
38 $connection = GeneralUtility::makeInstance(ConnectionPool::class);
39 foreach (RecyclerUtility::getModifyableTables() as $tableName) {
40 $deletedField = RecyclerUtility::getDeletedField($tableName);
41 if ($deletedField) {
42 // Determine whether the table has deleted records:
43 $queryBuilder = $connection->getQueryBuilderForTable($tableName);
44 $queryBuilder->getRestrictions()->removeAll();
45
46 $deletedCount = $queryBuilder->count('uid')
47 ->from($tableName)
48 ->where(
49 $queryBuilder->expr()->neq(
50 $deletedField,
51 $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)
52 )
53 )
54 ->execute()
55 ->fetchColumn();
56
57 if ($deletedCount) {
58 /* @var $deletedDataObject DeletedRecords */
59 $deletedDataObject = GeneralUtility::makeInstance(DeletedRecords::class);
60 $deletedData = $deletedDataObject->loadData($startUid, $tableName, $depth)->getDeletedRows();
61 if (isset($deletedData[$tableName])) {
62 if ($deletedRecordsInTable = count($deletedData[$tableName])) {
63 $deletedRecordsTotal += $deletedRecordsInTable;
64 $tables[] = [
65 $tableName,
66 $deletedRecordsInTable,
67 $lang->sL($GLOBALS['TCA'][$tableName]['ctrl']['title'] ?? $tableName)
68 ];
69 }
70 }
71 }
72 }
73 }
74 $jsonArray = $tables;
75 array_unshift($jsonArray, [
76 '',
77 $deletedRecordsTotal,
78 $lang->sL('LLL:EXT:recycler/Resources/Private/Language/locallang.xlf:label_allrecordtypes')
79 ]);
80 return $jsonArray;
81 }
82
83 /**
84 * Returns an instance of LanguageService
85 *
86 * @return \TYPO3\CMS\Core\Localization\LanguageService
87 */
88 protected function getLanguageService()
89 {
90 return $GLOBALS['LANG'];
91 }
92 }