[TASK] Use fully qualified name resolution in PHP 5.5
[Packages/TYPO3.CMS.git] / typo3 / sysext / lang / Classes / Domain / Repository / LanguageRepository.php
1 <?php
2 namespace TYPO3\CMS\Lang\Domain\Repository;
3 /**
4 * This file is part of the TYPO3 CMS project.
5 *
6 * It is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License, either version 2
8 * of the License, or any later version.
9 *
10 * For the full copyright and license information, please read the
11 * LICENSE.txt file that was distributed with this source code.
12 *
13 * The TYPO3 project - inspiring people to share!
14 */
15
16 /**
17 * Language repository
18 *
19 * @author Sebastian Fischer <typo3@evoweb.de>
20 */
21 class LanguageRepository {
22
23 /**
24 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
25 */
26 protected $objectManager;
27
28 /**
29 * @var array
30 */
31 protected $selectedLanguages = array();
32
33 /**
34 * @var \TYPO3\CMS\Core\Localization\Locales
35 */
36 protected $locales;
37
38 /**
39 * @var array
40 */
41 protected $languages = array();
42
43 /**
44 * @var string
45 */
46 protected $configurationPath = 'EXTCONF/lang';
47
48 /**
49 * Constructor of the language repository
50 */
51 public function __construct() {
52 try {
53 $globalSettings = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Configuration\ConfigurationManager::class)->getLocalConfigurationValueByPath($this->configurationPath);
54 $this->selectedLanguages = (array)$globalSettings['availableLanguages'];
55 } catch (\Exception $e) {
56 \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Configuration\ConfigurationManager::class)->setLocalConfigurationValueByPath(
57 $this->configurationPath,
58 array('availableLanguages' => array())
59 );
60 }
61 }
62
63 /**
64 * Injects the object manager
65 *
66 * @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager
67 * @return void
68 */
69 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager) {
70 $this->objectManager = $objectManager;
71 }
72
73 /**
74 * Injects the locales
75 *
76 * @param \TYPO3\CMS\Core\Localization\Locales $locales
77 * @return void
78 */
79 public function injectLocales(\TYPO3\CMS\Core\Localization\Locales $locales) {
80 $this->locales = $locales;
81 }
82
83 /**
84 * Returns all objects of this repository.
85 *
86 * @return array
87 */
88 public function findAll() {
89 if (!count($this->languages)) {
90 $languages = $this->locales->getLanguages();
91 array_shift($languages);
92
93 foreach ($languages as $locale => $language) {
94 $label = htmlspecialchars($GLOBALS['LANG']->sL('LLL:EXT:setup/mod/locallang.xlf:lang_' . $locale));
95 if ($label === '') {
96 $label = htmlspecialchars($language);
97 }
98
99 $this->languages[$locale] = $this->objectManager->get(
100 \TYPO3\CMS\Lang\Domain\Model\Language::class,
101 $locale,
102 $label,
103 in_array($locale, $this->selectedLanguages)
104 );
105 }
106
107 usort($this->languages, function($a, $b) {
108 /** @var $a \TYPO3\CMS\Lang\Domain\Model\Language */
109 /** @var $b \TYPO3\CMS\Lang\Domain\Model\Language */
110 if ($a->getLanguage() == $b->getLanguage()) {
111 return 0;
112 }
113 return $a->getLanguage() < $b->getLanguage() ? -1 : 1;
114 });
115 }
116
117 return $this->languages;
118 }
119
120 /**
121 * Find selected languages
122 *
123 * @return array
124 */
125 public function findSelected() {
126 $languages = $this->findAll();
127
128 $result = array();
129 /** @var $language \TYPO3\CMS\Lang\Domain\Model\Language */
130 foreach ($languages as $language) {
131 if ($language->getSelected()) {
132 $result[] = $language;
133 }
134 }
135
136 return $result;
137 }
138
139 /**
140 * Update selected languages
141 *
142 * @param array $languages
143 * @return array
144 */
145 public function updateSelectedLanguages($languages) {
146 // Add possible dependencies for selected languages
147 $dependencies = array();
148 foreach ($languages as $language) {
149 $dependencies = array_merge($dependencies, $this->locales->getLocaleDependencies($language));
150 }
151 if (count($dependencies)) {
152 $languages = array_unique(array_merge($languages, $dependencies));
153 }
154
155 $dir = count($languages) - count($this->selectedLanguages);
156 $diff = $dir < 0 ? array_diff($this->selectedLanguages, $languages) : array_diff($languages, $this->selectedLanguages);
157
158 \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Configuration\ConfigurationManager::class)->setLocalConfigurationValueByPath(
159 $this->configurationPath,
160 array('availableLanguages' => $languages)
161 );
162
163 return array(
164 'success' => count($diff) > 0,
165 'dir' => $dir,
166 'diff' => array_values($diff),
167 'languages' => $languages
168 );
169 }
170 }