TYPO3  7.6
ClearCacheToolbarItem.php
Go to the documentation of this file.
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 
24 
29 {
33  protected $cacheActions = array();
34 
38  protected $optionValues = array();
39 
43  protected $iconFactory;
44 
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' => $languageService->sL('LLL:EXT:lang/locallang_core.xlf:flushPageCachesTitle', true),
63  'description' => $languageService->sL('LLL:EXT:lang/locallang_core.xlf:flushPageCachesDescription', true),
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' => $languageService->sL('LLL:EXT:lang/locallang_core.xlf:flushGeneralCachesTitle', true),
75  'description' => $languageService->sL('LLL:EXT:lang/locallang_core.xlf:flushGeneralCachesDescription', true),
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' => $languageService->sL('LLL:EXT:lang/locallang_core.xlf:flushSystemCachesTitle', true),
91  'description' => $languageService->sL('LLL:EXT:lang/locallang_core.xlf:flushSystemCachesDescription', true),
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 
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 
136  public function getItem()
137  {
138  $title = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:rm.clearCache_clearCache', true);
139  return '<span title="' . $title . '">'
140  . $this->iconFactory->getIcon('apps-toolbar-menu-cache', Icon::SIZE_SMALL)->render('inline')
141  . '</span>';
142  }
143 
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 
170  public function getAdditionalAttributes()
171  {
172  return array();
173  }
174 
180  public function hasDropDown()
181  {
182  return true;
183  }
184 
190  public function getIndex()
191  {
192  return 25;
193  }
194 
200  protected function getBackendUser()
201  {
202  return $GLOBALS['BE_USER'];
203  }
204 
210  protected function getPageRenderer()
211  {
212  return GeneralUtility::makeInstance(PageRenderer::class);
213  }
214 
220  protected function getLanguageService()
221  {
222  return $GLOBALS['LANG'];
223  }
224 }