added the shortcut menu, part of the cleaner backend project
[Packages/TYPO3.CMS.git] / typo3 / classes / class.shortcutmenu.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 if(TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_AJAX) {
29 require_once('interfaces/interface.backend_toolbaritem.php');
30 require_once(PATH_t3lib.'class.t3lib_loadmodules.php');
31 require_once(PATH_typo3.'sysext/lang/lang.php');
32
33 $GLOBALS['LANG'] = t3lib_div::makeInstance('language');
34 $GLOBALS['LANG']->init($GLOBALS['BE_USER']->uc['lang']);
35 $GLOBALS['LANG']->includeLLFile('EXT:lang/locallang_misc.xml');
36
37 // needed to get the correct icons when reloading the menu after saving it
38 $loadModules = t3lib_div::makeInstance('t3lib_loadModules');
39 $loadModules->load($GLOBALS['TBE_MODULES']);
40 }
41
42
43 /**
44 * class to render the shortcut menu
45 *
46 * $Id$
47 *
48 * @author Ingo Renner <ingo@typo3.org>
49 * @package TYPO3
50 * @subpackage core
51 */
52 class ShortcutMenu implements backend_toolbarItem {
53
54 private $shortcutGroups;
55
56 /**
57 * all available shortcuts
58 *
59 * @var array
60 */
61 private $shortcuts;
62
63 /**
64 * labels of all groups.
65 * If value is 1, the system will try to find a label in the locallang array.
66 *
67 * @var array
68 */
69 private $groupLabels;
70
71 /**
72 * reference back to the backend object
73 *
74 * @var TYPO3backend
75 */
76 private $backendReference;
77
78 /**
79 * constructor
80 *
81 * @return void
82 */
83 public function __construct() {
84 $this->shortcuts = array();
85
86 // by default, 5 groups are set
87 $this->shortcutGroups = array(
88 1 => '1',
89 2 => '1',
90 3 => '1',
91 4 => '1',
92 5 => '1',
93 );
94
95 $this->shortcutGroups = $this->initShortcutGroups();
96 $this->shortcuts = $this->initShortcuts();
97 }
98
99 /**
100 * sets the backend reference
101 *
102 * @param TYPO3backend backend object reference
103 */
104 public function setBackend(&$backendReference) {
105 $this->backendReference = $backendReference;
106 }
107
108 /**
109 * Creates the shortcut menu (default renderer)
110 *
111 * @return string workspace selector as HTML select
112 */
113 public function render() {
114 $this->addJavascriptToBackend();
115
116 $shortcutMenu = array();
117
118 $shortcutMenu[] = '<a href="#" class="toolbar-item"><img'.t3lib_iconWorks::skinImg($this->backPath, 'gfx/toolbar_shortcut.png', 'width="16" height="16"').' title="Shortcuts" alt="" /></a>';
119 $shortcutMenu[] = '<div class="toolbar-item-menu" style="display: none;">';
120 $shortcutMenu[] = $this->renderMenu();
121 $shortcutMenu[] = '</div>';
122
123 return implode("\n", $shortcutMenu);
124 }
125
126 /**
127 * renders the pure contents of the menu
128 *
129 * @return string the menu's content
130 */
131 public function renderMenu() {
132
133 $groupIcon = '<img'.t3lib_iconWorks::skinImg($this->backPath, 'gfx/i/sysf.gif', 'width="18" height="16"').' title="Shortcut Group" alt="" />';
134 $editIcon = '<img'.t3lib_iconWorks::skinImg($this->backPath, 'gfx/edit2.gif', 'width="11" height="12"').' title="Edit Shortcut" alt=""';
135 $deleteIcon = '<img'.t3lib_iconWorks::skinImg($this->backPath, 'gfx/garbage.gif', 'width="11" height="12"').' title="Delete Shortcut" alt="" />';
136
137 $shortcutMenu[] = '<table border="0" cellspacing="0" cellpadding="0" class="shortcut-list">';
138
139 // render shortcuts with no group (group id = 0) first
140 $noGroupShortcuts = $this->getShortcutsByGroup(0);
141 foreach($noGroupShortcuts as $shortcut) {
142 $shortcutMenu[] = '
143 <tr id="shortcut-'.$shortcut['raw']['uid'].'" class="shortcut">
144 <td class="shortcut-icon">'.$shortcut['icon'].'</td>
145 <td class="shortcut-label">
146 <a id="shortcut-label-'.$shortcut['raw']['uid'].'" href="" onclick="'.$shortcut['action'].'">'.$shortcut['label'].'</a>
147 </td>
148 <td class="shortcut-edit">'.$editIcon.' id="shortcut-edit-'.$shortcut['raw']['uid'].'" /></td>
149 <td class="shortcut-delete">'.$deleteIcon.'</td>
150 </tr>';
151 }
152
153 // now render groups and the contained shortcuts
154 $groups = $this->getGroupsFromShortcuts();
155 krsort($groups, SORT_NUMERIC);
156 foreach($groups as $groupId => $groupLabel) {
157 if($groupId != 0 ) {
158 $shortcutGroup = '
159 <tr class="shortcut-group" id="shortcut-group-'.$groupId.'">
160 <td class="shortcut-group-icon">'.$groupIcon.'</td>
161 <td class="shortcut-group-label">'.$groupLabel.'</td>
162 <td colspan="2">&nbsp;</td>
163 </tr>';
164
165 $shortcuts = $this->getShortcutsByGroup($groupId);
166 $i = 0;
167 foreach($shortcuts as $shortcut) {
168 $i++;
169
170 $firstRow = '';
171 if($i == 1) {
172 $firstRow = ' first-row';
173 }
174
175 $shortcutGroup .= '
176 <tr id="shortcut-'.$shortcut['raw']['uid'].'" class="shortcut'.$firstRow.'">
177 <td class="shortcut-icon">'.$shortcut['icon'].'</td>
178 <td class="shortcut-label">
179 <a id="shortcut-label-'.$shortcut['raw']['uid'].'" href="" onclick="'.$shortcut['action'].'">'.$shortcut['label'].'</a>
180 </td>
181 <td class="shortcut-edit">'.$editIcon.' id="shortcut-edit-'.$shortcut['raw']['uid'].'" /></td>
182 <td class="shortcut-delete">'.$deleteIcon.'</td>
183 </tr>';
184 }
185
186 $shortcutMenu[] = $shortcutGroup;
187 }
188 }
189
190 $shortcutMenu[] = '</table>';
191 $compiledShortcutMenu = implode("\n", $shortcutMenu);
192
193 return $compiledShortcutMenu;
194 }
195
196 /**
197 * renders the menu so that it can be returned as response to an AJAX call
198 *
199 * @param array array of parameters from the AJAX interface, currently unused
200 * @param TYPO3AJAX object of type TYPO3AJAX
201 * @return void
202 */
203 public function renderAjax($params = array(), TYPO3AJAX &$ajaxObj = null) {
204 $menuContent = $this->renderMenu();
205
206 $ajaxObj->addContent('shortcutMenu', $menuContent);
207 }
208
209 /**
210 * adds the neccessary javascript ot the backend
211 *
212 * @return void
213 */
214 private function addJavascriptToBackend() {
215 $this->backendReference->addJavascriptFile('typo3/js/shortcutmenu.js');
216 }
217
218 /**
219 * returns additional attributes for the list item in the toolbar
220 *
221 * @return string list item HTML attibutes
222 */
223 public function getAdditionalAttributes() {
224 return ' id="shortcut-menu"';
225 }
226
227 /**
228 * retrieves the shortcuts for the current user
229 *
230 * @return array array of shortcuts
231 */
232 private function initShortcuts() {
233 $shortcuts = array();
234 $globalGroups = $this->getGlobalShortcutGroups();
235
236 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
237 '*',
238 'sys_be_shortcuts',
239 '((userid = '.$GLOBALS['BE_USER']->user['uid'].' AND sc_group>=0) OR sc_group IN ('.implode(',', array_keys($globalGroups)).'))',
240 '',
241 'sc_group,sorting'
242 );
243
244 // Traverse shortcuts
245 while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
246 $shortcut = array('raw' => $row);
247 $moduleParts = explode('|', $row['module_name']);
248 $row['module_name'] = $moduleParts[0];
249 $row['M_module_name'] = $moduleParts[1];
250 $moduleParts = explode('_', $row['M_module_name'] ?
251 $row['M_module_name'] :
252 $row['module_name']
253 );
254 $queryParts = parse_url($row['url']);
255
256 // check for module access
257 if(!$GLOBALS['BE_USER']->isAdmin()) {
258 if(!isset($GLOBALS['LANG']->moduleLabels['tabs_images'][implode('_', $moduleParts).'_tab'])) {
259 // nice hack to check if the user has access to this module
260 // - otherwise the translation label would not have been loaded :-)
261 continue;
262 }
263
264 $pageId = $this->getLinkedPageId($row['url']);
265 if(t3lib_div::testInt($pageId)) {
266 // check for webmount access
267 if(!$GLOBALS['BE_USER']->isInWebMount($page_id)) {
268 continue;
269 }
270
271 // check for record access
272 $pageRow = t3lib_BEfunc::getRecord('pages', $pageId);
273 if(!$GLOBALS['BE_USER']->doesUserHaveAccess($pageRow, $perms = 1)) {
274 continue;
275 }
276 }
277 }
278
279 $shortcutGroup = $row['sc_group'];
280 if($shortcutGroup && strcmp($lastGroup, $shortcutGroup) && ($shortcutGroup != -100)) {
281 $shortcut['groupLabel'] = $this->getShortcutGroupLabel($shortcutGroup);
282 }
283
284 if($row['description']) {
285 $shortcut['label'] = $row['description'];
286 } else {
287 $shortcut['label'] = t3lib_div::fixed_lgd(rawurldecode($queryParts['query']), 150);
288 }
289
290 $shortcut['group'] = $shortcutGroup;
291 $shortcut['icon'] = $this->getShortcutIcon($row['module_name']);
292 $shortcut['iconTitle'] = $this->getShortcutIconTitle($shortcutLabel, $row['module_name'], $row['M_module_name']);
293 $shortcut['action'] = 'jump(unescape(\''.rawurlencode($row['url']).'\'),\''.implode('_',$moduleParts).'\',\''.$moduleParts[0].'\');';
294
295 $lastGroup = $row['sc_group'];
296 $shortcuts[] = $shortcut;
297 }
298
299 return $shortcuts;
300 }
301
302 /**
303 * gets shortcuts for a specific group
304 *
305 * @param integer group Id
306 * @return array array of shortcuts that matched the group
307 */
308 private function getShortcutsByGroup($groupId) {
309 $shortcuts = array();
310
311 foreach($this->shortcuts as $shortcut) {
312 if($shortcut['group'] == $groupId) {
313 $shortcuts[] = $shortcut;
314 }
315 }
316
317 return $shortcuts;
318 }
319
320 /**
321 * gets a shortcut by its uid
322 *
323 * @param integer shortcut id to get the complete shortcut for
324 * @return mixed an array containing the shortcut's data on success or false on failure
325 */
326 private function getShortcutById($shortcutId) {
327 $returnShortcut = false;
328
329 foreach($this->shortcuts as $shortcut) {
330 if($shortcut['raw']['uid'] == (int) $shortcutId) {
331 $returnShortcut = $shortcut;
332 continue;
333 }
334 }
335
336 return $returnShortcut;
337 }
338
339 /**
340 * gets the available shortcut groups from default gropups, user TSConfig,
341 * and global groups
342 *
343 * @param array array of parameters from the AJAX interface, currently unused
344 * @param TYPO3AJAX object of type TYPO3AJAX
345 * @return array
346 */
347 private function initShortcutGroups($params = array(), TYPO3AJAX &$ajaxObj = null) {
348 // groups from TSConfig
349 $userShortcutGroups = $GLOBALS['BE_USER']->getTSConfig('options.shortcutGroups');
350
351 if(is_array($userShortcutGroups['properties']) && count($userShortcutGroups['properties'])) {
352 foreach($userShortcutGroups['properties'] as $groupId => $label) {
353 if(strcmp('', $label) && strcmp('0', $label)) {
354 $this->shortcutGroups[$groupId] = (string) $label;
355 } elseif($GLOBALS['BE_USER']->isAdmin()) {
356 unset($this->shortcutGroups[$groupId]);
357 }
358 }
359 }
360
361 // generate global groups, all global groups have negative IDs.
362 if(count($this->shortcutGroups)) {
363 $groups = $this->shortcutGroups;
364 foreach($groups as $groupId => $groupLabel) {
365 $this->shortcutGroups[($groupId * -1)] = $groupLabel;
366 }
367 }
368
369 // group -100 is kind of superglobal and can't be changed.
370 $this->shortcutGroups[-100] = 1;
371
372 // add labels
373 foreach($this->shortcutGroups as $groupId => $groupLabel) {
374 $label = $groupLabel;
375
376 if($groupLabel == '1') {
377 $label = $GLOBALS['LANG']->getLL('shortcut_group_'.abs($groupId), 1);
378
379 if(empty($label)) {
380 // fallback label
381 $label = $GLOBALS['LANG']->getLL('shortcut_group', 1).' '.abs($groupId);
382 }
383 }
384
385 if($groupId < 0) {
386 // global group
387 $label = $GLOBALS['LANG']->getLL('shortcut_global', 1).': '.
388 (!empty($label) ?
389 $label :
390 abs($groupId)
391 );
392
393 if($groupId == -100) {
394 $label = $GLOBALS['LANG']->getLL('shortcut_global', 1).': '.$GLOBALS['LANG']->getLL('shortcut_all', 1);
395 }
396 }
397
398 $this->shortcutGroups[$groupId] = $label;
399 }
400
401 return $this->shortcutGroups;
402 }
403
404 /**
405 * gets the available shortcut groups
406 *
407 * @param array array of parameters from the AJAX interface, currently unused
408 * @param TYPO3AJAX object of type TYPO3AJAX
409 * @return void
410 */
411 public function getAjaxShortcutGroups($params = array(), TYPO3AJAX &$ajaxObj = null) {
412
413 //TODO remove global (negative id) groups if the user is no admin !!!
414
415 $ajaxObj->addContent('shortcutGroups', $this->shortcutGroups);
416 $ajaxObj->setContentFormat('json');
417 }
418
419 /**
420 * deletes a shortcut through an AJAX call
421 *
422 * @param array array of parameters from the AJAX interface, currently unused
423 * @param TYPO3AJAX object of type TYPO3AJAX
424 * @return void
425 */
426 public function deleteAjaxShortcut($params = array(), TYPO3AJAX &$ajaxObj = null) {
427 $shortcutId = (int) t3lib_div::_POST('shortcutId');
428 $fullShortcut = $this->getShortcutById($shortcutId);
429 $ajaxReturn = 'failed';
430
431 if($fullShortcut['raw']['userid'] == $GLOBALS['BE_USER']->user['uid']) {
432 $GLOBALS['TYPO3_DB']->exec_DELETEquery(
433 'sys_be_shortcuts',
434 'uid = '.$shortcutId
435 );
436
437 if($GLOBALS['TYPO3_DB']->sql_affected_rows() == 1) {
438 $ajaxReturn = 'deleted';
439 }
440 }
441
442 $ajaxObj->addContent('delete', $ajaxReturn);
443 }
444
445 /**
446 * creates a shortcut through an AJAX call
447 *
448 * @param array array of parameters from the AJAX interface, currently unused
449 * @param TYPO3AJAX object of type TYPO3AJAX
450 * @return void
451 */
452 public function createAjaxShortcut($params = array(), TYPO3AJAX &$ajaxObj = null) {
453 $shortcutCreated = 'failed';
454 $shortcutName = 'Shortcut'; // default name
455
456 $url = urldecode(t3lib_div::_POST('url'));
457 $module = t3lib_div::_POST('module');
458 $motherModule = t3lib_div::_POST('motherModName');
459
460 // Lookup the title of this page and use it as default description
461 $pageId = $this->getLinkedPageId($url);
462
463 if(t3lib_div::testInt($pageId)) {
464 $page = t3lib_BEfunc::getRecord('pages', $pageId);
465 if(count($page)) {
466 // set the name to the title of the page
467 $shortcutName = $page['title'];
468 }
469 } else {
470 if (preg_match('/\/$/', $pageId)) {
471 // if $pageId is a string and ends with a slash,
472 // assume it is a fileadmin reference and set
473 // the description to the basename of that path
474 $shortcutName = basename($pageId);
475 }
476 }
477
478 // adding the shortcut
479 if($module && $url) {
480 $fieldValues = array(
481 'userid' => $GLOBALS['BE_USER']->user['uid'],
482 'module_name' => $module.'|'.$motherModule,
483 'url' => $url,
484 'description' => $shortcutName,
485 'sorting' => time(),
486 );
487 $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_be_shortcuts', $fieldValues);
488
489 if($GLOBALS['TYPO3_DB']->sql_affected_rows() == 1) {
490 $shortcutCreated = 'success';
491 }
492 }
493
494 $ajaxObj->addContent('create', $shortcutCreated);
495 }
496
497 /**
498 * gets called when a shortcut is changed, checks whether the user has
499 * permissions to do so and saves the changes if everything is ok
500 *
501 * @param array array of parameters from the AJAX interface, currently unused
502 * @param TYPO3AJAX object of type TYPO3AJAX
503 * @return void
504 */
505 public function setAjaxShortcut($params = array(), TYPO3AJAX &$ajaxObj = null) {
506
507 $shortcutId = (int) t3lib_div::_POST('shortcutId');
508 $shortcutName = strip_tags(t3lib_div::_POST('value'));
509 $shortcutGroupId = (int) t3lib_div::_POST('shortcut-group');
510
511 if($shortcutGroupId > 0 || $GLOBALS['BE_USER']->isAdmin()) {
512 // users can delete only their own shortcuts (except admins)
513 $addUserWhere = (!$GLOBALS['BE_USER']->isAdmin() ?
514 ' AND userid='.intval($GLOBALS['BE_USER']->user['uid'])
515 : ''
516 );
517
518 $fieldValues = array(
519 'description' => $shortcutName,
520 'sc_group' => $shortcutGroupId
521 );
522
523 if($fieldValues['sc_group'] < 0 && !$GLOBALS['BE_USER']->isAdmin()) {
524 $fieldValues['sc_group'] = 0;
525 }
526
527 $GLOBALS['TYPO3_DB']->exec_UPDATEquery(
528 'sys_be_shortcuts',
529 'uid='.$shortcutId.$addUserWhere,
530 $fieldValues
531 );
532
533 $affectedRows = $GLOBALS['TYPO3_DB']->sql_affected_rows();
534 if($affectedRows == 1) {
535 $ajaxObj->addContent('shortcut', $shortcutName);
536 } else {
537 $ajaxObj->addContent('shortcut', 'failed');
538 }
539 }
540
541 $ajaxObj->setContentFormat('plain');
542 }
543
544 /**
545 * gets the label for a shortcut group
546 *
547 * @param integer a shortcut group id
548 * @return string the shortcut group label, can be an empty string if no group was found for the id
549 */
550 private function getShortcutGroupLabel($groupId) {
551 $label = '';
552
553 if($this->shortcutGroups[$groupId]) {
554 $label = $this->shortcutGroups[$groupId];
555 }
556
557 return $label;
558 }
559
560 /**
561 * gets a list of global groups, shortcuts in these groups are available to all users
562 *
563 * @return array array of global groups
564 */
565 private function getGlobalShortcutGroups() {
566 $globalGroups = array();
567
568 foreach($this->shortcutGroups as $groupId => $groupLabel) {
569 if($groupId < 0) {
570 $globalGroups[$groupId] = $groupLabel;
571 }
572 }
573
574 return $globalGroups;
575 }
576
577 /**
578 * runs through the available shortcuts an collects their groups
579 *
580 * @return array array of groups which have shortcuts
581 */
582 private function getGroupsFromShortcuts() {
583 $groups = array();
584
585 foreach($this->shortcuts as $shortcut) {
586 $groups[$shortcut['group']] = $this->shortcutGroups[$shortcut['group']];
587 }
588
589 return array_unique($groups);
590 }
591
592 /**
593 * gets the icon for the shortcut
594 *
595 * @param string backend module name
596 * @return string shortcut icon as img tag
597 */
598 private function getShortcutIcon($moduleName) {
599
600 switch($moduleName) {
601 case 'xMOD_alt_doc.php':
602 $icon = 'gfx/edit2.gif';
603 break;
604 case 'xMOD_file_edit.php':
605 $icon = 'gfx/edit_file.gif';
606 break;
607 case 'xMOD_wizard_rte.php':
608 $icon = 'gfx/edit_rtewiz.gif';
609 break;
610 default:
611 if($GLOBALS['LANG']->moduleLabels['tabs_images'][$moduleName.'_tab']) {
612 $icon = $GLOBALS['LANG']->moduleLabels['tabs_images'][$moduleName.'_tab'];
613
614 // change icon of fileadmin references - otherwise it doesn't differ with Web->List
615 $icon = str_replace('mod/file/list/list.gif', 'mod/file/file.gif', $icon);
616
617 if(t3lib_div::isAbsPath($icon)) {
618 $icon = '../'.substr($icon, strlen(PATH_site));
619 }
620 } else {
621 $icon = 'gfx/dummy_module.gif';
622 }
623 }
624
625 $icon = '<img src="'.$icon.'" alt="shortcut icon" />';
626
627 return $icon;
628 }
629
630 /**
631 * Returns title for the shortcut icon
632 *
633 * @param string shortcut label
634 * @param string backend module name (key)
635 * @param string parent module label
636 * @return string title for the shortcut icon
637 */
638 private function getShortcutIconTitle($shortcutLabel, $moduleName, $parentModuleName = '') {
639 $title = '';
640
641 if(substr($moduleName, 0, 5) == 'xMOD_') {
642 $title = substr($moduleName, 5);
643 } else {
644 $splitModuleName = explode('_', $moduleName);
645 $title = $GLOBALS['LANG']->moduleLabels['tabs'][$splitModuleName[0].'_tab'];
646
647 if(count($splitModuleName) > 1) {
648 $title .= '>'.$GLOBALS['LANG']->moduleLabels['tabs'][$moduleName.'_tab'];
649 }
650 }
651
652 if($parentModuleName) {
653 $title .= ' ('.$parentModuleName.')';
654 }
655
656 $title .= ': '.$shortcutLabel;
657
658 return $title;
659 }
660
661 /**
662 * Return the ID of the page in the URL if found.
663 *
664 * @param string The URL of the current shortcut link
665 * @return string If a page ID was found, it is returned. Otherwise: 0
666 */
667 private function getLinkedPageId($url) {
668 return preg_replace('/.*[\?&]id=([^&]+).*/', '$1', $url);
669 }
670
671 }
672
673
674 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['typo3/classes/class.shortcutmenu.php']) {
675 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['typo3/classes/class.shortcutmenu.php']);
676 }
677
678 ?>