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