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