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