‪TYPO3CMS  9.5
LocalizationRepository.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
5 
6 /*
7  * This file is part of the TYPO3 CMS project.
8  *
9  * It is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License, either version 2
11  * of the License, or any later version.
12  *
13  * For the full copyright and license information, please read the
14  * LICENSE.txt file that was distributed with this source code.
15  *
16  * The TYPO3 project - inspiring people to share!
17  */
18 
19 use Doctrine\DBAL\Driver\Statement;
28 
35 {
40 
42  {
43  $this->translationConfigurationProvider = ‪$translationConfigurationProvider ?? GeneralUtility::makeInstance(TranslationConfigurationProvider::class);
44  }
45 
53  public function ‪fetchOriginLanguage(int $pageId, int $localizedLanguage): array
54  {
55  $queryBuilder = $this->‪getQueryBuilderWithWorkspaceRestriction('tt_content');
56 
57  $queryBuilder->select('tt_content_orig.sys_language_uid')
58  ->from('tt_content')
59  ->join(
60  'tt_content',
61  'tt_content',
62  'tt_content_orig',
63  $queryBuilder->expr()->eq(
64  'tt_content.l10n_source',
65  $queryBuilder->quoteIdentifier('tt_content_orig.uid')
66  )
67  )
68  ->where(
69  $queryBuilder->expr()->eq(
70  'tt_content.pid',
71  $queryBuilder->createNamedParameter($pageId, \PDO::PARAM_INT)
72  ),
73  $queryBuilder->expr()->eq(
74  'tt_content.sys_language_uid',
75  $queryBuilder->createNamedParameter($localizedLanguage, \PDO::PARAM_INT)
76  ),
77  $queryBuilder->expr()->neq(
78  'tt_content_orig.sys_language_uid',
79  $queryBuilder->createNamedParameter(-1, \PDO::PARAM_INT)
80  )
81  )
82  ->groupBy('tt_content_orig.sys_language_uid');
83  $this->‪getAllowedLanguageConstraintsForBackendUser($pageId, $queryBuilder, $this->‪getBackendUser(), 'tt_content_orig');
84 
85  return $queryBuilder->execute()->fetch() ?: [];
86  }
87 
96  public function ‪getLocalizedRecordCount(int $pageId, int $languageId): int
97  {
98  $queryBuilder = $this->‪getQueryBuilderWithWorkspaceRestriction('tt_content');
99 
100  $rowCount = $queryBuilder->count('uid')
101  ->from('tt_content')
102  ->where(
103  $queryBuilder->expr()->eq(
104  'sys_language_uid',
105  $queryBuilder->createNamedParameter($languageId, \PDO::PARAM_INT)
106  ),
107  $queryBuilder->expr()->eq(
108  'pid',
109  $queryBuilder->createNamedParameter($pageId, \PDO::PARAM_INT)
110  ),
111  $queryBuilder->expr()->neq(
112  'l10n_source',
113  $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)
114  )
115  )
116  ->execute()
117  ->fetchColumn(0);
118 
119  return (int)$rowCount;
120  }
121 
129  public function ‪fetchAvailableLanguages(int $pageId, int $languageId): array
130  {
131  $queryBuilder = $this->‪getQueryBuilderWithWorkspaceRestriction('tt_content');
132  $queryBuilder->select('sys_language_uid')
133  ->from('tt_content')
134  ->where(
135  $queryBuilder->expr()->eq(
136  'pid',
137  $queryBuilder->createNamedParameter($pageId, \PDO::PARAM_INT)
138  ),
139  $queryBuilder->expr()->neq(
140  'sys_language_uid',
141  $queryBuilder->createNamedParameter($languageId, \PDO::PARAM_INT)
142  )
143  )
144  ->groupBy('sys_language_uid');
145 
146  $this->‪getAllowedLanguageConstraintsForBackendUser($pageId, $queryBuilder, $this->‪getBackendUser());
147  $languages = $queryBuilder->execute()->fetchAll();
148  return $languages ?: [];
149  }
150 
160  protected function ‪getAllowedLanguageConstraintsForBackendUser(int $pageId, ‪QueryBuilder $queryBuilder, ‪BackendUserAuthentication $backendUser, string $alias = ''): void
161  {
162  if ($backendUser->‪isAdmin()) {
163  return;
164  }
165  // This always includes default language
166  $allowedLanguages = $this->translationConfigurationProvider->getSystemLanguages($pageId);
167  $queryBuilder->‪andWhere(
168  $queryBuilder->‪expr()->‪in(
169  ($alias === '' ? '' : ($alias . '.')) . 'sys_language_uid',
170  $queryBuilder->‪createNamedParameter(array_keys($allowedLanguages), Connection::PARAM_INT_ARRAY)
171  )
172  );
173  }
174 
184  public function ‪getRecordsToCopyDatabaseResult(int $pageId, int $destLanguageId, int $languageId, string ‪$fields = '*'): Statement
185  {
186  $originalUids = [];
187 
188  // Get original uid of existing elements triggered language
189  $queryBuilder = $this->‪getQueryBuilderWithWorkspaceRestriction('tt_content');
190 
191  $originalUidsStatement = $queryBuilder
192  ->‪select('l10n_source')
193  ->‪from('tt_content')
194  ->‪where(
195  $queryBuilder->‪expr()->‪eq(
196  'sys_language_uid',
197  $queryBuilder->‪createNamedParameter($destLanguageId, \PDO::PARAM_INT)
198  ),
199  $queryBuilder->‪expr()->‪eq(
200  'pid',
201  $queryBuilder->‪createNamedParameter($pageId, \PDO::PARAM_INT)
202  )
203  )
204  ->‪execute();
205 
206  while ($origUid = $originalUidsStatement->fetchColumn(0)) {
207  $originalUids[] = (int)$origUid;
208  }
209 
210  $queryBuilder->‪select(...GeneralUtility::trimExplode(',', ‪$fields, true))
211  ->from('tt_content')
212  ->‪where(
213  $queryBuilder->‪expr()->‪eq(
214  'sys_language_uid',
215  $queryBuilder->‪createNamedParameter($languageId, \PDO::PARAM_INT)
216  ),
217  $queryBuilder->‪expr()->‪eq(
218  'pid',
219  $queryBuilder->‪createNamedParameter($pageId, \PDO::PARAM_INT)
220  )
221  )
222  ->orderBy('sorting');
223 
224  if (!empty($originalUids)) {
225  $queryBuilder
226  ->‪andWhere(
227  $queryBuilder->‪expr()->‪notIn(
228  'uid',
229  $queryBuilder->‪createNamedParameter($originalUids, Connection::PARAM_INT_ARRAY)
230  )
231  );
232  }
233 
234  return $queryBuilder->‪execute();
235  }
236 
242  protected function ‪getBackendUser(): BackendUserAuthentication
243  {
244  return ‪$GLOBALS['BE_USER'];
245  }
246 
254  protected function ‪getQueryBuilderWithWorkspaceRestriction(string $tableName): QueryBuilder
255  {
256  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($tableName);
257  $queryBuilder->‪getRestrictions()
258  ->‪removeAll()
259  ->‪add(GeneralUtility::makeInstance(DeletedRestriction::class))
260  ->‪add(GeneralUtility::makeInstance(BackendWorkspaceRestriction::class));
261 
262  return $queryBuilder;
263  }
264 }
‪TYPO3\CMS\Core\Database\Query\QueryBuilder\select
‪QueryBuilder select(string ... $selects)
Definition: QueryBuilder.php:390
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder\eq
‪string eq(string $fieldName, $value)
Definition: ExpressionBuilder.php:107
‪TYPO3\CMS\Backend\Domain\Repository\Localization\LocalizationRepository
Definition: LocalizationRepository.php:35
‪TYPO3\CMS\Core\Database\Query\QueryBuilder\createNamedParameter
‪string createNamedParameter($value, int $type=\PDO::PARAM_STR, string $placeHolder=null)
Definition: QueryBuilder.php:894
‪TYPO3\CMS\Core\Database\Query\Restriction\QueryRestrictionContainerInterface\removeAll
‪QueryRestrictionContainerInterface removeAll()
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder\notIn
‪string notIn(string $fieldName, $value)
Definition: ExpressionBuilder.php:260
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication\isAdmin
‪bool isAdmin()
Definition: BackendUserAuthentication.php:294
‪TYPO3\CMS\Backend\Domain\Repository\Localization\LocalizationRepository\getRecordsToCopyDatabaseResult
‪Statement getRecordsToCopyDatabaseResult(int $pageId, int $destLanguageId, int $languageId, string $fields=' *')
Definition: LocalizationRepository.php:183
‪TYPO3\CMS\Backend\Domain\Repository\Localization\LocalizationRepository\getAllowedLanguageConstraintsForBackendUser
‪getAllowedLanguageConstraintsForBackendUser(int $pageId, QueryBuilder $queryBuilder, BackendUserAuthentication $backendUser, string $alias='')
Definition: LocalizationRepository.php:159
‪TYPO3\CMS\Core\Database\Query\Restriction\BackendWorkspaceRestriction
Definition: BackendWorkspaceRestriction.php:28
‪TYPO3\CMS\Core\Database\Query\QueryBuilder\getRestrictions
‪QueryRestrictionContainerInterface getRestrictions()
Definition: QueryBuilder.php:89
‪TYPO3\CMS\Backend\Domain\Repository\Localization\LocalizationRepository\__construct
‪__construct(TranslationConfigurationProvider $translationConfigurationProvider=null)
Definition: LocalizationRepository.php:40
‪TYPO3\CMS\Backend\Domain\Repository\Localization\LocalizationRepository\getQueryBuilderWithWorkspaceRestriction
‪QueryBuilder getQueryBuilderWithWorkspaceRestriction(string $tableName)
Definition: LocalizationRepository.php:253
‪$fields
‪$fields
Definition: pages.php:4
‪TYPO3\CMS\Backend\Domain\Repository\Localization\LocalizationRepository\getLocalizedRecordCount
‪int getLocalizedRecordCount(int $pageId, int $languageId)
Definition: LocalizationRepository.php:95
‪TYPO3\CMS\Core\Database\Query\QueryBuilder
Definition: QueryBuilder.php:47
‪TYPO3\CMS\Core\Database\Query\QueryBuilder\from
‪QueryBuilder from(string $from, string $alias=null)
Definition: QueryBuilder.php:506
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder\in
‪string in(string $fieldName, $value)
Definition: ExpressionBuilder.php:242
‪TYPO3\CMS\Backend\Domain\Repository\Localization\LocalizationRepository\fetchAvailableLanguages
‪array fetchAvailableLanguages(int $pageId, int $languageId)
Definition: LocalizationRepository.php:128
‪TYPO3\CMS\Core\Database\Query\QueryBuilder\andWhere
‪QueryBuilder andWhere(... $where)
Definition: QueryBuilder.php:647
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:45
‪TYPO3\CMS\Core\Database\Query\QueryBuilder\execute
‪Doctrine DBAL Driver Statement int execute()
Definition: QueryBuilder.php:178
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:31
‪TYPO3\CMS\Backend\Domain\Repository\Localization
Definition: LocalizationRepository.php:4
‪TYPO3\CMS\Backend\Configuration\TranslationConfigurationProvider
Definition: TranslationConfigurationProvider.php:35
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction
Definition: DeletedRestriction.php:26
‪TYPO3\CMS\Backend\Domain\Repository\Localization\LocalizationRepository\fetchOriginLanguage
‪array fetchOriginLanguage(int $pageId, int $localizedLanguage)
Definition: LocalizationRepository.php:52
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:44
‪TYPO3\CMS\Core\Database\Query\Restriction\QueryRestrictionContainerInterface\add
‪QueryRestrictionContainerInterface add(QueryRestrictionInterface $restriction)
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Database\Query\QueryBuilder\where
‪QueryBuilder where(... $predicates)
Definition: QueryBuilder.php:630
‪TYPO3\CMS\Core\Database\Query\QueryBuilder\expr
‪ExpressionBuilder expr()
Definition: QueryBuilder.php:125
‪TYPO3\CMS\Backend\Domain\Repository\Localization\LocalizationRepository\getBackendUser
‪BackendUserAuthentication getBackendUser()
Definition: LocalizationRepository.php:241
‪TYPO3\CMS\Backend\Domain\Repository\Localization\LocalizationRepository\$translationConfigurationProvider
‪TranslationConfigurationProvider $translationConfigurationProvider
Definition: LocalizationRepository.php:38