8b61163f111ec236b477db6b9027ffcfb44d5e93
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Backend / ToolbarItems / ClearCacheToolbarItem.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 TYPO3\CMS\Backend\Toolbar\ToolbarItemInterface;
18 use TYPO3\CMS\Backend\Toolbar\ClearCacheActionsHookInterface;
19 use TYPO3\CMS\Backend\Utility\BackendUtility;
20 use TYPO3\CMS\Backend\Utility\IconUtility;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Core\Page\PageRenderer;
23
24 /**
25 * Render cache clearing toolbar item
26 */
27 class ClearCacheToolbarItem implements ToolbarItemInterface {
28
29 /**
30 * @var array
31 */
32 protected $cacheActions = array();
33
34 /**
35 * @var array
36 */
37 protected $optionValues = array();
38
39 /**
40 * Constructor
41 *
42 * @throws \UnexpectedValueException
43 */
44 public function __construct() {
45 $backendUser = $this->getBackendUser();
46 $languageService = $this->getLanguageService();
47
48 $this->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/Toolbar/ClearCacheMenu');
49
50 // Clear all page-related caches
51 if ($backendUser->isAdmin() || $backendUser->getTSConfigVal('options.clearCache.pages')) {
52 $this->cacheActions[] = array(
53 'id' => 'pages',
54 'title' => $languageService->sL('LLL:EXT:lang/locallang_core.xlf:flushPageCachesTitle', TRUE),
55 'description' => $languageService->sL('LLL:EXT:lang/locallang_core.xlf:flushPageCachesDescription', TRUE),
56 'href' => BackendUtility::getModuleUrl('tce_db', ['vC' => $backendUser->veriCode(), 'cacheCmd' => 'pages', 'ajaxCall' => 1]) . BackendUtility::getUrlToken('tceAction'),
57 'icon' => IconUtility::getSpriteIcon('actions-system-cache-clear-impact-low')
58 );
59 $this->optionValues[] = 'pages';
60 }
61
62 // Clear cache for ALL tables!
63 if ($backendUser->isAdmin() || $backendUser->getTSConfigVal('options.clearCache.all')) {
64 $this->cacheActions[] = array(
65 'id' => 'all',
66 'title' => $languageService->sL('LLL:EXT:lang/locallang_core.xlf:flushGeneralCachesTitle', TRUE),
67 'description' => $languageService->sL('LLL:EXT:lang/locallang_core.xlf:flushGeneralCachesDescription', TRUE),
68 'href' => BackendUtility::getModuleUrl('tce_db', ['vC' => $backendUser->veriCode(), 'cacheCmd' => 'all', 'ajaxCall' => 1]) . BackendUtility::getUrlToken('tceAction'),
69 'icon' => IconUtility::getSpriteIcon('actions-system-cache-clear-impact-medium')
70 );
71 $this->optionValues[] = 'all';
72 }
73
74 // Clearing of system cache (core cache, class cache etc)
75 // is only shown explicitly if activated for a BE-user (not activated for admins by default)
76 // or if the system runs in development mode
77 // or if $GLOBALS['TYPO3_CONF_VARS']['SYS']['clearCacheSystem'] is set (only for admins)
78 if ($backendUser->getTSConfigVal('options.clearCache.system') || GeneralUtility::getApplicationContext()->isDevelopment()
79 || ((bool)$GLOBALS['TYPO3_CONF_VARS']['SYS']['clearCacheSystem'] === TRUE && $backendUser->isAdmin())) {
80 $this->cacheActions[] = array(
81 'id' => 'system',
82 'title' => $languageService->sL('LLL:EXT:lang/locallang_core.xlf:flushSystemCachesTitle', TRUE),
83 'description' => $languageService->sL('LLL:EXT:lang/locallang_core.xlf:flushSystemCachesDescription', TRUE),
84 'href' => BackendUtility::getModuleUrl('tce_db', ['vC' => $backendUser->veriCode(), 'cacheCmd' => 'system', 'ajaxCall' => 1]) . BackendUtility::getUrlToken('tceAction'),
85 'icon' => IconUtility::getSpriteIcon('actions-system-cache-clear-impact-high')
86 );
87 $this->optionValues[] = 'system';
88 }
89
90 // Hook for manipulating cacheActions
91 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['additionalBackendItems']['cacheActions'])) {
92 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['additionalBackendItems']['cacheActions'] as $cacheAction) {
93 $hookObject = GeneralUtility::getUserObj($cacheAction);
94 if (!$hookObject instanceof ClearCacheActionsHookInterface) {
95 throw new \UnexpectedValueException('$hookObject must implement interface ' . ClearCacheActionsHookInterface::class, 1228262000);
96 }
97 $hookObject->manipulateCacheActions($this->cacheActions, $this->optionValues);
98 }
99 }
100 }
101
102 /**
103 * Checks whether the user has access to this toolbar item
104 *
105 * @return bool TRUE if user has access, FALSE if not
106 */
107 public function checkAccess() {
108 $backendUser = $this->getBackendUser();
109 if ($backendUser->isAdmin()) {
110 return TRUE;
111 }
112 if (is_array($this->optionValues)) {
113 foreach ($this->optionValues as $value) {
114 if ($backendUser->getTSConfigVal('options.clearCache.' . $value)) {
115 return TRUE;
116 }
117 }
118 }
119 return FALSE;
120 }
121
122 /**
123 * Render clear cache icon
124 *
125 * @return string Icon HTML
126 */
127 public function getItem() {
128 $title = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:rm.clearCache_clearCache', TRUE);
129 return IconUtility::getSpriteIcon('apps-toolbar-menu-cache', array('title' => $title));
130 }
131
132 /**
133 * Render drop down
134 *
135 * @return string Drop down HTML
136 */
137 public function getDropDown() {
138 $result = array();
139 $result[] = '<ul class="dropdown-list">';
140 foreach ($this->cacheActions as $cacheAction) {
141 $title = $cacheAction['description'] ?: $cacheAction['title'];
142 $result[] = '<li>';
143 $result[] = '<a class="dropdown-list-link" href="' . htmlspecialchars($cacheAction['href']) . '" title="' . htmlspecialchars($title) . '">';
144 $result[] = $cacheAction['icon'] . ' ' . htmlspecialchars($cacheAction['title']);
145 $result[] = '</a>';
146 $result[] = '</li>';
147 }
148 $result[] = '</ul>';
149 return implode(LF, $result);
150 }
151
152 /**
153 * No additional attributes needed.
154 *
155 * @return array
156 */
157 public function getAdditionalAttributes() {
158 return array();
159 }
160
161 /**
162 * This item has a drop down
163 *
164 * @return bool
165 */
166 public function hasDropDown() {
167 return TRUE;
168 }
169
170 /**
171 * Position relative to others
172 *
173 * @return int
174 */
175 public function getIndex() {
176 return 25;
177 }
178
179 /**
180 * Returns the current BE user.
181 *
182 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
183 */
184 protected function getBackendUser() {
185 return $GLOBALS['BE_USER'];
186 }
187
188 /**
189 * Returns current PageRenderer
190 *
191 * @return PageRenderer
192 */
193 protected function getPageRenderer() {
194 return GeneralUtility::makeInstance(PageRenderer::class);
195 }
196
197 /**
198 * Returns LanguageService
199 *
200 * @return \TYPO3\CMS\Lang\LanguageService
201 */
202 protected function getLanguageService() {
203 return $GLOBALS['LANG'];
204 }
205
206 }