98da2af25cf79c4ad82aabf4c4fb40e13af0b4f2
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / Page / LocalizationController.php
1 <?php
2 namespace TYPO3\CMS\Backend\Controller\Page;
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 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;
25
26 /**
27 * LocalizationController handles the AJAX requests for record localization
28 */
29 class LocalizationController
30 {
31 /**
32 * @var IconFactory
33 */
34 protected $iconFactory;
35
36 /**
37 * @var LocalizationRepository
38 */
39 protected $localizationRepository;
40
41 /**
42 * Constructor
43 */
44 public function __construct()
45 {
46 $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
47 $this->localizationRepository = GeneralUtility::makeInstance(LocalizationRepository::class);
48 }
49
50 /**
51 * Get used languages in a colPos of a page
52 *
53 * @param ServerRequestInterface $request
54 * @param ResponseInterface $response
55 * @return ResponseInterface
56 */
57 public function getUsedLanguagesInPageAndColumn(ServerRequestInterface $request, ResponseInterface $response)
58 {
59 $params = $request->getQueryParams();
60 if (!isset($params['pageId'], $params['colPos'], $params['languageId'])) {
61 $response = $response->withStatus(500);
62 return $response;
63 }
64
65 $pageId = (int)$params['pageId'];
66 $colPos = (int)$params['colPos'];
67 $languageId = (int)$params['languageId'];
68
69 /** @var TranslationConfigurationProvider $translationProvider */
70 $translationProvider = GeneralUtility::makeInstance(TranslationConfigurationProvider::class);
71 $systemLanguages = $translationProvider->getSystemLanguages($pageId);
72
73 $availableLanguages = [];
74
75 // First check whether column has localized records
76 $elementsInColumnCount = $this->localizationRepository->getLocalizedRecordCount($pageId, $colPos, $languageId);
77
78 if ($elementsInColumnCount === 0) {
79 $fetchedAvailableLanguages = $this->localizationRepository->fetchAvailableLanguages($pageId, $colPos, $languageId);
80 $availableLanguages[] = $systemLanguages[0];
81
82 foreach ($fetchedAvailableLanguages as $language) {
83 if (isset($systemLanguages[$language['uid']])) {
84 $availableLanguages[] = $systemLanguages[$language['uid']];
85 }
86 }
87 } else {
88 $result = $this->localizationRepository->fetchOriginLanguage($pageId, $colPos, $languageId);
89 $availableLanguages[] = $systemLanguages[$result['sys_language_uid']];
90 }
91
92 // Pre-render all flag icons
93 foreach ($availableLanguages as &$language) {
94 if ($language['flagIcon'] === 'empty-empty') {
95 $language['flagIcon'] = '';
96 } else {
97 $language['flagIcon'] = $this->iconFactory->getIcon($language['flagIcon'], Icon::SIZE_SMALL)->render();
98 }
99 }
100
101 $response->getBody()->write(json_encode($availableLanguages));
102 return $response;
103 }
104
105 /**
106 * Get a prepared summary of records being translated
107 *
108 * @param ServerRequestInterface $request
109 * @param ResponseInterface $response
110 * @return ResponseInterface
111 */
112 public function getRecordLocalizeSummary(ServerRequestInterface $request, ResponseInterface $response)
113 {
114 $params = $request->getQueryParams();
115 if (!isset($params['pageId'], $params['colPos'], $params['destLanguageId'], $params['languageId'])) {
116 $response = $response->withStatus(500);
117 return $response;
118 }
119
120 $records = [];
121 $databaseConnection = $this->getDatabaseConnection();
122 $res = $this->localizationRepository->getRecordsToCopyDatabaseResult($params['pageId'], $params['colPos'], $params['destLanguageId'], $params['languageId'], '*');
123 while ($row = $databaseConnection->sql_fetch_assoc($res)) {
124 $records[] = [
125 'icon' => $this->iconFactory->getIconForRecord('tt_content', $row, Icon::SIZE_SMALL)->render(),
126 'title' => $row[$GLOBALS['TCA']['tt_content']['ctrl']['label']],
127 'uid' => $row['uid']
128 ];
129 }
130 $databaseConnection->sql_free_result($res);
131
132 $response->getBody()->write(json_encode($records));
133 return $response;
134 }
135
136 /**
137 * @param ServerRequestInterface $request
138 * @param ResponseInterface $response
139 * @return ResponseInterface
140 */
141 public function getRecordUidsToCopy(ServerRequestInterface $request, ResponseInterface $response)
142 {
143 $params = $request->getQueryParams();
144 if (!isset($params['pageId'], $params['colPos'], $params['languageId'])) {
145 $response = $response->withStatus(500);
146 return $response;
147 }
148
149 $pageId = (int)$params['pageId'];
150 $colPos = (int)$params['colPos'];
151 $languageId = (int)$params['languageId'];
152 $databaseConnection = $this->getDatabaseConnection();
153
154 $res = $this->localizationRepository->getRecordsToCopyDatabaseResult($pageId, $colPos, $languageId, 'uid');
155 $uids = [];
156 while ($row = $databaseConnection->sql_fetch_assoc($res)) {
157 $uids[] = (int)$row['uid'];
158 }
159 $databaseConnection->sql_free_result($res);
160
161 $response->getBody()->write(json_encode($uids));
162 return $response;
163 }
164
165 /**
166 * @param ServerRequestInterface $request
167 * @param ResponseInterface $response
168 * @return ResponseInterface
169 */
170 public function localizeRecords(ServerRequestInterface $request, ResponseInterface $response)
171 {
172 $params = $request->getQueryParams();
173 if (!isset($params['pageId'], $params['srcLanguageId'], $params['destLanguageId'], $params['action'], $params['uidList'])) {
174 $response = $response->withStatus(500);
175 return $response;
176 }
177
178 if ($params['action'] !== 'copyFromLanguage' && $params['action'] !== 'localize') {
179 $response->getBody()->write('Invalid action "' . $params['action'] . '" called.');
180 $response = $response->withStatus(500);
181 return $response;
182 }
183
184 $pageId = (int)$params['pageId'];
185 $srcLanguageId = (int)$params['srcLanguageId'];
186 $destLanguageId = (int)$params['destLanguageId'];
187 $params['uidList'] = array_reverse($params['uidList']);
188
189 // Build command map
190 $cmd = [
191 'tt_content' => []
192 ];
193
194 for ($i = 0, $count = count($params['uidList']); $i < $count; ++$i) {
195 $currentUid = $params['uidList'][$i];
196
197 if ($params['action'] === 'localize') {
198 if ($srcLanguageId === 0) {
199 $cmd['tt_content'][$currentUid] = [
200 'localize' => $destLanguageId
201 ];
202 } else {
203 $cmd['tt_content'][$currentUid] = [
204 'copy' => [
205 'action' => 'paste',
206 'target' => $pageId,
207 'update' => [
208 'sys_language_uid' => $destLanguageId
209 ]
210 ]
211 ];
212 }
213 } else {
214 $cmd['tt_content'][$currentUid] = [
215 'copy' => [
216 'action' => 'paste',
217 'target' => $pageId,
218 'update' => [
219 'sys_language_uid' => $destLanguageId,
220 'l18n_parent' => 0
221 ]
222 ]
223 ];
224 }
225 }
226
227 /** @var DataHandler $dataHandler */
228 $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
229 $dataHandler->start([], $cmd);
230 $dataHandler->process_cmdmap();
231
232 $response->getBody()->write(json_encode([]));
233 return $response;
234 }
235
236 /**
237 * Returns the database connection
238 *
239 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
240 */
241 protected function getDatabaseConnection()
242 {
243 return $GLOBALS['TYPO3_DB'];
244 }
245 }