[FEATURE] Add translation handling module
[Packages/TYPO3.CMS.git] / typo3 / sysext / lang / Classes / Domain / Repository / LanguageRepository.php
1 <?php
2 namespace TYPO3\CMS\Lang\Domain\Repository;
3 /***************************************************************
4 * Copyright notice
5 *
6 * (c) 2012 Sebastian Fischer <typo3@evoweb.de>
7 * All rights reserved
8 *
9 * This script is part of the TYPO3 project. The TYPO3 project is
10 * free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * The GNU General Public License can be found at
16 * http://www.gnu.org/copyleft/gpl.html.
17 *
18 * This script is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * This copyright notice MUST APPEAR in all copies of the script!
24 ***************************************************************/
25
26 /**
27 * Language repository
28 *
29 * @author Sebastian Fischer <typo3@evoweb.de>
30 * @package lang
31 * @subpackage LanguageRepository
32 */
33 class LanguageRepository {
34
35 /**
36 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
37 */
38 protected $objectManager;
39
40 /**
41 * @var array
42 */
43 protected $selectedLanguages = array();
44
45 /**
46 * @var \TYPO3\CMS\Core\Localization\Locales
47 */
48 protected $locales;
49
50 /**
51 * @var array
52 */
53 protected $languages = array();
54
55 /**
56 * @var string
57 */
58 protected $configurationPath = 'EXTCONF/lang';
59
60 /**
61 * Constructor of the language repository
62 */
63 public function __construct() {
64 try {
65 $globalSettings = \TYPO3\CMS\Core\Configuration\ConfigurationManager::getLocalConfigurationValueByPath($this->configurationPath);
66 $this->selectedLanguages = (array) $globalSettings['availableLanguages'];
67 } catch (\Exception $e) {
68 \TYPO3\CMS\Core\Configuration\ConfigurationManager::setLocalConfigurationValueByPath(
69 $this->configurationPath,
70 array('availableLanguages' => array())
71 );
72 }
73 }
74
75 /**
76 * Injects the object manager
77 *
78 * @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager
79 * @return void
80 */
81 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager) {
82 $this->objectManager = $objectManager;
83 }
84
85 /**
86 * Injects the locales
87 *
88 * @param \TYPO3\CMS\Core\Localization\Locales $locales
89 * @return void
90 */
91 public function injectLocales(\TYPO3\CMS\Core\Localization\Locales $locales) {
92 $this->locales = $locales;
93 }
94
95 /**
96 * Returns all objects of this repository.
97 *
98 * @return array
99 */
100 public function findAll() {
101 if (!count($this->languages)) {
102 $languages = $this->locales->getLanguages();
103 array_shift($languages);
104
105 foreach ($languages as $locale => $language) {
106 $label = htmlspecialchars($GLOBALS['LANG']->sL('LLL:EXT:setup/mod/locallang.xml:lang_' . $locale));
107 if ($label === '') {
108 $label = htmlspecialchars($language);
109 }
110
111 $this->languages[$locale] = $this->objectManager->create(
112 'TYPO3\CMS\Lang\Domain\Model\Language',
113 $locale,
114 $label,
115 in_array($locale, $this->selectedLanguages)
116 );
117 }
118
119 usort($this->languages, function($a, $b) {
120 /** @var $a \TYPO3\CMS\Lang\Domain\Model\Language */
121 /** @var $b \TYPO3\CMS\Lang\Domain\Model\Language */
122 if ($a->getLanguage() == $b->getLanguage()) {
123 return 0;
124 }
125 return $a->getLanguage() < $b->getLanguage() ? -1 : 1;
126 });
127 }
128
129 return $this->languages;
130 }
131
132 /**
133 * Find selected languages
134 *
135 * @return array
136 */
137 public function findSelected() {
138 $languages = $this->findAll();
139
140 $result = array();
141 /** @var $language \TYPO3\CMS\Lang\Domain\Model\Language */
142 foreach ($languages as $language) {
143 if ($language->getSelected()) {
144 $result[] = $language;
145 }
146 }
147
148 return $result;
149 }
150
151 /**
152 * Update selected languages
153 *
154 * @param array $languages
155 * @return array
156 */
157 public function updateSelectedLanguages($languages) {
158 // Add possible dependencies for selected languages
159 $dependencies = array();
160 foreach ($languages as $language) {
161 $dependencies = array_merge($dependencies, $this->locales->getLocaleDependencies($language));
162 }
163 if (count($dependencies)) {
164 $languages = array_unique(array_merge($languages, $dependencies));
165 }
166
167 $dir = count($languages) - count($this->selectedLanguages);
168 $diff = $dir < 0 ? array_diff($this->selectedLanguages, $languages) : array_diff($languages, $this->selectedLanguages);
169
170 \TYPO3\CMS\Core\Configuration\ConfigurationManager::setLocalConfigurationValueByPath(
171 $this->configurationPath,
172 array('availableLanguages' => $languages)
173 );
174
175 return array(
176 'success' => count($diff) > 0,
177 'dir' => $dir,
178 'diff' => array_values($diff),
179 'languages' => $languages
180 );
181 }
182 }
183
184 ?>