‪TYPO3CMS  9.5
LocalizationController.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
5 
6 /*
7  * This file is part of the TYPO3 CMS project.
8  *
9  * It is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License, either version 2
11  * of the License, or any later version.
12  *
13  * For the full copyright and license information, please read the
14  * LICENSE.txt file that was distributed with this source code.
15  *
16  * The TYPO3 project - inspiring people to share!
17  */
18 
19 use Psr\Http\Message\ResponseInterface;
20 use Psr\Http\Message\ServerRequestInterface;
32 
39 {
43  const ‪ACTION_COPY = 'copyFromLanguage';
44 
48  const ‪ACTION_LOCALIZE = 'localize';
49 
53  protected ‪$iconFactory;
54 
59 
63  public function ‪__construct()
64  {
65  $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
66  $this->localizationRepository = GeneralUtility::makeInstance(LocalizationRepository::class);
67  }
68 
75  public function ‪getUsedLanguagesInPage(ServerRequestInterface $request): ResponseInterface
76  {
77  $params = $request->getQueryParams();
78  if (!isset($params['pageId'], $params['languageId'])) {
79  return new ‪JsonResponse(null, 400);
80  }
81 
82  $pageId = (int)$params['pageId'];
83  $languageId = (int)$params['languageId'];
84 
86  $translationProvider = GeneralUtility::makeInstance(TranslationConfigurationProvider::class);
87  $systemLanguages = $translationProvider->getSystemLanguages($pageId);
88 
89  $availableLanguages = [];
90 
91  // First check whether column has localized records
92  $elementsInColumnCount = $this->localizationRepository->getLocalizedRecordCount($pageId, $languageId);
93 
94  if ($elementsInColumnCount === 0) {
95  $fetchedAvailableLanguages = $this->localizationRepository->fetchAvailableLanguages($pageId, $languageId);
96  foreach ($fetchedAvailableLanguages as $language) {
97  if (isset($systemLanguages[$language['sys_language_uid']])) {
98  $availableLanguages[] = $systemLanguages[$language['sys_language_uid']];
99  }
100  }
101  } else {
102  $result = $this->localizationRepository->fetchOriginLanguage($pageId, $languageId);
103  $availableLanguages[] = $systemLanguages[$result['sys_language_uid']];
104  }
105 
106  // Pre-render all flag icons
107  foreach ($availableLanguages as &$language) {
108  if ($language['flagIcon'] === 'empty-empty') {
109  $language['flagIcon'] = '';
110  } else {
111  $language['flagIcon'] = $this->iconFactory->getIcon($language['flagIcon'], ‪Icon::SIZE_SMALL)->render();
112  }
113  }
114 
115  return (new JsonResponse())->setPayload($availableLanguages);
116  }
117 
124  public function ‪getRecordLocalizeSummary(ServerRequestInterface $request): ResponseInterface
125  {
126  $params = $request->getQueryParams();
127  if (!isset($params['pageId'], $params['destLanguageId'], $params['languageId'])) {
128  return new JsonResponse(null, 400);
129  }
130 
131  $pageId = (int)$params['pageId'];
132  $destLanguageId = (int)$params['destLanguageId'];
133  $languageId = (int)$params['languageId'];
134 
135  $records = [];
136  $result = $this->localizationRepository->getRecordsToCopyDatabaseResult(
137  $pageId,
138  $destLanguageId,
139  $languageId,
140  '*'
141  );
142 
143  while ($row = $result->fetch()) {
144  ‪BackendUtility::workspaceOL('tt_content', $row, -99, true);
145  if (!$row || ‪VersionState::cast($row['t3ver_state'])->equals(‪VersionState::DELETE_PLACEHOLDER)) {
146  continue;
147  }
148  $colPos = $row['colPos'];
149  if (!isset($records[$colPos])) {
150  $records[$colPos] = [];
151  }
152  $records[$colPos][] = [
153  'icon' => $this->iconFactory->getIconForRecord('tt_content', $row, ‪Icon::SIZE_SMALL)->render(),
154  'title' => $row[‪$GLOBALS['TCA']['tt_content']['ctrl']['label']],
155  'uid' => $row['uid']
156  ];
157  }
158 
159  return (new JsonResponse())->setPayload([
160  'records' => $records,
161  'columns' => $this->‪getPageColumns($pageId),
162  ]);
163  }
164 
169  public function ‪localizeRecords(ServerRequestInterface $request): ResponseInterface
170  {
171  $params = $request->getQueryParams();
172  if (!isset($params['pageId'], $params['srcLanguageId'], $params['destLanguageId'], $params['action'], $params['uidList'])) {
173  return new JsonResponse(null, 400);
174  }
175 
176  if ($params['action'] !== static::ACTION_COPY && $params['action'] !== static::ACTION_LOCALIZE) {
177  $response = new Response('php://temp', 400, ['Content-Type' => 'application/json; charset=utf-8']);
178  $response->getBody()->write('Invalid action "' . $params['action'] . '" called.');
179  return $response;
180  }
181 
182  // Filter transmitted but invalid uids
183  $params['uidList'] = $this->‪filterInvalidUids(
184  (int)$params['pageId'],
185  (int)$params['destLanguageId'],
186  (int)$params['srcLanguageId'],
187  $params['uidList']
188  );
189 
190  $this->‪process($params);
191 
192  return (new JsonResponse())->setPayload([]);
193  }
194 
205  protected function ‪filterInvalidUids(
206  int $pageId,
207  int $destLanguageId,
208  int $srcLanguageId,
209  array $transmittedUidList
210  ): array {
211  // Get all valid uids that can be processed
212  $validUidList = $this->localizationRepository->getRecordsToCopyDatabaseResult(
213  $pageId,
214  $destLanguageId,
215  $srcLanguageId,
216  'uid'
217  );
218 
219  return array_intersect(array_unique($transmittedUidList), array_column($validUidList->fetchAll(), 'uid'));
220  }
221 
227  protected function ‪process($params): void
228  {
229  $destLanguageId = (int)$params['destLanguageId'];
230 
231  // Build command map
232  $cmd = [
233  'tt_content' => []
234  ];
235 
236  if (isset($params['uidList']) && is_array($params['uidList'])) {
237  foreach ($params['uidList'] as $currentUid) {
238  if ($params['action'] === static::ACTION_LOCALIZE) {
239  $cmd['tt_content'][$currentUid] = [
240  'localize' => $destLanguageId
241  ];
242  } else {
243  $cmd['tt_content'][$currentUid] = [
244  'copyToLanguage' => $destLanguageId,
245  ];
246  }
247  }
248  }
249 
250  $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
251  $dataHandler->start([], $cmd);
252  $dataHandler->process_cmdmap();
253  }
254 
259  protected function ‪getPageColumns(int $pageId): array
260  {
261  $columns = [];
262  $backendLayoutView = GeneralUtility::makeInstance(BackendLayoutView::class);
263  $backendLayouts = $backendLayoutView->getSelectedBackendLayout($pageId);
264 
265  foreach ($backendLayouts['__items'] as $backendLayout) {
266  $columns[(int)$backendLayout[1]] = $backendLayout[0];
267  }
268 
269  return [
270  'columns' => $columns,
271  'columnList' => $backendLayouts['__colPosList'],
272  ];
273  }
274 }
‪TYPO3\CMS\Core\DataHandling\DataHandler
Definition: DataHandler.php:81
‪TYPO3\CMS\Core\Imaging\Icon\SIZE_SMALL
‪const SIZE_SMALL
Definition: Icon.php:29
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:25
‪TYPO3\CMS\Backend\Controller\Page\LocalizationController\__construct
‪__construct()
Definition: LocalizationController.php:61
‪TYPO3\CMS\Backend\Domain\Repository\Localization\LocalizationRepository
Definition: LocalizationRepository.php:35
‪TYPO3\CMS\Backend\Controller\Page\LocalizationController\getUsedLanguagesInPage
‪ResponseInterface getUsedLanguagesInPage(ServerRequestInterface $request)
Definition: LocalizationController.php:73
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:31
‪TYPO3\CMS\Backend\Controller\Page\LocalizationController\getRecordLocalizeSummary
‪ResponseInterface getRecordLocalizeSummary(ServerRequestInterface $request)
Definition: LocalizationController.php:122
‪TYPO3\CMS\Core\Versioning\VersionState\DELETE_PLACEHOLDER
‪const DELETE_PLACEHOLDER
Definition: VersionState.php:54
‪TYPO3\CMS\Backend\Controller\Page
Definition: LocalizationController.php:4
‪TYPO3\CMS\Backend\Controller\Page\LocalizationController\$iconFactory
‪IconFactory $iconFactory
Definition: LocalizationController.php:52
‪TYPO3\CMS\Core\Type\Enumeration\cast
‪static static cast($value)
Definition: Enumeration.php:182
‪TYPO3\CMS\Core\Http\Response
Definition: Response.php:28
‪TYPO3\CMS\Backend\Controller\Page\LocalizationController
Definition: LocalizationController.php:39
‪TYPO3\CMS\Backend\Controller\Page\LocalizationController\ACTION_LOCALIZE
‪const ACTION_LOCALIZE
Definition: LocalizationController.php:48
‪TYPO3\CMS\Backend\Utility\BackendUtility
Definition: BackendUtility.php:72
‪TYPO3\CMS\Core\Versioning\VersionState
Definition: VersionState.php:23
‪TYPO3\CMS\Backend\Configuration\TranslationConfigurationProvider
Definition: TranslationConfigurationProvider.php:35
‪TYPO3\CMS\Backend\View\BackendLayoutView
Definition: BackendLayoutView.php:28
‪TYPO3\CMS\Core\Http\JsonResponse
Definition: JsonResponse.php:25
‪TYPO3\CMS\Backend\Controller\Page\LocalizationController\ACTION_COPY
‪const ACTION_COPY
Definition: LocalizationController.php:43
‪$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:4048
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Backend\Controller\Page\LocalizationController\process
‪process($params)
Definition: LocalizationController.php:225
‪TYPO3\CMS\Backend\Controller\Page\LocalizationController\$localizationRepository
‪LocalizationRepository $localizationRepository
Definition: LocalizationController.php:56
‪TYPO3\CMS\Backend\Controller\Page\LocalizationController\localizeRecords
‪ResponseInterface localizeRecords(ServerRequestInterface $request)
Definition: LocalizationController.php:167
‪TYPO3\CMS\Backend\Controller\Page\LocalizationController\filterInvalidUids
‪array filterInvalidUids(int $pageId, int $destLanguageId, int $srcLanguageId, array $transmittedUidList)
Definition: LocalizationController.php:203
‪TYPO3\CMS\Backend\Controller\Page\LocalizationController\getPageColumns
‪array getPageColumns(int $pageId)
Definition: LocalizationController.php:257