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