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