fixed bug #7270: Shortcut menu does not load user's shortcuts if user is not admin...
[Packages/TYPO3.CMS.git] / typo3 / classes / class.modulemenu.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2007 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 require_once(PATH_typo3.'sysext/lang/lang.php');
31
32 $GLOBALS['LANG'] = t3lib_div::makeInstance('language');
33 $GLOBALS['LANG']->init($GLOBALS['BE_USER']->uc['lang']);
34 $GLOBALS['LANG']->includeLLFile('EXT:lang/locallang_misc.xml');
35 }
36
37 /**
38 * class to render the TYPO3 backend menu for the modules
39 *
40 * @author Ingo Renner <ingo@typo3.org>
41 * @package TYPO3
42 * @subpackage core
43 */
44 class ModuleMenu {
45
46 /**
47 * module loading object
48 *
49 * @var t3lib_loadModules
50 */
51 protected $moduleLoader;
52
53 private $backPath;
54 private $linkModules;
55 private $loadedModules;
56 private $fsMod; //TODO find a more descriptive name, left over from alt_menu_functions
57
58 /**
59 * constructor, initializes several variables
60 *
61 * @return void
62 */
63 public function __construct() {
64
65 $this->backPath = '';
66 $this->fsMod = array();
67 $this->linkModules = true;
68
69 // Loads the backend modules available for the logged in user.
70 $this->moduleLoader = t3lib_div::makeInstance('t3lib_loadModules');
71 $this->moduleLoader->observeWorkspaces = true;
72 $this->moduleLoader->load($GLOBALS['TBE_MODULES']);
73 $this->loadedModules = $this->moduleLoader->modules;
74
75 }
76
77 /**
78 * sets the path back to /typo3/
79 *
80 * @param string path back to /typo3/
81 * @return void
82 */
83 public function setBackPath($backPath) {
84 if(!is_string($backPath)) {
85 throw new InvalidArgumentException('parameter $backPath must be of type string', 1193315266);
86 }
87
88 $this->backPath = $backPath;
89 }
90
91 /**
92 * loads the collapse states for the main modules from user's configuration (uc)
93 *
94 * @return array collapse states
95 */
96 private function getCollapsedStates() {
97
98 $collapsedStates = array();
99 if($GLOBALS['BE_USER']->uc['moduleData']['moduleMenu']) {
100 $collapsedStates = $GLOBALS['BE_USER']->uc['moduleData']['moduleMenu'];
101 }
102
103 return $collapsedStates;
104 }
105
106 /**
107 * returns the loaded modules
108 *
109 * @return array array of loaded modules
110 */
111 public function getLoadedModules() {
112 return $this->loadedModules;
113 }
114
115 /**
116 * saves the menu's toggle state in the backend user's uc
117 *
118 * @param array array of parameters from the AJAX interface, currently unused
119 * @param TYPO3AJAX object of type TYPO3AJAX
120 * @return void
121 */
122 public function saveMenuState($params, &$ajaxObj) {
123 $menuItem = t3lib_div::_POST('menuid');
124 $state = t3lib_div::_POST('state') === 'true' ? 1 : 0;
125
126 $GLOBALS['BE_USER']->uc['moduleData']['menuState'][$menuItem] = $state;
127 $GLOBALS['BE_USER']->writeUC();
128 }
129
130 /**
131 * renders the backend menu as unordered list
132 *
133 * @param boolean optional parameter used to switch wrapping the menu in ul tags off for AJAX calls
134 * @return string menu html code to use in the backend
135 */
136 public function render($wrapInUl = true) {
137 $menu = '';
138 $onBlur = $GLOBALS['CLIENT']['FORMSTYLE'] ? 'this.blur();' : '';
139
140 $rawModuleData = $this->getRawModuleData();
141
142 foreach($rawModuleData as $moduleKey => $moduleData) {
143 $menuState = $GLOBALS['BE_USER']->uc['moduleData']['menuState'][$moduleKey];
144 $moduleLabel = $moduleData['title'];
145
146 if($moduleData['link'] && $this->linkModules) {
147 $moduleLabel = '<a href="#" onclick="top.goToModule(\''.$moduleData['name'].'\');'.$onBlur.'return false;">'.$moduleLabel.'</a>';
148 }
149
150 //TODO make icon a background image using css
151 $menu .= '<li id="'.$moduleKey.'" class="menuSection" title="'.$moduleData['description'].'"><div>'.$moduleData['icon']['html'].' '.$moduleLabel.'</div>';
152
153 // traverse submodules
154 if(is_array($moduleData['subitems'])) {
155 $menu .= $this->renderSubModules($moduleData['subitems'], $menuState);
156 }
157
158 $menu .= '</li>'."\n";
159 }
160
161 return $wrapInUl ? '<ul id="typo3-menu">'."\n".$menu.'</ul>'."\n" : $menu;
162 }
163
164 /**
165 * renders the backend menu as unordered list as an AJAX response without
166 * the wrapping ul tags
167 *
168 * @param array array of parameters from the AJAX interface, currently unused
169 * @param TYPO3AJAX object of type TYPO3AJAX
170 * @return void
171 */
172 public function renderAjax($params = array(), TYPO3AJAX &$ajaxObj = null) {
173 $menu = $this->render(false);
174 $menuSwitch = $this->getGotoModuleJavascript();
175
176 // JS rocks: we can just overwrite a function with a new definition.
177 // and yes, we actually do that =)
178 $menuSwitchUpdate = '
179 <script type="text/javascript">
180 top.goToModule = '.$menuSwitch.';
181 </script>';
182
183 $ajaxObj->addContent('typo3-menu', $menu.$menuSwitchUpdate);
184 }
185
186 /**
187 * renders submodules
188 *
189 * @param array array of (sub)module data
190 * @param boolean collapse state of menu item, defaults to false
191 * @return string (sub)module html code
192 */
193 public function renderSubModules($modules, $menuState=false) {
194 $moduleMenu = '';
195 $onBlur = $GLOBALS['CLIENT']['FORMSTYLE'] ? 'this.blur();' : '';
196
197 foreach($modules as $moduleKey => $moduleData) {
198 // Setting additional JavaScript
199 $additionalJavascript = '';
200 if($moduleData['parentNavigationFrameScript']) {
201 $parentModuleName = substr($moduleData['name'], 0, strpos($moduleData['name'], '_'));
202 $additionalJavascript = "+'&id='+top.rawurlencode(top.fsMod.recentIds['".$parentModuleName."'])";
203 }
204
205 if($moduleData['link'] && $this->linkModules) {
206
207 $onClickString = htmlspecialchars('top.goToModule(\''.$moduleData['name'].'\');'.$onBlur.'return false;');
208 $submoduleLink = '<a href="#" onclick="'.$onClickString.'" title="'.$moduleData['description'].'">'
209 //TODO make icon a background image using css
210 .$moduleData['icon']['html'].' '
211 .'<span>'.htmlspecialchars($moduleData['title']).'</span>'
212 .'</a>';
213 }
214
215 $moduleMenu .= '<li id="'.$moduleData['cssId'].'">'.$submoduleLink.'</li>'."\n";
216 }
217
218 return '<ul'.($menuState ? ' style="display:none;"' : '').'>'."\n".$moduleMenu.'</ul>'."\n";
219 }
220
221 /**
222 * gets the raw module data
223 *
224 * @return array multi dimension array with module data
225 */
226 public function getRawModuleData() {
227 $modules = array();
228
229 // Remove the 'doc' module?
230 if($GLOBALS['BE_USER']->getTSConfigVal('options.disableDocModuleInAB')) {
231 unset($this->loadedModules['doc']);
232 }
233
234 foreach($this->loadedModules as $moduleName => $moduleData) {
235 $moduleNavigationFramePrefix = $this->getNavigationFramePrefix($moduleData);
236
237 if($moduleNavigationFramePrefix) {
238 $this->fsMod[$moduleName] = 'fsMod.recentIds["'.$moduleName.'"]="";';
239 }
240
241 $moduleLink = '';
242 if(!is_array($moduleData['sub'])) {
243 $moduleLink = $moduleData['script'];
244 }
245 $moduleLink = t3lib_div::resolveBackPath($moduleLink);
246
247 $moduleKey = $moduleName.'_tab';
248 $moduleCssId = 'ID_'.t3lib_div::md5int($moduleName);
249 $moduleIcon = $this->getModuleIcon($moduleKey);
250
251 if($moduleLink && $moduleNavigationFramePrefix) {
252 $moduleLink = $moduleNavigationFramePrefix.rawurlencode($moduleLink);
253 }
254
255 $modules[$moduleKey] = array(
256 'name' => $moduleName,
257 'title' => $GLOBALS['LANG']->moduleLabels['tabs'][$moduleKey],
258 'onclick' => 'top.goToModule(\''.$moduleName.'\');',
259 'cssId' => $moduleCssId,
260 'icon' => $moduleIcon,
261 'link' => $moduleLink,
262 'prefix' => $moduleNavigationFramePrefix,
263 'description' => $GLOBALS['LANG']->moduleLabels['labels'][$moduleKey.'label']
264 );
265
266 if(is_array($moduleData['sub'])) {
267
268 foreach($moduleData['sub'] as $submoduleName => $submoduleData) {
269 $submoduleLink = t3lib_div::resolveBackPath($submoduleData['script']);
270 $submoduleNavigationFramePrefix = $this->getNavigationFramePrefix($moduleData, $submoduleData);
271
272 $submoduleKey = $moduleName.'_'.$submoduleName.'_tab';
273 $submoduleCssId = 'ID_'.t3lib_div::md5int($moduleName.'_'.$submoduleName);
274 $submoduleIcon = $this->getModuleIcon($submoduleKey);
275 $submoduleDescription = $GLOBALS['LANG']->moduleLabels['labels'][$submoduleKey.'label'];
276
277 $originalLink = $submoduleLink;
278 if($submoduleLink && $submoduleNavigationFramePrefix) {
279 $submoduleLink = $submoduleNavigationFramePrefix.rawurlencode($submoduleLink);
280 }
281
282 $modules[$moduleKey]['subitems'][$submoduleKey] = array(
283 'name' => $moduleName.'_'.$submoduleName,
284 'title' => $GLOBALS['LANG']->moduleLabels['tabs'][$submoduleKey],
285 'onclick' => 'top.goToModule(\''.$moduleName.'_'.$submoduleName.'\');',
286 'cssId' => $submoduleCssId,
287 'icon' => $submoduleIcon,
288 'link' => $submoduleLink,
289 'originalLink' => $originalLink,
290 'prefix' => $submoduleNavigationFramePrefix,
291 'description' => $submoduleDescription
292 );
293
294 if($moduleData['navFrameScript']) {
295 $modules[$moduleKey]['subitems'][$submoduleKey]['parentNavigationFrameScript'] = $moduleData['navFrameScript'];
296 }
297 }
298 }
299 }
300
301 return $modules;
302 }
303
304 /**
305 * gets the module icon and its size
306 *
307 * @param string module key
308 * @return array icon data array with 'filename', 'size', and 'html'
309 */
310 private function getModuleIcon($moduleKey) {
311 $icon = array();
312 $iconFileRelative = $this->getModuleIconRelative($GLOBALS['LANG']->moduleLabels['tabs_images'][$moduleKey]);
313 $iconFileAbsolute = $this->getModuleIconAbsolute($GLOBALS['LANG']->moduleLabels['tabs_images'][$moduleKey]);
314 $iconSizes = @getimagesize($iconFileAbsolute);
315 $iconTitle = $GLOBALS['LANG']->moduleLabels['tabs'][$moduleKey];
316
317 $icon['filename'] = $iconFileRelative;
318 $icon['size'] = $iconSizes[3];
319 $icon['title'] = htmlspecialchars($iconTitle);
320 $icon['html'] = '<img src="'.$iconFileRelative.'" '.$iconSizes[3].' title="'.htmlspecialchars($iconTitle).'" alt="'.htmlspecialchars($iconTitle).'" />';
321
322 return $icon;
323 }
324
325 /**
326 * Returns the filename readable for the script from PATH_typo3.
327 * That means absolute names are just returned while relative names are
328 * prepended with the path pointing back to typo3/ dir
329 *
330 * @param string icon filename
331 * @return string icon filename with absolute path
332 * @see getModuleIconRelative()
333 */
334 private function getModuleIconAbsolute($iconFilename) {
335
336 if(!t3lib_div::isAbsPath($iconFilename)) {
337 $iconFilename = $this->backPath.$iconFilename;
338 }
339
340 return $iconFilename;
341 }
342
343 /**
344 * Returns relative path to the icon filename for use in img-tags
345 *
346 * @param string icon filename
347 * @return string icon filename with relative path
348 * @see getModuleIconAbsolute()
349 */
350 private function getModuleIconRelative($iconFilename) {
351 if(t3lib_div::isAbsPath($iconFilename)) {
352 $iconFilename = '../'.substr($iconFilename, strlen(PATH_site));
353 }
354
355 return $this->backPath.$iconFilename;
356 }
357
358 /**
359 * Returns a prefix used to call the navigation frame with parameters which then will call the scripts defined in the modules info array.
360 *
361 * @param array module data array
362 * @param array submodule data array
363 * @return string result URL string
364 */
365 private function getNavigationFramePrefix($moduleData, $subModuleData = array()) {
366 $prefix = '';
367
368 $navigationFrameScript = $moduleData['navFrameScript'];
369 if($subModuleData['navFrameScript']) {
370 $navigationFrameScript = $subModuleData['navFrameScript'];
371 }
372
373 $navigationFrameParameter = $moduleData['navFrameScriptParam'];
374 if($subModuleData['navFrameScriptParam']) {
375 $navigationFrameParameter = $subModuleData['navFrameScriptParam'];
376 }
377
378 if($navigationFrameScript) {
379 $navigationFrameScript = t3lib_div::resolveBackPath($navigationFrameScript);
380 $navigationFrameScript = $this->appendQuestionmarkToLink($navigationFrameScript);
381
382 if($GLOBALS['BE_USER']->uc['condensedMode']) {
383 $prefix = $navigationFrameScript.$navigationFrameParameter.'&currentSubScript=';
384 } else {
385 $prefix = 'alt_mod_frameset.php?'
386 .'fW="+top.TS.navFrameWidth+"'
387 .'&nav="+top.TS.PATH_typo3+"'
388 .rawurlencode($navigationFrameScript.$navigationFrameParameter)
389 .'&script=';
390 }
391 }
392
393 return $prefix;
394 }
395
396 /**
397 * generates javascript code to switch between modules
398 *
399 * @return string javascript code snippet to switch modules
400 */
401 public function getGotoModuleJavascript() {
402
403 $moduleJavascriptCommands = array();
404 $rawModuleData = $this->getRawModuleData();
405
406 foreach($rawModuleData as $mainModuleKey => $mainModuleData) {
407 if($mainModuleData['subitems']) {
408 foreach($mainModuleData['subitems'] as $subModuleKey => $subModuleData) {
409
410 $parentModuleName = substr($subModuleData['name'], 0, strpos($subModuleData['name'], '_'));
411 $javascriptCommand = '';
412
413 // Setting additional JavaScript if frameset script:
414 $additionalJavascript = '';
415 if($subModuleData['parentNavigationFrameScript']) {
416 $additionalJavascript = "+'&id='+top.rawurlencode(top.fsMod.recentIds['".$parentModuleName."'])";
417 }
418
419 if($subModuleData['link'] && $this->linkModules) {
420 // For condensed mode, send &cMR parameter to frameset script.
421 if($additionalJavascript && $GLOBALS['BE_USER']->uc['condensedMode']) {
422 $additionalJavascript .= "+(cMR?'&cMR=1':'')";
423 }
424
425 $javascriptCommand = '
426 $(\'content\').src = top.getModuleUrl(top.TS.PATH_typo3+"'.$this->appendQuestionmarkToLink($subModuleData['link']).'"'.$additionalJavascript.'+additionalGetVariables);
427 top.fsMod.currentMainLoaded="'.$parentModuleName.'";
428 ';
429
430 if($subModuleData['navFrameScript']) {
431 $javascriptCommand .= '
432 top.currentSubScript="'.$subModuleData['originalLink'].'";';
433 }
434
435 if(!$GLOBALS['BE_USER']->uc['condensedMode'] && $subModuleData['parentNavigationFrameScript']) {
436 $additionalJavascript = "+'&id='+top.rawurlencode(top.fsMod.recentIds['".$parentModuleName."'])";
437
438 $submoduleNavigationFrameScript = $subModuleData['navigationFrameScript'] ? $subModuleData['navigationFrameScript'] : $subModuleData['parentNavigationFrameScript'];
439 $submoduleNavigationFrameScript = t3lib_div::resolveBackPath($submoduleNavigationFrameScript);
440
441 // add GET parameters for sub module to the navigation script
442 $submoduleNavigationFrameScript = $this->appendQuestionmarkToLink($submoduleNavigationFrameScript).$subModuleData['navigationFrameScript'];
443
444 $javascriptCommand = '
445 if (top.content.list_frame && top.fsMod.currentMainLoaded=="'.$parentModuleName.'") {
446 top.currentSubScript="'.$subModuleData['originalLink'].'";
447 top.content.list_frame.location=top.getModuleUrl(top.TS.PATH_typo3+"'.$this->appendQuestionmarkToLink($subModuleData['originalLink']).'"'.$additionalJavascript.'+additionalGetVariables);
448 if(top.currentSubNavScript!="'.$submoduleNavigationFrameScript.'") {
449 top.currentSubNavScript="'.$submoduleNavigationFrameScript.'";
450 top.content.nav_frame.location=top.getModuleUrl(top.TS.PATH_typo3+"'.$submoduleNavigationFrameScript.'");
451 }
452 } else {
453 top.content.location=top.TS.PATH_typo3+(
454 top.nextLoadModuleUrl?
455 "'.($subModuleData['prefix'] ? $this->appendQuestionmarkToLink($subModuleData['link']).'&exScript=' : '').'listframe_loader.php":
456 "'.$this->appendQuestionmarkToLink($subModuleData['link']).'"'.$additionalJavascript.'+additionalGetVariables
457 );
458 top.fsMod.currentMainLoaded="'.$parentModuleName.'";
459 top.currentSubScript="'.$subModuleData['originalLink'].'";
460 }
461 ';
462 }
463
464 $javascriptCommand .= '
465 top.highlightModuleMenuItem("'.$subModuleData['cssId'].'");
466 ';
467 $moduleJavascriptCommands[] = " case '".$subModuleData['name']."': \n ".$javascriptCommand." \n break;";
468 }
469 }
470 } elseif(!$mainModuleData['subitems'] && !empty($mainModuleData['link'])) {
471 // main module has no sub modules but instead is linked itself (doc module f.e.)
472 $javascriptCommand = '
473 top.content.location=top.getModuleUrl(top.TS.PATH_typo3+"'.$this->appendQuestionmarkToLink($mainModuleData['link']).'"+additionalGetVariables);
474 top.highlightModuleMenuItem("'.$mainModuleData['cssId'].'", 1);
475 ';
476 $moduleJavascriptCommands[] = " case '".$mainModuleData['name']."': \n ".$javascriptCommand." \n break;";
477 }
478 }
479
480 $javascriptCode = 'function(modName, cMR_flag, addGetVars) {
481 var additionalGetVariables = "";
482 if (addGetVars) additionalGetVariables = addGetVars;
483
484 var cMR = 0;
485 if (cMR_flag) cMR = 1;
486
487 currentModuleLoaded = modName;
488
489 switch(modName) {'
490 ."\n".implode("\n", $moduleJavascriptCommands)."\n".'
491 }
492 }';
493
494 return $javascriptCode;
495 }
496
497 /**
498 * Appends a '?' if there is none in the string already
499 *
500 * @param string Link URL
501 * @return string link URl appended with ? if there wasn't one
502 */
503 private function appendQuestionmarkToLink($link) {
504 if(!strstr($link, '?')) {
505 $link .= '?';
506 }
507
508 return $link;
509 }
510
511 /**
512 * renders the logout button form
513 *
514 * @return string html code snippet displaying the logout button
515 */
516 public function renderLogoutButton() {
517 $buttonLabel = $GLOBALS['BE_USER']->user['ses_backuserid'] ? 'LLL:EXT:lang/locallang_core.php:buttons.exit' : 'LLL:EXT:lang/locallang_core.php:buttons.logout';
518
519 $buttonForm = '
520 <form action="logout.php" target="_top">
521 <input type="submit" value="'.$GLOBALS['LANG']->sL($buttonLabel, 1).'" />
522 </form>';
523
524 return $buttonForm;
525 }
526
527 /**
528 * turns linking of modules on or off
529 *
530 * @param boolean status for linking modules with a-tags, set to false to turn lining off
531 */
532 public function setLinkModules($linkModules) {
533 if(!is_bool($linkModules)) {
534 throw new InvalidArgumentException('parameter $linkModules must be of type bool', 1193326558);
535 }
536
537 $this->linkModules = $linkModules;
538 }
539
540 /**
541 * gets the frameset (leftover) helper
542 *
543 * @return array array of javascript snippets
544 */
545 public function getFsMod() {
546 return $this->fsMod;
547 }
548 }
549
550
551 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['typo3/classes/class.modulemenu.php']) {
552 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['typo3/classes/class.modulemenu.php']);
553 }
554
555 ?>