[CLEANUP] Add missing empty lines before classes' closing curly braces
[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 /**
18 * Language repository
19 *
20 * @author Sebastian Fischer <typo3@evoweb.de>
21 */
22 class LanguageRepository {
23
24 /**
25 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
26 */
27 protected $objectManager;
28
29 /**
30 * @var array
31 */
32 protected $selectedLanguages = array();
33
34 /**
35 * @var \TYPO3\CMS\Core\Localization\Locales
36 */
37 protected $locales;
38
39 /**
40 * @var array
41 */
42 protected $languages = array();
43
44 /**
45 * @var string
46 */
47 protected $configurationPath = 'EXTCONF/lang';
48
49 /**
50 * Constructor of the language repository
51 */
52 public function __construct() {
53 try {
54 $globalSettings = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Configuration\ConfigurationManager::class)->getLocalConfigurationValueByPath($this->configurationPath);
55 $this->selectedLanguages = (array)$globalSettings['availableLanguages'];
56 } catch (\Exception $e) {
57 \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Configuration\ConfigurationManager::class)->setLocalConfigurationValueByPath(
58 $this->configurationPath,
59 array('availableLanguages' => array())
60 );
61 }
62 }
63
64 /**
65 * Injects the object manager
66 *
67 * @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager
68 * @return void
69 */
70 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager) {
71 $this->objectManager = $objectManager;
72 }
73
74 /**
75 * Injects the locales
76 *
77 * @param \TYPO3\CMS\Core\Localization\Locales $locales
78 * @return void
79 */
80 public function injectLocales(\TYPO3\CMS\Core\Localization\Locales $locales) {
81 $this->locales = $locales;
82 }
83
84 /**
85 * Returns all objects of this repository.
86 *
87 * @return array
88 */
89 public function findAll() {
90 if (!count($this->languages)) {
91 $languages = $this->locales->getLanguages();
92 array_shift($languages);
93
94 foreach ($languages as $locale => $language) {
95 $label = htmlspecialchars($GLOBALS['LANG']->sL('LLL:EXT:setup/mod/locallang.xlf:lang_' . $locale));
96 if ($label === '') {
97 $label = htmlspecialchars($language);
98 }
99
100 $this->languages[$locale] = $this->objectManager->get(
101 \TYPO3\CMS\Lang\Domain\Model\Language::class,
102 $locale,
103 $label,
104 in_array($locale, $this->selectedLanguages)
105 );
106 }
107
108 usort($this->languages, function($a, $b) {
109 /** @var $a \TYPO3\CMS\Lang\Domain\Model\Language */
110 /** @var $b \TYPO3\CMS\Lang\Domain\Model\Language */
111 if ($a->getLanguage() == $b->getLanguage()) {
112 return 0;
113 }
114 return $a->getLanguage() < $b->getLanguage() ? -1 : 1;
115 });
116 }
117
118 return $this->languages;
119 }
120
121 /**
122 * Find selected languages
123 *
124 * @return array
125 */
126 public function findSelected() {
127 $languages = $this->findAll();
128
129 $result = array();
130 /** @var $language \TYPO3\CMS\Lang\Domain\Model\Language */
131 foreach ($languages as $language) {
132 if ($language->getSelected()) {
133 $result[] = $language;
134 }
135 }
136
137 return $result;
138 }
139
140 /**
141 * Update selected languages
142 *
143 * @param array $languages
144 * @return array
145 */
146 public function updateSelectedLanguages($languages) {
147 // Add possible dependencies for selected languages
148 $dependencies = array();
149 foreach ($languages as $language) {
150 $dependencies = array_merge($dependencies, $this->locales->getLocaleDependencies($language));
151 }
152 if (count($dependencies)) {
153 $languages = array_unique(array_merge($languages, $dependencies));
154 }
155
156 $dir = count($languages) - count($this->selectedLanguages);
157 $diff = $dir < 0 ? array_diff($this->selectedLanguages, $languages) : array_diff($languages, $this->selectedLanguages);
158
159 \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Configuration\ConfigurationManager::class)->setLocalConfigurationValueByPath(
160 $this->configurationPath,
161 array('availableLanguages' => $languages)
162 );
163
164 return array(
165 'success' => count($diff) > 0,
166 'dir' => $dir,
167 'diff' => array_values($diff),
168 'languages' => $languages
169 );
170 }
171
172 }