‪TYPO3CMS  10.4
LocalizationController.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 Psr\EventDispatcher\EventDispatcherInterface;
21 use Psr\Http\Message\ResponseInterface;
22 use Psr\Http\Message\ServerRequestInterface;
35 
42 {
46  const ‪ACTION_COPY = 'copyFromLanguage';
47 
51  const ‪ACTION_LOCALIZE = 'localize';
52 
56  protected ‪$iconFactory;
57 
62 
66  protected ‪$eventDispatcher;
67 
71  public function ‪__construct()
72  {
73  $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
74  $this->localizationRepository = GeneralUtility::makeInstance(LocalizationRepository::class);
75  $this->eventDispatcher = GeneralUtility::makeInstance(EventDispatcherInterface::class);
76  }
77 
84  public function ‪getUsedLanguagesInPage(ServerRequestInterface $request): ResponseInterface
85  {
86  $params = $request->getQueryParams();
87  if (!isset($params['pageId'], $params['languageId'])) {
88  return new ‪JsonResponse(null, 400);
89  }
90 
91  $pageId = (int)$params['pageId'];
92  $languageId = (int)$params['languageId'];
93 
95  $translationProvider = GeneralUtility::makeInstance(TranslationConfigurationProvider::class);
96  $systemLanguages = $translationProvider->getSystemLanguages($pageId);
97 
98  $availableLanguages = [];
99 
100  // First check whether column has localized records
101  $elementsInColumnCount = $this->localizationRepository->getLocalizedRecordCount($pageId, $languageId);
102 
103  if ($elementsInColumnCount === 0) {
104  $fetchedAvailableLanguages = $this->localizationRepository->fetchAvailableLanguages($pageId, $languageId);
105  foreach ($fetchedAvailableLanguages as $language) {
106  if (isset($systemLanguages[$language['sys_language_uid']])) {
107  $availableLanguages[] = $systemLanguages[$language['sys_language_uid']];
108  }
109  }
110  } else {
111  $result = $this->localizationRepository->fetchOriginLanguage($pageId, $languageId);
112  $availableLanguages[] = $systemLanguages[$result['sys_language_uid']];
113  }
114  // Language "All" should not appear as a source of translations (see bug 92757) and keys should be sequential
115  $availableLanguages = array_values(
116  array_filter($availableLanguages, function (array $languageRecord): bool {
117  return (int)$languageRecord['uid'] !== -1;
118  })
119  );
120 
121  // Pre-render all flag icons
122  foreach ($availableLanguages as &$language) {
123  if ($language['flagIcon'] === 'empty-empty') {
124  $language['flagIcon'] = '';
125  } else {
126  $language['flagIcon'] = $this->iconFactory->getIcon($language['flagIcon'], ‪Icon::SIZE_SMALL)->render();
127  }
128  }
129 
130  return (new JsonResponse())->setPayload($availableLanguages);
131  }
132 
139  public function ‪getRecordLocalizeSummary(ServerRequestInterface $request): ResponseInterface
140  {
141  $params = $request->getQueryParams();
142  if (!isset($params['pageId'], $params['destLanguageId'], $params['languageId'])) {
143  return new JsonResponse(null, 400);
144  }
145 
146  $pageId = (int)$params['pageId'];
147  $destLanguageId = (int)$params['destLanguageId'];
148  $languageId = (int)$params['languageId'];
149 
150  $records = [];
151  $result = $this->localizationRepository->getRecordsToCopyDatabaseResult(
152  $pageId,
153  $destLanguageId,
154  $languageId,
155  '*'
156  );
157 
158  $flatRecords = [];
159  while ($row = $result->fetch()) {
160  ‪BackendUtility::workspaceOL('tt_content', $row, -99, true);
161  if (!$row || ‪VersionState::cast($row['t3ver_state'])->equals(‪VersionState::DELETE_PLACEHOLDER)) {
162  continue;
163  }
164  $colPos = $row['colPos'];
165  if (!isset($records[$colPos])) {
166  $records[$colPos] = [];
167  }
168  $records[$colPos][] = [
169  'icon' => $this->iconFactory->getIconForRecord('tt_content', $row, ‪Icon::SIZE_SMALL)->render(),
170  'title' => $row[‪$GLOBALS['TCA']['tt_content']['ctrl']['label']],
171  'uid' => $row['uid']
172  ];
173  $flatRecords[] = $row;
174  }
175 
176  return (new JsonResponse())->setPayload([
177  'records' => $records,
178  'columns' => $this->‪getPageColumns($pageId, $flatRecords, $params),
179  ]);
180  }
181 
186  public function ‪localizeRecords(ServerRequestInterface $request): ResponseInterface
187  {
188  $params = $request->getQueryParams();
189  if (!isset($params['pageId'], $params['srcLanguageId'], $params['destLanguageId'], $params['action'], $params['uidList'])) {
190  return new JsonResponse(null, 400);
191  }
192 
193  if ($params['action'] !== static::ACTION_COPY && $params['action'] !== static::ACTION_LOCALIZE) {
194  $response = new Response('php://temp', 400, ['Content-Type' => 'application/json; charset=utf-8']);
195  $response->getBody()->write('Invalid action "' . $params['action'] . '" called.');
196  return $response;
197  }
198 
199  // Filter transmitted but invalid uids
200  $params['uidList'] = $this->‪filterInvalidUids(
201  (int)$params['pageId'],
202  (int)$params['destLanguageId'],
203  (int)$params['srcLanguageId'],
204  $params['uidList']
205  );
206 
207  $this->‪process($params);
208 
209  return (new JsonResponse())->setPayload([]);
210  }
211 
222  protected function ‪filterInvalidUids(
223  int $pageId,
224  int $destLanguageId,
225  int $srcLanguageId,
226  array $transmittedUidList
227  ): array {
228  // Get all valid uids that can be processed
229  $validUidList = $this->localizationRepository->getRecordsToCopyDatabaseResult(
230  $pageId,
231  $destLanguageId,
232  $srcLanguageId,
233  'uid'
234  );
235 
236  return array_intersect(array_unique($transmittedUidList), array_column($validUidList->fetchAll(), 'uid'));
237  }
238 
244  protected function ‪process($params): void
245  {
246  $destLanguageId = (int)$params['destLanguageId'];
247 
248  // Build command map
249  $cmd = [
250  'tt_content' => []
251  ];
252 
253  if (isset($params['uidList']) && is_array($params['uidList'])) {
254  foreach ($params['uidList'] as $currentUid) {
255  if ($params['action'] === static::ACTION_LOCALIZE) {
256  $cmd['tt_content'][$currentUid] = [
257  'localize' => $destLanguageId
258  ];
259  } else {
260  $cmd['tt_content'][$currentUid] = [
261  'copyToLanguage' => $destLanguageId,
262  ];
263  }
264  }
265  }
266 
267  $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
268  $dataHandler->start([], $cmd);
269  $dataHandler->process_cmdmap();
270  }
271 
278  protected function ‪getPageColumns(int $pageId, array $flatRecords, array $params): array
279  {
280  $columns = [];
281  $backendLayoutView = GeneralUtility::makeInstance(BackendLayoutView::class);
282  $backendLayout = $backendLayoutView->getBackendLayoutForPage($pageId);
283 
284  foreach ($backendLayout->getUsedColumns() as $columnPos => $columnLabel) {
285  $columns[$columnPos] = ‪$GLOBALS['LANG']->sL($columnLabel);
286  }
287 
288  $event = GeneralUtility::makeInstance(AfterPageColumnsSelectedForLocalizationEvent::class, $columns, array_values($backendLayout->getColumnPositionNumbers()), $backendLayout, $flatRecords, $params);
289  $this->eventDispatcher->dispatch($event);
290 
291  return [
292  'columns' => $event->getColumns(),
293  'columnList' => $event->getColumnList()
294  ];
295  }
296 }
‪TYPO3\CMS\Core\DataHandling\DataHandler
Definition: DataHandler.php:84
‪TYPO3\CMS\Core\Imaging\Icon\SIZE_SMALL
‪const SIZE_SMALL
Definition: Icon.php:30
‪TYPO3\CMS\Backend\Controller\Page\LocalizationController\getPageColumns
‪array getPageColumns(int $pageId, array $flatRecords, array $params)
Definition: LocalizationController.php:275
‪TYPO3\CMS\Backend\Controller\Event\AfterPageColumnsSelectedForLocalizationEvent
Definition: AfterPageColumnsSelectedForLocalizationEvent.php:30
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:26
‪TYPO3\CMS\Backend\Controller\Page\LocalizationController\__construct
‪__construct()
Definition: LocalizationController.php:68
‪TYPO3\CMS\Backend\Domain\Repository\Localization\LocalizationRepository
Definition: LocalizationRepository.php:37
‪TYPO3\CMS\Backend\Controller\Page\LocalizationController\getUsedLanguagesInPage
‪ResponseInterface getUsedLanguagesInPage(ServerRequestInterface $request)
Definition: LocalizationController.php:81
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:33
‪TYPO3\CMS\Backend\Controller\Page\LocalizationController\getRecordLocalizeSummary
‪ResponseInterface getRecordLocalizeSummary(ServerRequestInterface $request)
Definition: LocalizationController.php:136
‪TYPO3\CMS\Core\Versioning\VersionState\DELETE_PLACEHOLDER
‪const DELETE_PLACEHOLDER
Definition: VersionState.php:55
‪TYPO3\CMS\Backend\Controller\Page
Definition: LocalizationController.php:18
‪TYPO3\CMS\Backend\Controller\Page\LocalizationController\$iconFactory
‪IconFactory $iconFactory
Definition: LocalizationController.php:55
‪TYPO3\CMS\Core\Type\Enumeration\cast
‪static static cast($value)
Definition: Enumeration.php:186
‪TYPO3\CMS\Core\Http\Response
Definition: Response.php:30
‪TYPO3\CMS\Backend\Controller\Page\LocalizationController
Definition: LocalizationController.php:42
‪TYPO3\CMS\Backend\Controller\Page\LocalizationController\ACTION_LOCALIZE
‪const ACTION_LOCALIZE
Definition: LocalizationController.php:51
‪TYPO3\CMS\Backend\Utility\BackendUtility
Definition: BackendUtility.php:75
‪TYPO3\CMS\Core\Versioning\VersionState
Definition: VersionState.php:24
‪TYPO3\CMS\Backend\Configuration\TranslationConfigurationProvider
Definition: TranslationConfigurationProvider.php:35
‪TYPO3\CMS\Backend\View\BackendLayoutView
Definition: BackendLayoutView.php:36
‪TYPO3\CMS\Core\Http\JsonResponse
Definition: JsonResponse.php:26
‪TYPO3\CMS\Backend\Controller\Page\LocalizationController\ACTION_COPY
‪const ACTION_COPY
Definition: LocalizationController.php:46
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Backend\Utility\BackendUtility\workspaceOL
‪static workspaceOL($table, &$row, $wsid=-99, $unsetMovePointers=false)
Definition: BackendUtility.php:3586
‪TYPO3\CMS\Backend\Controller\Page\LocalizationController\$eventDispatcher
‪EventDispatcherInterface $eventDispatcher
Definition: LocalizationController.php:63
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Backend\Controller\Page\LocalizationController\process
‪process($params)
Definition: LocalizationController.php:241
‪TYPO3\CMS\Backend\Controller\Page\LocalizationController\$localizationRepository
‪LocalizationRepository $localizationRepository
Definition: LocalizationController.php:59
‪TYPO3\CMS\Backend\Controller\Page\LocalizationController\localizeRecords
‪ResponseInterface localizeRecords(ServerRequestInterface $request)
Definition: LocalizationController.php:183
‪TYPO3\CMS\Backend\Controller\Page\LocalizationController\filterInvalidUids
‪array filterInvalidUids(int $pageId, int $destLanguageId, int $srcLanguageId, array $transmittedUidList)
Definition: LocalizationController.php:219