09de6b8729fe5a5f77a806f105684a081811d352
[TYPO3CMS/Extensions/vcc.git] / Classes / Hook / DocHeaderButtonsHook.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2012 Nicole Cordes <cordes@cps-it.de>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * Adds the cache clear button to the edit form
27 *
28 * @author Nicole Cordes <cordes@cps-it.de>
29 * @package TYPO3
30 * @subpackage vcc
31 */
32 class tx_vcc_hook_docHeaderButtonsHook {
33
34 /**
35 * @var tx_vcc_service_communicationService|NULL
36 */
37 var $communicationService = NULL;
38
39 /**
40 * @var tx_vcc_service_tsConfigService|NULL
41 */
42 var $tsConfigService = NULL;
43
44 /**
45 * @var array
46 */
47 var $params = array();
48
49 /**
50 * @var template|NULL
51 */
52 var $pObj = NULL;
53
54 /**
55 * @var string
56 */
57 var $permsClause = '';
58
59 /**
60 * Initialize the object
61 *
62 * @return void
63 */
64 public function __construct() {
65 $communicationService = t3lib_div::makeInstance('tx_vcc_service_communicationService');
66 $this->injectCommunicationService($communicationService);
67
68 $tsConfigService = t3lib_div::makeInstance('tx_vcc_service_tsConfigService');
69 $this->injectTsConfigService($tsConfigService);
70
71 $this->permsClause = $GLOBALS['BE_USER']->getPagePermsClause(2);
72 }
73
74 /**
75 * Injects the communication service
76 *
77 * @param tx_vcc_service_communicationService $communicationService
78 *
79 * @return void
80 */
81 public function injectCommunicationService(tx_vcc_service_communicationService $communicationService) {
82 $this->communicationService = $communicationService;
83 }
84
85 /**
86 * Injects the TSConfig service
87 *
88 * @param tx_vcc_service_tsConfigService $tsConfigService
89 *
90 * @return void
91 */
92 public function injectTsConfigService(tx_vcc_service_tsConfigService $tsConfigService) {
93 $this->tsConfigService = $tsConfigService;
94 }
95
96 /**
97 * Checks access to the record and adds the clear cache button
98 *
99 * @param array $params
100 * @param template $pObj
101 *
102 * @return void
103 */
104 public function addButton($params, $pObj) {
105 $this->params = $params;
106 $this->pObj = $pObj;
107
108 $record = array();
109 $table = '';
110
111 // For web -> page view or web -> list view
112 if ($this->pObj->scriptID === 'ext/cms/layout/db_layout.php' || $this->pObj->scriptID === 'ext/recordlist/mod1/index.php') {
113 $id = t3lib_div::_GP('id');
114 if (is_object($GLOBALS['SOBE']) && $GLOBALS['SOBE']->current_sys_language) {
115 $table = 'pages_language_overlay';
116 $record = t3lib_BEfunc::getRecordsByField($table, 'pid', $id, ' AND ' . $table . '.sys_language_uid=' . intval($GLOBALS['SOBE']->current_sys_language), '', '', '1');
117 if (is_array($record) && !empty($record)) {
118 $record = $record[0];
119 }
120 } else {
121 $table = 'pages';
122 $record = array(
123 'uid' => $id,
124 'pid' => $id
125 );
126 }
127 } elseif ($this->pObj->scriptID === 'typo3/alt_doc.php') { // For record edit
128 $editConf = t3lib_div::_GP('edit');
129 if (is_array($editConf) && !empty($editConf)) {
130 // Finding the current table
131 reset($editConf);
132 $table = key($editConf);
133
134 // Finding the first id and get the records pid
135 reset($editConf[$table]);
136 $recordUid = key($editConf[$table]);
137 // If table is pages we need uid (as pid) to get TSconfig
138 if ($table === 'pages') {
139 $record['uid'] = $recordUid;
140 $record['pid'] = $recordUid;
141 } else {
142 $record = t3lib_BEfunc::getRecord($table, $recordUid, 'uid, pid');
143 }
144 }
145 }
146
147 if (isset($record['pid']) && $record['pid'] > 0) {
148 if ($this->isModuleAccessible($record['pid'], $table)) {
149 // Process last request
150 $button = $this->process($table, $record['uid']);
151
152 // Generate button with form for list view
153 if ($this->pObj->scriptID === 'ext/recordlist/mod1/index.php') {
154 $button .= $this->generateButton(TRUE);
155 } else { // Generate plain input button
156 $button .= $this->generateButton();
157 }
158
159 // Add button to button list and extend layout
160 $this->params['buttons']['vcc'] = $button;
161 $buttonWrap = t3lib_parsehtml::getSubpart($pObj->moduleTemplate, '###BUTTON_GROUP_WRAP###');
162 $this->params['markers']['BUTTONLIST_LEFT'] .= t3lib_parsehtml::substituteMarker($buttonWrap, '###BUTTONS###', trim($button));
163 }
164 }
165 }
166
167 /**
168 * Returns the icon button on condition wrapped with a form
169 *
170 * @param boolean $wrapWithForm
171 *
172 * @return string
173 */
174 protected function generateButton($wrapWithForm = FALSE) {
175 $html = '<input type="image" class="c-inputButton" name="_clearvarnishcache" src="clear.gif" title="Clear Varnish cache" />';
176
177 if ($wrapWithForm) {
178 $html = '<form action="'. t3lib_div::getindpenv('REQUEST_URI') . '" method="post">' . $html . '</form>';
179 }
180
181 return t3lib_iconWorks::getSpriteIcon(
182 'extensions-vcc-clearVarnishCache',
183 array(
184 'html' => $html
185 )
186 );
187 }
188
189 /**
190 * Checks if the button could be inserted
191 *
192 * @param integer $pageId
193 * @param string $table
194 *
195 * @return boolean
196 */
197 protected function isModuleAccessible($pageId, $table) {
198 $access = FALSE;
199
200 // Check edit rights for page as cache can be flushed then only
201 $pageinfo = t3lib_BEfunc::readPageAccess($pageId, $this->permsClause);
202 if ($pageinfo !== FALSE) {
203 // Get TSconfig for extension
204 $tsConfig = $this->tsConfigService->getConfiguration($pageId);
205 if (isset($tsConfig[$table]) && !empty($tsConfig[$table])) {
206 $access = TRUE;
207 }
208 }
209
210 return $access;
211 }
212
213 /**
214 * Evaluate request and send clear cache commands
215 *
216 * @param string $table
217 * @param integer $uid
218 *
219 * @return string
220 */
221 protected function process($table, $uid) {
222 $string = '';
223 if (isset($_POST['_clearvarnishcache_x'])) {
224 $resultArray = $this->communicationService->sendClearCacheCommandForTables($table, $uid);
225 $string = $this->communicationService->generateBackendMessage($resultArray);
226 }
227
228 return $string;
229 }
230 }
231
232 if (defined('TYPO3_MODE') && $GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/vcc/Classes/Hook/DocHeaderButtonsHook.php']) {
233 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/vcc/Classes/Hooks/DocHeaderButtonsHook.php']);
234 }
235
236 ?>