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