TYPO3 CMS  TYPO3_8-7
LocalizationController.php
Go to the documentation of this file.
1 <?php
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 
27 
32 {
36  const ACTION_COPY = 'copyFromLanguage';
37 
41  const ACTION_LOCALIZE = 'localize';
42 
46  protected $iconFactory;
47 
52 
56  public function __construct()
57  {
58  $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
59  $this->localizationRepository = GeneralUtility::makeInstance(LocalizationRepository::class);
60  }
61 
69  public function getUsedLanguagesInPageAndColumn(ServerRequestInterface $request, ResponseInterface $response)
70  {
71  $params = $request->getQueryParams();
72  if (!isset($params['pageId'], $params['colPos'], $params['languageId'])) {
73  $response = $response->withStatus(400);
74  return $response;
75  }
76 
77  $pageId = (int)$params['pageId'];
78  $colPos = (int)$params['colPos'];
79  $languageId = (int)$params['languageId'];
80 
82  $translationProvider = GeneralUtility::makeInstance(TranslationConfigurationProvider::class);
83  $systemLanguages = $translationProvider->getSystemLanguages($pageId);
84 
85  $availableLanguages = [];
86 
87  // First check whether column has localized records
88  $elementsInColumnCount = $this->localizationRepository->getLocalizedRecordCount($pageId, $colPos, $languageId);
89 
90  if ($elementsInColumnCount === 0) {
91  $fetchedAvailableLanguages = $this->localizationRepository->fetchAvailableLanguages($pageId, $colPos, $languageId);
92  $availableLanguages[] = $systemLanguages[0];
93 
94  foreach ($fetchedAvailableLanguages as $language) {
95  if (isset($systemLanguages[$language['uid']])) {
96  $availableLanguages[] = $systemLanguages[$language['uid']];
97  }
98  }
99  } else {
100  $result = $this->localizationRepository->fetchOriginLanguage($pageId, $colPos, $languageId);
101  $availableLanguages[] = $systemLanguages[$result['sys_language_uid']];
102  }
103 
104  // Pre-render all flag icons
105  foreach ($availableLanguages as &$language) {
106  if ($language['flagIcon'] === 'empty-empty') {
107  $language['flagIcon'] = '';
108  } else {
109  $language['flagIcon'] = $this->iconFactory->getIcon($language['flagIcon'], Icon::SIZE_SMALL)->render();
110  }
111  }
112 
113  $response->getBody()->write(json_encode($availableLanguages));
114  return $response;
115  }
116 
124  public function getRecordLocalizeSummary(ServerRequestInterface $request, ResponseInterface $response)
125  {
126  $params = $request->getQueryParams();
127  if (!isset($params['pageId'], $params['colPos'], $params['destLanguageId'], $params['languageId'])) {
128  $response = $response->withStatus(400);
129  return $response;
130  }
131 
132  $records = [];
133  $result = $this->localizationRepository->getRecordsToCopyDatabaseResult(
134  $params['pageId'],
135  $params['colPos'],
136  $params['destLanguageId'],
137  $params['languageId'],
138  '*'
139  );
140 
141  while ($row = $result->fetch()) {
142  BackendUtility::workspaceOL('tt_content', $row, -99, true);
143  if (!$row || VersionState::cast($row['t3ver_state'])->equals(VersionState::DELETE_PLACEHOLDER)) {
144  continue;
145  }
146  $records[] = [
147  'icon' => $this->iconFactory->getIconForRecord('tt_content', $row, Icon::SIZE_SMALL)->render(),
148  'title' => $row[$GLOBALS['TCA']['tt_content']['ctrl']['label']],
149  'uid' => $row['uid']
150  ];
151  }
152 
153  $response->getBody()->write(json_encode($records));
154  return $response;
155  }
156 
163  public function getRecordUidsToCopy(ServerRequestInterface $request, ResponseInterface $response)
164  {
166  $params = $request->getQueryParams();
167  if (!isset($params['pageId'], $params['colPos'], $params['languageId'])) {
168  $response = $response->withStatus(400);
169  return $response;
170  }
171 
172  $pageId = (int)$params['pageId'];
173  $colPos = (int)$params['colPos'];
174  $languageId = (int)$params['languageId'];
175 
176  $result = $this->localizationRepository->getRecordsToCopyDatabaseResult($pageId, $colPos, $languageId, 'uid');
177  $uids = [];
178  while ($row = $result->fetch()) {
179  $uids[] = (int)$row['uid'];
180  }
181 
182  $response->getBody()->write(json_encode($uids));
183  return $response;
184  }
185 
191  public function localizeRecords(ServerRequestInterface $request, ResponseInterface $response)
192  {
193  $params = $request->getQueryParams();
194  if (!isset($params['pageId'], $params['srcLanguageId'], $params['destLanguageId'], $params['action'], $params['uidList'])) {
195  $response = $response->withStatus(400);
196  return $response;
197  }
198 
199  if ($params['action'] !== static::ACTION_COPY && $params['action'] !== static::ACTION_LOCALIZE) {
200  $response->getBody()->write('Invalid action "' . $params['action'] . '" called.');
201  $response = $response->withStatus(400);
202  return $response;
203  }
204 
205  $this->process($params);
206 
207  $response->getBody()->write(json_encode([]));
208  return $response;
209  }
210 
216  protected function process($params)
217  {
218  $destLanguageId = (int)$params['destLanguageId'];
219 
220  // Build command map
221  $cmd = [
222  'tt_content' => []
223  ];
224 
225  if (isset($params['uidList']) && is_array($params['uidList'])) {
226  foreach ($params['uidList'] as $currentUid) {
227  if ($params['action'] === static::ACTION_LOCALIZE) {
228  $cmd['tt_content'][$currentUid] = [
229  'localize' => $destLanguageId
230  ];
231  } else {
232  $cmd['tt_content'][$currentUid] = [
233  'copyToLanguage' => $destLanguageId,
234  ];
235  }
236  }
237  }
238 
239  $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
240  $dataHandler->start([], $cmd);
241  $dataHandler->process_cmdmap();
242  }
243 }
static workspaceOL($table, &$row, $wsid=-99, $unsetMovePointers=false)
static makeInstance($className,... $constructorArguments)
getRecordUidsToCopy(ServerRequestInterface $request, ResponseInterface $response)
localizeRecords(ServerRequestInterface $request, ResponseInterface $response)
getRecordLocalizeSummary(ServerRequestInterface $request, ResponseInterface $response)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']