‪TYPO3CMS  ‪main
TranslationConfigurationProvider.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 TYPO3\CMS\Backend\Utility\BackendUtility;
31 
39 {
40  protected array ‪$systemLanguageCache = [];
41 
49  public function getSystemLanguages($pageId = 0)
50  {
51  if (isset($this->‪systemLanguageCache[$pageId])) {
52  return $this->‪systemLanguageCache[$pageId];
53  }
55  ‪$siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
56  if ($pageId === 0) {
57  // Used for e.g. filelist, where there is no site selected
58  // This also means that there is no "-1" (All Languages) selectable.
59  $sites = ‪$siteFinder->getAllSites();
60  foreach ($sites as $site) {
63  $site->getAvailableLanguages($this->getBackendUserAuthentication()),
64  $site,
65  true
66  );
67  }
68  } else {
69  try {
70  $site = ‪$siteFinder->getSiteByPageId((int)$pageId);
71  } catch (SiteNotFoundException $e) {
72  $site = new NullSite();
73  }
74  $siteLanguages = $site->getAvailableLanguages($this->‪getBackendUserAuthentication(), true);
75  if (!isset($siteLanguages[0])) {
76  $siteLanguages[0] = $site->getDefaultLanguage();
77  }
80  $siteLanguages,
81  $site,
82  false
83  );
84  }
88  }
89 
90  protected function ‪addSiteLanguagesToConsolidatedList(array ‪$allSystemLanguages, array $languagesOfSpecificSite, ‪SiteInterface $site, bool $includeSiteSuffix): array
91  {
92  foreach ($languagesOfSpecificSite as $language) {
93  $languageId = $language->getLanguageId();
94  if (isset(‪$allSystemLanguages[$languageId])) {
95  // Language already provided by another site, just add the label separately
96  ‪$allSystemLanguages[$languageId]['title'] .= ', ' . $language->getTitle() . ' [Site: ' . $site->‪getIdentifier() . ']';
97  } else {
98  ‪$allSystemLanguages[$languageId] = [
99  'uid' => $languageId,
100  'title' => $language->getTitle() . ($includeSiteSuffix ? ' [Site: ' . $site->‪getIdentifier() . ']' : ''),
101  'flagIcon' => $language->getFlagIdentifier(),
102  ];
103  }
104  }
105  return ‪$allSystemLanguages;
106  }
107 
119  public function ‪translationInfo($table, ‪$uid, $languageUid = 0, array $row = null, $selFieldList = '')
120  {
121  if (!‪$GLOBALS['TCA'][$table] || !‪$uid) {
122  return 'No table "' . $table . '" or no UID value';
123  }
124  if ($row === null) {
125  $row = BackendUtility::getRecordWSOL($table, ‪$uid);
126  }
127  if (!is_array($row)) {
128  return 'Record "' . $table . '_' . ‪$uid . '" was not found';
129  }
130  if (!BackendUtility::isTableLocalizable($table)) {
131  return 'Translation is not supported for this table!';
132  }
133  if ($row[‪$GLOBALS['TCA'][$table]['ctrl']['languageField']] > 0) {
134  return 'Record "' . $table . '_' . ‪$uid . '" seems to be a translation already (has a language value "' . $row[‪$GLOBALS['TCA'][$table]['ctrl']['languageField']] . '", relation to record "' . $row[‪$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']] . '")';
135  }
136  if ($row[‪$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']] != 0) {
137  return 'Record "' . $table . '_' . ‪$uid . '" seems to be a translation already (has a relation to record "' . $row[‪$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']] . '")';
138  }
139  // Look for translations of this record, index by language field value:
140  if (!empty($selFieldList)) {
141  if (is_array($selFieldList)) {
142  $selectFields = $selFieldList;
143  } else {
144  $selectFields = GeneralUtility::trimExplode(',', $selFieldList);
145  }
146  } else {
147  $selectFields = ['uid', ‪$GLOBALS['TCA'][$table]['ctrl']['languageField']];
148  }
149  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
150  $queryBuilder->getRestrictions()
151  ->removeAll()
152  ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
153  ->add(GeneralUtility::makeInstance(WorkspaceRestriction::class, $this->‪getBackendUserAuthentication()->workspace));
154  $queryBuilder
155  ->select(...$selectFields)
156  ->from($table)
157  ->where(
158  $queryBuilder->expr()->eq(
159  ‪$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'],
160  $queryBuilder->createNamedParameter(‪$uid, ‪Connection::PARAM_INT)
161  ),
162  $queryBuilder->expr()->eq(
163  'pid',
164  $queryBuilder->createNamedParameter(
165  $row['pid'],
167  )
168  )
169  );
170  if (!$languageUid) {
171  $queryBuilder->andWhere(
172  $queryBuilder->expr()->gt(
173  ‪$GLOBALS['TCA'][$table]['ctrl']['languageField'],
174  $queryBuilder->createNamedParameter(0, ‪Connection::PARAM_INT)
175  )
176  );
177  } else {
178  $queryBuilder
179  ->andWhere(
180  $queryBuilder->expr()->eq(
181  ‪$GLOBALS['TCA'][$table]['ctrl']['languageField'],
182  $queryBuilder->createNamedParameter($languageUid, ‪Connection::PARAM_INT)
183  )
184  );
185  }
186  $translationRecords = $queryBuilder
187  ->executeQuery()
188  ->fetchAllAssociative();
189 
190  $translations = [];
191  $translationsErrors = [];
192  foreach ($translationRecords as $translationRecord) {
193  if (!isset($translations[$translationRecord[‪$GLOBALS['TCA'][$table]['ctrl']['languageField']]])) {
194  $translations[$translationRecord[‪$GLOBALS['TCA'][$table]['ctrl']['languageField']]] = $translationRecord;
195  } else {
196  $translationsErrors[$translationRecord[‪$GLOBALS['TCA'][$table]['ctrl']['languageField']]][] = $translationRecord;
197  }
198  }
199  return [
200  'table' => $table,
201  'uid' => ‪$uid,
202  'CType' => $row['CType'] ?? '',
203  'sys_language_uid' => $row[‪$GLOBALS['TCA'][$table]['ctrl']['languageField'] ?? null] ?? null,
204  'translations' => $translations,
205  'excessive_translations' => $translationsErrors,
206  ];
207  }
208 
210  {
211  return ‪$GLOBALS['BE_USER'];
212  }
213 }
‪TYPO3\CMS\Core\Site\Entity\SiteInterface
Definition: SiteInterface.php:26
‪TYPO3\CMS\Backend\Configuration\TranslationConfigurationProvider\$siteFinder
‪$siteFinder
Definition: TranslationConfigurationProvider.php:55
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:50
‪TYPO3\CMS\Backend\Configuration\TranslationConfigurationProvider\translationInfo
‪mixed translationInfo($table, $uid, $languageUid=0, array $row=null, $selFieldList='')
Definition: TranslationConfigurationProvider.php:119
‪TYPO3\CMS\Core\Site\Entity\NullSite
Definition: NullSite.php:32
‪TYPO3\CMS\Core\Exception\SiteNotFoundException
Definition: SiteNotFoundException.php:25
‪TYPO3\CMS\Core\Site\SiteFinder
Definition: SiteFinder.php:31
‪TYPO3\CMS\Backend\Configuration\TranslationConfigurationProvider\getBackendUserAuthentication
‪getBackendUserAuthentication()
Definition: TranslationConfigurationProvider.php:209
‪TYPO3\CMS\Backend\Configuration\TranslationConfigurationProvider\addSiteLanguagesToConsolidatedList
‪addSiteLanguagesToConsolidatedList(array $allSystemLanguages, array $languagesOfSpecificSite, SiteInterface $site, bool $includeSiteSuffix)
Definition: TranslationConfigurationProvider.php:90
‪TYPO3\CMS\Backend\Configuration\TranslationConfigurationProvider\$allSystemLanguages
‪return $allSystemLanguages
Definition: TranslationConfigurationProvider.php:87
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:60
‪TYPO3\CMS\Backend\Configuration
Definition: BackendUserConfiguration.php:18
‪TYPO3\CMS\Backend\Configuration\TranslationConfigurationProvider\systemLanguageCache
‪array< LanguageRef, function getSystemLanguages( $pageId=0) { if(isset( $this->systemLanguageCache[ $pageId])) { return $this-> systemLanguageCache[$pageId]
Definition: TranslationConfigurationProvider.php:52
‪TYPO3\CMS\Backend\Configuration\TranslationConfigurationProvider\$allSystemLanguages
‪$allSystemLanguages
Definition: TranslationConfigurationProvider.php:54
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:39
‪TYPO3\CMS\Backend\Configuration\TranslationConfigurationProvider
Definition: TranslationConfigurationProvider.php:39
‪TYPO3\CMS\Webhooks\Message\$uid
‪identifier readonly int $uid
Definition: PageModificationMessage.php:35
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction
Definition: DeletedRestriction.php:28
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:48
‪TYPO3\CMS\Backend\Configuration\TranslationConfigurationProvider\$systemLanguageCache
‪array $systemLanguageCache
Definition: TranslationConfigurationProvider.php:40
‪TYPO3\CMS\Core\Site\Entity\SiteInterface\getIdentifier
‪getIdentifier()
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51
‪TYPO3\CMS\Core\Database\Query\Restriction\WorkspaceRestriction
Definition: WorkspaceRestriction.php:39