TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
LocalizationController.php
Go to the documentation of this file.
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;
25 
30 {
34  const ACTION_COPY = 'copyFromLanguage';
35 
39  const ACTION_LOCALIZE = 'localize';
40 
44  protected $iconFactory;
45 
50 
54  public function __construct()
55  {
56  $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
57  $this->localizationRepository = GeneralUtility::makeInstance(LocalizationRepository::class);
58  }
59 
67  public function getUsedLanguagesInPageAndColumn(ServerRequestInterface $request, ResponseInterface $response)
68  {
69  $params = $request->getQueryParams();
70  if (!isset($params['pageId'], $params['colPos'], $params['languageId'])) {
71  $response = $response->withStatus(500);
72  return $response;
73  }
74 
75  $pageId = (int)$params['pageId'];
76  $colPos = (int)$params['colPos'];
77  $languageId = (int)$params['languageId'];
78 
80  $translationProvider = GeneralUtility::makeInstance(TranslationConfigurationProvider::class);
81  $systemLanguages = $translationProvider->getSystemLanguages($pageId);
82 
83  $availableLanguages = [];
84 
85  // First check whether column has localized records
86  $elementsInColumnCount = $this->localizationRepository->getLocalizedRecordCount($pageId, $colPos, $languageId);
87 
88  if ($elementsInColumnCount === 0) {
89  $fetchedAvailableLanguages = $this->localizationRepository->fetchAvailableLanguages($pageId, $colPos, $languageId);
90  $availableLanguages[] = $systemLanguages[0];
91 
92  foreach ($fetchedAvailableLanguages as $language) {
93  if (isset($systemLanguages[$language['uid']])) {
94  $availableLanguages[] = $systemLanguages[$language['uid']];
95  }
96  }
97  } else {
98  $result = $this->localizationRepository->fetchOriginLanguage($pageId, $colPos, $languageId);
99  $availableLanguages[] = $systemLanguages[$result['sys_language_uid']];
100  }
101 
102  // Pre-render all flag icons
103  foreach ($availableLanguages as &$language) {
104  if ($language['flagIcon'] === 'empty-empty') {
105  $language['flagIcon'] = '';
106  } else {
107  $language['flagIcon'] = $this->iconFactory->getIcon($language['flagIcon'], Icon::SIZE_SMALL)->render();
108  }
109  }
110 
111  $response->getBody()->write(json_encode($availableLanguages));
112  return $response;
113  }
114 
122  public function getRecordLocalizeSummary(ServerRequestInterface $request, ResponseInterface $response)
123  {
124  $params = $request->getQueryParams();
125  if (!isset($params['pageId'], $params['colPos'], $params['destLanguageId'], $params['languageId'])) {
126  $response = $response->withStatus(500);
127  return $response;
128  }
129 
130  $records = [];
131  $result = $this->localizationRepository->getRecordsToCopyDatabaseResult(
132  $params['pageId'],
133  $params['colPos'],
134  $params['destLanguageId'],
135  $params['languageId'],
136  '*'
137  );
138 
139  while ($row = $result->fetch()) {
140  $records[] = [
141  'icon' => $this->iconFactory->getIconForRecord('tt_content', $row, Icon::SIZE_SMALL)->render(),
142  'title' => $row[$GLOBALS['TCA']['tt_content']['ctrl']['label']],
143  'uid' => $row['uid']
144  ];
145  }
146 
147  $response->getBody()->write(json_encode($records));
148  return $response;
149  }
150 
157  public function getRecordUidsToCopy(ServerRequestInterface $request, ResponseInterface $response)
158  {
160  $params = $request->getQueryParams();
161  if (!isset($params['pageId'], $params['colPos'], $params['languageId'])) {
162  $response = $response->withStatus(500);
163  return $response;
164  }
165 
166  $pageId = (int)$params['pageId'];
167  $colPos = (int)$params['colPos'];
168  $languageId = (int)$params['languageId'];
169 
170  $result = $this->localizationRepository->getRecordsToCopyDatabaseResult($pageId, $colPos, $languageId, 'uid');
171  $uids = [];
172  while ($row = $result->fetch()) {
173  $uids[] = (int)$row['uid'];
174  }
175 
176  $response->getBody()->write(json_encode($uids));
177  return $response;
178  }
179 
185  public function localizeRecords(ServerRequestInterface $request, ResponseInterface $response)
186  {
187  $params = $request->getQueryParams();
188  if (!isset($params['pageId'], $params['srcLanguageId'], $params['destLanguageId'], $params['action'], $params['uidList'])) {
189  $response = $response->withStatus(500);
190  return $response;
191  }
192 
193  if ($params['action'] !== static::ACTION_COPY && $params['action'] !== static::ACTION_LOCALIZE) {
194  $response->getBody()->write('Invalid action "' . $params['action'] . '" called.');
195  $response = $response->withStatus(500);
196  return $response;
197  }
198 
199  $this->process($params);
200 
201  $response->getBody()->write(json_encode([]));
202  return $response;
203  }
204 
210  protected function process($params)
211  {
212  $pageId = (int)$params['pageId'];
213  $srcLanguageId = (int)$params['srcLanguageId'];
214  $destLanguageId = (int)$params['destLanguageId'];
215 
216  // Build command map
217  $cmd = [
218  'tt_content' => []
219  ];
220 
221  for ($i = 0, $count = count($params['uidList']); $i < $count; ++$i) {
222  $currentUid = $params['uidList'][$i];
223 
224  if ($params['action'] === static::ACTION_LOCALIZE) {
225  $cmd['tt_content'][$currentUid] = [
226  'localize' => $destLanguageId
227  ];
228  } else {
229  $previousUid = $this->localizationRepository->getPreviousLocalizedRecordUid(
230  'tt_content',
231  $currentUid,
232  $pageId,
233  $srcLanguageId,
234  $destLanguageId
235  );
236  $cmd['tt_content'][$currentUid] = [
237  'copy' => [
238  'action' => 'paste',
239  'target' => -$previousUid,
240  'update' => [
241  'sys_language_uid' => $destLanguageId,
242  'l18n_parent' => 0
243  ]
244  ]
245  ];
246  }
247  }
248 
250  $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
251  $dataHandler->start([], $cmd);
252  $dataHandler->process_cmdmap();
253  }
254 }
localizeRecords(ServerRequestInterface $request, ResponseInterface $response)
getRecordUidsToCopy(ServerRequestInterface $request, ResponseInterface $response)
if(TYPO3_MODE=== 'BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
static makeInstance($className,...$constructorArguments)
getRecordLocalizeSummary(ServerRequestInterface $request, ResponseInterface $response)