TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
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 
25 
30 {
34  protected $cacheActions = [];
35 
39  protected $optionValues = [];
40 
44  protected $iconFactory;
45 
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/Resources/Private/Language/locallang_core.xlf:flushPageCachesTitle')),
64  'description' => htmlspecialchars($languageService->sL('LLL:EXT:lang/Resources/Private/Language/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/Resources/Private/Language/locallang_core.xlf:flushAllCachesTitle2')),
78  'description' => htmlspecialchars($languageService->sL('LLL:EXT:lang/Resources/Private/Language/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 
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 
123  public function getItem()
124  {
125  if ($this->hasDropDown()) {
126  $templateReference = 'ClearCacheToolbarItem.html';
127  $icon = $this->iconFactory->getIcon('apps-toolbar-menu-cache', Icon::SIZE_SMALL)->render('inline');
128  $variables = [
129  'title' => 'LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:rm.clearCache_clearCache',
130  'icon' => $icon
131  ];
132  } else {
133  $templateReference = 'ClearCacheToolbarItemSingle.html';
134  $cacheAction = end($this->cacheActions);
135  $variables['link'] = $cacheAction['href'];
136  $variables['title'] = $cacheAction['title'];
137  $variables['icon'] = $cacheAction['icon'];
138  }
139  $view = $this->getFluidTemplateObject($templateReference);
140  $view->assignMultiple($variables);
141 
142  return $view->render();
143  }
144 
150  public function getDropDown()
151  {
152  $view = $this->getFluidTemplateObject('ClearCacheToolbarItemDropDown.html');
153  $view->assignMultiple([
154  'title' => 'LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:rm.clearCache_clearCache',
155  'cacheActions' => $this->cacheActions,
156  ]
157  );
158 
159  return $view->render();
160  }
161 
167  public function getAdditionalAttributes()
168  {
169  return [];
170  }
171 
177  public function hasDropDown()
178  {
179  return count($this->cacheActions) > 1;
180  }
181 
187  public function getIndex()
188  {
189  return 25;
190  }
191 
197  protected function getBackendUser()
198  {
199  return $GLOBALS['BE_USER'];
200  }
201 
207  protected function getPageRenderer()
208  {
209  return GeneralUtility::makeInstance(PageRenderer::class);
210  }
211 
217  protected function getLanguageService()
218  {
219  return $GLOBALS['LANG'];
220  }
221 
229  protected function getFluidTemplateObject(string $filename):StandaloneView
230  {
232  $view = GeneralUtility::makeInstance(StandaloneView::class);
233  $view->setLayoutRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Layouts')]);
234  $view->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Partials/ToolbarItems')]);
235  $view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates/ToolbarItems')]);
236 
237  $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates/ToolbarItems/' . $filename));
238 
239  $view->getRequest()->setControllerExtensionName('Backend');
240  return $view;
241  }
242 }
if(TYPO3_MODE=== 'BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
static makeInstance($className,...$constructorArguments)
static getFileAbsFileName($filename, $_=null, $_2=null)