[BUGFIX] Clear cache inconsistency in toolbar
[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 all caches is only shown if explicitly enabled via TSConfig
71 // or if BE-User is admin and the TSconfig explicitly disables the possibility for admins.
72 // This is useful for big production systems where admins accidentally could slow down the system.
73 if ($backendUser->getTSConfigVal('options.clearCache.all') || ($backendUser->isAdmin() && $backendUser->getTSConfigVal('options.clearCache.all') !== '0')) {
74 $this->cacheActions[] = array(
75 'id' => 'all',
76 'title' => htmlspecialchars($languageService->sL('LLL:EXT:lang/locallang_core.xlf:flushAllCachesTitle2')),
77 'description' => htmlspecialchars($languageService->sL('LLL:EXT:lang/locallang_core.xlf:flushAllCachesDescription2')),
78 'href' => BackendUtility::getModuleUrl('tce_db', ['vC' => $backendUser->veriCode(), 'cacheCmd' => 'all']),
79 'icon' => $this->iconFactory->getIcon('actions-system-cache-clear-impact-high', Icon::SIZE_SMALL)->render()
80 );
81 $this->optionValues[] = 'all';
82 }
83
84 // Hook for manipulating cacheActions
85 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['additionalBackendItems']['cacheActions'])) {
86 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['additionalBackendItems']['cacheActions'] as $cacheAction) {
87 $hookObject = GeneralUtility::getUserObj($cacheAction);
88 if (!$hookObject instanceof ClearCacheActionsHookInterface) {
89 throw new \UnexpectedValueException('$hookObject must implement interface ' . ClearCacheActionsHookInterface::class, 1228262000);
90 }
91 $hookObject->manipulateCacheActions($this->cacheActions, $this->optionValues);
92 }
93 }
94 }
95
96 /**
97 * Checks whether the user has access to this toolbar item
98 *
99 * @return bool TRUE if user has access, FALSE if not
100 */
101 public function checkAccess()
102 {
103 $backendUser = $this->getBackendUser();
104 if ($backendUser->isAdmin()) {
105 return true;
106 }
107 if (is_array($this->optionValues)) {
108 foreach ($this->optionValues as $value) {
109 if ($backendUser->getTSConfigVal('options.clearCache.' . $value)) {
110 return true;
111 }
112 }
113 }
114 return false;
115 }
116
117 /**
118 * Render clear cache icon
119 *
120 * @return string Icon HTML
121 */
122 public function getItem()
123 {
124 $title = htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:rm.clearCache_clearCache'));
125 return '<span title="' . $title . '">'
126 . $this->iconFactory->getIcon('apps-toolbar-menu-cache', Icon::SIZE_SMALL)->render('inline')
127 . '</span>';
128 }
129
130 /**
131 * Render drop down
132 *
133 * @return string Drop down HTML
134 */
135 public function getDropDown()
136 {
137 $result = array();
138 $result[] = '<ul class="dropdown-list">';
139 foreach ($this->cacheActions as $cacheAction) {
140 $title = $cacheAction['description'] ?: $cacheAction['title'];
141 $result[] = '<li>';
142 $result[] = '<a class="dropdown-list-link" href="' . htmlspecialchars($cacheAction['href']) . '">';
143 $result[] = $cacheAction['icon'] . ' ' . htmlspecialchars($cacheAction['title']);
144 $result[] = '<br/><small>' . htmlspecialchars($title) . '</small>';
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 {
159 return array();
160 }
161
162 /**
163 * This item has a drop down
164 *
165 * @return bool
166 */
167 public function hasDropDown()
168 {
169 return true;
170 }
171
172 /**
173 * Position relative to others
174 *
175 * @return int
176 */
177 public function getIndex()
178 {
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 {
189 return $GLOBALS['BE_USER'];
190 }
191
192 /**
193 * Returns current PageRenderer
194 *
195 * @return PageRenderer
196 */
197 protected function getPageRenderer()
198 {
199 return GeneralUtility::makeInstance(PageRenderer::class);
200 }
201
202 /**
203 * Returns LanguageService
204 *
205 * @return \TYPO3\CMS\Lang\LanguageService
206 */
207 protected function getLanguageService()
208 {
209 return $GLOBALS['LANG'];
210 }
211 }