[TASK] Fluidification of ClearCacheToolbarItem
[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 use TYPO3\CMS\Fluid\View\StandaloneView;
25
26 /**
27 * Render cache clearing toolbar item
28 */
29 class ClearCacheToolbarItem implements ToolbarItemInterface
30 {
31 /**
32 * @var array
33 */
34 protected $cacheActions = [];
35
36 /**
37 * @var array
38 */
39 protected $optionValues = [];
40
41 /**
42 * @var IconFactory
43 */
44 protected $iconFactory;
45
46 /**
47 * Constructor
48 *
49 * @throws \UnexpectedValueException
50 */
51 public function __construct()
52 {
53 $backendUser = $this->getBackendUser();
54 $languageService = $this->getLanguageService();
55 $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
56
57 $this->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/Toolbar/ClearCacheMenu');
58
59 // Clear all page-related caches
60 if ($backendUser->isAdmin() || $backendUser->getTSConfigVal('options.clearCache.pages')) {
61 $this->cacheActions[] = [
62 'id' => 'pages',
63 'title' => htmlspecialchars($languageService->sL('LLL:EXT:lang/locallang_core.xlf:flushPageCachesTitle')),
64 'description' => htmlspecialchars($languageService->sL('LLL:EXT:lang/locallang_core.xlf:flushPageCachesDescription')),
65 'href' => BackendUtility::getModuleUrl('tce_db', ['vC' => $backendUser->veriCode(), 'cacheCmd' => 'pages']),
66 'icon' => $this->iconFactory->getIcon('actions-system-cache-clear-impact-low', Icon::SIZE_SMALL)->render()
67 ];
68 $this->optionValues[] = 'pages';
69 }
70
71 // Clearing of all caches is only shown if explicitly enabled via TSConfig
72 // or if BE-User is admin and the TSconfig explicitly disables the possibility for admins.
73 // This is useful for big production systems where admins accidentally could slow down the system.
74 if ($backendUser->getTSConfigVal('options.clearCache.all') || ($backendUser->isAdmin() && $backendUser->getTSConfigVal('options.clearCache.all') !== '0')) {
75 $this->cacheActions[] = [
76 'id' => 'all',
77 'title' => htmlspecialchars($languageService->sL('LLL:EXT:lang/locallang_core.xlf:flushAllCachesTitle2')),
78 'description' => htmlspecialchars($languageService->sL('LLL:EXT:lang/locallang_core.xlf:flushAllCachesDescription2')),
79 'href' => BackendUtility::getModuleUrl('tce_db', ['vC' => $backendUser->veriCode(), 'cacheCmd' => 'all']),
80 'icon' => $this->iconFactory->getIcon('actions-system-cache-clear-impact-high', Icon::SIZE_SMALL)->render()
81 ];
82 $this->optionValues[] = 'all';
83 }
84
85 // Hook for manipulating cacheActions
86 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['additionalBackendItems']['cacheActions'])) {
87 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['additionalBackendItems']['cacheActions'] as $cacheAction) {
88 $hookObject = GeneralUtility::getUserObj($cacheAction);
89 if (!$hookObject instanceof ClearCacheActionsHookInterface) {
90 throw new \UnexpectedValueException($cacheAction . ' must implement interface ' . ClearCacheActionsHookInterface::class, 1228262000);
91 }
92 $hookObject->manipulateCacheActions($this->cacheActions, $this->optionValues);
93 }
94 }
95 }
96
97 /**
98 * Checks whether the user has access to this toolbar item
99 *
100 * @return bool TRUE if user has access, FALSE if not
101 */
102 public function checkAccess()
103 {
104 $backendUser = $this->getBackendUser();
105 if ($backendUser->isAdmin()) {
106 return true;
107 }
108 if (is_array($this->optionValues)) {
109 foreach ($this->optionValues as $value) {
110 if ($backendUser->getTSConfigVal('options.clearCache.' . $value)) {
111 return true;
112 }
113 }
114 }
115 return false;
116 }
117
118 /**
119 * Render clear cache icon
120 *
121 * @return string Icon HTML
122 */
123 public function getItem()
124 {
125 $icon = $this->iconFactory->getIcon('apps-toolbar-menu-cache', Icon::SIZE_SMALL)->render('inline');
126
127 $view = $this->getFluidTemplateObject('ClearCacheToolbarItem.html');
128 $view->assignMultiple([
129 'title' => 'LLL:EXT:lang/locallang_core.xlf:rm.clearCache_clearCache',
130 'icon' => $icon
131 ]
132 );
133
134 return $view->render();
135 }
136
137 /**
138 * Render drop down
139 *
140 * @return string Drop down HTML
141 */
142 public function getDropDown()
143 {
144 $view = $this->getFluidTemplateObject('ClearCacheToolbarItemDropDown.html');
145 $view->assignMultiple([
146 'title' => 'LLL:EXT:lang/locallang_core.xlf:rm.clearCache_clearCache',
147 'cacheActions' => $this->cacheActions,
148 ]
149 );
150
151 return $view->render();
152 }
153
154 /**
155 * No additional attributes needed.
156 *
157 * @return array
158 */
159 public function getAdditionalAttributes()
160 {
161 return [];
162 }
163
164 /**
165 * This item has a drop down
166 *
167 * @return bool
168 */
169 public function hasDropDown()
170 {
171 return true;
172 }
173
174 /**
175 * Position relative to others
176 *
177 * @return int
178 */
179 public function getIndex()
180 {
181 return 25;
182 }
183
184 /**
185 * Returns the current BE user.
186 *
187 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
188 */
189 protected function getBackendUser()
190 {
191 return $GLOBALS['BE_USER'];
192 }
193
194 /**
195 * Returns current PageRenderer
196 *
197 * @return PageRenderer
198 */
199 protected function getPageRenderer()
200 {
201 return GeneralUtility::makeInstance(PageRenderer::class);
202 }
203
204 /**
205 * Returns LanguageService
206 *
207 * @return \TYPO3\CMS\Lang\LanguageService
208 */
209 protected function getLanguageService()
210 {
211 return $GLOBALS['LANG'];
212 }
213
214 /**
215 * Returns a new standalone view, shorthand function
216 *
217 * @param string $filename Which templateFile should be used.
218 *
219 * @return StandaloneView
220 */
221 protected function getFluidTemplateObject(string $filename):StandaloneView
222 {
223 /** @var StandaloneView $view */
224 $view = GeneralUtility::makeInstance(StandaloneView::class);
225 $view->setLayoutRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Layouts')]);
226 $view->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Partials/ToolbarItems')]);
227 $view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates/ToolbarItems')]);
228
229 $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates/ToolbarItems/' . $filename));
230
231 $view->getRequest()->setControllerExtensionName('Backend');
232 return $view;
233 }
234 }