93311d6e6f812b067f3e4efdf8e9e8138f338dd7
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Toolbar / ShortcutToolbarItem.php
1 <?php
2 namespace TYPO3\CMS\Backend\Toolbar;
3
4 /**
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Backend\Utility\IconUtility;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Core\Utility\MathUtility;
21
22 /**
23 * Class to render the shortcut menu
24 *
25 * @author Ingo Renner <ingo@typo3.org>
26 */
27 class ShortcutToolbarItem implements ToolbarItemInterface {
28
29 const SUPERGLOBAL_GROUP = -100;
30
31 /**
32 * @var string
33 */
34 public $perms_clause;
35
36 /**
37 * @var string
38 */
39 public $backPath;
40
41 /**
42 * @var array
43 */
44 public $fieldArray;
45
46 /**
47 * All available shortcuts
48 *
49 * @var array
50 */
51 protected $shortcuts;
52
53 /**
54 * @var array
55 */
56 protected $shortcutGroups;
57
58 /**
59 * Labels of all groups.
60 * If value is 1, the system will try to find a label in the locallang array.
61 *
62 * @var array
63 */
64 protected $groupLabels;
65
66 /**
67 * Reference back to the backend object
68 *
69 * @var \TYPO3\CMS\Backend\Controller\BackendController
70 */
71 protected $backendReference;
72
73 /**
74 * Constructor
75 *
76 * @param \TYPO3\CMS\Backend\Controller\BackendController $backendReference TYPO3 backend object reference
77 */
78 public function __construct(\TYPO3\CMS\Backend\Controller\BackendController &$backendReference = NULL) {
79 if (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_AJAX) {
80 $GLOBALS['LANG']->includeLLFile('EXT:lang/locallang_misc.xlf');
81 // Needed to get the correct icons when reloading the menu after saving it
82 $loadModules = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Module\ModuleLoader::class);
83 $loadModules->load($GLOBALS['TBE_MODULES']);
84 }
85 $this->backendReference = $backendReference;
86 $this->shortcuts = array();
87 // By default, 5 groups are set
88 $this->shortcutGroups = array(
89 1 => '1',
90 2 => '1',
91 3 => '1',
92 4 => '1',
93 5 => '1'
94 );
95 $this->shortcutGroups = $this->initShortcutGroups();
96 $this->shortcuts = $this->initShortcuts();
97 }
98
99 /**
100 * Checks whether the user has access to this toolbar item
101 *
102 * @return bool TRUE if user has access, FALSE if not
103 */
104 public function checkAccess() {
105 return (bool)$GLOBALS['BE_USER']->getTSConfigVal('options.enableBookmarks');
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 $title = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:toolbarItems.bookmarks', TRUE);
115 $this->addJavascriptToBackend();
116 $shortcutMenu = array();
117 $shortcutMenu[] = '<a href="#" class="dropdown-toggle" data-toggle="dropdown">' . IconUtility::getSpriteIcon('apps-toolbar-menu-shortcut', array('title' => $title)) . '</a>';
118 $shortcutMenu[] = '<div class="dropdown-menu" role="menu">';
119 $shortcutMenu[] = $this->renderMenu();
120 $shortcutMenu[] = '</div>';
121 return implode(LF, $shortcutMenu);
122 }
123
124 /**
125 * Renders the pure contents of the menu
126 *
127 * @return string The menu's content
128 */
129 public function renderMenu() {
130 $shortcutGroup = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:toolbarItems.bookmarksGroup', TRUE);
131 $shortcutEdit = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:toolbarItems.bookmarksEdit', TRUE);
132 $shortcutDelete = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:toolbarItems.bookmarksDelete', TRUE);
133 $groupIcon = '<img' . IconUtility::skinImg($this->backPath, 'gfx/i/sysf.gif', 'width="18" height="16"') . ' title="' . $shortcutGroup . '" alt="' . $shortcutGroup . '" />';
134 $editIcon = '<img' . IconUtility::skinImg($this->backPath, 'gfx/edit2.gif', 'width="11" height="12"') . ' title="' . $shortcutEdit . '" alt="' . $shortcutEdit . '"';
135 $deleteIcon = '<img' . IconUtility::skinImg($this->backPath, 'gfx/garbage.gif', 'width="11" height="12"') . ' title="' . $shortcutDelete . '" alt="' . $shortcutDelete . '" />';
136 $shortcutMenu[] = '<table border="0" cellspacing="0" cellpadding="0" class="shortcut-list">';
137 // Render shortcuts with no group (group id = 0) first
138 $noGroupShortcuts = $this->getShortcutsByGroup(0);
139 foreach ($noGroupShortcuts as $shortcut) {
140 $shortcutMenu[] = '
141 <tr id="shortcut-' . $shortcut['raw']['uid'] . '" class="shortcut">
142 <td class="shortcut-icon">' . $shortcut['icon'] . '</td>
143 <td class="shortcut-label">
144 <a id="shortcut-label-' . $shortcut['raw']['uid'] . '" href="#" onclick="' . $shortcut['action'] . '; return false;">' . htmlspecialchars($shortcut['label']) . '</a>
145 </td>
146 <td class="shortcut-edit">' . $editIcon . ' id="shortcut-edit-' . $shortcut['raw']['uid'] . '" /></td>
147 <td class="shortcut-delete">' . $deleteIcon . '</td>
148 </tr>';
149 }
150 // Now render groups and the contained shortcuts
151 $groups = $this->getGroupsFromShortcuts();
152 krsort($groups, SORT_NUMERIC);
153 foreach ($groups as $groupId => $groupLabel) {
154 if ($groupId != 0) {
155 $shortcutGroup = '
156 <tr class="shortcut-group" id="shortcut-group-' . $groupId . '">
157 <td class="shortcut-group-icon">' . $groupIcon . '</td>
158 <td class="shortcut-group-label">' . $groupLabel . '</td>
159 <td colspan="2">&nbsp;</td>
160 </tr>';
161 $shortcuts = $this->getShortcutsByGroup($groupId);
162 $i = 0;
163 foreach ($shortcuts as $shortcut) {
164 $i++;
165 $firstRow = '';
166 if ($i == 1) {
167 $firstRow = ' first-row';
168 }
169 $shortcutGroup .= '
170 <tr id="shortcut-' . $shortcut['raw']['uid'] . '" class="shortcut' . $firstRow . '">
171 <td class="shortcut-icon">' . $shortcut['icon'] . '</td>
172 <td class="shortcut-label">
173 <a id="shortcut-label-' . $shortcut['raw']['uid'] . '" href="#" onclick="' . $shortcut['action'] . '; return false;">' . htmlspecialchars($shortcut['label']) . '</a>
174 </td>
175 <td class="shortcut-edit">' . $editIcon . ' id="shortcut-edit-' . $shortcut['raw']['uid'] . '" /></td>
176 <td class="shortcut-delete">' . $deleteIcon . '</td>
177 </tr>';
178 }
179 $shortcutMenu[] = $shortcutGroup;
180 }
181 }
182 if (count($shortcutMenu) == 1) {
183 // No shortcuts added yet, show a small help message how to add shortcuts
184 $title = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:toolbarItems.bookmarks', TRUE);
185 $icon = IconUtility::getSpriteIcon('actions-system-shortcut-new', array(
186 'title' => $title
187 ));
188 $label = str_replace('%icon%', $icon, $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_misc.xlf:bookmarkDescription'));
189 $shortcutMenu[] = '<tr><td style="padding:1px 2px; color: #838383;">' . $label . '</td></tr>';
190 }
191 $shortcutMenu[] = '</table>';
192 $compiledShortcutMenu = implode(LF, $shortcutMenu);
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 $params Array of parameters from the AJAX interface, currently unused
200 * @param \TYPO3\CMS\Core\Http\AjaxRequestHandler $ajaxObj Object of type AjaxRequestHandler
201 * @return void
202 */
203 public function renderAjax($params = array(), \TYPO3\CMS\Core\Http\AjaxRequestHandler &$ajaxObj = NULL) {
204 $menuContent = $this->renderMenu();
205 $ajaxObj->addContent('shortcutMenu', $menuContent);
206 }
207
208 /**
209 * Adds the necessary JavaScript to the backend
210 *
211 * @return void
212 */
213 protected function addJavascriptToBackend() {
214 $this->backendReference->addJavascriptFile('sysext/backend/Resources/Public/JavaScript/shortcutmenu.js');
215 }
216
217 /**
218 * Returns additional attributes for the list item in the toolbar
219 *
220 * This should not contain the "class" or "id" attribute.
221 * Use the methods for setting these attributes
222 *
223 * @return string List item HTML attibutes
224 */
225 public function getAdditionalAttributes() {
226 return '';
227 }
228
229 /**
230 * Return attribute id name
231 *
232 * @return string The name of the ID attribute
233 */
234 public function getIdAttribute() {
235 return 'shortcut-menu';
236 }
237
238 /**
239 * Returns extra classes
240 *
241 * @return array
242 */
243 public function getExtraClasses() {
244 return array();
245 }
246
247 /**
248 * Get dropdown
249 *
250 * @return bool
251 */
252 public function getDropdown() {
253 return TRUE;
254 }
255
256 /**
257 * Retrieves the shortcuts for the current user
258 *
259 * @return array Array of shortcuts
260 */
261 protected function initShortcuts() {
262 $globalGroupIdList = implode(',', array_keys($this->getGlobalShortcutGroups()));
263 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
264 '*',
265 'sys_be_shortcuts',
266 '(userid = ' . (int)$GLOBALS['BE_USER']->user['uid'] . ' AND sc_group>=0) OR sc_group IN (' . $globalGroupIdList . ')',
267 '',
268 'sc_group,sorting'
269 );
270 // Traverse shortcuts
271 $lastGroup = 0;
272 $shortcuts = array();
273 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
274 $shortcut = array('raw' => $row);
275
276 list($row['module_name'], $row['M_module_name']) = explode('|', $row['module_name']);
277
278 $queryParts = parse_url($row['url']);
279 $queryParameters = GeneralUtility::explodeUrl2Array($queryParts['query'], 1);
280 if ($row['module_name'] === 'xMOD_alt_doc.php' && is_array($queryParameters['edit'])) {
281 $shortcut['table'] = key($queryParameters['edit']);
282 $shortcut['recordid'] = key($queryParameters['edit'][$shortcut['table']]);
283 if ($queryParameters['edit'][$shortcut['table']][$shortcut['recordid']] === 'edit') {
284 $shortcut['type'] = 'edit';
285 } elseif ($queryParameters['edit'][$shortcut['table']][$shortcut['recordid']] === 'new') {
286 $shortcut['type'] = 'new';
287 }
288 if (substr($shortcut['recordid'], -1) === ',') {
289 $shortcut['recordid'] = substr($shortcut['recordid'], 0, -1);
290 }
291 } else {
292 $shortcut['type'] = 'other';
293 }
294 // Check for module access
295 $moduleName = $row['M_module_name'] ?: $row['module_name'];
296 $pageId = $this->getLinkedPageId($row['url']);
297 if (!$GLOBALS['BE_USER']->isAdmin()) {
298 if (!isset($GLOBALS['LANG']->moduleLabels['tabs_images'][$moduleName . '_tab'])) {
299 // Nice hack to check if the user has access to this module
300 // - otherwise the translation label would not have been loaded :-)
301 continue;
302 }
303 if (MathUtility::canBeInterpretedAsInteger($pageId)) {
304 // Check for webmount access
305 if (!$GLOBALS['BE_USER']->isInWebMount($pageId)) {
306 continue;
307 }
308 // Check for record access
309 $pageRow = BackendUtility::getRecord('pages', $pageId);
310 if (!$GLOBALS['BE_USER']->doesUserHaveAccess($pageRow, ($perms = 1))) {
311 continue;
312 }
313 }
314 }
315 $moduleParts = explode('_', $moduleName);
316 $shortcutGroup = (int)$row['sc_group'];
317 if ($shortcutGroup && $lastGroup !== $shortcutGroup && $shortcutGroup !== self::SUPERGLOBAL_GROUP) {
318 $shortcut['groupLabel'] = $this->getShortcutGroupLabel($shortcutGroup);
319 }
320 $lastGroup = $shortcutGroup;
321
322 if ($row['description']) {
323 $shortcut['label'] = $row['description'];
324 } else {
325 $shortcut['label'] = GeneralUtility::fixed_lgd_cs(rawurldecode($queryParts['query']), 150);
326 }
327 $shortcut['group'] = $shortcutGroup;
328 $shortcut['icon'] = $this->getShortcutIcon($row, $shortcut);
329 $shortcut['iconTitle'] = $this->getShortcutIconTitle($shortcut['label'], $row['module_name'], $row['M_module_name']);
330 $shortcut['action'] = 'jump(unescape(\'' . rawurlencode($this->getTokenUrl($row['url'])) . '\'),\'' . $moduleName . '\',\'' . $moduleParts[0] . '\', ' . (int)$pageId . ');';
331
332 $shortcuts[] = $shortcut;
333 }
334 return $shortcuts;
335 }
336
337 /**
338 * Adds the correct token, if the url is a mod.php script
339 *
340 * @param string $url
341 * @return string
342 */
343 protected function getTokenUrl($url) {
344 $parsedUrl = parse_url($url);
345 parse_str($parsedUrl['query'], $parameters);
346
347 // parse the returnUrl and replace the module token of it
348 if (isset($parameters['returnUrl'])) {
349 $parsedReturnUrl = parse_url($parameters['returnUrl']);
350 parse_str($parsedReturnUrl['query'], $returnUrlParameters);
351 if (strpos($parsedReturnUrl['path'], 'mod.php') !== FALSE && isset($returnUrlParameters['M'])) {
352 $module = $returnUrlParameters['M'];
353 $returnUrl = BackendUtility::getModuleUrl($module, $returnUrlParameters);
354 $parameters['returnUrl'] = $returnUrl;
355 $url = $parsedUrl['path'] . '?' . http_build_query($parameters);
356 }
357 }
358
359 if (strpos($parsedUrl['path'], 'mod.php') !== FALSE && isset($parameters['M'])) {
360 $module = $parameters['M'];
361 $url = str_replace('mod.php', '', $parsedUrl['path']) . BackendUtility::getModuleUrl($module, $parameters);
362 }
363 return $url;
364 }
365
366 /**
367 * Gets shortcuts for a specific group
368 *
369 * @param int $groupId Group Id
370 * @return array Array of shortcuts that matched the group
371 */
372 protected function getShortcutsByGroup($groupId) {
373 $shortcuts = array();
374 foreach ($this->shortcuts as $shortcut) {
375 if ($shortcut['group'] == $groupId) {
376 $shortcuts[] = $shortcut;
377 }
378 }
379 return $shortcuts;
380 }
381
382 /**
383 * Gets a shortcut by its uid
384 *
385 * @param int $shortcutId Shortcut id to get the complete shortcut for
386 * @return mixed An array containing the shortcut's data on success or FALSE on failure
387 */
388 protected function getShortcutById($shortcutId) {
389 $returnShortcut = FALSE;
390 foreach ($this->shortcuts as $shortcut) {
391 if ($shortcut['raw']['uid'] == (int)$shortcutId) {
392 $returnShortcut = $shortcut;
393 continue;
394 }
395 }
396 return $returnShortcut;
397 }
398
399 /**
400 * Gets the available shortcut groups from default groups, user TSConfig, and global groups
401 *
402 * @return array
403 */
404 protected function initShortcutGroups() {
405 // Groups from TSConfig
406 $bookmarkGroups = $GLOBALS['BE_USER']->getTSConfigProp('options.bookmarkGroups');
407 if (is_array($bookmarkGroups) && count($bookmarkGroups)) {
408 foreach ($bookmarkGroups as $groupId => $label) {
409 if (!empty($label)) {
410 $this->shortcutGroups[$groupId] = (string)$label;
411 } elseif ($GLOBALS['BE_USER']->isAdmin()) {
412 unset($this->shortcutGroups[$groupId]);
413 }
414 }
415 }
416 // Generate global groups, all global groups have negative IDs.
417 if (count($this->shortcutGroups)) {
418 $groups = $this->shortcutGroups;
419 foreach ($groups as $groupId => $groupLabel) {
420 $this->shortcutGroups[$groupId * -1] = $groupLabel;
421 }
422 }
423 // Group -100 is kind of superglobal and can't be changed.
424 $this->shortcutGroups[self::SUPERGLOBAL_GROUP] = 1;
425 // Add labels
426 foreach ($this->shortcutGroups as $groupId => $groupLabel) {
427 $groupId = (int)$groupId;
428 $label = $groupLabel;
429 if ($groupLabel == '1') {
430 $label = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_misc.xlf:bookmark_group_' . abs($groupId), TRUE);
431 if (empty($label)) {
432 // Fallback label
433 $label = $GLOBALS['LANG']->getLL('bookmark_group', 1) . ' ' . abs($groupId);
434 }
435 }
436 if ($groupId < 0) {
437 // Global group
438 $label = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_misc.xlf:bookmark_global', TRUE) . ': ' . (!empty($label) ? $label : abs($groupId));
439 if ($groupId === self::SUPERGLOBAL_GROUP) {
440 $label = $GLOBALS['LANG']->getLL('bookmark_global', 1) . ': ' . $GLOBALS['LANG']->getLL('bookmark_all', 1);
441 }
442 }
443 $this->shortcutGroups[$groupId] = $label;
444 }
445 return $this->shortcutGroups;
446 }
447
448 /**
449 * gets the available shortcut groups
450 *
451 * @param array $params Array of parameters from the AJAX interface, currently unused
452 * @param \TYPO3\CMS\Core\Http\AjaxRequestHandler $ajaxObj Object of type AjaxRequestHandler
453 * @return void
454 */
455 public function getAjaxShortcutGroups($params = array(), \TYPO3\CMS\Core\Http\AjaxRequestHandler &$ajaxObj = NULL) {
456 $shortcutGroups = $this->shortcutGroups;
457 if (!$GLOBALS['BE_USER']->isAdmin()) {
458 foreach ($shortcutGroups as $groupId => $groupName) {
459 if ((int)$groupId < 0) {
460 unset($shortcutGroups[$groupId]);
461 }
462 }
463 }
464 $ajaxObj->addContent('shortcutGroups', $shortcutGroups);
465 $ajaxObj->setContentFormat('json');
466 }
467
468 /**
469 * Deletes a shortcut through an AJAX call
470 *
471 * @param array $params Array of parameters from the AJAX interface, currently unused
472 * @param \TYPO3\CMS\Core\Http\AjaxRequestHandler $ajaxObj Object of type AjaxRequestHandler
473 * @return void
474 */
475 public function deleteAjaxShortcut($params = array(), \TYPO3\CMS\Core\Http\AjaxRequestHandler &$ajaxObj = NULL) {
476 $shortcutId = (int)GeneralUtility::_POST('shortcutId');
477 $fullShortcut = $this->getShortcutById($shortcutId);
478 $ajaxReturn = 'failed';
479 if ($fullShortcut['raw']['userid'] == $GLOBALS['BE_USER']->user['uid']) {
480 $GLOBALS['TYPO3_DB']->exec_DELETEquery('sys_be_shortcuts', 'uid = ' . $shortcutId);
481 if ($GLOBALS['TYPO3_DB']->sql_affected_rows() == 1) {
482 $ajaxReturn = 'deleted';
483 }
484 }
485 $ajaxObj->addContent('delete', $ajaxReturn);
486 }
487
488 /**
489 * Creates a shortcut through an AJAX call
490 *
491 * @param array $params Array of parameters from the AJAX interface, currently unused
492 * @param \TYPO3\CMS\Core\Http\AjaxRequestHandler $ajaxObj Oject of type AjaxRequestHandler
493 * @return void
494 */
495 public function createAjaxShortcut($params = array(), \TYPO3\CMS\Core\Http\AjaxRequestHandler &$ajaxObj = NULL) {
496 $shortcutCreated = 'failed';
497 // Default name
498 $shortcutName = 'Shortcut';
499 $shortcutNamePrepend = '';
500 $url = GeneralUtility::_POST('url');
501 $module = GeneralUtility::_POST('module');
502 $motherModule = GeneralUtility::_POST('motherModName');
503 // Determine shortcut type
504 $queryParts = parse_url($url);
505 $queryParameters = GeneralUtility::explodeUrl2Array($queryParts['query'], 1);
506 // Proceed only if no scheme is defined, as URL is expected to be relative
507 if (empty($queryParts['scheme'])) {
508 if (is_array($queryParameters['edit'])) {
509 $shortcut['table'] = key($queryParameters['edit']);
510 $shortcut['recordid'] = key($queryParameters['edit'][$shortcut['table']]);
511 if ($queryParameters['edit'][$shortcut['table']][$shortcut['recordid']] == 'edit') {
512 $shortcut['type'] = 'edit';
513 $shortcutNamePrepend = $GLOBALS['LANG']->getLL('shortcut_edit', 1);
514 } elseif ($queryParameters['edit'][$shortcut['table']][$shortcut['recordid']] == 'new') {
515 $shortcut['type'] = 'new';
516 $shortcutNamePrepend = $GLOBALS['LANG']->getLL('shortcut_create', 1);
517 }
518 } else {
519 $shortcut['type'] = 'other';
520 }
521 // Lookup the title of this page and use it as default description
522 $pageId = $shortcut['recordid'] ? $shortcut['recordid'] : $this->getLinkedPageId($url);
523 if (MathUtility::canBeInterpretedAsInteger($pageId)) {
524 $page = BackendUtility::getRecord('pages', $pageId);
525 if (count($page)) {
526 // Set the name to the title of the page
527 if ($shortcut['type'] == 'other') {
528 $shortcutName = $page['title'];
529 } else {
530 $shortcutName = $shortcutNamePrepend . ' ' . $GLOBALS['LANG']->sL($GLOBALS['TCA'][$shortcut['table']]['ctrl']['title']) . ' (' . $page['title'] . ')';
531 }
532 }
533 } else {
534 $dirName = urldecode($pageId);
535 if (preg_match('/\\/$/', $dirName)) {
536 // If $pageId is a string and ends with a slash,
537 // assume it is a fileadmin reference and set
538 // the description to the basename of that path
539 $shortcutName .= ' ' . basename($dirName);
540 }
541 }
542 // adding the shortcut
543 if ($module && $url) {
544 $fieldValues = array(
545 'userid' => $GLOBALS['BE_USER']->user['uid'],
546 'module_name' => $module . '|' . $motherModule,
547 'url' => $url,
548 'description' => $shortcutName,
549 'sorting' => $GLOBALS['EXEC_TIME']
550 );
551 $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_be_shortcuts', $fieldValues);
552 if ($GLOBALS['TYPO3_DB']->sql_affected_rows() == 1) {
553 $shortcutCreated = 'success';
554 }
555 }
556 $ajaxObj->addContent('create', $shortcutCreated);
557 }
558 }
559
560 /**
561 * Gets called when a shortcut is changed, checks whether the user has
562 * permissions to do so and saves the changes if everything is ok
563 *
564 * @param array $params Array of parameters from the AJAX interface, currently unused
565 * @param \TYPO3\CMS\Core\Http\AjaxRequestHandler $ajaxObj Object of type AjaxRequestHandler
566 * @return void
567 */
568 public function setAjaxShortcut($params = array(), \TYPO3\CMS\Core\Http\AjaxRequestHandler &$ajaxObj = NULL) {
569 $shortcutId = (int)GeneralUtility::_POST('shortcutId');
570 $shortcutName = strip_tags(GeneralUtility::_POST('value'));
571 $shortcutGroupId = (int)GeneralUtility::_POST('shortcut-group');
572 if ($shortcutGroupId > 0 || $GLOBALS['BE_USER']->isAdmin()) {
573 // Users can delete only their own shortcuts (except admins)
574 $addUserWhere = !$GLOBALS['BE_USER']->isAdmin() ? ' AND userid=' . (int)$GLOBALS['BE_USER']->user['uid'] : '';
575 $fieldValues = array(
576 'description' => $shortcutName,
577 'sc_group' => $shortcutGroupId
578 );
579 if ($fieldValues['sc_group'] < 0 && !$GLOBALS['BE_USER']->isAdmin()) {
580 $fieldValues['sc_group'] = 0;
581 }
582 $GLOBALS['TYPO3_DB']->exec_UPDATEquery('sys_be_shortcuts', 'uid=' . $shortcutId . $addUserWhere, $fieldValues);
583 $affectedRows = $GLOBALS['TYPO3_DB']->sql_affected_rows();
584 if ($affectedRows == 1) {
585 $ajaxObj->addContent('shortcut', $shortcutName);
586 } else {
587 $ajaxObj->addContent('shortcut', 'failed');
588 }
589 }
590 $ajaxObj->setContentFormat('plain');
591 }
592
593 /**
594 * Gets the label for a shortcut group
595 *
596 * @param int $groupId A shortcut group id
597 * @return string The shortcut group label, can be an empty string if no group was found for the id
598 */
599 protected function getShortcutGroupLabel($groupId) {
600 return isset($this->shortcutGroups[$groupId]) ? $this->shortcutGroups[$groupId] : '';
601 }
602
603 /**
604 * Gets a list of global groups, shortcuts in these groups are available to all users
605 *
606 * @return array Array of global groups
607 */
608 protected function getGlobalShortcutGroups() {
609 $globalGroups = array();
610 foreach ($this->shortcutGroups as $groupId => $groupLabel) {
611 if ($groupId < 0) {
612 $globalGroups[$groupId] = $groupLabel;
613 }
614 }
615 return $globalGroups;
616 }
617
618 /**
619 * runs through the available shortcuts an collects their groups
620 *
621 * @return array Array of groups which have shortcuts
622 */
623 protected function getGroupsFromShortcuts() {
624 $groups = array();
625 foreach ($this->shortcuts as $shortcut) {
626 $groups[$shortcut['group']] = $this->shortcutGroups[$shortcut['group']];
627 }
628 return array_unique($groups);
629 }
630
631 /**
632 * Gets the icon for the shortcut
633 *
634 * @param array $row
635 * @param array $shortcut
636 * @return string Shortcut icon as img tag
637 */
638 protected function getShortcutIcon($row, $shortcut) {
639 switch ($row['module_name']) {
640 case 'xMOD_alt_doc.php':
641 $table = $shortcut['table'];
642 $recordid = $shortcut['recordid'];
643 $icon = '';
644 if ($shortcut['type'] == 'edit') {
645 // Creating the list of fields to include in the SQL query:
646 $selectFields = $this->fieldArray;
647 $selectFields[] = 'uid';
648 $selectFields[] = 'pid';
649 if ($table == 'pages') {
650 $selectFields[] = 'module';
651 $selectFields[] = 'extendToSubpages';
652 $selectFields[] = 'doktype';
653 }
654 if (is_array($GLOBALS['TCA'][$table]['ctrl']['enablecolumns'])) {
655 $selectFields = array_merge($selectFields, $GLOBALS['TCA'][$table]['ctrl']['enablecolumns']);
656 }
657 if ($GLOBALS['TCA'][$table]['ctrl']['type']) {
658 $selectFields[] = $GLOBALS['TCA'][$table]['ctrl']['type'];
659 }
660 if ($GLOBALS['TCA'][$table]['ctrl']['typeicon_column']) {
661 $selectFields[] = $GLOBALS['TCA'][$table]['ctrl']['typeicon_column'];
662 }
663 if ($GLOBALS['TCA'][$table]['ctrl']['versioningWS']) {
664 $selectFields[] = 't3ver_state';
665 }
666 // Unique list!
667 $selectFields = array_unique($selectFields);
668 $permissionClause = $table === 'pages' && $this->perms_clause ? ' AND ' . $this->perms_clause : '';
669 $sqlQueryParts = array(
670 'SELECT' => implode(',', $selectFields),
671 'FROM' => $table,
672 'WHERE' => 'uid IN (' . $recordid . ') ' . $permissionClause . BackendUtility::deleteClause($table) . BackendUtility::versioningPlaceholderClause($table)
673 );
674 $result = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($sqlQueryParts);
675 $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result);
676 $icon = IconUtility::getIcon($table, $row, $this->backPath);
677 } elseif ($shortcut['type'] == 'new') {
678 $icon = IconUtility::getIcon($table, array(), $this->backPath);
679 }
680 $icon = IconUtility::skinImg($this->backPath, $icon, '', 1);
681 break;
682 case 'file_edit':
683 $icon = 'gfx/edit_file.gif';
684 break;
685 case 'wizard_rte':
686 $icon = 'gfx/edit_rtewiz.gif';
687 break;
688 default:
689 if ($GLOBALS['LANG']->moduleLabels['tabs_images'][$row['module_name'] . '_tab']) {
690 $icon = $GLOBALS['LANG']->moduleLabels['tabs_images'][$row['module_name'] . '_tab'];
691 // Change icon of fileadmin references - otherwise it doesn't differ with Web->List
692 $icon = str_replace('mod/file/list/list.gif', 'mod/file/file.gif', $icon);
693 if (GeneralUtility::isAbsPath($icon)) {
694 $icon = '../' . \TYPO3\CMS\Core\Utility\PathUtility::stripPathSitePrefix($icon);
695 }
696 } else {
697 $icon = 'gfx/dummy_module.gif';
698 }
699 }
700 return '<img src="' . $icon . '" alt="' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:toolbarItems.shortcut', TRUE) . '" title="' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:toolbarItems.shortcut', TRUE) . '" />';
701 }
702
703 /**
704 * Returns title for the shortcut icon
705 *
706 * @param string $shortcutLabel Shortcut label
707 * @param string $moduleName Backend module name (key)
708 * @param string $parentModuleName Parent module label
709 * @return string Title for the shortcut icon
710 */
711 protected function getShortcutIconTitle($shortcutLabel, $moduleName, $parentModuleName = '') {
712 $title = '';
713 if (substr($moduleName, 0, 5) == 'xMOD_') {
714 $title = substr($moduleName, 5);
715 } else {
716 $splitModuleName = explode('_', $moduleName);
717 $title = $GLOBALS['LANG']->moduleLabels['tabs'][$splitModuleName[0] . '_tab'];
718 if (count($splitModuleName) > 1) {
719 $title .= '>' . $GLOBALS['LANG']->moduleLabels['tabs'][($moduleName . '_tab')];
720 }
721 }
722 if ($parentModuleName) {
723 $title .= ' (' . $parentModuleName . ')';
724 }
725 $title .= ': ' . $shortcutLabel;
726 return $title;
727 }
728
729 /**
730 * Return the ID of the page in the URL if found.
731 *
732 * @param string $url The URL of the current shortcut link
733 * @return string If a page ID was found, it is returned. Otherwise: 0
734 */
735 protected function getLinkedPageId($url) {
736 return preg_replace('/.*[\\?&]id=([^&]+).*/', '$1', $url);
737 }
738
739 }