[!!!][FEATURE] Refactor and streamline click menu / context menu
[Packages/TYPO3.CMS.git] / typo3 / sysext / version / Classes / ContextMenu / ItemProvider.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Version\ContextMenu;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Backend\ContextMenu\ItemProviders\AbstractProvider;
19 use TYPO3\CMS\Backend\Utility\BackendUtility;
20
21 /**
22 * Context menu item provider for version module
23 */
24 class ItemProvider extends AbstractProvider
25 {
26 /**
27 * @var array
28 */
29 protected $itemsConfiguration = [
30 'versioning' => [
31 'type' => 'item',
32 'label' => 'LLL:EXT:version/Resources/Private/Language/locallang.xlf:title',
33 'iconIdentifier' => 'actions-version-page-open',
34 'callbackAction' => 'openVersionModule'
35 ]
36 ];
37
38 /**
39 * @return bool
40 */
41 public function canHandle(): bool
42 {
43 return isset($GLOBALS['TCA'][$this->table]) && $GLOBALS['TCA'][$this->table]['ctrl']['versioningWS'];
44 }
45
46 /**
47 * This needs to be lower than priority of the RecordProvider
48 *
49 * @return int
50 */
51 public function getPriority(): int
52 {
53 return 55;
54 }
55
56 /**
57 * Registers custom JS module with item onclick behaviour
58 *
59 * @param string $itemName
60 * @return array
61 */
62 protected function getAdditionalAttributes(string $itemName): array
63 {
64 $url = BackendUtility::getModuleUrl('web_txversionM1', ['table' => $this->table, 'uid' => $this->identifier]);
65 return [
66 'data-action-url' => htmlspecialchars($url),
67 'data-callback-module' => 'TYPO3/CMS/Version/ContextMenuActions'];
68 }
69
70 /**
71 * Adds import/export items to the "submenu" if available
72 *
73 * @param array $items
74 * @return array
75 */
76 public function addItems(array $items): array
77 {
78 $this->initDisabledItems();
79
80 if (isset($items['delete'])) {
81 $localItems = $this->prepareItems($this->itemsConfiguration);
82 $position = array_search('delete', array_keys($items), true);
83
84 $beginning = array_slice($items, 0, $position+1, true);
85 $end = array_slice($items, $position, null, true);
86
87 $items = $beginning + $localItems + $end;
88 }
89 return $items;
90 }
91
92 /**
93 * @param string $itemName
94 * @param string $type
95 * @return bool
96 */
97 protected function canRender(string $itemName, string $type): bool
98 {
99 if (in_array($itemName, $this->disabledItems, true)) {
100 return false;
101 }
102 $canRender = false;
103 switch ($itemName) {
104 case 'versioning':
105 $canRender = (int)$this->identifier > 0 && $this->backendUser->check('modules', 'web_txversionM1');
106 break;
107 }
108 return $canRender;
109 }
110 }