ClearCacheToolbarItem.php 8.23 KB
Newer Older
1
<?php
2
namespace TYPO3\CMS\Backend\Backend\ToolbarItems;
3

4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
8
9
 * 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.
10
 *
11
12
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
15
 * The TYPO3 project - inspiring people to share!
 */
Nicole Cordes's avatar
Nicole Cordes committed
16

17
use TYPO3\CMS\Backend\Toolbar\ClearCacheActionsHookInterface;
18
use TYPO3\CMS\Backend\Toolbar\ToolbarItemInterface;
Nicole Cordes's avatar
Nicole Cordes committed
19
use TYPO3\CMS\Backend\Utility\BackendUtility;
20
21
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
22
use TYPO3\CMS\Core\Page\PageRenderer;
23
use TYPO3\CMS\Core\Utility\GeneralUtility;
24
use TYPO3\CMS\Fluid\View\StandaloneView;
Nicole Cordes's avatar
Nicole Cordes committed
25

26
/**
27
 * Render cache clearing toolbar item
28
 */
29
30
31
32
33
class ClearCacheToolbarItem implements ToolbarItemInterface
{
    /**
     * @var array
     */
34
    protected $cacheActions = [];
35
36
37
38

    /**
     * @var array
     */
39
    protected $optionValues = [];
40

41
42
43
44
45
    /**
     * @var IconFactory
     */
    protected $iconFactory;

46
47
48
49
50
51
52
53
54
    /**
     * Constructor
     *
     * @throws \UnexpectedValueException
     */
    public function __construct()
    {
        $backendUser = $this->getBackendUser();
        $languageService = $this->getLanguageService();
55
        $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
56
57
58
59
60

        $this->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/Toolbar/ClearCacheMenu');

        // Clear all page-related caches
        if ($backendUser->isAdmin() || $backendUser->getTSConfigVal('options.clearCache.pages')) {
61
            $this->cacheActions[] = [
62
                'id' => 'pages',
63
64
                'title' => htmlspecialchars($languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:flushPageCachesTitle')),
                'description' => htmlspecialchars($languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:flushPageCachesDescription')),
65
                'href' => BackendUtility::getModuleUrl('tce_db', ['vC' => $backendUser->veriCode(), 'cacheCmd' => 'pages']),
66
                'icon' => $this->iconFactory->getIcon('actions-system-cache-clear-impact-low', Icon::SIZE_SMALL)->render()
67
            ];
68
69
70
            $this->optionValues[] = 'pages';
        }

71
72
73
74
        // Clearing of all caches is only shown if explicitly enabled via TSConfig
        // or if BE-User is admin and the TSconfig explicitly disables the possibility for admins.
        // This is useful for big production systems where admins accidentally could slow down the system.
        if ($backendUser->getTSConfigVal('options.clearCache.all') || ($backendUser->isAdmin() && $backendUser->getTSConfigVal('options.clearCache.all') !== '0')) {
75
            $this->cacheActions[] = [
76
                'id' => 'all',
77
78
                'title' => htmlspecialchars($languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:flushAllCachesTitle2')),
                'description' => htmlspecialchars($languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:flushAllCachesDescription2')),
79
                'href' => BackendUtility::getModuleUrl('tce_db', ['vC' => $backendUser->veriCode(), 'cacheCmd' => 'all']),
80
                'icon' => $this->iconFactory->getIcon('actions-system-cache-clear-impact-high', Icon::SIZE_SMALL)->render()
81
            ];
82
            $this->optionValues[] = 'all';
83
84
85
86
87
88
89
        }

        // Hook for manipulating cacheActions
        if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['additionalBackendItems']['cacheActions'])) {
            foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['additionalBackendItems']['cacheActions'] as $cacheAction) {
                $hookObject = GeneralUtility::getUserObj($cacheAction);
                if (!$hookObject instanceof ClearCacheActionsHookInterface) {
90
                    throw new \UnexpectedValueException($cacheAction . ' must implement interface ' . ClearCacheActionsHookInterface::class, 1228262000);
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
                }
                $hookObject->manipulateCacheActions($this->cacheActions, $this->optionValues);
            }
        }
    }

    /**
     * Checks whether the user has access to this toolbar item
     *
     * @return bool TRUE if user has access, FALSE if not
     */
    public function checkAccess()
    {
        $backendUser = $this->getBackendUser();
        if ($backendUser->isAdmin()) {
            return true;
        }
        if (is_array($this->optionValues)) {
            foreach ($this->optionValues as $value) {
                if ($backendUser->getTSConfigVal('options.clearCache.' . $value)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * Render clear cache icon
     *
     * @return string Icon HTML
     */
    public function getItem()
    {
125
126
127
128
        if ($this->hasDropDown()) {
            $templateReference = 'ClearCacheToolbarItem.html';
            $icon = $this->iconFactory->getIcon('apps-toolbar-menu-cache', Icon::SIZE_SMALL)->render('inline');
            $variables = [
129
                'title' => 'LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:rm.clearCache_clearCache',
130
                'icon' => $icon
131
132
133
134
135
136
137
138
139
140
            ];
        } else {
            $templateReference = 'ClearCacheToolbarItemSingle.html';
            $cacheAction = end($this->cacheActions);
            $variables['link'] = $cacheAction['href'];
            $variables['title'] = $cacheAction['title'];
            $variables['icon'] = $cacheAction['icon'];
        }
        $view = $this->getFluidTemplateObject($templateReference);
        $view->assignMultiple($variables);
141
142

        return $view->render();
143
144
145
146
147
148
149
150
151
    }

    /**
     * Render drop down
     *
     * @return string Drop down HTML
     */
    public function getDropDown()
    {
152
153
        $view = $this->getFluidTemplateObject('ClearCacheToolbarItemDropDown.html');
        $view->assignMultiple([
154
                'title' =>  'LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:rm.clearCache_clearCache',
155
156
157
158
159
                'cacheActions' => $this->cacheActions,
            ]
        );

        return $view->render();
160
161
162
163
164
165
166
167
168
    }

    /**
     * No additional attributes needed.
     *
     * @return array
     */
    public function getAdditionalAttributes()
    {
169
        return [];
170
171
172
173
174
175
176
177
178
    }

    /**
     * This item has a drop down
     *
     * @return bool
     */
    public function hasDropDown()
    {
179
        return count($this->cacheActions) > 1;
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
    }

    /**
     * Position relative to others
     *
     * @return int
     */
    public function getIndex()
    {
        return 25;
    }

    /**
     * Returns the current BE user.
     *
     * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
     */
    protected function getBackendUser()
    {
        return $GLOBALS['BE_USER'];
    }

    /**
     * Returns current PageRenderer
     *
     * @return PageRenderer
     */
    protected function getPageRenderer()
    {
        return GeneralUtility::makeInstance(PageRenderer::class);
    }

    /**
     * Returns LanguageService
     *
     * @return \TYPO3\CMS\Lang\LanguageService
     */
    protected function getLanguageService()
    {
        return $GLOBALS['LANG'];
    }
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241

    /**
     * Returns a new standalone view, shorthand function
     *
     * @param string $filename Which templateFile should be used.
     *
     * @return StandaloneView
     */
    protected function getFluidTemplateObject(string $filename):StandaloneView
    {
        /** @var StandaloneView $view */
        $view = GeneralUtility::makeInstance(StandaloneView::class);
        $view->setLayoutRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Layouts')]);
        $view->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Partials/ToolbarItems')]);
        $view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates/ToolbarItems')]);

        $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates/ToolbarItems/' . $filename));

        $view->getRequest()->setControllerExtensionName('Backend');
        return $view;
    }
242
}