TYPO3 CMS  TYPO3_8-7
LanguageRepository.php
Go to the documentation of this file.
1 <?php
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 
23 
28 {
32  protected $objectManager;
33 
37  protected $locales;
38 
42  protected $selectedLocales = [];
43 
47  protected $languages = [];
48 
52  protected $configurationPath = 'EXTCONF/lang';
53 
57  protected $registryService;
58 
63  {
64  $this->objectManager = $objectManager;
65  }
66 
70  public function injectLocales(Locales $locales)
71  {
72  $this->locales = $locales;
73  }
74 
79  {
80  $this->registryService = $registryService;
81  }
82 
86  public function __construct()
87  {
88  if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['lang']['availableLanguages'])) {
89  $this->selectedLocales = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['lang']['availableLanguages'];
90  } else {
91  $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
92  $configurationManager->setLocalConfigurationValueByPath(
93  $this->configurationPath,
94  ['availableLanguages' => []]
95  );
96  }
97  }
98 
104  public function findAll()
105  {
106  if (empty($this->languages)) {
107  $languages = $this->locales->getLanguages();
108  array_shift($languages);
109  foreach ($languages as $locale => $language) {
110  $label = htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:setup/Resources/Private/Language/locallang.xlf:lang_' . $locale));
111  if ($label === '') {
112  $label = htmlspecialchars($language);
113  }
114  $this->languages[$locale] = $this->objectManager->get(
115  Language::class,
116  $locale,
117  $label,
118  in_array($locale, $this->selectedLocales),
119  $this->registryService->get($locale)
120  );
121  }
122  usort($this->languages, function ($a, $b) {
125  if ($a->getLabel() == $b->getLabel()) {
126  return 0;
127  }
128  return $a->getLabel() < $b->getLabel() ? -1 : 1;
129  });
130  }
131  return $this->languages;
132  }
133 
139  public function findSelected()
140  {
141  $languages = $this->findAll();
142  $result = [];
143  foreach ($languages as $language) {
144  if ($language->getSelected()) {
145  $result[] = $language;
146  }
147  }
148  return $result;
149  }
150 
158  {
159  // Add possible dependencies for selected languages
160  $dependencies = [];
161  foreach ($languages as $language) {
162  $dependencies = array_merge($dependencies, $this->locales->getLocaleDependencies($language));
163  }
164  if (!empty($dependencies)) {
165  $languages = array_unique(array_merge($languages, $dependencies));
166  }
167  $dir = count($languages) - count($this->selectedLocales);
168  $diff = $dir < 0 ? array_diff($this->selectedLocales, $languages) : array_diff($languages, $this->selectedLocales);
169  GeneralUtility::makeInstance(ConfigurationManager::class)->setLocalConfigurationValueByPath(
170  $this->configurationPath,
171  ['availableLanguages' => $languages]
172  );
173  return [
174  'success' => !empty($diff),
175  'dir' => $dir,
176  'diff' => array_values($diff),
177  'languages' => $languages
178  ];
179  }
180 
187  public function activateByLocale($locale)
188  {
189  $languages = $this->findAll();
190  $locales = [];
191  foreach ($languages as $language) {
192  if ($language->getSelected() || $language->getLocale() === $locale) {
193  $locales[] = $language->getLocale();
194  }
195  }
196  return $this->updateSelectedLanguages($locales);
197  }
198 
205  public function deactivateByLocale($locale)
206  {
207  $languages = $this->findAll();
208  $locales = [];
209  foreach ($languages as $language) {
210  if ($language->getSelected() && $language->getLocale() !== $locale) {
211  $locales[] = $language->getLocale();
212  }
213  }
214  return $this->updateSelectedLanguages($locales);
215  }
216 
222  protected function getLanguageService()
223  {
224  return $GLOBALS['LANG'];
225  }
226 }
injectObjectManager(ObjectManagerInterface $objectManager)
static makeInstance($className,... $constructorArguments)
injectRegistryService(RegistryService $registryService)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']