45a56ff0a0c0230c8f580e30373275b6203ca610
[Packages/TYPO3.CMS.git] / typo3 / sysext / lang / Classes / Controller / LanguageController.php
1 <?php
2 namespace TYPO3\CMS\Lang\Controller;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012 Sebastian Fischer <typo3@evoweb.de>
8 * 2012 Kai Vogel <kai.vogel@speedprogs.de>
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Language controller handling the selection of available languages and update of extension translations
30 *
31 * @author Sebastian Fischer <typo3@evoweb.de>
32 * @author Kai Vogel <kai.vogel@speedprogs.de>
33 */
34 class LanguageController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {
35
36 /**
37 * Status codes for AJAX response
38 */
39 const TRANSLATION_NOT_AVAILABLE = 0;
40 const TRANSLATION_AVAILABLE = 1;
41 const TRANSLATION_FAILED = 2;
42 const TRANSLATION_OK = 3;
43 const TRANSLATION_INVALID = 4;
44 const TRANSLATION_UPDATED = 5;
45
46 /**
47 * @var \TYPO3\CMS\Lang\Domain\Repository\LanguageRepository
48 * @inject
49 */
50 protected $languageRepository;
51
52 /**
53 * @var \TYPO3\CMS\Lang\Domain\Repository\ExtensionRepository
54 * @inject
55 */
56 protected $extensionRepository;
57
58 /**
59 * @var \TYPO3\CMS\Extensionmanager\Utility\Repository\Helper
60 * @inject
61 */
62 protected $repositoryHelper;
63
64 /**
65 * @var \TYPO3\CMS\Lang\Utility\Connection\Ter
66 * @inject
67 */
68 protected $terConnection;
69
70 /**
71 * @var array
72 */
73 protected $translationStates = array();
74
75 /**
76 * JSON actions
77 * @var array
78 */
79 protected $jsonActions = array('updateTranslation');
80
81 /**
82 * Force JSON output for defined actions
83 *
84 * @param \TYPO3\CMS\Extbase\Mvc\View\ViewInterface $view The view to be initialized
85 * @return void
86 */
87 protected function initializeView(\TYPO3\CMS\Extbase\Mvc\View\ViewInterface $view) {
88 $actionName = $this->request->getControllerActionName();
89 if (in_array($actionName, $this->jsonActions)) {
90 $viewObjectName = 'TYPO3\\CMS\\Lang\\View\\Language\\' . ucfirst($actionName) . 'Json';
91 $this->view = $this->objectManager->get($viewObjectName);
92 $this->view->setControllerContext($this->controllerContext);
93 $this->view->initializeView();
94 }
95 }
96
97 /**
98 * Index action
99 *
100 * @param \TYPO3\CMS\Lang\Domain\Model\LanguageSelectionForm $languageSelectionForm
101 * @param mixed $extensions Extensions to show in form
102 * @return void
103 * @dontvalidate $languageSelectionForm
104 * @dontvalidate $extensions
105 */
106 public function indexAction(\TYPO3\CMS\Lang\Domain\Model\LanguageSelectionForm $languageSelectionForm = NULL, $extensions = NULL) {
107 if ($languageSelectionForm === NULL) {
108 $languageSelectionForm = $this->objectManager->get('TYPO3\\CMS\\Lang\\Domain\\Model\\LanguageSelectionForm');
109 $languageSelectionForm->setLanguages($this->languageRepository->findAll());
110 $languageSelectionForm->setSelectedLanguages($this->languageRepository->findSelected());
111 }
112
113 if (empty($extensions)) {
114 $extensions = $this->extensionRepository->findAll();
115 }
116
117 $this->view->assign('languageSelectionForm', $languageSelectionForm);
118 $this->view->assign('extensions', $extensions);
119 }
120
121 /**
122 * Update the language selection form
123 *
124 * @param \TYPO3\CMS\Lang\Domain\Model\LanguageSelectionForm $languageSelectionForm
125 * @return void
126 * @dontvalidate $languageSelectionForm
127 */
128 public function updateLanguageSelectionAction(\TYPO3\CMS\Lang\Domain\Model\LanguageSelectionForm $languageSelectionForm) {
129 if ($languageSelectionForm !== NULL) {
130 $this->languageRepository->updateSelectedLanguages($languageSelectionForm->getSelectedLanguages());
131 }
132 $this->redirect('index');
133 }
134
135 /**
136 * Update translation for one extension
137 *
138 * @param string $extension The extension key
139 * @param mixed $locales List or array of locales to update
140 * @return void
141 */
142 public function updateTranslationAction($extension, $locales) {
143 if (is_string($locales)) {
144 $locales = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $locales);
145 }
146 $locales = array_flip((array) $locales);
147
148 foreach ($locales as $locale => $key) {
149 $state = static::TRANSLATION_INVALID;
150 try {
151 $state = $this->getTranslationStateForExtension($extension, $locale);
152 if ($state === static::TRANSLATION_AVAILABLE) {
153 $state = $this->updateTranslationForExtension($extension, $locale);
154 }
155 } catch (\Exception $exception) {
156 $error = $exception->getMessage();
157 }
158 $locales[$locale] = array(
159 'state' => $state,
160 'error' => $error,
161 );
162 }
163
164 $this->view->assign('extension', $extension);
165 $this->view->assign('locales', $locales);
166 }
167
168 /**
169 * Returns the translation state for an extension
170 *
171 * @param string $extensionKey The extension key
172 * @param string $locale Locale to return
173 * @return integer Translation state
174 */
175 protected function getTranslationStateForExtension($extensionKey, $locale) {
176 if (empty($extensionKey) || empty($locale)) {
177 return static::TRANSLATION_INVALID;
178 }
179
180 $identifier = $extensionKey . '-' . $locale;
181 if (isset($this->translationStates[$identifier])) {
182 return $this->translationStates[$identifier];
183 }
184
185 $selectedLanguages = $this->languageRepository->findSelected();
186 if (empty($selectedLanguages) || !is_array($selectedLanguages)) {
187 return static::TRANSLATION_INVALID;
188 }
189
190 $mirrorUrl = $this->repositoryHelper->getMirrors()->getMirrorUrl();
191 $status = $this->terConnection->fetchTranslationStatus($extensionKey, $mirrorUrl);
192
193 foreach ($selectedLanguages as $language) {
194 $stateLocale = $language->getLocale();
195 $stateIdentifier = $extensionKey . '-' . $stateLocale;
196 $this->translationStates[$stateIdentifier] = static::TRANSLATION_INVALID;
197
198 if (empty($status[$stateLocale]) || !is_array($status[$stateLocale])) {
199 $this->translationStates[$stateIdentifier] = static::TRANSLATION_NOT_AVAILABLE;
200 continue;
201 }
202
203 $md5 = $this->getTranslationFileMd5($extensionKey, $stateLocale);
204 if ($md5 !== $status[$stateLocale]['md5']) {
205 $this->translationStates[$stateIdentifier] = static::TRANSLATION_AVAILABLE;
206 continue;
207 }
208
209 $this->translationStates[$stateIdentifier] = static::TRANSLATION_OK;
210 }
211
212 return $this->translationStates[$identifier];
213 }
214
215 /**
216 * Returns the md5 of a translation file
217 *
218 * @param string $extensionKey The extension key
219 * @param string $locale The locale
220 * @return string The md5 value
221 */
222 protected function getTranslationFileMd5($extensionKey, $locale) {
223 if (empty($extensionKey) || empty($locale)) {
224 return '';
225 }
226 $fileName = PATH_site . 'typo3temp' . DIRECTORY_SEPARATOR . $extensionKey . '-l10n-' . $locale . '.zip';
227 if (is_file($fileName)) {
228 return md5_file($fileName);
229 }
230 return '';
231 }
232
233 /**
234 * Update the translation for an extension
235 *
236 * @param string $extensionKey The extension key
237 * @param string $locale Locale to update
238 * @return integer Translation state
239 */
240 protected function updateTranslationForExtension($extensionKey, $locale) {
241 if (empty($extensionKey) || empty($locale)) {
242 return static::TRANSLATION_INVALID;
243 }
244
245 $state = static::TRANSLATION_FAILED;
246 $mirrorUrl = $this->repositoryHelper->getMirrors()->getMirrorUrl();
247 $updateResult = $this->terConnection->updateTranslation($extensionKey, $locale, $mirrorUrl);
248 if ($updateResult === TRUE) {
249 $state = static::TRANSLATION_UPDATED;
250 }
251
252 return $state;
253 }
254
255 }
256 ?>