[TASK] Remove @package and @subpackage annotations
[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 */
31 class LanguageRepository {
32
33 /**
34 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
35 */
36 protected $objectManager;
37
38 /**
39 * @var array
40 */
41 protected $selectedLanguages = array();
42
43 /**
44 * @var \TYPO3\CMS\Core\Localization\Locales
45 */
46 protected $locales;
47
48 /**
49 * @var array
50 */
51 protected $languages = array();
52
53 /**
54 * @var string
55 */
56 protected $configurationPath = 'EXTCONF/lang';
57
58 /**
59 * Constructor of the language repository
60 */
61 public function __construct() {
62 try {
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())
69 );
70 }
71 }
72
73 /**
74 * Injects the object manager
75 *
76 * @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager
77 * @return void
78 */
79 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager) {
80 $this->objectManager = $objectManager;
81 }
82
83 /**
84 * Injects the locales
85 *
86 * @param \TYPO3\CMS\Core\Localization\Locales $locales
87 * @return void
88 */
89 public function injectLocales(\TYPO3\CMS\Core\Localization\Locales $locales) {
90 $this->locales = $locales;
91 }
92
93 /**
94 * Returns all objects of this repository.
95 *
96 * @return array
97 */
98 public function findAll() {
99 if (!count($this->languages)) {
100 $languages = $this->locales->getLanguages();
101 array_shift($languages);
102
103 foreach ($languages as $locale => $language) {
104 $label = htmlspecialchars($GLOBALS['LANG']->sL('LLL:EXT:setup/mod/locallang.xml:lang_' . $locale));
105 if ($label === '') {
106 $label = htmlspecialchars($language);
107 }
108
109 $this->languages[$locale] = $this->objectManager->create(
110 'TYPO3\CMS\Lang\Domain\Model\Language',
111 $locale,
112 $label,
113 in_array($locale, $this->selectedLanguages)
114 );
115 }
116
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()) {
121 return 0;
122 }
123 return $a->getLanguage() < $b->getLanguage() ? -1 : 1;
124 });
125 }
126
127 return $this->languages;
128 }
129
130 /**
131 * Find selected languages
132 *
133 * @return array
134 */
135 public function findSelected() {
136 $languages = $this->findAll();
137
138 $result = array();
139 /** @var $language \TYPO3\CMS\Lang\Domain\Model\Language */
140 foreach ($languages as $language) {
141 if ($language->getSelected()) {
142 $result[] = $language;
143 }
144 }
145
146 return $result;
147 }
148
149 /**
150 * Update selected languages
151 *
152 * @param array $languages
153 * @return array
154 */
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));
160 }
161 if (count($dependencies)) {
162 $languages = array_unique(array_merge($languages, $dependencies));
163 }
164
165 $dir = count($languages) - count($this->selectedLanguages);
166 $diff = $dir < 0 ? array_diff($this->selectedLanguages, $languages) : array_diff($languages, $this->selectedLanguages);
167
168 \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager')->setLocalConfigurationValueByPath(
169 $this->configurationPath,
170 array('availableLanguages' => $languages)
171 );
172
173 return array(
174 'success' => count($diff) > 0,
175 'dir' => $dir,
176 'diff' => array_values($diff),
177 'languages' => $languages
178 );
179 }
180 }
181
182 ?>