2 namespace TYPO3\CMS\Backend\Controller\Page
;
5 * This file is part of the TYPO3 CMS project.
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.
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
14 * The TYPO3 project - inspiring people to share!
17 use Psr\Http\Message\ResponseInterface
;
18 use Psr\Http\Message\ServerRequestInterface
;
19 use TYPO3\CMS\Backend\Configuration\TranslationConfigurationProvider
;
20 use TYPO3\CMS\Backend\Domain\Repository\Localization\LocalizationRepository
;
21 use TYPO3\CMS\Core\DataHandling\DataHandler
;
22 use TYPO3\CMS\Core\Imaging\Icon
;
23 use TYPO3\CMS\Core\Imaging\IconFactory
;
24 use TYPO3\CMS\Core\Utility\GeneralUtility
;
27 * LocalizationController handles the AJAX requests for record localization
29 class LocalizationController
34 const ACTION_COPY
= 'copyFromLanguage';
39 const ACTION_LOCALIZE
= 'localize';
44 protected $iconFactory;
47 * @var LocalizationRepository
49 protected $localizationRepository;
54 public function __construct()
56 $this->iconFactory
= GeneralUtility
::makeInstance(IconFactory
::class);
57 $this->localizationRepository
= GeneralUtility
::makeInstance(LocalizationRepository
::class);
61 * Get used languages in a colPos of a page
63 * @param ServerRequestInterface $request
64 * @param ResponseInterface $response
65 * @return ResponseInterface
67 public function getUsedLanguagesInPageAndColumn(ServerRequestInterface
$request, ResponseInterface
$response)
69 $params = $request->getQueryParams();
70 if (!isset($params['pageId'], $params['colPos'], $params['languageId'])) {
71 $response = $response->withStatus(400);
75 $pageId = (int)$params['pageId'];
76 $colPos = (int)$params['colPos'];
77 $languageId = (int)$params['languageId'];
79 /** @var TranslationConfigurationProvider $translationProvider */
80 $translationProvider = GeneralUtility
::makeInstance(TranslationConfigurationProvider
::class);
81 $systemLanguages = $translationProvider->getSystemLanguages($pageId);
83 $availableLanguages = [];
85 // First check whether column has localized records
86 $elementsInColumnCount = $this->localizationRepository
->getLocalizedRecordCount($pageId, $colPos, $languageId);
88 if ($elementsInColumnCount === 0) {
89 $fetchedAvailableLanguages = $this->localizationRepository
->fetchAvailableLanguages($pageId, $colPos, $languageId);
90 $availableLanguages[] = $systemLanguages[0];
92 foreach ($fetchedAvailableLanguages as $language) {
93 if (isset($systemLanguages[$language['uid']])) {
94 $availableLanguages[] = $systemLanguages[$language['uid']];
98 $result = $this->localizationRepository
->fetchOriginLanguage($pageId, $colPos, $languageId);
99 $availableLanguages[] = $systemLanguages[$result['sys_language_uid']];
102 // Pre-render all flag icons
103 foreach ($availableLanguages as &$language) {
104 if ($language['flagIcon'] === 'empty-empty') {
105 $language['flagIcon'] = '';
107 $language['flagIcon'] = $this->iconFactory
->getIcon($language['flagIcon'], Icon
::SIZE_SMALL
)->render();
111 $response->getBody()->write(json_encode($availableLanguages));
116 * Get a prepared summary of records being translated
118 * @param ServerRequestInterface $request
119 * @param ResponseInterface $response
120 * @return ResponseInterface
122 public function getRecordLocalizeSummary(ServerRequestInterface
$request, ResponseInterface
$response)
124 $params = $request->getQueryParams();
125 if (!isset($params['pageId'], $params['colPos'], $params['destLanguageId'], $params['languageId'])) {
126 $response = $response->withStatus(400);
131 $result = $this->localizationRepository
->getRecordsToCopyDatabaseResult(
134 $params['destLanguageId'],
135 $params['languageId'],
139 while ($row = $result->fetch()) {
141 'icon' => $this->iconFactory
->getIconForRecord('tt_content', $row, Icon
::SIZE_SMALL
)->render(),
142 'title' => $row[$GLOBALS['TCA']['tt_content']['ctrl']['label']],
147 $response->getBody()->write(json_encode($records));
152 * @param ServerRequestInterface $request
153 * @param ResponseInterface $response
154 * @return ResponseInterface
156 public function localizeRecords(ServerRequestInterface
$request, ResponseInterface
$response)
158 $params = $request->getQueryParams();
159 if (!isset($params['pageId'], $params['srcLanguageId'], $params['destLanguageId'], $params['action'], $params['uidList'])) {
160 $response = $response->withStatus(400);
164 if ($params['action'] !== static::ACTION_COPY
&& $params['action'] !== static::ACTION_LOCALIZE
) {
165 $response->getBody()->write('Invalid action "' . $params['action'] . '" called.');
166 $response = $response->withStatus(400);
170 // Filter transmitted but invalid uids
171 $params['uidList'] = $this->filterInvalidUids(
172 (int)$params['pageId'],
173 (int)$params['colPos'],
174 (int)$params['destLanguageId'],
175 (int)$params['srcLanguageId'],
179 $this->process($params);
181 $response->getBody()->write(json_encode([]));
186 * Gets all possible UIDs of a page, colPos and language that might be processed and removes invalid UIDs that might
191 * @param int $destLanguageId
192 * @param int $srcLanguageId
193 * @param array $transmittedUidList
196 protected function filterInvalidUids(
201 array $transmittedUidList
203 // Get all valid uids that can be processed
204 $validUidList = $result = $this->localizationRepository
->getRecordsToCopyDatabaseResult(
212 return array_intersect(array_unique($transmittedUidList), array_column($validUidList->fetchAll(), 'uid'));
216 * Processes the localization actions
218 * @param array $params
220 protected function process($params)
222 $destLanguageId = (int)$params['destLanguageId'];
229 if (isset($params['uidList']) && is_array($params['uidList'])) {
230 foreach ($params['uidList'] as $currentUid) {
231 if ($params['action'] === static::ACTION_LOCALIZE
) {
232 $cmd['tt_content'][$currentUid] = [
233 'localize' => $destLanguageId
236 $cmd['tt_content'][$currentUid] = [
237 'copyToLanguage' => $destLanguageId,
243 $dataHandler = GeneralUtility
::makeInstance(DataHandler
::class);
244 $dataHandler->start([], $cmd);
245 $dataHandler->process_cmdmap();