[TASK] Move LanguageService to EXT:core
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / FormDataProvider / DatabaseLanguageRows.php
1 <?php
2 namespace TYPO3\CMS\Backend\Form\FormDataProvider;
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\Configuration\TranslationConfigurationProvider;
18 use TYPO3\CMS\Backend\Form\Exception\DatabaseDefaultLanguageException;
19 use TYPO3\CMS\Backend\Form\FormDataProviderInterface;
20 use TYPO3\CMS\Backend\Utility\BackendUtility;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22
23 /**
24 * Add language related data to result array
25 */
26 class DatabaseLanguageRows implements FormDataProviderInterface
27 {
28 /**
29 * Fetch default language if handled record is a localized one,
30 * unserialize transOrigDiffSourceField if it is defined,
31 * fetch additional languages if requested.
32 *
33 * @param array $result
34 * @return array
35 * @throws DatabaseDefaultLanguageException
36 */
37 public function addData(array $result)
38 {
39 if (!empty($result['processedTca']['ctrl']['languageField'])
40 && !empty($result['processedTca']['ctrl']['transOrigPointerField'])
41 ) {
42 $languageField = $result['processedTca']['ctrl']['languageField'];
43 $fieldWithUidOfDefaultRecord = $result['processedTca']['ctrl']['transOrigPointerField'];
44
45 if (isset($result['databaseRow'][$languageField]) && $result['databaseRow'][$languageField] > 0
46 && isset($result['databaseRow'][$fieldWithUidOfDefaultRecord]) && $result['databaseRow'][$fieldWithUidOfDefaultRecord] > 0
47 ) {
48 // Table pages has its overlays in pages_language_overlay, this is accounted here
49 $tableNameWithDefaultRecords = $result['tableName'];
50 if ($tableNameWithDefaultRecords === 'pages_language_overlay') {
51 $tableNameWithDefaultRecords = 'pages';
52 }
53
54 // Default language record of localized record
55 $defaultLanguageRow = $this->getRecordWorkspaceOverlay(
56 $tableNameWithDefaultRecords,
57 (int)$result['databaseRow'][$fieldWithUidOfDefaultRecord]
58 );
59 if (empty($defaultLanguageRow)) {
60 throw new DatabaseDefaultLanguageException(
61 'Default language record with id ' . (int)$result['databaseRow'][$fieldWithUidOfDefaultRecord]
62 . ' not found in table ' . $result['tableName'] . ' while editing record ' . $result['databaseRow']['uid'],
63 1438249426
64 );
65 }
66 $result['defaultLanguageRow'] = $defaultLanguageRow;
67
68 // Unserialize the "original diff source" if given
69 if (!empty($result['processedTca']['ctrl']['transOrigDiffSourceField'])
70 && !empty($result['databaseRow'][$result['processedTca']['ctrl']['transOrigDiffSourceField']])
71 ) {
72 $defaultLanguageKey = $result['tableName'] . ':' . (int)$result['databaseRow']['uid'];
73 $result['defaultLanguageDiffRow'][$defaultLanguageKey] = unserialize($result['databaseRow'][$result['processedTca']['ctrl']['transOrigDiffSourceField']]);
74 }
75
76 // Add language overlays from further localizations if requested
77 // @todo: Permission check if user is in "restrict ot language" is missing here.
78 // @todo: The TranslationConfigurationProvider is more stupid than good for us ... invent a better translation overlay api!
79 if (!empty($result['userTsConfig']['options.']['additionalPreviewLanguages'])) {
80 $additionalLanguageUids = GeneralUtility::intExplode(',', $result['userTsConfig']['options.']['additionalPreviewLanguages'], true);
81 /** @var TranslationConfigurationProvider $translationProvider */
82 $translationProvider = GeneralUtility::makeInstance(TranslationConfigurationProvider::class);
83 foreach ($additionalLanguageUids as $additionalLanguageUid) {
84 // Continue if this system language record does not exist or if 0 or -1 is requested
85 // or if row is the same as the to-be-displayed row
86 if ($additionalLanguageUid <= 0
87 || !isset($result['systemLanguageRows'][$additionalLanguageUid])
88 || $additionalLanguageUid === (int)$result['databaseRow'][$languageField]
89 ) {
90 continue;
91 }
92 $translationInfo = $translationProvider->translationInfo(
93 $tableNameWithDefaultRecords,
94 (int)$result['databaseRow'][$fieldWithUidOfDefaultRecord],
95 $additionalLanguageUid
96 );
97 if (!empty($translationInfo['translations'][$additionalLanguageUid]['uid'])) {
98 $record = $this->getRecordWorkspaceOverlay(
99 $result['tableName'],
100 (int)$translationInfo['translations'][$additionalLanguageUid]['uid']
101 );
102 $result['additionalLanguageRows'][$additionalLanguageUid] = $record;
103 }
104 }
105 }
106
107 // @todo do that only if l10n_parent > 0 (not in "free mode")?
108 if (!empty($result['processedTca']['ctrl']['translationSource'])
109 && is_string($result['processedTca']['ctrl']['translationSource'])
110 ) {
111 $translationSourceFieldName = $result['processedTca']['ctrl']['translationSource'];
112 if (isset($result['databaseRow'][$translationSourceFieldName])
113 && $result['databaseRow'][$translationSourceFieldName] > 0
114 ) {
115 $uidOfTranslationSource = $result['databaseRow'][$translationSourceFieldName];
116 $result['sourceLanguageRow'] = $this->getRecordWorkspaceOverlay(
117 $result['tableName'],
118 $uidOfTranslationSource
119 );
120 }
121 }
122 }
123 }
124
125 return $result;
126 }
127
128 /**
129 * Retrieve the requested row from the database
130 *
131 * @param string $tableName
132 * @param int $uid
133 * @return array
134 */
135 protected function getRecordWorkspaceOverlay(string $tableName, int $uid): array
136 {
137 $row = BackendUtility::getRecordWSOL($tableName, $uid);
138
139 return $row ?: [];
140 }
141 }