[TASK] TYPO3 code must not use the else if construct
[Packages/TYPO3.CMS.git] / typo3 / classes / class.modulemenu.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2007-2011 Ingo Renner <ingo@typo3.org>
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
29 if(TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_AJAX) {
30 $GLOBALS['LANG']->includeLLFile('EXT:lang/locallang_misc.xml');
31 }
32
33 /**
34 * class to render the TYPO3 backend menu for the modules
35 *
36 * @author Ingo Renner <ingo@typo3.org>
37 * @package TYPO3
38 * @subpackage core
39 */
40 class ModuleMenu {
41
42 /**
43 * module loading object
44 *
45 * @var t3lib_loadModules
46 */
47 protected $moduleLoader;
48
49 protected $backPath;
50 protected $linkModules;
51 protected $loadedModules;
52
53
54 /**
55 * constructor, initializes several variables
56 *
57 * @return void
58 */
59 public function __construct() {
60
61 $this->backPath = '';
62 $this->linkModules = true;
63
64 // Loads the backend modules available for the logged in user.
65 $this->moduleLoader = t3lib_div::makeInstance('t3lib_loadModules');
66 $this->moduleLoader->observeWorkspaces = true;
67 $this->moduleLoader->load($GLOBALS['TBE_MODULES']);
68 $this->loadedModules = $this->moduleLoader->modules;
69
70 }
71
72 /**
73 * sets the path back to /typo3/
74 *
75 * @param string path back to /typo3/
76 * @return void
77 */
78 public function setBackPath($backPath) {
79 if(!is_string($backPath)) {
80 throw new InvalidArgumentException('parameter $backPath must be of type string', 1193315266);
81 }
82
83 $this->backPath = $backPath;
84 }
85
86 /**
87 * loads the collapse states for the main modules from user's configuration (uc)
88 *
89 * @return array collapse states
90 */
91 protected function getCollapsedStates() {
92
93 $collapsedStates = array();
94 if($GLOBALS['BE_USER']->uc['moduleData']['moduleMenu']) {
95 $collapsedStates = $GLOBALS['BE_USER']->uc['moduleData']['moduleMenu'];
96 }
97
98 return $collapsedStates;
99 }
100
101 /**
102 * ModuleMenu Store loading data
103 *
104 * @param array_type $params
105 * @param object $ajaxObj
106 */
107 public function getModuleData($params, $ajaxObj) {
108 $data = array('success' => TRUE, 'root' => array());
109 $rawModuleData = $this->getRawModuleData();
110 $index = 0;
111 foreach($rawModuleData as $moduleKey => $moduleData) {
112 $key = substr($moduleKey, 8);
113 $num = count($data['root']);
114 if($moduleData['link'] != 'dummy.php' || ($moduleData['link'] == 'dummy.php' && is_array($moduleData['subitems'])) ) {
115 $data['root'][$num]['key'] = $key;
116 $data['root'][$num]['menuState'] = $GLOBALS['BE_USER']->uc['moduleData']['menuState'][$moduleKey];
117 $data['root'][$num]['label'] = $moduleData['title'];
118 $data['root'][$num]['subitems'] = is_array($moduleData['subitems']) ? count($moduleData['subitems']) : 0;
119
120
121 if($moduleData['link'] && $this->linkModules) {
122 $data['root'][$num]['link'] = 'top.goToModule(\'' . $moduleData['name'] . '\')';
123 }
124
125 // traverse submodules
126 if (is_array($moduleData['subitems'])) {
127 foreach($moduleData['subitems'] as $subKey => $subData) {
128 $data['root'][$num]['sub'][] = array(
129 'name' => $subData['name'],
130 'description' => $subData['description'],
131 'label' => $subData['title'],
132 'icon' => $subData['icon']['filename'],
133 'navframe' => $subData['parentNavigationFrameScript'],
134 'link' => $subData['link'],
135 'originalLink' => $subData['originalLink'],
136 'index' => $index++,
137 'navigationFrameScript' => $subData['navigationFrameScript'],
138 'navigationFrameScriptParam' => $subData['navigationFrameScriptParam'],
139 'navigationComponentId' => $subData['navigationComponentId'],
140 );
141 }
142 }
143 }
144 }
145 if ($ajaxObj) {
146 $ajaxObj->setContent($data);
147 $ajaxObj->setContentFormat('jsonbody');
148
149 } else {
150 return $data;
151 }
152 }
153
154 /**
155 * returns the loaded modules
156 *
157 * @return array array of loaded modules
158 */
159 public function getLoadedModules() {
160 return $this->loadedModules;
161 }
162
163 /**
164 * saves the menu's toggle state in the backend user's uc
165 *
166 * @param array array of parameters from the AJAX interface, currently unused
167 * @param TYPO3AJAX object of type TYPO3AJAX
168 * @return void
169 */
170 public function saveMenuState($params, $ajaxObj) {
171 $menuItem = t3lib_div::_POST('menuid');
172 $state = t3lib_div::_POST('state') === 'true' ? 1 : 0;
173
174 $GLOBALS['BE_USER']->uc['moduleData']['menuState'][$menuItem] = $state;
175 $GLOBALS['BE_USER']->writeUC();
176 }
177
178
179 /**
180 * gets the raw module data
181 *
182 * @return array multi dimension array with module data
183 */
184 public function getRawModuleData() {
185 $modules = array();
186
187 // Remove the 'doc' module?
188 if($GLOBALS['BE_USER']->getTSConfigVal('options.disableDocModuleInAB')) {
189 unset($this->loadedModules['doc']);
190 }
191
192 foreach($this->loadedModules as $moduleName => $moduleData) {
193 $moduleLink = '';
194 if(!is_array($moduleData['sub'])) {
195 $moduleLink = $moduleData['script'];
196 }
197 $moduleLink = t3lib_div::resolveBackPath($moduleLink);
198
199 $moduleKey = 'modmenu_' . $moduleName;
200 $moduleIcon = $this->getModuleIcon($moduleKey);
201
202 $modules[$moduleKey] = array(
203 'name' => $moduleName,
204 'title' => $GLOBALS['LANG']->moduleLabels['tabs'][$moduleName . '_tab'],
205 'onclick' => 'top.goToModule(\''.$moduleName.'\');',
206 'icon' => $moduleIcon,
207 'link' => $moduleLink,
208 'description' => $GLOBALS['LANG']->moduleLabels['labels'][$moduleKey.'label']
209 );
210
211 if (!is_array($moduleData['sub']) && $moduleData['script'] != 'dummy.php') {
212 // Work around for modules with own main entry, but being self the only submodule
213 $modules[$moduleKey]['subitems'][$moduleKey] = array(
214 'name' => $moduleName,
215 'title' => $GLOBALS['LANG']->moduleLabels['tabs'][$moduleName . '_tab'],
216 'onclick' => 'top.goToModule(\'' . $moduleName . '\');',
217 'icon' => $this->getModuleIcon($moduleName . '_tab'),
218 'link' => $moduleLink,
219 'originalLink' => $moduleLink,
220 'description' => $GLOBALS['LANG']->moduleLabels['labels'][$moduleKey . 'label'],
221 'navigationFrameScript' => NULL,
222 'navigationFrameScriptParam' => NULL,
223 'navigationComponentId' => NULL,
224 );
225 } elseif (is_array($moduleData['sub'])) {
226 foreach($moduleData['sub'] as $submoduleName => $submoduleData) {
227 $submoduleLink = t3lib_div::resolveBackPath($submoduleData['script']);
228
229 $submoduleKey = $moduleName . '_' . $submoduleName . '_tab';
230 $submoduleIcon = $this->getModuleIcon($submoduleKey);
231 $submoduleDescription = $GLOBALS['LANG']->moduleLabels['labels'][$submoduleKey . 'label'];
232
233 $originalLink = $submoduleLink;
234
235 $modules[$moduleKey]['subitems'][$submoduleKey] = array(
236 'name' => $moduleName . '_' . $submoduleName,
237 'title' => $GLOBALS['LANG']->moduleLabels['tabs'][$submoduleKey],
238 'onclick' => 'top.goToModule(\'' . $moduleName . '_' . $submoduleName . '\');',
239 'icon' => $submoduleIcon,
240 'link' => $submoduleLink,
241 'originalLink' => $originalLink,
242 'description' => $submoduleDescription,
243 'navigationFrameScript' => $submoduleData['navFrameScript'],
244 'navigationFrameScriptParam' => $submoduleData['navFrameScriptParam'],
245 'navigationComponentId' => $submoduleData['navigationComponentId'],
246 );
247
248 if($moduleData['navFrameScript']) {
249 $modules[$moduleKey]['subitems'][$submoduleKey]['parentNavigationFrameScript'] = $moduleData['navFrameScript'];
250 }
251 }
252 }
253 }
254
255 return $modules;
256 }
257
258 /**
259 * gets the module icon and its size
260 *
261 * @param string module key
262 * @return array icon data array with 'filename', 'size', and 'html'
263 */
264 protected function getModuleIcon($moduleKey) {
265 $icon = array(
266 'filename' => '',
267 'size' => '',
268 'title' => '',
269 'html' => ''
270 );
271
272 $iconFileRelative = $this->getModuleIconRelative($GLOBALS['LANG']->moduleLabels['tabs_images'][$moduleKey]);
273 $iconFileAbsolute = $this->getModuleIconAbsolute($GLOBALS['LANG']->moduleLabels['tabs_images'][$moduleKey]);
274 $iconSizes = @getimagesize($iconFileAbsolute);
275 $iconTitle = $GLOBALS['LANG']->moduleLabels['tabs'][$moduleKey];
276
277 if(!empty($iconFileRelative)) {
278 $icon['filename'] = $iconFileRelative;
279 $icon['size'] = $iconSizes[3];
280 $icon['title'] = htmlspecialchars($iconTitle);
281 $icon['html'] = '<img src="' . $iconFileRelative . '" ' . $iconSizes[3] .
282 ' title="' . htmlspecialchars($iconTitle) . '" alt="' . htmlspecialchars($iconTitle) . '" />';
283 }
284
285 return $icon;
286 }
287
288 /**
289 * Returns the filename readable for the script from PATH_typo3.
290 * That means absolute names are just returned while relative names are
291 * prepended with the path pointing back to typo3/ dir
292 *
293 * @param string icon filename
294 * @return string icon filename with absolute path
295 * @see getModuleIconRelative()
296 */
297 protected function getModuleIconAbsolute($iconFilename) {
298
299 if(!t3lib_div::isAbsPath($iconFilename)) {
300 $iconFilename = $this->backPath . $iconFilename;
301 }
302
303 return $iconFilename;
304 }
305
306 /**
307 * Returns relative path to the icon filename for use in img-tags
308 *
309 * @param string icon filename
310 * @return string icon filename with relative path
311 * @see getModuleIconAbsolute()
312 */
313 protected function getModuleIconRelative($iconFilename) {
314 if (t3lib_div::isAbsPath($iconFilename)) {
315 $iconFilename = '../' . substr($iconFilename, strlen(PATH_site));
316 }
317 return $this->backPath.$iconFilename;
318 }
319
320
321
322 /**
323 * Appends a '?' if there is none in the string already
324 *
325 * @param string Link URL
326 * @return string link URl appended with ? if there wasn't one
327 */
328 protected function appendQuestionmarkToLink($link) {
329 if(!strstr($link, '?')) {
330 $link .= '?';
331 }
332
333 return $link;
334 }
335
336 /**
337 * renders the logout button form
338 *
339 * @return string html code snippet displaying the logout button
340 */
341 public function renderLogoutButton() {
342 $buttonLabel = $GLOBALS['BE_USER']->user['ses_backuserid'] ? 'LLL:EXT:lang/locallang_core.php:buttons.exit' : 'LLL:EXT:lang/locallang_core.php:buttons.logout';
343
344 $buttonForm = '
345 <form action="logout.php" target="_top">
346 <input type="submit" value="&nbsp;' . $GLOBALS['LANG']->sL($buttonLabel, 1) . '&nbsp;" />
347 </form>';
348
349 return $buttonForm;
350 }
351
352 /**
353 * turns linking of modules on or off
354 *
355 * @param boolean status for linking modules with a-tags, set to false to turn lining off
356 */
357 public function setLinkModules($linkModules) {
358 if(!is_bool($linkModules)) {
359 throw new InvalidArgumentException('parameter $linkModules must be of type bool', 1193326558);
360 }
361
362 $this->linkModules = $linkModules;
363 }
364
365 }
366
367
368 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['typo3/classes/class.modulemenu.php'])) {
369 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['typo3/classes/class.modulemenu.php']);
370 }
371
372 ?>