[TASK] EXT:lang CodeCleanup
[Packages/TYPO3.CMS.git] / typo3 / sysext / lang / Classes / Controller / LanguageController.php
1 <?php
2 namespace TYPO3\CMS\Lang\Controller;
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 TYPO3\CMS\Backend\Template\Components\ButtonBar;
18 use TYPO3\CMS\Backend\Template\Components\Menu\Menu;
19 use TYPO3\CMS\Backend\Template\Components\Menu\MenuItem;
20 use TYPO3\CMS\Backend\View\BackendTemplateView;
21 use TYPO3\CMS\Core\Imaging\Icon;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\CMS\Lang\Domain\Model\Extension;
24 use TYPO3\CMS\Lang\Domain\Repository\ExtensionRepository;
25 use TYPO3\CMS\Lang\Domain\Repository\LanguageRepository;
26 use TYPO3\CMS\Lang\Service\RegistryService;
27 use TYPO3\CMS\Lang\Service\TranslationService;
28 use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
29 use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
30 use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
31
32 /**
33 * Language controller
34 */
35 class LanguageController extends ActionController
36 {
37 /**
38 * @var string
39 */
40 protected $defaultViewObjectName = BackendTemplateView::class;
41
42 /**
43 * @var BackendTemplateView
44 */
45 protected $view;
46
47 /**
48 * @var \TYPO3\CMS\Lang\Domain\Repository\LanguageRepository
49 */
50 protected $languageRepository;
51
52 /**
53 * @var \TYPO3\CMS\Lang\Domain\Repository\ExtensionRepository
54 */
55 protected $extensionRepository;
56
57 /**
58 * @var \TYPO3\CMS\Lang\Service\TranslationService
59 */
60 protected $translationService;
61
62 /**
63 * @var \TYPO3\CMS\Lang\Service\RegistryService
64 */
65 protected $registryService;
66
67 /**
68 * @param \TYPO3\CMS\Lang\Domain\Repository\LanguageRepository $languageRepository
69 */
70 public function injectLanguageRepository(LanguageRepository $languageRepository)
71 {
72 $this->languageRepository = $languageRepository;
73 }
74
75 /**
76 * @param \TYPO3\CMS\Lang\Domain\Repository\ExtensionRepository $extensionRepository
77 */
78 public function injectExtensionRepository(ExtensionRepository $extensionRepository)
79 {
80 $this->extensionRepository = $extensionRepository;
81 }
82
83 /**
84 * @param \TYPO3\CMS\Lang\Service\TranslationService $translationService
85 */
86 public function injectTranslationService(TranslationService $translationService)
87 {
88 $this->translationService = $translationService;
89 }
90
91 /**
92 * @param \TYPO3\CMS\Lang\Service\RegistryService $registryService
93 */
94 public function injectRegistryService(RegistryService $registryService)
95 {
96 $this->registryService = $registryService;
97 }
98
99 /**
100 * List languages
101 *
102 * @return void
103 */
104 public function listLanguagesAction()
105 {
106 $this->prepareDocHeaderMenu();
107 $this->prepareDocHeaderButtons();
108
109 $languages = $this->languageRepository->findAll();
110 $this->view->assign('languages', $languages);
111 }
112
113 /**
114 * List translations
115 *
116 * @return void
117 */
118 public function listTranslationsAction()
119 {
120 $this->prepareDocHeaderMenu();
121
122 $languages = $this->languageRepository->findSelected();
123 $this->view->assign('languages', $languages);
124 }
125
126 /**
127 * Returns the translations
128 *
129 * @return void
130 */
131 public function getTranslationsAction()
132 {
133 $this->view->assign('extensions', $this->extensionRepository->findAll());
134 $this->view->assign('languages', $this->languageRepository->findSelected());
135 }
136
137 /**
138 * Fetch all translations for given locale
139 *
140 * @param array $data The request data
141 * @return void
142 */
143 public function updateLanguageAction(array $data)
144 {
145 $numberOfExtensionsToUpdate = 10;
146 $response = array(
147 'success' => false,
148 'progress' => 0,
149 );
150 $progress = 0;
151 if (!empty($data['locale'])) {
152 $allCount = 0;
153 for ($i = 0; $i < $numberOfExtensionsToUpdate; $i++) {
154 $offset = (int)$data['count'] * $numberOfExtensionsToUpdate + $i;
155 /** @var Extension $extension */
156 $extension = $this->extensionRepository->findOneByOffset($offset);
157 if (empty($extension)) {
158 // No more extensions to update
159 break;
160 }
161 if ($allCount === 0) {
162 $allCount = (int)$this->extensionRepository->countAll();
163 }
164 $extensionKey = $extension->getKey();
165 $result = $this->translationService->updateTranslation($extensionKey, $data['locale']);
166 $progress = round((($offset + 1) * 100) / $allCount, 2);
167 $response['result'][$data['locale']][$extensionKey] = $result[$data['locale']];
168 if (empty($result[$extensionKey][$data['locale']]['error'])) {
169 $response['success'] = true;
170 } else {
171 // Could not update an extension, stop here!
172 $response['success'] = false;
173 break;
174 }
175 }
176 }
177 if ($response['success']) {
178 $this->registryService->set($data['locale'], $GLOBALS['EXEC_TIME']);
179 $response['timestamp'] = $GLOBALS['EXEC_TIME'];
180 $response['progress'] = $progress > 100 ? 100 : $progress;
181 }
182 $this->view->assign('response', $response);
183 }
184
185 /**
186 * Fetch the translation for given extension and locale
187 *
188 * @param array $data The request data
189 * @return void
190 */
191 public function updateTranslationAction(array $data)
192 {
193 $response = array('success' => false);
194 if (!empty($data['extension']) && !empty($data['locale'])) {
195 $result = $this->translationService->updateTranslation($data['extension'], $data['locale']);
196 if (empty($result[$data['extension']][$data['locale']]['error'])) {
197 $response = array(
198 'success' => true,
199 'result' => $result,
200 );
201 }
202 }
203 $this->view->assign('response', $response);
204 }
205
206 /**
207 * Activate a language
208 *
209 * @param array $data The request data
210 * @return void
211 */
212 public function activateLanguageAction(array $data)
213 {
214 $response = array('success' => false);
215 if (!empty($data['locale'])) {
216 $response = $this->languageRepository->activateByLocale($data['locale']);
217 }
218 $this->view->assign('response', $response);
219 }
220
221 /**
222 * Deactivate a language
223 *
224 * @param array $data The request data
225 * @return void
226 */
227 public function deactivateLanguageAction(array $data)
228 {
229 $response = array('success' => false);
230 if (!empty($data['locale'])) {
231 $response = $this->languageRepository->deactivateByLocale($data['locale']);
232 }
233 $this->view->assign('response', $response);
234 }
235
236 /**
237 * DocHeaderMenu
238 */
239 protected function prepareDocHeaderMenu()
240 {
241 $this->view->getModuleTemplate()->setModuleName('typo3-module-lang');
242 $this->view->getModuleTemplate()->setModuleId('typo3-module-lang');
243
244 $this->view->getModuleTemplate()->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Lang/LanguageModule');
245
246 $extensionKey = 'lang';
247 $addJsInlineLabels = [
248 'flashmessage.error',
249 'flashmessage.information',
250 'flashmessage.success',
251 'flashmessage.multipleErrors',
252 'flashmessage.updateComplete',
253 'flashmessage.canceled',
254 'flashmessage.languageActivated',
255 'flashmessage.languageDeactivated',
256 'flashmessage.errorOccurred',
257 'table.processing',
258 'table.search',
259 'table.loadingRecords',
260 'table.zeroRecords',
261 'table.emptyTable',
262 'table.dateFormat',
263 ];
264 foreach ($addJsInlineLabels as $key) {
265 $label = LocalizationUtility::translate($key, $extensionKey);
266 $this->view->getModuleTemplate()->getPageRenderer()->addInlineLanguageLabel($key, $label);
267 }
268
269 $uriBuilder = $this->objectManager->get(UriBuilder::class);
270 $uriBuilder->setRequest($this->request);
271
272 /** @var Menu $menu */
273 $menu = GeneralUtility::makeInstance(Menu::class);
274 $menu->setIdentifier('_languageMenu');
275 $menu->setLabel($this->getLanguageService()->sL('LLL:EXT:lang/locallang_general.xlf:LGL.language', true));
276
277 /** @var MenuItem $languageListMenuItem */
278 $languageListMenuItem = GeneralUtility::makeInstance(MenuItem::class);
279 $action = 'listLanguages';
280 $isActive = $this->request->getControllerActionName() === $action ? true : false;
281 $languageListMenuItem->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang.xlf:header.languages'));
282 $uri = $uriBuilder->reset()->uriFor('listLanguages', array(), 'Language');
283 $languageListMenuItem->setHref($uri)->setActive($isActive);
284
285 /** @var MenuItem $translationMenuItem */
286 $translationMenuItem = GeneralUtility::makeInstance(MenuItem::class);
287 $action = 'listTranslations';
288 $isActive = $this->request->getControllerActionName() === $action ? true : false;
289 $translationMenuItem->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang.xlf:header.translations'));
290 $uri = $uriBuilder->reset()->uriFor('listTranslations', array(), 'Language');
291 $translationMenuItem->setHref($uri)->setActive($isActive);
292
293 $menu->addMenuItem($languageListMenuItem);
294 $menu->addMenuItem($translationMenuItem);
295 $this->view->getModuleTemplate()->getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);
296 $this->view->getModuleTemplate()->setFlashMessageQueue($this->controllerContext->getFlashMessageQueue());
297 }
298
299 /**
300 * Returns LanguageService
301 *
302 * @return \TYPO3\CMS\Lang\LanguageService
303 */
304 protected function getLanguageService()
305 {
306 return $GLOBALS['LANG'];
307 }
308
309 /**
310 * DocHeaderButtons
311 */
312 protected function prepareDocHeaderButtons()
313 {
314 // @todo: the html structure needed to operate the buttons correctly is broken now.
315 // @todo: LanguageModule.js and backend.css -> div.typo3-module-lang div.menuItems
316
317 $downloadAllButton = $this->view->getModuleTemplate()->getDocHeaderComponent()->getButtonBar()->makeLinkButton()
318 ->setIcon($this->view->getModuleTemplate()->getIconFactory()->getIcon('actions-system-extension-download', Icon::SIZE_SMALL))
319 ->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang.xlf:button.downloadAll'))
320 ->setClasses('menuItem updateItem t3js-button-update')
321 ->setDataAttributes(['action' => 'updateActiveLanguages'])
322 ->setHref('#');
323 $this->view->getModuleTemplate()->getDocHeaderComponent()->getButtonBar()->addButton($downloadAllButton, ButtonBar::BUTTON_POSITION_LEFT);
324
325 $cancelButton = $this->view->getModuleTemplate()->getDocHeaderComponent()->getButtonBar()->makeLinkButton()
326 ->setIcon($this->view->getModuleTemplate()->getIconFactory()->getIcon('actions-document-close', Icon::SIZE_SMALL))
327 ->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang.xlf:button.cancel'))
328 ->setClasses('menuItem cancelItem disabled t3js-button-cancel')
329 ->setDataAttributes(['action' => 'cancelLanguageUpdate'])
330 ->setHref('#');
331
332 $this->view->getModuleTemplate()->getDocHeaderComponent()->getButtonBar()->addButton($cancelButton, ButtonBar::BUTTON_POSITION_LEFT);
333 }
334 }