-
Notifications
You must be signed in to change notification settings - Fork 638
/
LocalizationController.php
246 lines (215 loc) · 8.17 KB
/
LocalizationController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php
namespace TYPO3\CMS\Backend\Controller\Page;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Backend\Configuration\TranslationConfigurationProvider;
use TYPO3\CMS\Backend\Domain\Repository\Localization\LocalizationRepository;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\DataHandling\DataHandler;
use TYPO3\CMS\Core\Http\JsonResponse;
use TYPO3\CMS\Core\Http\Response;
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Versioning\VersionState;
/**
* LocalizationController handles the AJAX requests for record localization
*/
class LocalizationController
{
/**
* @var string
*/
const ACTION_COPY = 'copyFromLanguage';
/**
* @var string
*/
const ACTION_LOCALIZE = 'localize';
/**
* @var IconFactory
*/
protected $iconFactory;
/**
* @var LocalizationRepository
*/
protected $localizationRepository;
/**
* Constructor
*/
public function __construct()
{
$this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
$this->localizationRepository = GeneralUtility::makeInstance(LocalizationRepository::class);
}
/**
* Get used languages in a colPos of a page
*
* @param ServerRequestInterface $request
* @return ResponseInterface
*/
public function getUsedLanguagesInPageAndColumn(ServerRequestInterface $request): ResponseInterface
{
$params = $request->getQueryParams();
if (!isset($params['pageId'], $params['colPos'], $params['languageId'])) {
return new JsonResponse(null, 400);
}
$pageId = (int)$params['pageId'];
$colPos = (int)$params['colPos'];
$languageId = (int)$params['languageId'];
/** @var TranslationConfigurationProvider $translationProvider */
$translationProvider = GeneralUtility::makeInstance(TranslationConfigurationProvider::class);
$systemLanguages = $translationProvider->getSystemLanguages($pageId);
$availableLanguages = [];
// First check whether column has localized records
$elementsInColumnCount = $this->localizationRepository->getLocalizedRecordCount($pageId, $colPos, $languageId);
if ($elementsInColumnCount === 0) {
$fetchedAvailableLanguages = $this->localizationRepository->fetchAvailableLanguages($pageId, $colPos, $languageId);
$availableLanguages[] = $systemLanguages[0];
foreach ($fetchedAvailableLanguages as $language) {
if (isset($systemLanguages[$language['uid']])) {
$availableLanguages[] = $systemLanguages[$language['uid']];
}
}
} else {
$result = $this->localizationRepository->fetchOriginLanguage($pageId, $colPos, $languageId);
$availableLanguages[] = $systemLanguages[$result['sys_language_uid']];
}
// Pre-render all flag icons
foreach ($availableLanguages as &$language) {
if ($language['flagIcon'] === 'empty-empty') {
$language['flagIcon'] = '';
} else {
$language['flagIcon'] = $this->iconFactory->getIcon($language['flagIcon'], Icon::SIZE_SMALL)->render();
}
}
return (new JsonResponse())->setPayload($availableLanguages);
}
/**
* Get a prepared summary of records being translated
*
* @param ServerRequestInterface $request
* @return ResponseInterface
*/
public function getRecordLocalizeSummary(ServerRequestInterface $request): ResponseInterface
{
$params = $request->getQueryParams();
if (!isset($params['pageId'], $params['colPos'], $params['destLanguageId'], $params['languageId'])) {
return new JsonResponse(null, 400);
}
$records = [];
$result = $this->localizationRepository->getRecordsToCopyDatabaseResult(
$params['pageId'],
$params['colPos'],
$params['destLanguageId'],
$params['languageId'],
'*'
);
while ($row = $result->fetch()) {
BackendUtility::workspaceOL('tt_content', $row, -99, true);
if (!$row || VersionState::cast($row['t3ver_state'])->equals(VersionState::DELETE_PLACEHOLDER)) {
continue;
}
$records[] = [
'icon' => $this->iconFactory->getIconForRecord('tt_content', $row, Icon::SIZE_SMALL)->render(),
'title' => $row[$GLOBALS['TCA']['tt_content']['ctrl']['label']],
'uid' => $row['uid']
];
}
return (new JsonResponse())->setPayload($records);
}
/**
* @param ServerRequestInterface $request
* @return ResponseInterface
*/
public function localizeRecords(ServerRequestInterface $request): ResponseInterface
{
$params = $request->getQueryParams();
if (!isset($params['pageId'], $params['srcLanguageId'], $params['destLanguageId'], $params['action'], $params['uidList'])) {
return new JsonResponse(null, 400);
}
if ($params['action'] !== static::ACTION_COPY && $params['action'] !== static::ACTION_LOCALIZE) {
$response = new Response('php://temp', 400, ['Content-Type' => 'application/json; charset=utf-8']);
$response->getBody()->write('Invalid action "' . $params['action'] . '" called.');
return $response;
}
// Filter transmitted but invalid uids
$params['uidList'] = $this->filterInvalidUids(
(int)$params['pageId'],
(int)$params['colPos'],
(int)$params['destLanguageId'],
(int)$params['srcLanguageId'],
$params['uidList']
);
$this->process($params);
return (new JsonResponse())->setPayload([]);
}
/**
* Gets all possible UIDs of a page, colPos and language that might be processed and removes invalid UIDs that might
* be smuggled in.
*
* @param int $pageId
* @param int $colPos
* @param int $destLanguageId
* @param int $srcLanguageId
* @param array $transmittedUidList
* @return array
*/
protected function filterInvalidUids(
int $pageId,
int $colPos,
int $destLanguageId,
int $srcLanguageId,
array $transmittedUidList
): array {
// Get all valid uids that can be processed
$validUidList = $result = $this->localizationRepository->getRecordsToCopyDatabaseResult(
$pageId,
$colPos,
$destLanguageId,
$srcLanguageId,
'uid'
);
return array_intersect(array_unique($transmittedUidList), array_column($validUidList->fetchAll(), 'uid'));
}
/**
* Processes the localization actions
*
* @param array $params
*/
protected function process($params)
{
$destLanguageId = (int)$params['destLanguageId'];
// Build command map
$cmd = [
'tt_content' => []
];
if (isset($params['uidList']) && is_array($params['uidList'])) {
foreach ($params['uidList'] as $currentUid) {
if ($params['action'] === static::ACTION_LOCALIZE) {
$cmd['tt_content'][$currentUid] = [
'localize' => $destLanguageId
];
} else {
$cmd['tt_content'][$currentUid] = [
'copyToLanguage' => $destLanguageId,
];
}
}
}
$dataHandler = GeneralUtility::makeInstance(DataHandler::class);
$dataHandler->start([], $cmd);
$dataHandler->process_cmdmap();
}
}