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