TYPO3 CMS  TYPO3_7-6
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 
18 
23 {
27  protected $objectManager;
28 
32  protected $locales;
33 
37  protected $selectedLocales = [];
38 
42  protected $languages = [];
43 
47  protected $configurationPath = 'EXTCONF/lang';
48 
52  protected $registryService;
53 
57  public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
58  {
59  $this->objectManager = $objectManager;
60  }
61 
65  public function injectLocales(\TYPO3\CMS\Core\Localization\Locales $locales)
66  {
67  $this->locales = $locales;
68  }
69 
73  public function injectRegistryService(\TYPO3\CMS\Lang\Service\RegistryService $registryService)
74  {
75  $this->registryService = $registryService;
76  }
77 
81  public function __construct()
82  {
83  $configurationManager = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Configuration\ConfigurationManager::class);
84  try {
85  $globalSettings = $configurationManager->getLocalConfigurationValueByPath($this->configurationPath);
86  $this->selectedLocales = (array)$globalSettings['availableLanguages'];
87  } catch (\Exception $e) {
88  $configurationManager->setLocalConfigurationValueByPath(
89  $this->configurationPath,
90  ['availableLanguages' => []]
91  );
92  }
93  }
94 
100  public function findAll()
101  {
102  if (empty($this->languages)) {
103  $languages = $this->locales->getLanguages();
104  array_shift($languages);
105  foreach ($languages as $locale => $language) {
106  $label = htmlspecialchars($GLOBALS['LANG']->sL('LLL:EXT:setup/Resources/Private/Language/locallang.xlf:lang_' . $locale));
107  if ($label === '') {
108  $label = htmlspecialchars($language);
109  }
110  $this->languages[$locale] = $this->objectManager->get(
111  \TYPO3\CMS\Lang\Domain\Model\Language::class,
112  $locale,
113  $label,
114  in_array($locale, $this->selectedLocales),
115  $this->registryService->get($locale)
116  );
117  }
118  usort($this->languages, function ($a, $b) {
121  if ($a->getLabel() == $b->getLabel()) {
122  return 0;
123  }
124  return $a->getLabel() < $b->getLabel() ? -1 : 1;
125  });
126  }
127  return $this->languages;
128  }
129 
135  public function findSelected()
136  {
137  $languages = $this->findAll();
138  $result = [];
139  foreach ($languages as $language) {
140  if ($language->getSelected()) {
141  $result[] = $language;
142  }
143  }
144  return $result;
145  }
146 
154  {
155  // Add possible dependencies for selected languages
156  $dependencies = [];
157  foreach ($languages as $language) {
158  $dependencies = array_merge($dependencies, $this->locales->getLocaleDependencies($language));
159  }
160  if (!empty($dependencies)) {
161  $languages = array_unique(array_merge($languages, $dependencies));
162  }
163  $dir = count($languages) - count($this->selectedLocales);
164  $diff = $dir < 0 ? array_diff($this->selectedLocales, $languages) : array_diff($languages, $this->selectedLocales);
165  GeneralUtility::makeInstance(\TYPO3\CMS\Core\Configuration\ConfigurationManager::class)->setLocalConfigurationValueByPath(
166  $this->configurationPath,
167  ['availableLanguages' => $languages]
168  );
169  return [
170  'success' => !empty($diff),
171  'dir' => $dir,
172  'diff' => array_values($diff),
173  'languages' => $languages
174  ];
175  }
176 
183  public function activateByLocale($locale)
184  {
185  $languages = $this->findAll();
186  $locales = [];
187  foreach ($languages as $language) {
188  if ($language->getSelected() || $language->getLocale() === $locale) {
189  $locales[] = $language->getLocale();
190  }
191  }
192  return $this->updateSelectedLanguages($locales);
193  }
194 
201  public function deactivateByLocale($locale)
202  {
203  $languages = $this->findAll();
204  $locales = [];
205  foreach ($languages as $language) {
206  if ($language->getSelected() && $language->getLocale() !== $locale) {
207  $locales[] = $language->getLocale();
208  }
209  }
210  return $this->updateSelectedLanguages($locales);
211  }
212 }
injectRegistryService(\TYPO3\CMS\Lang\Service\RegistryService $registryService)
injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
injectLocales(\TYPO3\CMS\Core\Localization\Locales $locales)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']