TYPO3 CMS  TYPO3_8-7
ClearCacheToolbarItem.php
Go to the documentation of this file.
1 <?php
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 
32 {
36  protected $cacheActions = [];
37 
41  protected $optionValues = [];
42 
46  public function __construct()
47  {
48  $this->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/Toolbar/ClearCacheMenu');
49  $backendUser = $this->getBackendUser();
50 
51  // Clear all page-related caches
52  if ($backendUser->isAdmin() || $backendUser->getTSConfigVal('options.clearCache.pages')) {
53  $this->cacheActions[] = [
54  'id' => 'pages',
55  'title' => 'LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:flushPageCachesTitle',
56  'description' => 'LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:flushPageCachesDescription',
57  'href' => BackendUtility::getModuleUrl('tce_db', ['cacheCmd' => 'pages']),
58  'iconIdentifier' => 'actions-system-cache-clear-impact-low'
59  ];
60  $this->optionValues[] = 'pages';
61  }
62 
63  // Clearing of all caches is only shown if explicitly enabled via TSConfig
64  // or if BE-User is admin and the TSconfig explicitly disables the possibility for admins.
65  // This is useful for big production systems where admins accidentally could slow down the system.
66  if ($backendUser->getTSConfigVal('options.clearCache.all') || ($backendUser->isAdmin() && $backendUser->getTSConfigVal('options.clearCache.all') !== '0')) {
67  $this->cacheActions[] = [
68  'id' => 'all',
69  'title' => 'LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:flushAllCachesTitle2',
70  'description' => 'LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:flushAllCachesDescription2',
71  'href' => BackendUtility::getModuleUrl('tce_db', ['cacheCmd' => 'all']),
72  'iconIdentifier' => 'actions-system-cache-clear-impact-high'
73  ];
74  $this->optionValues[] = 'all';
75  }
76 
77  // Hook for manipulating cacheActions
78  if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['additionalBackendItems']['cacheActions'])) {
79  foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['additionalBackendItems']['cacheActions'] as $cacheAction) {
80  $hookObject = GeneralUtility::getUserObj($cacheAction);
81  if (!$hookObject instanceof ClearCacheActionsHookInterface) {
82  throw new \UnexpectedValueException($cacheAction . ' must implement interface ' . ClearCacheActionsHookInterface::class, 1228262000);
83  }
84  $hookObject->manipulateCacheActions($this->cacheActions, $this->optionValues);
85  }
86  }
87  }
88 
94  public function checkAccess()
95  {
96  $backendUser = $this->getBackendUser();
97  if ($backendUser->isAdmin()) {
98  return true;
99  }
100  foreach ($this->optionValues as $value) {
101  if ($backendUser->getTSConfigVal('options.clearCache.' . $value)) {
102  return true;
103  }
104  }
105  return false;
106  }
107 
113  public function getItem()
114  {
115  if ($this->hasDropDown()) {
116  return $this->getFluidTemplateObject('ClearCacheToolbarItem.html')->render();
117  }
118  $view = $this->getFluidTemplateObject('ClearCacheToolbarItemSingle.html');
119  $cacheAction = end($this->cacheActions);
120  $view->assignMultiple([
121  'link' => $cacheAction['href'],
122  'title' => $cacheAction['title'],
123  'iconIdentifier' => $cacheAction['iconIdentifier'],
124  ]);
125  return $view->render();
126  }
127 
133  public function getDropDown()
134  {
135  $view = $this->getFluidTemplateObject('ClearCacheToolbarItemDropDown.html');
136  $view->assign('cacheActions', $this->cacheActions);
137  return $view->render();
138  }
139 
145  public function getAdditionalAttributes()
146  {
147  return [];
148  }
149 
155  public function hasDropDown()
156  {
157  return count($this->cacheActions) > 1;
158  }
159 
165  public function getIndex()
166  {
167  return 25;
168  }
169 
175  protected function getBackendUser()
176  {
177  return $GLOBALS['BE_USER'];
178  }
179 
183  protected function getPageRenderer()
184  {
185  return GeneralUtility::makeInstance(PageRenderer::class);
186  }
187 
194  protected function getFluidTemplateObject(string $filename): StandaloneView
195  {
196  $view = GeneralUtility::makeInstance(StandaloneView::class);
197  $view->setLayoutRootPaths(['EXT:backend/Resources/Private/Layouts']);
198  $view->setPartialRootPaths(['EXT:backend/Resources/Private/Partials/ToolbarItems']);
199  $view->setTemplateRootPaths(['EXT:backend/Resources/Private/Templates/ToolbarItems']);
200 
201  $view->setTemplate($filename);
202 
203  $view->getRequest()->setControllerExtensionName('Backend');
204  return $view;
205  }
206 }
static makeInstance($className,... $constructorArguments)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']