2 namespace TYPO3\CMS\Lang\Domain\Repository
;
3 /***************************************************************
6 * (c) 2012 Sebastian Fischer <typo3@evoweb.de>
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.
15 * The GNU General Public License can be found at
16 * http://www.gnu.org/copyleft/gpl.html.
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.
23 * This copyright notice MUST APPEAR in all copies of the script!
24 ***************************************************************/
29 * @author Sebastian Fischer <typo3@evoweb.de>
31 * @subpackage LanguageRepository
33 class LanguageRepository
{
36 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
38 protected $objectManager;
43 protected $selectedLanguages = array();
46 * @var \TYPO3\CMS\Core\Localization\Locales
53 protected $languages = array();
58 protected $configurationPath = 'EXTCONF/lang';
61 * Constructor of the language repository
63 public function __construct() {
65 $globalSettings = \TYPO3\CMS\Core\Utility\GeneralUtility
::makeInstance('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager')->getLocalConfigurationValueByPath($this->configurationPath
);
66 $this->selectedLanguages
= (array) $globalSettings['availableLanguages'];
67 } catch (\Exception
$e) {
68 \TYPO3\CMS\Core\Utility\GeneralUtility
::makeInstance('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager')->setLocalConfigurationValueByPath(
69 $this->configurationPath
,
70 array('availableLanguages' => array())
76 * Injects the object manager
78 * @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager
81 public function injectObjectManager(\TYPO3\CMS\Extbase\
Object\ObjectManager
$objectManager) {
82 $this->objectManager
= $objectManager;
88 * @param \TYPO3\CMS\Core\Localization\Locales $locales
91 public function injectLocales(\TYPO3\CMS\Core\Localization\Locales
$locales) {
92 $this->locales
= $locales;
96 * Returns all objects of this repository.
100 public function findAll() {
101 if (!count($this->languages
)) {
102 $languages = $this->locales
->getLanguages();
103 array_shift($languages);
105 foreach ($languages as $locale => $language) {
106 $label = htmlspecialchars($GLOBALS['LANG']->sL('LLL:EXT:setup/mod/locallang.xml:lang_' . $locale));
108 $label = htmlspecialchars($language);
111 $this->languages
[$locale] = $this->objectManager
->create(
112 'TYPO3\CMS\Lang\Domain\Model\Language',
115 in_array($locale, $this->selectedLanguages
)
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()) {
125 return $a->getLanguage() < $b->getLanguage() ?
-1 : 1;
129 return $this->languages
;
133 * Find selected languages
137 public function findSelected() {
138 $languages = $this->findAll();
141 /** @var $language \TYPO3\CMS\Lang\Domain\Model\Language */
142 foreach ($languages as $language) {
143 if ($language->getSelected()) {
144 $result[] = $language;
152 * Update selected languages
154 * @param array $languages
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));
163 if (count($dependencies)) {
164 $languages = array_unique(array_merge($languages, $dependencies));
167 $dir = count($languages) - count($this->selectedLanguages
);
168 $diff = $dir < 0 ?
array_diff($this->selectedLanguages
, $languages) : array_diff($languages, $this->selectedLanguages
);
170 \TYPO3\CMS\Core\Utility\GeneralUtility
::makeInstance('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager')->setLocalConfigurationValueByPath(
171 $this->configurationPath
,
172 array('availableLanguages' => $languages)
176 'success' => count($diff) > 0,
178 'diff' => array_values($diff),
179 'languages' => $languages