[CLEANUP] Streamline backend toolbar items
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Backend / ToolbarItems / ShortcutToolbarItem.php
1 <?php
2 namespace TYPO3\CMS\Backend\Backend\ToolbarItems;
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 Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
19 use TYPO3\CMS\Backend\Module\ModuleLoader;
20 use TYPO3\CMS\Backend\Toolbar\ToolbarItemInterface;
21 use TYPO3\CMS\Backend\Utility\BackendUtility;
22 use TYPO3\CMS\Core\Database\Connection;
23 use TYPO3\CMS\Core\Database\ConnectionPool;
24 use TYPO3\CMS\Core\Database\Query\QueryHelper;
25 use TYPO3\CMS\Core\Imaging\Icon;
26 use TYPO3\CMS\Core\Imaging\IconFactory;
27 use TYPO3\CMS\Core\Page\PageRenderer;
28 use TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException;
29 use TYPO3\CMS\Core\Resource\ResourceFactory;
30 use TYPO3\CMS\Core\Utility\GeneralUtility;
31 use TYPO3\CMS\Core\Utility\MathUtility;
32 use TYPO3\CMS\Fluid\View\StandaloneView;
33
34 /**
35 * Class to render the shortcut menu
36 */
37 class ShortcutToolbarItem implements ToolbarItemInterface
38 {
39 /**
40 * @const integer Number of super global group
41 */
42 const SUPERGLOBAL_GROUP = -100;
43
44 /**
45 * @var string
46 */
47 public $perms_clause;
48
49 /**
50 * @var array
51 */
52 public $fieldArray;
53
54 /**
55 * All available shortcuts
56 *
57 * @var array
58 */
59 protected $shortcuts;
60
61 /**
62 * @var array
63 */
64 protected $shortcutGroups;
65
66 /**
67 * Labels of all groups.
68 * If value is 1, the system will try to find a label in the locallang array.
69 *
70 * @var array
71 */
72 protected $groupLabels;
73
74 /**
75 * @var IconFactory
76 */
77 protected $iconFactory;
78
79 /**
80 * @var ModuleLoader
81 */
82 protected $moduleLoader;
83
84 /**
85 * Constructor
86 */
87 public function __construct()
88 {
89 $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
90 $this->getLanguageService()->includeLLFile('EXT:lang/Resources/Private/Language/locallang_misc.xlf');
91 // Needed to get the correct icons when reloading the menu after saving it
92 $this->moduleLoader = GeneralUtility::makeInstance(ModuleLoader::class);
93 $this->moduleLoader->load($GLOBALS['TBE_MODULES']);
94
95 // By default, 5 groups are set
96 $this->shortcutGroups = [
97 1 => '1',
98 2 => '1',
99 3 => '1',
100 4 => '1',
101 5 => '1'
102 ];
103 $this->shortcutGroups = $this->initShortcutGroups();
104 $this->shortcuts = $this->initShortcuts();
105
106 $this->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/Toolbar/ShortcutMenu');
107 $languageService = $this->getLanguageService();
108 $this->getPageRenderer()->addInlineLanguageLabelArray([
109 'bookmark.delete' => $languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:toolbarItems.bookmarksDelete'),
110 'bookmark.confirmDelete' => $languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:toolbarItems.confirmBookmarksDelete'),
111 'bookmark.create' => $languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:toolbarItems.createBookmark'),
112 'bookmark.savedTitle' => $languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:toolbarItems.bookmarkSavedTitle'),
113 'bookmark.savedMessage' => $languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:toolbarItems.bookmarkSavedMessage'),
114 ]);
115 }
116
117 /**
118 * Checks whether the user has access to this toolbar item
119 *
120 * @return bool TRUE if user has access, FALSE if not
121 */
122 public function checkAccess()
123 {
124 return (bool)$this->getBackendUser()->getTSConfigVal('options.enableBookmarks');
125 }
126
127 /**
128 * Render shortcut icon
129 *
130 * @return string HTML
131 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidExtensionNameException
132 * @throws \InvalidArgumentException
133 */
134 public function getItem()
135 {
136 return $this->getFluidTemplateObject('Item.html')->render();
137 }
138
139 /**
140 * Render drop down content
141 *
142 * @return string HTML
143 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidExtensionNameException
144 * @throws \InvalidArgumentException
145 */
146 public function getDropDown()
147 {
148 $shortcutMenu = [];
149 $groups = $this->getGroupsFromShortcuts();
150 arsort($groups, SORT_NUMERIC);
151 foreach ($groups as $groupId => $groupLabel) {
152 $shortcutMenu[] = [
153 'id' => (int)$groupId,
154 'title' => $groupLabel,
155 'shortcuts' => $this->getShortcutsByGroup($groupId)
156 ];
157 }
158
159 $dropDownView = $this->getFluidTemplateObject('DropDown.html');
160 $dropDownView->assign('shortcutMenu', $shortcutMenu);
161 return $dropDownView->render();
162 }
163
164 /**
165 * Renders the menu so that it can be returned as response to an AJAX call
166 *
167 * @param ServerRequestInterface $request
168 * @param ResponseInterface $response
169 * @return ResponseInterface
170 */
171 public function menuAction(ServerRequestInterface $request, ResponseInterface $response)
172 {
173 $menuContent = $this->getDropDown();
174
175 $response->getBody()->write($menuContent);
176 $response = $response->withHeader('Content-Type', 'text/html; charset=utf-8');
177 return $response;
178 }
179
180 /**
181 * This toolbar item needs no additional attributes
182 *
183 * @return array
184 */
185 public function getAdditionalAttributes()
186 {
187 return [];
188 }
189
190 /**
191 * This item has a drop down
192 *
193 * @return bool
194 */
195 public function hasDropDown()
196 {
197 return true;
198 }
199
200 /**
201 * Retrieves the shortcuts for the current user
202 *
203 * @return array Array of shortcuts
204 */
205 protected function initShortcuts()
206 {
207 $backendUser = $this->getBackendUser();
208 // Traverse shortcuts
209 $lastGroup = 0;
210 $shortcuts = [];
211 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
212 ->getQueryBuilderForTable('sys_be_shortcuts');
213 $result = $queryBuilder->select('*')
214 ->from('sys_be_shortcuts')
215 ->where(
216 $queryBuilder->expr()->andX(
217 $queryBuilder->expr()->eq(
218 'userid',
219 $queryBuilder->createNamedParameter($backendUser->user['uid'], \PDO::PARAM_INT)
220 ),
221 $queryBuilder->expr()->gte(
222 'sc_group',
223 $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)
224 )
225 )
226 )
227 ->orWhere(
228 $queryBuilder->expr()->in(
229 'sc_group',
230 $queryBuilder->createNamedParameter(
231 array_keys($this->getGlobalShortcutGroups()),
232 Connection::PARAM_INT_ARRAY
233 )
234 )
235 )
236 ->orderBy('sc_group')
237 ->addOrderBy('sorting')
238 ->execute();
239
240 while ($row = $result->fetch()) {
241 $shortcut = ['raw' => $row];
242
243 list($row['module_name'], $row['M_module_name']) = explode('|', $row['module_name']);
244
245 $queryParts = parse_url($row['url']);
246 $queryParameters = GeneralUtility::explodeUrl2Array($queryParts['query'], 1);
247 if ($row['module_name'] === 'xMOD_alt_doc.php' && is_array($queryParameters['edit'])) {
248 $shortcut['table'] = key($queryParameters['edit']);
249 $shortcut['recordid'] = key($queryParameters['edit'][$shortcut['table']]);
250 if ($queryParameters['edit'][$shortcut['table']][$shortcut['recordid']] === 'edit') {
251 $shortcut['type'] = 'edit';
252 } elseif ($queryParameters['edit'][$shortcut['table']][$shortcut['recordid']] === 'new') {
253 $shortcut['type'] = 'new';
254 }
255 if (substr($shortcut['recordid'], -1) === ',') {
256 $shortcut['recordid'] = substr($shortcut['recordid'], 0, -1);
257 }
258 } else {
259 $shortcut['type'] = 'other';
260 }
261 // Check for module access
262 $moduleName = $row['M_module_name'] ?: $row['module_name'];
263
264 // Check if the user has access to this module
265 if (!is_array($this->moduleLoader->checkMod($moduleName))) {
266 continue;
267 }
268 $pageId = $this->getLinkedPageId($row['url']);
269
270 if (!$backendUser->isAdmin()) {
271 if (MathUtility::canBeInterpretedAsInteger($pageId)) {
272 // Check for webmount access
273 if ($backendUser->isInWebMount($pageId) === null) {
274 continue;
275 }
276 // Check for record access
277 $pageRow = BackendUtility::getRecord('pages', $pageId);
278 if ($pageRow === null) {
279 continue;
280 }
281 if (!$backendUser->doesUserHaveAccess($pageRow, ($perms = 1))) {
282 continue;
283 }
284 }
285 }
286 $moduleParts = explode('_', $moduleName);
287 $shortcutGroup = (int)$row['sc_group'];
288 if ($shortcutGroup && $lastGroup !== $shortcutGroup && $shortcutGroup !== self::SUPERGLOBAL_GROUP) {
289 $shortcut['groupLabel'] = $this->getShortcutGroupLabel($shortcutGroup);
290 }
291 $lastGroup = $shortcutGroup;
292
293 if ($row['description']) {
294 $shortcut['label'] = $row['description'];
295 } else {
296 $shortcut['label'] = GeneralUtility::fixed_lgd_cs(rawurldecode($queryParts['query']), 150);
297 }
298 $shortcut['group'] = $shortcutGroup;
299 $shortcut['icon'] = $this->getShortcutIcon($row, $shortcut);
300 $shortcut['iconTitle'] = $this->getShortcutIconTitle($shortcut['label'], $row['module_name'], $row['M_module_name']);
301 $shortcut['action'] = 'jump(' . GeneralUtility::quoteJSvalue($this->getTokenUrl($row['url'])) . ',' . GeneralUtility::quoteJSvalue($moduleName) . ',' . GeneralUtility::quoteJSvalue($moduleParts[0]) . ', ' . (int)$pageId . ');';
302
303 $shortcuts[] = $shortcut;
304 }
305 return $shortcuts;
306 }
307
308 /**
309 * Adds the correct token, if the url is an index.php script
310 *
311 * @param string $url
312 * @return string
313 */
314 protected function getTokenUrl($url)
315 {
316 $parsedUrl = parse_url($url);
317 parse_str($parsedUrl['query'], $parameters);
318
319 // parse the returnUrl and replace the module token of it
320 if (isset($parameters['returnUrl'])) {
321 $parsedReturnUrl = parse_url($parameters['returnUrl']);
322 parse_str($parsedReturnUrl['query'], $returnUrlParameters);
323 if (strpos($parsedReturnUrl['path'], 'index.php') !== false && isset($returnUrlParameters['M'])) {
324 $module = $returnUrlParameters['M'];
325 $returnUrl = BackendUtility::getModuleUrl($module, $returnUrlParameters);
326 $parameters['returnUrl'] = $returnUrl;
327 $url = $parsedUrl['path'] . '?' . http_build_query($parameters);
328 }
329 }
330
331 if (strpos($parsedUrl['path'], 'index.php') !== false && isset($parameters['M'])) {
332 $module = $parameters['M'];
333 $url = BackendUtility::getModuleUrl($module, $parameters);
334 } elseif (strpos($parsedUrl['path'], 'index.php') !== false && isset($parameters['route'])) {
335 $routePath = $parameters['route'];
336 /** @var \TYPO3\CMS\Backend\Routing\Router $router */
337 $router = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\Router::class);
338 try {
339 $route = $router->match($routePath);
340 if ($route) {
341 $routeIdentifier = $route->getOption('_identifier');
342 /** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
343 $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
344 unset($parameters['route']);
345 $url = (string)$uriBuilder->buildUriFromRoute($routeIdentifier, $parameters);
346 }
347 } catch (\TYPO3\CMS\Backend\Routing\Exception\ResourceNotFoundException $e) {
348 $url = '';
349 }
350 }
351 return $url;
352 }
353
354 /**
355 * Gets shortcuts for a specific group
356 *
357 * @param int $groupId Group Id
358 * @return array Array of shortcuts that matched the group
359 */
360 protected function getShortcutsByGroup($groupId)
361 {
362 $shortcuts = [];
363 foreach ($this->shortcuts as $shortcut) {
364 if ($shortcut['group'] == $groupId) {
365 $shortcuts[] = $shortcut;
366 }
367 }
368 return $shortcuts;
369 }
370
371 /**
372 * Gets a shortcut by its uid
373 *
374 * @param int $shortcutId Shortcut id to get the complete shortcut for
375 * @return mixed An array containing the shortcut's data on success or FALSE on failure
376 */
377 protected function getShortcutById($shortcutId)
378 {
379 $returnShortcut = false;
380 foreach ($this->shortcuts as $shortcut) {
381 if ($shortcut['raw']['uid'] == (int)$shortcutId) {
382 $returnShortcut = $shortcut;
383 continue;
384 }
385 }
386 return $returnShortcut;
387 }
388
389 /**
390 * Gets the available shortcut groups from default groups, user TSConfig, and global groups
391 *
392 * @return array
393 */
394 protected function initShortcutGroups()
395 {
396 $languageService = $this->getLanguageService();
397 $backendUser = $this->getBackendUser();
398 // Groups from TSConfig
399 $bookmarkGroups = $backendUser->getTSConfigProp('options.bookmarkGroups');
400 if (is_array($bookmarkGroups) && !empty($bookmarkGroups)) {
401 foreach ($bookmarkGroups as $groupId => $label) {
402 if (!empty($label)) {
403 $this->shortcutGroups[$groupId] = (string)$label;
404 } elseif ($backendUser->isAdmin()) {
405 unset($this->shortcutGroups[$groupId]);
406 }
407 }
408 }
409 // Generate global groups, all global groups have negative IDs.
410 if (!empty($this->shortcutGroups)) {
411 $groups = $this->shortcutGroups;
412 foreach ($groups as $groupId => $groupLabel) {
413 $this->shortcutGroups[$groupId * -1] = $groupLabel;
414 }
415 }
416 // Group -100 is kind of superglobal and can't be changed.
417 $this->shortcutGroups[self::SUPERGLOBAL_GROUP] = 1;
418 // Add labels
419 foreach ($this->shortcutGroups as $groupId => $groupLabel) {
420 $groupId = (int)$groupId;
421 $label = $groupLabel;
422 if ($groupLabel == '1') {
423 $label = htmlspecialchars($languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_misc.xlf:bookmark_group_' . abs($groupId)));
424 if (empty($label)) {
425 // Fallback label
426 $label = htmlspecialchars($languageService->getLL('bookmark_group')) . ' ' . abs($groupId);
427 }
428 }
429 if ($groupId < 0) {
430 // Global group
431 $label = htmlspecialchars($languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_misc.xlf:bookmark_global')) . ': ' . (!empty($label) ? $label : abs($groupId));
432 if ($groupId === self::SUPERGLOBAL_GROUP) {
433 $label = htmlspecialchars($languageService->getLL('bookmark_global')) . ': ' . htmlspecialchars($languageService->getLL('bookmark_all'));
434 }
435 }
436 $this->shortcutGroups[$groupId] = $label;
437 }
438
439 return $this->shortcutGroups;
440 }
441
442 /**
443 * Fetches the available shortcut groups, renders a form so it can be saved later on, usually called via AJAX
444 *
445 * @param ServerRequestInterface $request
446 * @param ResponseInterface $response
447 * @return ResponseInterface the full HTML for the form
448 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidExtensionNameException
449 * @throws \RuntimeException
450 * @throws \InvalidArgumentException
451 */
452 public function editFormAction(ServerRequestInterface $request, ResponseInterface $response)
453 {
454 $parsedBody = $request->getParsedBody();
455 $queryParams = $request->getQueryParams();
456
457 $selectedShortcutId = (int)($parsedBody['shortcutId'] ?? $queryParams['shortcutId']);
458 $selectedShortcutGroupId = (int)($parsedBody['shortcutGroup'] ?? $queryParams['shortcutGroup']);
459 $selectedShortcut = $this->getShortcutById($selectedShortcutId);
460 $shortcutGroups = $this->shortcutGroups;
461 if (!$this->getBackendUser()->isAdmin()) {
462 foreach ($shortcutGroups as $groupId => $groupName) {
463 if ((int)$groupId < 0) {
464 unset($shortcutGroups[$groupId]);
465 }
466 }
467 }
468
469 $editFormView = $this->getFluidTemplateObject('EditForm.html');
470 $editFormView->assign('selectedShortcutId', $selectedShortcutId);
471 $editFormView->assign('selectedShortcutGroupId', $selectedShortcutGroupId);
472 $editFormView->assign('selectedShortcut', $selectedShortcut);
473 $editFormView->assign('shortcutGroups', $shortcutGroups);
474
475 $response->getBody()->write($editFormView->render());
476 return $response->withHeader('Content-Type', 'text/html; charset=utf-8');
477 }
478
479 /**
480 * Deletes a shortcut through an AJAX call
481 *
482 * @param ServerRequestInterface $request
483 * @param ResponseInterface $response
484 * @return ResponseInterface
485 */
486 public function removeShortcutAction(ServerRequestInterface $request, ResponseInterface $response)
487 {
488 $parsedBody = $request->getParsedBody();
489 $queryParams = $request->getQueryParams();
490
491 $shortcutId = (int)(isset($parsedBody['shortcutId']) ? $parsedBody['shortcutId'] : $queryParams['shortcutId']);
492 $fullShortcut = $this->getShortcutById($shortcutId);
493 $success = false;
494 if ($fullShortcut['raw']['userid'] == $this->getBackendUser()->user['uid']) {
495 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
496 ->getQueryBuilderForTable('sys_be_shortcuts');
497 $affectedRows = $queryBuilder->delete('sys_be_shortcuts')
498 ->where(
499 $queryBuilder->expr()->eq(
500 'uid',
501 $queryBuilder->createNamedParameter($shortcutId, \PDO::PARAM_INT)
502 )
503 )
504 ->execute();
505 if ($affectedRows === 1) {
506 $success = true;
507 }
508 }
509 $response->getBody()->write(json_encode(['success' => $success]));
510 return $response;
511 }
512
513 /**
514 * Creates a shortcut through an AJAX call
515 *
516 * @param ServerRequestInterface $request
517 * @param ResponseInterface $response
518 * @return ResponseInterface
519 */
520 public function createShortcutAction(ServerRequestInterface $request, ResponseInterface $response)
521 {
522 $languageService = $this->getLanguageService();
523 $parsedBody = $request->getParsedBody();
524 $queryParams = $request->getQueryParams();
525
526 // Default name
527 $shortcutName = 'Shortcut';
528 $shortcutNamePrepend = '';
529 $url = isset($parsedBody['url']) ? $parsedBody['url'] : $queryParams['url'];
530
531 // Use given display name
532 if (!empty($parsedBody['displayName'])) {
533 $shortcutName = $parsedBody['displayName'];
534 }
535
536 // Determine shortcut type
537 $url = rawurldecode($url);
538 $queryParts = parse_url($url);
539 $queryParameters = GeneralUtility::explodeUrl2Array($queryParts['query'], true);
540
541 // Proceed only if no scheme is defined, as URL is expected to be relative
542 if (empty($queryParts['scheme'])) {
543 if (is_array($queryParameters['edit'])) {
544 $shortcut['table'] = key($queryParameters['edit']);
545 $shortcut['recordid'] = key($queryParameters['edit'][$shortcut['table']]);
546 $shortcut['pid'] = BackendUtility::getRecord($shortcut['table'], $shortcut['recordid'])['pid'];
547 if ($queryParameters['edit'][$shortcut['table']][$shortcut['recordid']] === 'edit') {
548 $shortcut['type'] = 'edit';
549 $shortcutNamePrepend = htmlspecialchars($languageService->getLL('shortcut_edit'));
550 } elseif ($queryParameters['edit'][$shortcut['table']][$shortcut['recordid']] === 'new') {
551 $shortcut['type'] = 'new';
552 $shortcutNamePrepend = htmlspecialchars($languageService->getLL('shortcut_create'));
553 }
554 } else {
555 $shortcut['type'] = 'other';
556 $shortcut['table'] = '';
557 $shortcut['recordid'] = 0;
558 }
559
560 // Check if given id is a combined identifier
561 if (!empty($queryParameters['id']) && preg_match('/^[0-9]+:/', $queryParameters['id'])) {
562 try {
563 $resourceFactory = ResourceFactory::getInstance();
564 $resource = $resourceFactory->getObjectFromCombinedIdentifier($queryParameters['id']);
565 $shortcutName = trim($shortcutNamePrepend . ' ' . $resource->getName());
566 } catch (ResourceDoesNotExistException $e) {
567 }
568 } else {
569 // Lookup the title of this page and use it as default description
570 $pageId = (int)($shortcut['pid'] ?: ($shortcut['recordid'] ?: $this->getLinkedPageId($url)));
571 $page = false;
572 if ($pageId) {
573 $page = BackendUtility::getRecord('pages', $pageId);
574 }
575 if (!empty($page)) {
576 // Set the name to the title of the page
577 if ($shortcut['type'] === 'other') {
578 if (empty($shortcutName)) {
579 $shortcutName = $page['title'];
580 } else {
581 $shortcutName .= ' (' . $page['title'] . ')';
582 }
583 } else {
584 $shortcutName = $shortcutNamePrepend . ' ' .
585 $languageService->sL($GLOBALS['TCA'][$shortcut['table']]['ctrl']['title']) .
586 ' (' . $page['title'] . ')';
587 }
588 } elseif ($shortcut['table'] !== '' && $shortcut['type'] !== 'other') {
589 $shortcutName = $shortcutNamePrepend . ' ' .
590 $languageService->sL($GLOBALS['TCA'][$shortcut['table']]['ctrl']['title']);
591 }
592 }
593
594 return $this->tryAddingTheShortcut($response, $url, $shortcutName);
595 }
596 }
597
598 /**
599 * Try to adding a shortcut
600 *
601 * @param ResponseInterface $response
602 * @param string $url
603 * @param string $shortcutName
604 * @return ResponseInterface
605 * @throws \InvalidArgumentException
606 */
607 protected function tryAddingTheShortcut(ResponseInterface $response, $url, $shortcutName)
608 {
609 $module = GeneralUtility::_POST('module');
610 $shortcutCreated = 'failed';
611
612 if (!empty($module) && !empty($url)) {
613 $shortcutCreated = 'alreadyExists';
614
615 if (!BackendUtility::shortcutExists($url) && !is_array($module)) {
616 $shortcutCreated = $this->addShortcut($url, $shortcutName, $module);
617 }
618 }
619
620 $response->getBody()->write($shortcutCreated);
621 return $response->withHeader('Content-Type', 'text/html; charset=utf-8');
622 }
623
624 /**
625 * Add a shortcut now with some user stuffs
626 *
627 * @param string $url
628 * @param string $shortcutName
629 * @param string $module
630 *
631 * @return string
632 */
633 protected function addShortcut($url, $shortcutName, $module)
634 {
635 $moduleLabels = $this->moduleLoader->getLabelsForModule($module);
636 if ($shortcutName === 'Shortcut' && !empty($moduleLabels['shortdescription'])) {
637 $shortcutName = $this->getLanguageService()->sL($moduleLabels['shortdescription']);
638 }
639
640 $motherModule = GeneralUtility::_POST('motherModName');
641 $fieldValues = [
642 'userid' => $this->getBackendUser()->user['uid'],
643 'module_name' => $module . '|' . $motherModule,
644 'url' => $url,
645 'description' => $shortcutName,
646 'sorting' => $GLOBALS['EXEC_TIME']
647 ];
648
649 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
650 ->getQueryBuilderForTable('sys_be_shortcuts');
651 $affectedRows = $queryBuilder
652 ->insert('sys_be_shortcuts')
653 ->values($fieldValues)
654 ->execute();
655
656 if ($affectedRows === 1) {
657 return 'success';
658 } else {
659 return 'failed';
660 }
661 }
662
663 /**
664 * Exists already a shortcut entry for this TYPO3 url?
665 *
666 * @param string $url
667 *
668 * @return bool
669 */
670 protected function shortcutExists($url)
671 {
672 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
673 ->getQueryBuilderForTable('sys_be_shortcuts');
674 $uid = $queryBuilder->select('uid')
675 ->from('sys_be_shortcuts')
676 ->where(
677 $queryBuilder->expr()->eq(
678 'userid',
679 $queryBuilder->createNamedParameter($this->getBackendUser()->user['uid'], \PDO::PARAM_INT)
680 ),
681 $queryBuilder->expr()->eq('url', $queryBuilder->createNamedParameter($url, \PDO::PARAM_STR))
682 )
683 ->execute()
684 ->fetchColumn();
685
686 return (bool)$uid;
687 }
688
689 /**
690 * Gets called when a shortcut is changed, checks whether the user has
691 * permissions to do so and saves the changes if everything is ok
692 *
693 * @param ServerRequestInterface $request
694 * @param ResponseInterface $response
695 * @return ResponseInterface
696 */
697 public function saveFormAction(ServerRequestInterface $request, ResponseInterface $response)
698 {
699 $parsedBody = $request->getParsedBody();
700 $queryParams = $request->getQueryParams();
701
702 $backendUser = $this->getBackendUser();
703 $shortcutId = (int)(isset($parsedBody['shortcutId']) ? $parsedBody['shortcutId'] : $queryParams['shortcutId']);
704 $shortcutName = strip_tags(isset($parsedBody['shortcutTitle']) ? $parsedBody['shortcutTitle'] : $queryParams['shortcutTitle']);
705 $shortcutGroupId = (int)(isset($parsedBody['shortcutGroup']) ? $parsedBody['shortcutGroup'] : $queryParams['shortcutGroup']);
706
707 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
708 ->getQueryBuilderForTable('sys_be_shortcuts');
709 $queryBuilder->update('sys_be_shortcuts')
710 ->where(
711 $queryBuilder->expr()->eq(
712 'uid',
713 $queryBuilder->createNamedParameter($shortcutId, \PDO::PARAM_INT)
714 )
715 )
716 ->set('description', $shortcutName)
717 ->set('sc_group', $shortcutGroupId);
718
719 if (!$backendUser->isAdmin()) {
720 // Users can only modify their own shortcuts
721 $queryBuilder->andWhere(
722 $queryBuilder->expr()->eq(
723 'userid',
724 $queryBuilder->createNamedParameter($backendUser->user['uid'], \PDO::PARAM_INT)
725 )
726 );
727
728 if ($shortcutGroupId < 0) {
729 $queryBuilder->set('sc_group', 0);
730 }
731 }
732
733 if ($queryBuilder->execute() === 1) {
734 $response->getBody()->write($shortcutName);
735 } else {
736 $response->getBody()->write('failed');
737 }
738 return $response->withHeader('Content-Type', 'text/html; charset=utf-8');
739 }
740
741 /**
742 * Gets the label for a shortcut group
743 *
744 * @param int $groupId A shortcut group id
745 * @return string The shortcut group label, can be an empty string if no group was found for the id
746 */
747 protected function getShortcutGroupLabel($groupId)
748 {
749 return isset($this->shortcutGroups[$groupId]) ? $this->shortcutGroups[$groupId] : '';
750 }
751
752 /**
753 * Gets a list of global groups, shortcuts in these groups are available to all users
754 *
755 * @return array Array of global groups
756 */
757 protected function getGlobalShortcutGroups()
758 {
759 $globalGroups = [];
760 foreach ($this->shortcutGroups as $groupId => $groupLabel) {
761 if ($groupId < 0) {
762 $globalGroups[$groupId] = $groupLabel;
763 }
764 }
765 return $globalGroups;
766 }
767
768 /**
769 * runs through the available shortcuts an collects their groups
770 *
771 * @return array Array of groups which have shortcuts
772 */
773 protected function getGroupsFromShortcuts()
774 {
775 $groups = [];
776 foreach ($this->shortcuts as $shortcut) {
777 $groups[$shortcut['group']] = $this->shortcutGroups[$shortcut['group']];
778 }
779 return array_unique($groups);
780 }
781
782 /**
783 * Gets the icon for the shortcut
784 *
785 * @param array $row
786 * @param array $shortcut
787 * @return string Shortcut icon as img tag
788 * @throws \InvalidArgumentException
789 */
790 protected function getShortcutIcon($row, $shortcut)
791 {
792 switch ($row['module_name']) {
793 case 'xMOD_alt_doc.php':
794 $table = $shortcut['table'];
795 $recordid = $shortcut['recordid'];
796 $icon = '';
797 if ($shortcut['type'] === 'edit') {
798 // Creating the list of fields to include in the SQL query:
799 $selectFields = $this->fieldArray;
800 $selectFields[] = 'uid';
801 $selectFields[] = 'pid';
802 if ($table === 'pages') {
803 $selectFields[] = 'module';
804 $selectFields[] = 'extendToSubpages';
805 $selectFields[] = 'doktype';
806 }
807 if (is_array($GLOBALS['TCA'][$table]['ctrl']['enablecolumns'])) {
808 $selectFields = array_merge($selectFields, $GLOBALS['TCA'][$table]['ctrl']['enablecolumns']);
809 }
810 if ($GLOBALS['TCA'][$table]['ctrl']['type']) {
811 $selectFields[] = $GLOBALS['TCA'][$table]['ctrl']['type'];
812 }
813 if ($GLOBALS['TCA'][$table]['ctrl']['typeicon_column']) {
814 $selectFields[] = $GLOBALS['TCA'][$table]['ctrl']['typeicon_column'];
815 }
816 if ($GLOBALS['TCA'][$table]['ctrl']['versioningWS']) {
817 $selectFields[] = 't3ver_state';
818 }
819
820 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
821 ->getQueryBuilderForTable($table);
822 $queryBuilder->select(...array_unique(array_values($selectFields)))
823 ->from($table)
824 ->where(
825 $queryBuilder->expr()->in(
826 'uid',
827 $queryBuilder->createNamedParameter($recordid, \PDO::PARAM_INT)
828 )
829 );
830
831 if ($table === 'pages' && $this->perms_clause) {
832 $queryBuilder->andWhere(QueryHelper::stripLogicalOperatorPrefix($this->perms_clause));
833 }
834
835 $row = $queryBuilder->execute()->fetch();
836
837 $icon = $this->iconFactory->getIconForRecord($table, (array)$row, Icon::SIZE_SMALL)->render();
838 } elseif ($shortcut['type'] === 'new') {
839 $icon = $this->iconFactory->getIconForRecord($table, [], Icon::SIZE_SMALL)->render();
840 }
841 break;
842 case 'file_edit':
843 $icon = $this->iconFactory->getIcon('mimetypes-text-html', Icon::SIZE_SMALL)->render();
844 break;
845 case 'wizard_rte':
846 $icon = $this->iconFactory->getIcon('mimetypes-word', Icon::SIZE_SMALL)->render();
847 break;
848 default:
849 $iconIdentifier = '';
850 $moduleName = $row['module_name'];
851 if (strpos($moduleName, '_') !== false) {
852 list($mainModule, $subModule) = explode('_', $moduleName, 2);
853 $iconIdentifier = $this->moduleLoader->modules[$mainModule]['sub'][$subModule]['iconIdentifier'];
854 } elseif (!empty($moduleName)) {
855 $iconIdentifier = $this->moduleLoader->modules[$moduleName]['iconIdentifier'];
856 }
857 if (!$iconIdentifier) {
858 $iconIdentifier = 'empty-empty';
859 }
860 $icon = $this->iconFactory->getIcon($iconIdentifier, Icon::SIZE_SMALL)->render();
861 }
862
863 return $icon;
864 }
865
866 /**
867 * Returns title for the shortcut icon
868 *
869 * @param string $shortcutLabel Shortcut label
870 * @param string $moduleName Backend module name (key)
871 * @param string $parentModuleName Parent module label
872 * @return string Title for the shortcut icon
873 */
874 protected function getShortcutIconTitle($shortcutLabel, $moduleName, $parentModuleName = '')
875 {
876 $languageService = $this->getLanguageService();
877 if (substr($moduleName, 0, 5) === 'xMOD_') {
878 $title = substr($moduleName, 5);
879 } else {
880 list($mainModule, $subModule) = explode('_', $moduleName);
881 $mainModuleLabels = $this->moduleLoader->getLabelsForModule($mainModule);
882 $title = $languageService->sL($mainModuleLabels['title']);
883 if (!empty($subModule)) {
884 $subModuleLabels = $this->moduleLoader->getLabelsForModule($moduleName);
885 $title .= '>' . $languageService->sL($subModuleLabels['title']);
886 }
887 }
888 if ($parentModuleName) {
889 $title .= ' (' . $parentModuleName . ')';
890 }
891 $title .= ': ' . $shortcutLabel;
892 return $title;
893 }
894
895 /**
896 * Return the ID of the page in the URL if found.
897 *
898 * @param string $url The URL of the current shortcut link
899 * @return string If a page ID was found, it is returned. Otherwise: 0
900 */
901 protected function getLinkedPageId($url)
902 {
903 return preg_replace('/.*[\\?&]id=([^&]+).*/', '$1', $url);
904 }
905
906 /**
907 * Position relative to others, live search should be very right
908 *
909 * @return int
910 */
911 public function getIndex()
912 {
913 return 20;
914 }
915
916 /**
917 * Returns the current BE user.
918 *
919 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
920 */
921 protected function getBackendUser()
922 {
923 return $GLOBALS['BE_USER'];
924 }
925
926 /**
927 * Returns current PageRenderer
928 *
929 * @return PageRenderer
930 */
931 protected function getPageRenderer()
932 {
933 return GeneralUtility::makeInstance(PageRenderer::class);
934 }
935
936 /**
937 * Returns LanguageService
938 *
939 * @return \TYPO3\CMS\Lang\LanguageService
940 */
941 protected function getLanguageService()
942 {
943 return $GLOBALS['LANG'];
944 }
945
946 /**
947 * returns a new standalone view, shorthand function
948 *
949 * @param string $templateFilename
950 * @return StandaloneView
951 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidExtensionNameException
952 * @throws \InvalidArgumentException
953 * @internal param string $templateFile
954 */
955 protected function getFluidTemplateObject(string $templateFilename): StandaloneView
956 {
957 $view = GeneralUtility::makeInstance(StandaloneView::class);
958 $view->setLayoutRootPaths(['EXT:backend/Resources/Private/Layouts']);
959 $view->setPartialRootPaths(['EXT:backend/Resources/Private/Partials']);
960 $view->setTemplateRootPaths(['EXT:backend/Resources/Private/Templates/ShortcutToolbarItem']);
961
962 $view->setTemplate($templateFilename);
963
964 $view->getRequest()->setControllerExtensionName('Backend');
965 return $view;
966 }
967 }