34ccf86377f28f455fba458a241b39561f3b790d
[Packages/TYPO3.CMS.git] / typo3 / sysext / em / classes / settings / class.tx_em_settings.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010 Steffen Kamper (info@sk-typo3.de)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Module: Extension manager, developer module
29 *
30 * @author Steffen Kamper <info@sk-typo3.de>
31 */
32
33
34 $GLOBALS['LANG']->includeLLFile(t3lib_extMgm::extPath('em', 'language/locallang.xml'));
35
36
37 class tx_em_Settings implements t3lib_Singleton {
38
39 public $MOD_MENU = array(); // Module menu items
40
41
42 /**
43 * Settings array
44 *
45 * @var array
46 */
47 protected $settings;
48
49
50 /**
51 * Constructor
52 *
53 */
54 public function __construct() {
55 $this->readSettings();
56 }
57
58 /**
59 * Get settings
60 */
61 public function getSettings() {
62 return $this->settings;
63 }
64
65 /**
66 * Get user Settings
67 */
68 public function getUserSettings() {
69 $userSettings = t3lib_beFunc::getModTSconfig(0, 'mod.tools_em');
70 return $userSettings['properties'];
71 }
72
73 /**
74 * Save user settings
75 *
76 * @param array $settings
77 */
78 public function saveSettings($settings) {
79 $this->settings = t3lib_BEfunc::getModuleData($this->MOD_MENU, $settings, 'tools_em');
80 return $this->settings;
81 }
82
83 /**
84 * Save single value in session settings
85 *
86 * @param string $name
87 * @param string $value
88 */
89 public function saveSetting($name, $value) {
90 t3lib_BEfunc::getModuleData($this->MOD_MENU, array($name => $value), 'tools_em');
91 }
92
93 /**
94 * Initial settings for extension manager module data
95 *
96 * @return void
97 */
98 protected function readSettings() {
99 $globalSettings = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['em']);
100 if (!is_array($globalSettings)) {
101 $globalSettings = array(
102 'displayMyExtensions' => 0,
103 'selectedLanguages' => array(),
104 'inlineToWindow' => 1,
105 );
106 }
107 $this->MOD_MENU = array(
108 'function' => array(
109 'loaded_list' => $GLOBALS['LANG']->getLL('menu_loaded_extensions'),
110 'installed_list' => $GLOBALS['LANG']->getLL('menu_install_extensions'),
111 'import' => $GLOBALS['LANG']->getLL('menu_import_extensions'),
112 'translations' => $GLOBALS['LANG']->getLL('menu_translation_handling'),
113 'settings' => $GLOBALS['LANG']->getLL('menu_settings'),
114 'extensionmanager' => $GLOBALS['LANG']->getLL('header'),
115 'updates' => $GLOBALS['LANG']->getLL('menu_extension_updates'),
116 ),
117 'listOrder' => array(
118 'cat' => $GLOBALS['LANG']->getLL('list_order_category'),
119 'author_company' => $GLOBALS['LANG']->getLL('list_order_author'),
120 'state' => $GLOBALS['LANG']->getLL('list_order_state'),
121 'type' => $GLOBALS['LANG']->getLL('list_order_type'),
122 ),
123 'display_details' => array(
124 1 => $GLOBALS['LANG']->getLL('show_details'),
125 0 => $GLOBALS['LANG']->getLL('show_description'),
126 2 => $GLOBALS['LANG']->getLL('show_more_details'),
127
128 3 => $GLOBALS['LANG']->getLL('show_technical'),
129 4 => $GLOBALS['LANG']->getLL('show_validating'),
130 5 => $GLOBALS['LANG']->getLL('show_changed'),
131 ),
132 'display_shy' => '',
133 'display_own' => '',
134 'display_obsolete' => '',
135 'display_installed' => '',
136 'display_files' => '',
137 'hide_shy' => 0,
138 'hide_obsolete' => 0,
139
140
141 'singleDetails' => array(
142 'info' => $GLOBALS['LANG']->getLL('details_info'),
143 'edit' => $GLOBALS['LANG']->getLL('details_edit'),
144 'backup' => $GLOBALS['LANG']->getLL('details_backup_delete'),
145 'dump' => $GLOBALS['LANG']->getLL('details_dump_db'),
146 'upload' => $GLOBALS['LANG']->getLL('details_upload'),
147 'updateModule' => $GLOBALS['LANG']->getLL('details_update'),
148 ),
149 'fe_u' => '',
150 'fe_p' => '',
151
152 'mirrorListURL' => '',
153 'rep_url' => '',
154 'extMirrors' => '',
155
156 // returns uid of currently selected repository
157 // default and hardcoded: 1 = TYPO3.org
158 'selectedRepository' => '1',
159 'selectedMirror' => '0',
160
161 'selectedLanguages' => '',
162
163 'mainTab' => '0',
164 );
165 $this->settings = t3lib_BEfunc::getModuleData($this->MOD_MENU, t3lib_div::_GP('SET'), 'tools_em');
166 $this->settings = array_merge($this->settings, $globalSettings);
167 }
168
169 /**
170 * Gets url for mirror
171 *
172 * @return string
173 */
174 public function getMirrorURL() {
175 if (strlen($this->settings['rep_url'])) {
176 return $this->settings['rep_url'];
177 }
178
179 $mirrors = unserialize($this->settings['extMirrors']);
180
181 if(!is_array($mirrors)) {
182 if ($this->settings['selectedRepository'] < 1) {
183 $this->settings['selectedRepository'] = 1;
184 }
185 }
186 /** @var $repository tx_em_Repository */
187 $repository = t3lib_div::makeInstance('tx_em_Repository', $this->settings['selectedRepository']);
188 if ($repository->getMirrorListUrl()) {
189 /** @var $objRepositoryUtility tx_em_Repository_Utility */
190 $objRepositoryUtility = t3lib_div::makeInstance('tx_em_Repository_Utility', $repository);
191 $mirrors = $objRepositoryUtility->getMirrors(TRUE)->getMirrors();
192 if(!is_array($mirrors)) {
193 return FALSE;
194 } else {
195 $this->settings['extMirrors'] = serialize($mirrors);
196 $this->saveSetting('extMirrors', $this->settings['extMirrors']);
197 }
198 }
199 if (!$this->settings['selectedMirror']) {
200 $rand = array_rand($mirrors);
201 $url = 'http://' . $mirrors[$rand]['host'] . $mirrors[$rand]['path'];
202 }
203 else {
204 $selectedMirror = NULL;
205 foreach ($mirrors as $mirror) {
206 if ($mirror['host'] === $this->settings['selectedMirror']) {
207 $selectedMirror = $mirror;
208 break;
209 }
210 }
211 $url = 'http://' . $selectedMirror['host'] . $selectedMirror['path'];
212 }
213
214 return $url;
215 }
216
217 /**
218 * Method returns registered extension repositories.
219 *
220 * Registered repositories are global (per installation) settings.
221 *
222 * @access public
223 * @return array of {@link em_repository em_repository} instances
224 * @see registerDefaultRepository(), setRegisteredRepositories()
225 */
226 public function getRegisteredRepositories() {
227 $registry = t3lib_div::makeInstance('t3lib_Registry');
228 $regRepos = $registry->get('core', 'em.repositories.registered');
229
230 // create default entry if there wasn't one
231 if (empty($regRepos)) {
232 $this->registerDefaultRepository();
233 $regRepos = $registry->get('core', 'em.repositories.registered');
234 }
235
236 return $regRepos;
237 }
238
239 /**
240 * Method creates default registered repositories entry.
241 *
242 * @access protected
243 * @return void
244 * @see getRegisteredRepository(), setRegisteredRepositories()
245 */
246 protected function registerDefaultRepository() {
247 $registry = t3lib_div::makeInstance('t3lib_Registry');
248 $defaultRepo = t3lib_div::makeInstance('tx_em_Repository');
249 $registry->set('core', 'em.repositories.registered', array($defaultRepo));
250 }
251
252 /**
253 * Method sets (persists) registered repositories.
254 *
255 * Registered repositories are global (per installation) settings.
256 *
257 * @access public
258 * @param array $repositories array of {@link em_repository em_repository} instances
259 * @see registerDefaultRepository(), setRegisteredRepositories()
260 * @throws InvalidArgumentException in case argument contains no instances of {@link em_repository em_repository}
261 */
262 public function setRegisteredRepositories(array $repositories) {
263 // removing mirror instances
264 foreach ($repositories as $repository) {
265 if ($repository instanceof em_repository) {
266 $repository->removeMirrors();
267 } else {
268 throw new InvalidArgumentException(get_class($this) . ': ' . 'No valid instances of em_repository given.');
269 }
270 }
271 if (count($repositories)) {
272 $registry = t3lib_div::makeInstance('t3lib_Registry');
273 $registry->set('core', 'em.repositories.registered', $repositories);
274 }
275 }
276
277 /**
278 * Method returns currently selected repository
279 *
280 * Selected repository is local (per user) settings.
281 *
282 * @access public
283 * @return em_repository repository instance that is currently selected by a BE user
284 * @see setSelectedRepository()
285 */
286 public function getSelectedRepository() {
287 return t3lib_div::makeInstance('tx_em_Repository', $this->settings['selectedRepository']);
288 }
289
290 /**
291 * Method sets currently selected repository.
292 *
293 * Selected repository is local (per user) settings.
294 *
295 * @todo STUB, implementation missing
296 * @access public
297 * @param em_repository $repository repository instance that is currently selected by a BE user
298 * @see getSelectedRepository()
299 */
300 public function setSelectedRepository(em_repository $repository) {
301 // this method would set sth. like "REPOSITORY_TITLE:INT" in a setting field
302 // REPOSITORY_TITLE = example: main
303 // INT = 0 means randomly selected mirror, >0 selects specific mirror
304 }
305
306 }
307
308 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['typo3/sysext/em/classes/settings/class.tx_em_settings.php'])) {
309 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['typo3/sysext/em/classes/repository/class.tx_em_settings.php']);
310 }
311
312 ?>