[BUGFIX] Make class name consistent
[Packages/TYPO3.CMS.git] / typo3 / sysext / workspaces / Classes / ExtDirect / MassActionHandler.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2011 Kasper Skårhøj (kasperYYYY@typo3.com)
6 * (c) 2010-2011 Workspaces Team (http://forge.typo3.org/projects/show/typo3v4-workspaces)
7 * All rights reserved
8 *
9 * This script is part of the TYPO3 project. The TYPO3 project is
10 * free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * The GNU General Public License can be found at
16 * http://www.gnu.org/copyleft/gpl.html.
17 * A copy is found in the textfile GPL.txt and important notices to the license
18 * from the author is found in LICENSE.txt distributed with these scripts.
19 *
20 *
21 * This script is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * This copyright notice MUST APPEAR in all copies of the script!
27 ***************************************************************/
28
29 /**
30 * Class encapsulates all actions which are triggered for all elements within the current workspace.
31 *
32 * @author Kasper Skårhøj (kasperYYYY@typo3.com)
33 * @author Workspaces Team (http://forge.typo3.org/projects/show/typo3v4-workspaces)
34 * @package Workspaces
35 * @subpackage ExtDirect
36 */
37 class Tx_Workspaces_ExtDirect_MassActionHandler extends Tx_Workspaces_ExtDirect_AbstractHandler {
38 const MAX_RECORDS_TO_PROCESS = 30;
39
40 /**
41 * Path to the locallang file
42 * @var string
43 */
44 private $pathToLocallang = 'LLL:EXT:workspaces/Resources/Private/Language/locallang.xml';
45
46 /**
47 * Get list of available mass workspace actions.
48 *
49 * @param object $parameter
50 * @return array $data
51 */
52 public function getMassStageActions($parameter) {
53 $actions = array();
54 $currentWorkspace = $this->getCurrentWorkspace();
55
56 // in case we're working within "All Workspaces" we can't provide Mass Actions
57 if ($currentWorkspace != Tx_Workspaces_Service_Workspaces::SELECT_ALL_WORKSPACES) {
58 $publishAccess = $GLOBALS['BE_USER']->workspacePublishAccess($currentWorkspace);
59 if ($publishAccess && !($GLOBALS['BE_USER']->workspaceRec['publish_access'] & 1)) {
60 $actions[] = array('action' => 'publish', 'title' => $GLOBALS['LANG']->sL($this->pathToLocallang . ':label_doaction_publish')
61 );
62 if ($GLOBALS['BE_USER']->workspaceSwapAccess()) {
63 $actions[] = array('action' => 'swap', 'title' => $GLOBALS['LANG']->sL($this->pathToLocallang . ':label_doaction_swap')
64 );
65 }
66 }
67
68 if ($currentWorkspace !== Tx_Workspaces_Service_Workspaces::LIVE_WORKSPACE_ID) {
69 $actions[] = array('action' => 'discard', 'title' => $GLOBALS['LANG']->sL($this->pathToLocallang . ':label_doaction_discard')
70 );
71 }
72 }
73
74 $result = array(
75 'total' => count($actions),
76 'data' => $actions
77 );
78 return $result;
79 }
80
81 /**
82 * Publishes the current workspace.
83 *
84 * @param stdclass $parameters
85 * @return array
86 */
87 public function publishWorkspace(stdclass $parameters) {
88 $result = array(
89 'init' => FALSE,
90 'total' => 0,
91 'processed' => 0,
92 'error' => FALSE
93 );
94
95 try {
96 if ($parameters->init) {
97 $cnt = $this->initPublishData($this->getCurrentWorkspace(), $parameters->swap);
98 $result['total'] = $cnt;
99 } else {
100 $result['processed'] = $this->processData($this->getCurrentWorkspace());
101 $result['total'] = $GLOBALS['BE_USER']->getSessionData('workspaceMassAction_total');
102 }
103 } catch (Exception $e) {
104 $result['error'] = $e->getMessage();
105 }
106 return $result;
107 }
108
109 /**
110 * Flushes the current workspace.
111 *
112 * @param stdclass $parameters
113 * @return array
114 */
115 public function flushWorkspace(stdclass $parameters) {
116 $result = array(
117 'init' => FALSE,
118 'total' => 0,
119 'processed' => 0,
120 'error' => FALSE
121 );
122
123 try {
124 if ($parameters->init) {
125 $cnt = $this->initFlushData($this->getCurrentWorkspace());
126 $result['total'] = $cnt;
127 } else {
128 $result['processed'] = $this->processData($this->getCurrentWorkspace());
129 $result['total'] = $GLOBALS['BE_USER']->getSessionData('workspaceMassAction_total');
130 }
131 } catch (Exception $e) {
132 $result['error'] = $e->getMessage();
133 }
134 return $result;
135 }
136
137 /**
138 * Initializes the command map to be used for publishing.
139 *
140 * @param integer $workspace
141 * @param boolean $swap
142 * @return integer
143 */
144 protected function initPublishData($workspace, $swap) {
145 $workspaceService = t3lib_div::makeInstance('Tx_Workspaces_Service_Workspaces');
146 // workspace might be -98 a.k.a "All Workspaces but that's save here
147 $publishData = $workspaceService->getCmdArrayForPublishWS($workspace, $swap);
148 $recordCount = 0;
149 foreach ($publishData as $table => $recs) {
150 $recordCount += count($recs);
151 }
152 if ($recordCount > 0) {
153 $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction', $publishData);
154 $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction_total', $recordCount);
155 $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction_processed', 0);
156 }
157 return $recordCount;
158 }
159
160 /**
161 * Initializes the command map to be used for flushing.
162 *
163 * @param integer $workspace
164 * @return integer
165 */
166 protected function initFlushData($workspace) {
167 $workspaceService = t3lib_div::makeInstance('Tx_Workspaces_Service_Workspaces');
168 // workspace might be -98 a.k.a "All Workspaces but that's save here
169 $flushData = $workspaceService->getCmdArrayForFlushWS($workspace);
170 $recordCount = 0;
171 foreach ($flushData as $table => $recs) {
172 $recordCount += count($recs);
173 }
174 if ($recordCount > 0) {
175 $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction', $flushData);
176 $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction_total', $recordCount);
177 $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction_processed', 0);
178 }
179 return $recordCount;
180 }
181
182 /**
183 * Processes the data.
184 *
185 * @param integer $workspace
186 * @return integer
187 */
188 protected function processData($workspace) {
189 $processData = $GLOBALS['BE_USER']->getSessionData('workspaceMassAction');
190 $recordsProcessed = $GLOBALS['BE_USER']->getSessionData('workspaceMassAction_processed');
191 $limitedCmd = array();
192 $numRecs = 0;
193
194 foreach ($processData as $table => $recs) {
195 foreach ($recs as $key => $value) {
196 $numRecs++;
197 $limitedCmd[$table][$key] = $value;
198 if ($numRecs == self::MAX_RECORDS_TO_PROCESS) {
199 break;
200 }
201 }
202 if ($numRecs == self::MAX_RECORDS_TO_PROCESS) {
203 break;
204 }
205 }
206
207 if ($numRecs == 0) {
208 // All done
209 $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction', NULL);
210 $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction_total', 0);
211 } else {
212 // Execute the commands:
213 $tce = t3lib_div::makeInstance('t3lib_TCEmain');
214 $tce->stripslashes_values = 0;
215 $tce->start(array(), $limitedCmd);
216 $tce->process_cmdmap();
217
218 $errors = $tce->errorLog;
219 if (count($errors) > 0) {
220 throw new Exception(implode(', ', $errors));
221 } else {
222 // Unset processed records
223 foreach ($limitedCmd as $table => $recs) {
224 foreach ($recs as $key => $value) {
225 $recordsProcessed++;
226 unset($processData[$table][$key]);
227 }
228 }
229 $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction', $processData);
230 $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction_processed', $recordsProcessed);
231 }
232 }
233
234 return $recordsProcessed;
235 }
236 }
237
238
239 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/workspaces/Classes/ExtDirect/MassActionHandler.php'])) {
240 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/workspaces/Classes/ExtDirect/MassActionHandler.php']);
241 }
242 ?>