d3e1f42c425f7036b2fea03398e7b54e4a9821fb
[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 /**
18 * Language controller
19 */
20 class LanguageController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
21 {
22 /**
23 * @var \TYPO3\CMS\Lang\Domain\Repository\LanguageRepository
24 */
25 protected $languageRepository;
26
27 /**
28 * @var \TYPO3\CMS\Lang\Domain\Repository\ExtensionRepository
29 */
30 protected $extensionRepository;
31
32 /**
33 * @var \TYPO3\CMS\Lang\Service\TranslationService
34 */
35 protected $translationService;
36
37 /**
38 * @var \TYPO3\CMS\Lang\Service\RegistryService
39 */
40 protected $registryService;
41
42 /**
43 * @param \TYPO3\CMS\Lang\Domain\Repository\LanguageRepository $languageRepository
44 */
45 public function injectLanguageRepository(\TYPO3\CMS\Lang\Domain\Repository\LanguageRepository $languageRepository)
46 {
47 $this->languageRepository = $languageRepository;
48 }
49
50 /**
51 * @param \TYPO3\CMS\Lang\Domain\Repository\ExtensionRepository $extensionRepository
52 */
53 public function injectExtensionRepository(\TYPO3\CMS\Lang\Domain\Repository\ExtensionRepository $extensionRepository)
54 {
55 $this->extensionRepository = $extensionRepository;
56 }
57
58 /**
59 * @param \TYPO3\CMS\Lang\Service\TranslationService $translationService
60 */
61 public function injectTranslationService(\TYPO3\CMS\Lang\Service\TranslationService $translationService)
62 {
63 $this->translationService = $translationService;
64 }
65
66 /**
67 * @param \TYPO3\CMS\Lang\Service\RegistryService $registryService
68 */
69 public function injectRegistryService(\TYPO3\CMS\Lang\Service\RegistryService $registryService)
70 {
71 $this->registryService = $registryService;
72 }
73
74 /**
75 * List languages
76 *
77 * @return void
78 */
79 public function listLanguagesAction()
80 {
81 $languages = $this->languageRepository->findAll();
82 $this->view->assign('languages', $languages);
83 }
84
85 /**
86 * List translations
87 *
88 * @return void
89 */
90 public function listTranslationsAction()
91 {
92 $languages = $this->languageRepository->findSelected();
93 $this->view->assign('languages', $languages);
94 }
95
96 /**
97 * Returns the translations
98 *
99 * @return void
100 */
101 public function getTranslationsAction()
102 {
103 $this->view->assign('extensions', $this->extensionRepository->findAll());
104 $this->view->assign('languages', $this->languageRepository->findSelected());
105 }
106
107 /**
108 * Fetch all translations for given locale
109 *
110 * @param array $data The request data
111 * @return void
112 */
113 public function updateLanguageAction(array $data)
114 {
115 $numberOfExtensionsToUpdate = 10;
116 $response = array(
117 'success' => false,
118 'progress' => 0,
119 );
120 if (!empty($data['locale'])) {
121 $allCount = 0;
122 for ($i = 0; $i < $numberOfExtensionsToUpdate; $i++) {
123 $offset = (int)$data['count'] * $numberOfExtensionsToUpdate + $i;
124 $extension = $this->extensionRepository->findOneByOffset($offset);
125 if (empty($extension)) {
126 // No more extensions to update
127 break;
128 }
129 if ($allCount === 0) {
130 $allCount = (int)$this->extensionRepository->countAll();
131 }
132 $extensionKey = $extension->getKey();
133 $result = $this->translationService->updateTranslation($extensionKey, $data['locale']);
134 $progress = round((($offset + 1) * 100) / $allCount, 2);
135 $response['result'][$data['locale']][$extensionKey] = $result[$data['locale']];
136 if (empty($result[$extensionKey][$data['locale']]['error'])) {
137 $response['success'] = true;
138 } else {
139 // Could not update an extension, stop here!
140 $response['success'] = false;
141 break;
142 }
143 }
144 }
145 if ($response['success']) {
146 $this->registryService->set($data['locale'], $GLOBALS['EXEC_TIME']);
147 $response['timestamp'] = $GLOBALS['EXEC_TIME'];
148 $response['progress'] = $progress > 100 ? 100 : $progress;
149 }
150 $this->view->assign('response', $response);
151 }
152
153 /**
154 * Fetch the translation for given extension and locale
155 *
156 * @param array $data The request data
157 * @return void
158 */
159 public function updateTranslationAction(array $data)
160 {
161 $response = array('success' => false);
162 if (!empty($data['extension']) && !empty($data['locale'])) {
163 $result = $this->translationService->updateTranslation($data['extension'], $data['locale']);
164 if (empty($result[$data['extension']][$data['locale']]['error'])) {
165 $response = array(
166 'success' => true,
167 'result' => $result,
168 );
169 }
170 }
171 $this->view->assign('response', $response);
172 }
173
174 /**
175 * Activate a language
176 *
177 * @param array $data The request data
178 * @return void
179 */
180 public function activateLanguageAction(array $data)
181 {
182 $response = array('success' => false);
183 if (!empty($data['locale'])) {
184 $response = $this->languageRepository->activateByLocale($data['locale']);
185 }
186 $this->view->assign('response', $response);
187 }
188
189 /**
190 * Deactivate a language
191 *
192 * @param array $data The request data
193 * @return void
194 */
195 public function deactivateLanguageAction(array $data)
196 {
197 $response = array('success' => false);
198 if (!empty($data['locale'])) {
199 $response = $this->languageRepository->deactivateByLocale($data['locale']);
200 }
201 $this->view->assign('response', $response);
202 }
203 }