2 namespace TYPO3\CMS\Backend\Backend\ToolbarItems
;
5 * This file is part of the TYPO3 CMS project.
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.
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
14 * The TYPO3 project - inspiring people to share!
17 use TYPO3\CMS\Backend\Toolbar\ClearCacheActionsHookInterface
;
18 use TYPO3\CMS\Backend\Toolbar\ToolbarItemInterface
;
19 use TYPO3\CMS\Backend\Utility\BackendUtility
;
20 use TYPO3\CMS\Core\Imaging\Icon
;
21 use TYPO3\CMS\Core\Imaging\IconFactory
;
22 use TYPO3\CMS\Core\Page\PageRenderer
;
23 use TYPO3\CMS\Core\Utility\GeneralUtility
;
26 * Render cache clearing toolbar item
28 class ClearCacheToolbarItem
implements ToolbarItemInterface
33 protected $cacheActions = array();
38 protected $optionValues = array();
43 protected $iconFactory;
48 * @throws \UnexpectedValueException
50 public function __construct()
52 $backendUser = $this->getBackendUser();
53 $languageService = $this->getLanguageService();
54 $this->iconFactory
= GeneralUtility
::makeInstance(IconFactory
::class);
56 $this->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/Toolbar/ClearCacheMenu');
58 // Clear all page-related caches
59 if ($backendUser->isAdmin() ||
$backendUser->getTSConfigVal('options.clearCache.pages')) {
60 $this->cacheActions
[] = array(
62 'title' => htmlspecialchars($languageService->sL('LLL:EXT:lang/locallang_core.xlf:flushPageCachesTitle')),
63 'description' => htmlspecialchars($languageService->sL('LLL:EXT:lang/locallang_core.xlf:flushPageCachesDescription')),
64 'href' => BackendUtility
::getModuleUrl('tce_db', ['vC' => $backendUser->veriCode(), 'cacheCmd' => 'pages', 'ajaxCall' => 1]),
65 'icon' => $this->iconFactory
->getIcon('actions-system-cache-clear-impact-low', Icon
::SIZE_SMALL
)->render()
67 $this->optionValues
[] = 'pages';
70 // Clear cache for ALL tables!
71 if ($backendUser->isAdmin() ||
$backendUser->getTSConfigVal('options.clearCache.all')) {
72 $this->cacheActions
[] = array(
74 'title' => htmlspecialchars($languageService->sL('LLL:EXT:lang/locallang_core.xlf:flushGeneralCachesTitle')),
75 'description' => htmlspecialchars($languageService->sL('LLL:EXT:lang/locallang_core.xlf:flushGeneralCachesDescription')),
76 'href' => BackendUtility
::getModuleUrl('tce_db', ['vC' => $backendUser->veriCode(), 'cacheCmd' => 'all', 'ajaxCall' => 1]),
77 'icon' => $this->iconFactory
->getIcon('actions-system-cache-clear-impact-medium', Icon
::SIZE_SMALL
)->render()
79 $this->optionValues
[] = 'all';
82 // Clearing of system cache (core cache, class cache etc)
83 // is only shown explicitly if activated for a BE-user (not activated for admins by default)
84 // or if the system runs in development mode
85 // or if $GLOBALS['TYPO3_CONF_VARS']['SYS']['clearCacheSystem'] is set (only for admins)
86 if ($backendUser->getTSConfigVal('options.clearCache.system') || GeneralUtility
::getApplicationContext()->isDevelopment()
87 ||
((bool)$GLOBALS['TYPO3_CONF_VARS']['SYS']['clearCacheSystem'] === true && $backendUser->isAdmin())) {
88 $this->cacheActions
[] = array(
90 'title' => htmlspecialchars($languageService->sL('LLL:EXT:lang/locallang_core.xlf:flushSystemCachesTitle')),
91 'description' => htmlspecialchars($languageService->sL('LLL:EXT:lang/locallang_core.xlf:flushSystemCachesDescription')),
92 'href' => BackendUtility
::getModuleUrl('tce_db', ['vC' => $backendUser->veriCode(), 'cacheCmd' => 'system', 'ajaxCall' => 1]),
93 'icon' => $this->iconFactory
->getIcon('actions-system-cache-clear-impact-high', Icon
::SIZE_SMALL
)->render()
95 $this->optionValues
[] = 'system';
98 // Hook for manipulating cacheActions
99 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['additionalBackendItems']['cacheActions'])) {
100 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['additionalBackendItems']['cacheActions'] as $cacheAction) {
101 $hookObject = GeneralUtility
::getUserObj($cacheAction);
102 if (!$hookObject instanceof ClearCacheActionsHookInterface
) {
103 throw new \
UnexpectedValueException('$hookObject must implement interface ' . ClearCacheActionsHookInterface
::class, 1228262000);
105 $hookObject->manipulateCacheActions($this->cacheActions
, $this->optionValues
);
111 * Checks whether the user has access to this toolbar item
113 * @return bool TRUE if user has access, FALSE if not
115 public function checkAccess()
117 $backendUser = $this->getBackendUser();
118 if ($backendUser->isAdmin()) {
121 if (is_array($this->optionValues
)) {
122 foreach ($this->optionValues
as $value) {
123 if ($backendUser->getTSConfigVal('options.clearCache.' . $value)) {
132 * Render clear cache icon
134 * @return string Icon HTML
136 public function getItem()
138 $title = htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:rm.clearCache_clearCache'));
139 return '<span title="' . $title . '">'
140 . $this->iconFactory
->getIcon('apps-toolbar-menu-cache', Icon
::SIZE_SMALL
)->render('inline')
147 * @return string Drop down HTML
149 public function getDropDown()
152 $result[] = '<ul class="dropdown-list">';
153 foreach ($this->cacheActions
as $cacheAction) {
154 $title = $cacheAction['description'] ?
: $cacheAction['title'];
156 $result[] = '<a class="dropdown-list-link" href="' . htmlspecialchars($cacheAction['href']) . '" title="' . htmlspecialchars($title) . '">';
157 $result[] = $cacheAction['icon'] . ' ' . htmlspecialchars($cacheAction['title']);
162 return implode(LF
, $result);
166 * No additional attributes needed.
170 public function getAdditionalAttributes()
176 * This item has a drop down
180 public function hasDropDown()
186 * Position relative to others
190 public function getIndex()
196 * Returns the current BE user.
198 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
200 protected function getBackendUser()
202 return $GLOBALS['BE_USER'];
206 * Returns current PageRenderer
208 * @return PageRenderer
210 protected function getPageRenderer()
212 return GeneralUtility
::makeInstance(PageRenderer
::class);
216 * Returns LanguageService
218 * @return \TYPO3\CMS\Lang\LanguageService
220 protected function getLanguageService()
222 return $GLOBALS['LANG'];