[BUGFIX] LanguageController has "mixed" type annotations
[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 * The view of this action returns JSON!
138 *
139 * @param string $extension The extension key
140 * @param string $locales Comma separated list of locales to update
141 * @return void
142 */
143 public function updateTranslationAction($extension, $locales) {
144 if (is_string($locales)) {
145 $locales = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $locales);
146 }
147 $locales = array_flip((array) $locales);
148
149 foreach ($locales as $locale => $key) {
150 $state = static::TRANSLATION_INVALID;
151 try {
152 $state = $this->getTranslationStateForExtension($extension, $locale);
153 if ($state === static::TRANSLATION_AVAILABLE) {
154 $state = $this->updateTranslationForExtension($extension, $locale);
155 }
156 } catch (\Exception $exception) {
157 $error = $exception->getMessage();
158 }
159 $locales[$locale] = array(
160 'state' => $state,
161 'error' => $error,
162 );
163 }
164
165 $this->view->assign('extension', $extension);
166 $this->view->assign('locales', $locales);
167 }
168
169 /**
170 * Returns the translation state for an extension
171 *
172 * @param string $extensionKey The extension key
173 * @param string $locale Locale to return
174 * @return integer Translation state
175 */
176 protected function getTranslationStateForExtension($extensionKey, $locale) {
177 if (empty($extensionKey) || empty($locale)) {
178 return static::TRANSLATION_INVALID;
179 }
180
181 $identifier = $extensionKey . '-' . $locale;
182 if (isset($this->translationStates[$identifier])) {
183 return $this->translationStates[$identifier];
184 }
185
186 $selectedLanguages = $this->languageRepository->findSelected();
187 if (empty($selectedLanguages) || !is_array($selectedLanguages)) {
188 return static::TRANSLATION_INVALID;
189 }
190
191 $mirrorUrl = $this->repositoryHelper->getMirrors()->getMirrorUrl();
192 $status = $this->terConnection->fetchTranslationStatus($extensionKey, $mirrorUrl);
193
194 foreach ($selectedLanguages as $language) {
195 $stateLocale = $language->getLocale();
196 $stateIdentifier = $extensionKey . '-' . $stateLocale;
197 $this->translationStates[$stateIdentifier] = static::TRANSLATION_INVALID;
198
199 if (empty($status[$stateLocale]) || !is_array($status[$stateLocale])) {
200 $this->translationStates[$stateIdentifier] = static::TRANSLATION_NOT_AVAILABLE;
201 continue;
202 }
203
204 $md5 = $this->getTranslationFileMd5($extensionKey, $stateLocale);
205 if ($md5 !== $status[$stateLocale]['md5']) {
206 $this->translationStates[$stateIdentifier] = static::TRANSLATION_AVAILABLE;
207 continue;
208 }
209
210 $this->translationStates[$stateIdentifier] = static::TRANSLATION_OK;
211 }
212
213 return $this->translationStates[$identifier];
214 }
215
216 /**
217 * Returns the md5 of a translation file
218 *
219 * @param string $extensionKey The extension key
220 * @param string $locale The locale
221 * @return string The md5 value
222 */
223 protected function getTranslationFileMd5($extensionKey, $locale) {
224 if (empty($extensionKey) || empty($locale)) {
225 return '';
226 }
227 $fileName = PATH_site . 'typo3temp' . DIRECTORY_SEPARATOR . $extensionKey . '-l10n-' . $locale . '.zip';
228 if (is_file($fileName)) {
229 return md5_file($fileName);
230 }
231 return '';
232 }
233
234 /**
235 * Update the translation for an extension
236 *
237 * @param string $extensionKey The extension key
238 * @param string $locale Locale to update
239 * @return integer Translation state
240 */
241 protected function updateTranslationForExtension($extensionKey, $locale) {
242 if (empty($extensionKey) || empty($locale)) {
243 return static::TRANSLATION_INVALID;
244 }
245
246 $state = static::TRANSLATION_FAILED;
247 $mirrorUrl = $this->repositoryHelper->getMirrors()->getMirrorUrl();
248 $updateResult = $this->terConnection->updateTranslation($extensionKey, $locale, $mirrorUrl);
249 if ($updateResult === TRUE) {
250 $state = static::TRANSLATION_UPDATED;
251 }
252
253 return $state;
254 }
255
256 }
257 ?>