[BUGFIX] Invalid HTML for the top toolbar in Backend
[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 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 \TYPO3\CMS\Backend\Controller\BackendController
55 */
56 protected $backendReference;
57
58 /**
59 * TODO potentially unused
60 * @var string
61 */
62 public $backPath = '';
63
64 /**
65 * Constructor
66 *
67 * @param \TYPO3\CMS\Backend\Controller\BackendController $backendReference TYPO3 backend object reference
68 * @throws \UnexpectedValueException
69 */
70 public function __construct(\TYPO3\CMS\Backend\Controller\BackendController &$backendReference = NULL) {
71 $this->backendReference = $backendReference;
72 $this->cacheActions = array();
73 $this->optionValues = array();
74 $backendUser = $this->getBackendUser();
75
76 // Clear all page-related caches
77 if ($backendUser->isAdmin() || $backendUser->getTSConfigVal('options.clearCache.pages')) {
78 $this->cacheActions[] = array(
79 'id' => 'pages',
80 'title' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:flushPageCachesTitle', TRUE),
81 'description' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:flushPageCachesDescription', TRUE),
82 'href' => $this->backPath . 'tce_db.php?vC=' . $backendUser->veriCode() . '&cacheCmd=pages&ajaxCall=1' . BackendUtility::getUrlToken('tceAction'),
83 'icon' => IconUtility::getSpriteIcon('actions-system-cache-clear-impact-low')
84 );
85 $this->optionValues[] = 'pages';
86 }
87
88 // Clear cache for ALL tables!
89 if ($backendUser->isAdmin() || $backendUser->getTSConfigVal('options.clearCache.all')) {
90 $this->cacheActions[] = array(
91 'id' => 'all',
92 'title' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:flushGeneralCachesTitle', TRUE),
93 'description' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:flushGeneralCachesDescription', TRUE),
94 'href' => $this->backPath . 'tce_db.php?vC=' . $backendUser->veriCode() . '&cacheCmd=all&ajaxCall=1' . BackendUtility::getUrlToken('tceAction'),
95 'icon' => IconUtility::getSpriteIcon('actions-system-cache-clear-impact-medium')
96 );
97 $this->optionValues[] = 'all';
98 }
99
100 // Clearing of system cache (core cache, class cache etc)
101 // is only shown explicitly if activated for a BE-user (not activated for admins by default)
102 // or if the system runs in development mode
103 if ($backendUser->getTSConfigVal('options.clearCache.system') || GeneralUtility::getApplicationContext()->isDevelopment()) {
104 $this->cacheActions[] = array(
105 'id' => 'system',
106 'title' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:flushSystemCachesTitle', TRUE),
107 'description' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:flushSystemCachesDescription', TRUE),
108 'href' => $this->backPath . 'tce_db.php?vC=' . $backendUser->veriCode() . '&cacheCmd=system&ajaxCall=1' . BackendUtility::getUrlToken('tceAction'),
109 'icon' => IconUtility::getSpriteIcon('actions-system-cache-clear-impact-high')
110 );
111 $this->optionValues[] = 'system';
112 }
113 // Hook for manipulating cacheActions
114 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['additionalBackendItems']['cacheActions'])) {
115 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['additionalBackendItems']['cacheActions'] as $cacheAction) {
116 $hookObject = GeneralUtility::getUserObj($cacheAction);
117 if (!$hookObject instanceof \TYPO3\CMS\Backend\Toolbar\ClearCacheActionsHookInterface) {
118 throw new \UnexpectedValueException('$hookObject must implement interface TYPO3\\CMS\\Backend\\Toolbar\\ClearCacheActionsHookInterface', 1228262000);
119 }
120 $hookObject->manipulateCacheActions($this->cacheActions, $this->optionValues);
121 }
122 }
123 }
124
125 /**
126 * Checks whether the user has access to this toolbar item
127 *
128 * @return boolean TRUE if user has access, FALSE if not
129 */
130 public function checkAccess() {
131 $backendUser = $this->getBackendUser();
132 if ($backendUser->isAdmin()) {
133 return TRUE;
134 }
135 if (is_array($this->optionValues)) {
136 foreach ($this->optionValues as $value) {
137 if ($backendUser->getTSConfigVal('options.clearCache.' . $value)) {
138 return TRUE;
139 }
140 }
141 }
142 return FALSE;
143 }
144
145 /**
146 * Creates the selector for workspaces
147 *
148 * @return string Workspace selector as HTML select
149 */
150 public function render() {
151 $title = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:rm.clearCache_clearCache', TRUE);
152 $this->addJavascriptToBackend();
153 $cacheMenu = array();
154 $cacheMenu[] = '<a href="#" class="toolbar-item">' . IconUtility::getSpriteIcon('apps-toolbar-menu-cache', array('title' => $title)) . '</a>';
155 $cacheMenu[] = '<ul class="toolbar-item-menu" style="display: none;">';
156 foreach ($this->cacheActions as $actionKey => $cacheAction) {
157 $cacheMenu[] = '<li><a href="' . htmlspecialchars($cacheAction['href'])
158 . '" title="' . htmlspecialchars($cacheAction['description'] ?: $cacheAction['title']) . '">'
159 . $cacheAction['icon'] . ' ' . htmlspecialchars($cacheAction['title']) . '</a></li>';
160 }
161 $cacheMenu[] = '</ul>';
162 return implode(LF, $cacheMenu);
163 }
164
165 /**
166 * Adds the necessary JavaScript to the backend
167 *
168 * @return void
169 */
170 protected function addJavascriptToBackend() {
171 $this->backendReference->addJavascriptFile('sysext/backend/Resources/Public/JavaScript/clearcachemenu.js');
172 }
173
174 /**
175 * Returns additional attributes for the list item in the toolbar
176 *
177 * @return string List item HTML attributes
178 */
179 public function getAdditionalAttributes() {
180 return 'id="clear-cache-actions-menu"';
181 }
182
183 /**
184 * Returns the current BE user.
185 *
186 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
187 */
188 protected function getBackendUser() {
189 return $GLOBALS['BE_USER'];
190 }
191 }