2 namespace TYPO3\CMS\Lang\Domain\Repository
;
3 /***************************************************************
6 * (c) 2012-2013 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 class LanguageRepository
{
34 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
36 protected $objectManager;
41 protected $selectedLanguages = array();
44 * @var \TYPO3\CMS\Core\Localization\Locales
51 protected $languages = array();
56 protected $configurationPath = 'EXTCONF/lang';
59 * Constructor of the language repository
61 public function __construct() {
63 $globalSettings = \TYPO3\CMS\Core\Utility\GeneralUtility
::makeInstance('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager')->getLocalConfigurationValueByPath($this->configurationPath
);
64 $this->selectedLanguages
= (array) $globalSettings['availableLanguages'];
65 } catch (\Exception
$e) {
66 \TYPO3\CMS\Core\Utility\GeneralUtility
::makeInstance('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager')->setLocalConfigurationValueByPath(
67 $this->configurationPath
,
68 array('availableLanguages' => array())
74 * Injects the object manager
76 * @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager
79 public function injectObjectManager(\TYPO3\CMS\Extbase\
Object\ObjectManager
$objectManager) {
80 $this->objectManager
= $objectManager;
86 * @param \TYPO3\CMS\Core\Localization\Locales $locales
89 public function injectLocales(\TYPO3\CMS\Core\Localization\Locales
$locales) {
90 $this->locales
= $locales;
94 * Returns all objects of this repository.
98 public function findAll() {
99 if (!count($this->languages
)) {
100 $languages = $this->locales
->getLanguages();
101 array_shift($languages);
103 foreach ($languages as $locale => $language) {
104 $label = htmlspecialchars($GLOBALS['LANG']->sL('LLL:EXT:setup/mod/locallang.xml:lang_' . $locale));
106 $label = htmlspecialchars($language);
109 $this->languages
[$locale] = $this->objectManager
->get(
110 'TYPO3\CMS\Lang\Domain\Model\Language',
113 in_array($locale, $this->selectedLanguages
)
117 usort($this->languages
, function($a, $b) {
118 /** @var $a \TYPO3\CMS\Lang\Domain\Model\Language */
119 /** @var $b \TYPO3\CMS\Lang\Domain\Model\Language */
120 if ($a->getLanguage() == $b->getLanguage()) {
123 return $a->getLanguage() < $b->getLanguage() ?
-1 : 1;
127 return $this->languages
;
131 * Find selected languages
135 public function findSelected() {
136 $languages = $this->findAll();
139 /** @var $language \TYPO3\CMS\Lang\Domain\Model\Language */
140 foreach ($languages as $language) {
141 if ($language->getSelected()) {
142 $result[] = $language;
150 * Update selected languages
152 * @param array $languages
155 public function updateSelectedLanguages($languages) {
156 // Add possible dependencies for selected languages
157 $dependencies = array();
158 foreach ($languages as $language) {
159 $dependencies = array_merge($dependencies, $this->locales
->getLocaleDependencies($language));
161 if (count($dependencies)) {
162 $languages = array_unique(array_merge($languages, $dependencies));
165 $dir = count($languages) - count($this->selectedLanguages
);
166 $diff = $dir < 0 ?
array_diff($this->selectedLanguages
, $languages) : array_diff($languages, $this->selectedLanguages
);
168 \TYPO3\CMS\Core\Utility\GeneralUtility
::makeInstance('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager')->setLocalConfigurationValueByPath(
169 $this->configurationPath
,
170 array('availableLanguages' => $languages)
174 'success' => count($diff) > 0,
176 'diff' => array_values($diff),
177 'languages' => $languages