162f98043353f78c937af210529fa4f35ed1b21e
[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', 'ajaxCall' => 1]),
65 'icon' => $this->iconFactory->getIcon('actions-system-cache-clear-impact-low', Icon::SIZE_SMALL)->render()
66 );
67 $this->optionValues[] = 'pages';
68 }
69
70 // Clear cache for ALL tables!
71 if ($backendUser->isAdmin() || $backendUser->getTSConfigVal('options.clearCache.all')) {
72 $this->cacheActions[] = array(
73 'id' => 'all',
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()
78 );
79 $this->optionValues[] = 'all';
80 }
81
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(
89 'id' => 'system',
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()
94 );
95 $this->optionValues[] = 'system';
96 }
97
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);
104 }
105 $hookObject->manipulateCacheActions($this->cacheActions, $this->optionValues);
106 }
107 }
108 }
109
110 /**
111 * Checks whether the user has access to this toolbar item
112 *
113 * @return bool TRUE if user has access, FALSE if not
114 */
115 public function checkAccess()
116 {
117 $backendUser = $this->getBackendUser();
118 if ($backendUser->isAdmin()) {
119 return true;
120 }
121 if (is_array($this->optionValues)) {
122 foreach ($this->optionValues as $value) {
123 if ($backendUser->getTSConfigVal('options.clearCache.' . $value)) {
124 return true;
125 }
126 }
127 }
128 return false;
129 }
130
131 /**
132 * Render clear cache icon
133 *
134 * @return string Icon HTML
135 */
136 public function getItem()
137 {
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')
141 . '</span>';
142 }
143
144 /**
145 * Render drop down
146 *
147 * @return string Drop down HTML
148 */
149 public function getDropDown()
150 {
151 $result = array();
152 $result[] = '<ul class="dropdown-list">';
153 foreach ($this->cacheActions as $cacheAction) {
154 $title = $cacheAction['description'] ?: $cacheAction['title'];
155 $result[] = '<li>';
156 $result[] = '<a class="dropdown-list-link" href="' . htmlspecialchars($cacheAction['href']) . '" title="' . htmlspecialchars($title) . '">';
157 $result[] = $cacheAction['icon'] . ' ' . htmlspecialchars($cacheAction['title']);
158 $result[] = '</a>';
159 $result[] = '</li>';
160 }
161 $result[] = '</ul>';
162 return implode(LF, $result);
163 }
164
165 /**
166 * No additional attributes needed.
167 *
168 * @return array
169 */
170 public function getAdditionalAttributes()
171 {
172 return array();
173 }
174
175 /**
176 * This item has a drop down
177 *
178 * @return bool
179 */
180 public function hasDropDown()
181 {
182 return true;
183 }
184
185 /**
186 * Position relative to others
187 *
188 * @return int
189 */
190 public function getIndex()
191 {
192 return 25;
193 }
194
195 /**
196 * Returns the current BE user.
197 *
198 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
199 */
200 protected function getBackendUser()
201 {
202 return $GLOBALS['BE_USER'];
203 }
204
205 /**
206 * Returns current PageRenderer
207 *
208 * @return PageRenderer
209 */
210 protected function getPageRenderer()
211 {
212 return GeneralUtility::makeInstance(PageRenderer::class);
213 }
214
215 /**
216 * Returns LanguageService
217 *
218 * @return \TYPO3\CMS\Lang\LanguageService
219 */
220 protected function getLanguageService()
221 {
222 return $GLOBALS['LANG'];
223 }
224 }