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