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