ItemProvider.php 3.77 KB
Newer Older
1
<?php
2

3
declare(strict_types=1);
4
5
6
7
8
9
10
11
12
13
14
15
16
17

/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

18
19
namespace TYPO3\CMS\Impexp\ContextMenu;

20
use TYPO3\CMS\Backend\ContextMenu\ItemProviders\AbstractProvider;
21
22
use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
23
24
25

/**
 * Context menu item provider adding export and import items
Alexander Nitsche's avatar
Alexander Nitsche committed
26
27
 *
 * @internal This class is not considered part of the public TYPO3 API.
28
29
30
31
32
33
34
35
36
37
38
 */
class ItemProvider extends AbstractProvider
{
    /**
     * @var array
     */
    protected $itemsConfiguration = [
        'exportT3d' => [
            'type' => 'item',
            'label' => 'LLL:EXT:impexp/Resources/Private/Language/locallang.xlf:export',
            'iconIdentifier' => 'actions-document-export-t3d',
39
            'callbackAction' => 'exportT3d',
40
41
42
43
44
45
        ],
        'importT3d' => [
            'type' => 'item',
            'label' => 'LLL:EXT:impexp/Resources/Private/Language/locallang.xlf:import',
            'iconIdentifier' => 'actions-document-import-t3d',
            'callbackAction' => 'importT3d',
46
        ],
47
48
49
50
51
52
53
54
55
    ];

    /**
     * Export item is added for all database records except files
     *
     * @return bool
     */
    public function canHandle(): bool
    {
56
        return $this->table !== 'sys_file';
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
    }

    /**
     * This needs to be lower than priority of the RecordProvider
     *
     * @return int
     */
    public function getPriority(): int
    {
        return 50;
    }

    /**
     * Adds import/export items to the "submenu" if available
     *
     * @param array $items
     * @return array
     */
    public function addItems(array $items): array
    {
        $this->initDisabledItems();
        $localItems = $this->prepareItems($this->itemsConfiguration);
        if (isset($items['more']['childItems'])) {
            $items['more']['childItems'] = $items['more']['childItems'] + $localItems;
        } else {
            $items += $localItems;
        }
        return $items;
    }

    /**
     * @param string $itemName
     * @param string $type
     * @return bool
     */
    protected function canRender(string $itemName, string $type): bool
    {
        if (in_array($itemName, $this->disabledItems, true)) {
            return false;
        }
        $canRender = false;
        switch ($itemName) {
            case 'exportT3d':
100
                $canRender = $this->backendUser->isExportEnabled();
101
102
                break;
            case 'importT3d':
103
                $canRender = $this->table === 'pages' && $this->backendUser->isImportEnabled();
104
105
106
107
108
109
110
111
112
113
114
115
116
                break;
        }
        return $canRender;
    }

    /**
     * Registers custom JS module with item onclick behaviour
     *
     * @param string $itemName
     * @return array
     */
    protected function getAdditionalAttributes(string $itemName): array
    {
117
        $attributes = [
118
            'data-callback-module' => '@typo3/impexp/context-menu-actions',
119
120
121
122
123
124
125
126
127
128
129
130
131
132
        ];

        // Add action url for items
        $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
        switch ($itemName) {
            case 'exportT3d':
                $attributes['data-action-url'] = htmlspecialchars((string)$uriBuilder->buildUriFromRoute('tx_impexp_export'));
                break;
            case 'importT3d':
                $attributes['data-action-url'] = htmlspecialchars((string)$uriBuilder->buildUriFromRoute('tx_impexp_import'));
                break;
        }

        return $attributes;
133
134
    }
}