[TASK] EXT:lang CodeCleanup
[Packages/TYPO3.CMS.git] / typo3 / sysext / lang / Classes / Domain / Repository / LanguageRepository.php
1 <?php
2 namespace TYPO3\CMS\Lang\Domain\Repository;
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\Core\Configuration\ConfigurationManager;
18 use TYPO3\CMS\Core\Localization\Locales;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
21 use TYPO3\CMS\Lang\Domain\Model\Language;
22 use TYPO3\CMS\Lang\Service\RegistryService;
23
24 /**
25 * Language repository
26 */
27 class LanguageRepository
28 {
29 /**
30 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
31 */
32 protected $objectManager;
33
34 /**
35 * @var \TYPO3\CMS\Core\Localization\Locales
36 */
37 protected $locales;
38
39 /**
40 * @var \TYPO3\CMS\Lang\Domain\Model\Language[]
41 */
42 protected $selectedLocales = array();
43
44 /**
45 * @var \TYPO3\CMS\Lang\Domain\Model\Language[]
46 */
47 protected $languages = array();
48
49 /**
50 * @var string
51 */
52 protected $configurationPath = 'EXTCONF/lang';
53
54 /**
55 * @var \TYPO3\CMS\Lang\Service\RegistryService
56 */
57 protected $registryService;
58
59 /**
60 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
61 */
62 public function injectObjectManager(ObjectManagerInterface $objectManager)
63 {
64 $this->objectManager = $objectManager;
65 }
66
67 /**
68 * @param \TYPO3\CMS\Core\Localization\Locales $locales
69 */
70 public function injectLocales(Locales $locales)
71 {
72 $this->locales = $locales;
73 }
74
75 /**
76 * @param \TYPO3\CMS\Lang\Service\RegistryService $registryService
77 */
78 public function injectRegistryService(RegistryService $registryService)
79 {
80 $this->registryService = $registryService;
81 }
82
83 /**
84 * Constructor of the language repository
85 */
86 public function __construct()
87 {
88 $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
89 try {
90 $globalSettings = $configurationManager->getLocalConfigurationValueByPath($this->configurationPath);
91 $this->selectedLocales = (array)$globalSettings['availableLanguages'];
92 } catch (\Exception $e) {
93 $configurationManager->setLocalConfigurationValueByPath(
94 $this->configurationPath,
95 array('availableLanguages' => array())
96 );
97 }
98 }
99
100 /**
101 * Returns all objects of this repository
102 *
103 * @return \TYPO3\CMS\Lang\Domain\Model\Language[] The language objects
104 */
105 public function findAll()
106 {
107 if (empty($this->languages)) {
108 $languages = $this->locales->getLanguages();
109 array_shift($languages);
110 foreach ($languages as $locale => $language) {
111 $label = htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:setup/Resources/Private/Language/locallang.xlf:lang_' . $locale));
112 if ($label === '') {
113 $label = htmlspecialchars($language);
114 }
115 $this->languages[$locale] = $this->objectManager->get(
116 Language::class,
117 $locale,
118 $label,
119 in_array($locale, $this->selectedLocales),
120 $this->registryService->get($locale)
121 );
122 }
123 usort($this->languages, function ($a, $b) {
124 /** @var $a \TYPO3\CMS\Lang\Domain\Model\Language */
125 /** @var $b \TYPO3\CMS\Lang\Domain\Model\Language */
126 if ($a->getLabel() == $b->getLabel()) {
127 return 0;
128 }
129 return $a->getLabel() < $b->getLabel() ? -1 : 1;
130 });
131 }
132 return $this->languages;
133 }
134
135 /**
136 * Find selected languages
137 *
138 * @return \TYPO3\CMS\Lang\Domain\Model\Language[] The language objects
139 */
140 public function findSelected()
141 {
142 $languages = $this->findAll();
143 $result = array();
144 foreach ($languages as $language) {
145 if ($language->getSelected()) {
146 $result[] = $language;
147 }
148 }
149 return $result;
150 }
151
152 /**
153 * Update selected languages
154 *
155 * @param array $languages The languages
156 * @return array Update information
157 */
158 public function updateSelectedLanguages($languages)
159 {
160 // Add possible dependencies for selected languages
161 $dependencies = array();
162 foreach ($languages as $language) {
163 $dependencies = array_merge($dependencies, $this->locales->getLocaleDependencies($language));
164 }
165 if (!empty($dependencies)) {
166 $languages = array_unique(array_merge($languages, $dependencies));
167 }
168 $dir = count($languages) - count($this->selectedLocales);
169 $diff = $dir < 0 ? array_diff($this->selectedLocales, $languages) : array_diff($languages, $this->selectedLocales);
170 GeneralUtility::makeInstance(ConfigurationManager::class)->setLocalConfigurationValueByPath(
171 $this->configurationPath,
172 array('availableLanguages' => $languages)
173 );
174 return array(
175 'success' => !empty($diff),
176 'dir' => $dir,
177 'diff' => array_values($diff),
178 'languages' => $languages
179 );
180 }
181
182 /**
183 * Add a language to list of selected languages
184 *
185 * @param string $locale The locale
186 * @return array Update information
187 */
188 public function activateByLocale($locale)
189 {
190 $languages = $this->findAll();
191 $locales = array();
192 foreach ($languages as $language) {
193 if ($language->getSelected() || $language->getLocale() === $locale) {
194 $locales[] = $language->getLocale();
195 }
196 }
197 return $this->updateSelectedLanguages($locales);
198 }
199
200 /**
201 * Remove a language from list of selected languages
202 *
203 * @param string $locale The locale
204 * @return array Update information
205 */
206 public function deactivateByLocale($locale)
207 {
208 $languages = $this->findAll();
209 $locales = array();
210 foreach ($languages as $language) {
211 if ($language->getSelected() && $language->getLocale() !== $locale) {
212 $locales[] = $language->getLocale();
213 }
214 }
215 return $this->updateSelectedLanguages($locales);
216 }
217
218 /**
219 * Returns LanguageService
220 *
221 * @return \TYPO3\CMS\Lang\LanguageService
222 */
223 protected function getLanguageService()
224 {
225 return $GLOBALS['LANG'];
226 }
227 }