dd4c3360533e40f9cfac6c331f35a0e32fd71729
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Toolbar / ClearCacheToolbarItem.php
1 <?php
2 namespace TYPO3\CMS\Backend\Toolbar;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2007-2013 Ingo Renner <ingo@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the text file GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 use TYPO3\CMS\Backend\Utility\BackendUtility;
31 use TYPO3\CMS\Backend\Utility\IconUtility;
32 use TYPO3\CMS\Core\Utility\GeneralUtility;
33
34 /**
35 * class to render the menu for the cache clearing actions
36 *
37 * @author Ingo Renner <ingo@typo3.org>
38 */
39 class ClearCacheToolbarItem implements \TYPO3\CMS\Backend\Toolbar\ToolbarItemHookInterface {
40
41 /**
42 * @var array
43 */
44 protected $cacheActions;
45
46 /**
47 * @var array
48 */
49 protected $optionValues;
50
51 /**
52 * Reference back to the backend object
53 *
54 * @var TYPO3backend
55 */
56 protected $backendReference;
57
58 /**
59 * Constructor
60 *
61 * @param \TYPO3\CMS\Backend\Controller\BackendController $backendReference TYPO3 backend object reference
62 */
63 public function __construct(\TYPO3\CMS\Backend\Controller\BackendController &$backendReference = NULL) {
64 $this->backendReference = $backendReference;
65 $this->cacheActions = array();
66 $this->optionValues = array('all', 'pages');
67
68 // Clear all page-related caches
69 if ($GLOBALS['BE_USER']->isAdmin() || $GLOBALS['BE_USER']->getTSConfigVal('options.clearCache.pages')) {
70 $this->cacheActions[] = array(
71 'id' => 'pages',
72 'title' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:flushPageCachesTitle', TRUE),
73 'description' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:flushPageCachesDescription', TRUE),
74 'href' => $this->backPath . 'tce_db.php?vC=' . $GLOBALS['BE_USER']->veriCode() . '&cacheCmd=pages&ajaxCall=1' . BackendUtility::getUrlToken('tceAction'),
75 'icon' => IconUtility::getSpriteIcon('actions-system-cache-clear-impact-low')
76 );
77 }
78
79 // Clear cache for ALL tables!
80 if ($GLOBALS['BE_USER']->isAdmin() || $GLOBALS['BE_USER']->getTSConfigVal('options.clearCache.all')) {
81 $this->cacheActions[] = array(
82 'id' => 'all',
83 'title' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:flushAllCachesTitle', TRUE),
84 'description' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:flushAllCachesDescription', TRUE),
85 'href' => $this->backPath . 'tce_db.php?vC=' . $GLOBALS['BE_USER']->veriCode() . '&cacheCmd=all&ajaxCall=1' . BackendUtility::getUrlToken('tceAction'),
86 'icon' => IconUtility::getSpriteIcon('actions-system-cache-clear-impact-medium')
87 );
88 }
89
90 // Clearing of system cache (core cache, class cache etc)
91 // is only shown explicitly if activated for a BE-user (not activated for admins by default)
92 // or if the system runs in development mode
93 if ($GLOBALS['BE_USER']->getTSConfigVal('options.clearCache.system') || GeneralUtility::getApplicationContext()->isDevelopment()) {
94 $this->cacheActions[] = array(
95 'id' => 'system',
96 'title' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:flushSystemCachesTitle', TRUE),
97 'description' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:flushSystemCachesDescription', TRUE),
98 'href' => $this->backPath . 'tce_db.php?vC=' . $GLOBALS['BE_USER']->veriCode() . '&cacheCmd=system&ajaxCall=1' . BackendUtility::getUrlToken('tceAction'),
99 'icon' => IconUtility::getSpriteIcon('actions-system-cache-clear-impact-high')
100 );
101 }
102 // Hook for manipulate cacheActions
103 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['additionalBackendItems']['cacheActions'])) {
104 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['additionalBackendItems']['cacheActions'] as $cacheAction) {
105 $hookObject = GeneralUtility::getUserObj($cacheAction);
106 if (!$hookObject instanceof \TYPO3\CMS\Backend\Toolbar\ClearCacheActionsHookInterface) {
107 throw new \UnexpectedValueException('$hookObject must implement interface TYPO3\\CMS\\Backend\\Toolbar\\ClearCacheActionsHookInterface', 1228262000);
108 }
109 $hookObject->manipulateCacheActions($this->cacheActions, $this->optionValues);
110 }
111 }
112 }
113
114 /**
115 * Checks whether the user has access to this toolbar item
116 *
117 * @return boolean TRUE if user has access, FALSE if not
118 */
119 public function checkAccess() {
120 if ($GLOBALS['BE_USER']->isAdmin()) {
121 return TRUE;
122 }
123 if (is_array($this->optionValues)) {
124 foreach ($this->optionValues as $value) {
125 if ($GLOBALS['BE_USER']->getTSConfigVal('options.clearCache.' . $value)) {
126 return TRUE;
127 }
128 }
129 }
130 return FALSE;
131 }
132
133 /**
134 * Creates the selector for workspaces
135 *
136 * @return string Workspace selector as HTML select
137 */
138 public function render() {
139 $title = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:rm.clearCache_clearCache', TRUE);
140 $this->addJavascriptToBackend();
141 $cacheMenu = array();
142 $cacheMenu[] = '<a href="#" class="toolbar-item">' . IconUtility::getSpriteIcon('apps-toolbar-menu-cache', array('title' => $title)) . '</a>';
143 $cacheMenu[] = '<ul class="toolbar-item-menu" style="display: none;">';
144 foreach ($this->cacheActions as $actionKey => $cacheAction) {
145 $cacheMenu[] = '<li><a href="' . htmlspecialchars($cacheAction['href']) . '" title="' . htmlspecialchars($cacheAction['description'] ?: $cacheAction['title']) . '">' . $cacheAction['icon'] . ' ' . htmlspecialchars($cacheAction['title']) . '</a></li>';
146 }
147 $cacheMenu[] = '</ul>';
148 return implode(LF, $cacheMenu);
149 }
150
151 /**
152 * Adds the necessary JavaScript to the backend
153 *
154 * @return void
155 */
156 protected function addJavascriptToBackend() {
157 $this->backendReference->addJavascriptFile('sysext/backend/Resources/Public/JavaScript/clearcachemenu.js');
158 }
159
160 /**
161 * Returns additional attributes for the list item in the toolbar
162 *
163 * @return string List item HTML attributes
164 */
165 public function getAdditionalAttributes() {
166 return ' id="clear-cache-actions-menu"';
167 }
168
169 }