a0e7cee3d2b34c65d01c9319912d346b9b065bc4
[Packages/TYPO3.CMS.git] / typo3 / sysext / workspaces / Classes / ExtDirect / MassActionHandler.php
1 <?php
2 namespace TYPO3\CMS\Workspaces\ExtDirect;
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
17 /**
18 * Class encapsulates all actions which are triggered for all elements within the current workspace.
19 *
20 * @author Kasper Skårhøj (kasperYYYY@typo3.com)
21 * @author Workspaces Team (http://forge.typo3.org/projects/show/typo3v4-workspaces)
22 */
23 class MassActionHandler extends AbstractHandler {
24
25 const MAX_RECORDS_TO_PROCESS = 30;
26
27 /**
28 * Path to the locallang file
29 *
30 * @var string
31 */
32 private $pathToLocallang = 'LLL:EXT:workspaces/Resources/Private/Language/locallang.xlf';
33
34 /**
35 * Get list of available mass workspace actions.
36 *
37 * @param object $parameter
38 * @return array $data
39 */
40 public function getMassStageActions($parameter) {
41 $actions = array();
42 $currentWorkspace = $this->getCurrentWorkspace();
43 $massActionsEnabled = $GLOBALS['BE_USER']->getTSConfigVal('options.workspaces.enableMassActions') !== '0';
44 // in case we're working within "All Workspaces" we can't provide Mass Actions
45 if ($currentWorkspace != \TYPO3\CMS\Workspaces\Service\WorkspaceService::SELECT_ALL_WORKSPACES && $massActionsEnabled) {
46 $publishAccess = $GLOBALS['BE_USER']->workspacePublishAccess($currentWorkspace);
47 if ($publishAccess && !($GLOBALS['BE_USER']->workspaceRec['publish_access'] & 1)) {
48 $actions[] = array('action' => 'publish', 'title' => $GLOBALS['LANG']->sL($this->pathToLocallang . ':label_doaction_publish'));
49 if ($GLOBALS['BE_USER']->workspaceSwapAccess()) {
50 $actions[] = array('action' => 'swap', 'title' => $GLOBALS['LANG']->sL($this->pathToLocallang . ':label_doaction_swap'));
51 }
52 }
53 if ($currentWorkspace !== \TYPO3\CMS\Workspaces\Service\WorkspaceService::LIVE_WORKSPACE_ID) {
54 $actions[] = array('action' => 'discard', 'title' => $GLOBALS['LANG']->sL($this->pathToLocallang . ':label_doaction_discard'));
55 }
56 }
57 $result = array(
58 'total' => count($actions),
59 'data' => $actions
60 );
61 return $result;
62 }
63
64 /**
65 * Publishes the current workspace.
66 *
67 * @param stdclass $parameters
68 * @return array
69 */
70 public function publishWorkspace(\stdclass $parameters) {
71 $result = array(
72 'init' => FALSE,
73 'total' => 0,
74 'processed' => 0,
75 'error' => FALSE
76 );
77 try {
78 if ($parameters->init) {
79 $language = $this->validateLanguageParameter($parameters);
80 $cnt = $this->initPublishData($this->getCurrentWorkspace(), $parameters->swap, $language);
81 $result['total'] = $cnt;
82 } else {
83 $result['processed'] = $this->processData($this->getCurrentWorkspace());
84 $result['total'] = $GLOBALS['BE_USER']->getSessionData('workspaceMassAction_total');
85 }
86 } catch (\Exception $e) {
87 $result['error'] = $e->getMessage();
88 }
89 return $result;
90 }
91
92 /**
93 * Flushes the current workspace.
94 *
95 * @param stdclass $parameters
96 * @return array
97 */
98 public function flushWorkspace(\stdclass $parameters) {
99 $result = array(
100 'init' => FALSE,
101 'total' => 0,
102 'processed' => 0,
103 'error' => FALSE
104 );
105 try {
106 if ($parameters->init) {
107 $language = $this->validateLanguageParameter($parameters);
108 $cnt = $this->initFlushData($this->getCurrentWorkspace(), $language);
109 $result['total'] = $cnt;
110 } else {
111 $result['processed'] = $this->processData($this->getCurrentWorkspace());
112 $result['total'] = $GLOBALS['BE_USER']->getSessionData('workspaceMassAction_total');
113 }
114 } catch (\Exception $e) {
115 $result['error'] = $e->getMessage();
116 }
117 return $result;
118 }
119
120 /**
121 * Initializes the command map to be used for publishing.
122 *
123 * @param int $workspace
124 * @param bool $swap
125 * @param int $language
126 * @return int
127 */
128 protected function initPublishData($workspace, $swap, $language = NULL) {
129 // workspace might be -98 a.k.a "All Workspaces but that's save here
130 $publishData = $this->getWorkspaceService()->getCmdArrayForPublishWS($workspace, $swap, 0, $language);
131 $recordCount = 0;
132 foreach ($publishData as $table => $recs) {
133 $recordCount += count($recs);
134 }
135 if ($recordCount > 0) {
136 $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction', $publishData);
137 $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction_total', $recordCount);
138 $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction_processed', 0);
139 }
140 return $recordCount;
141 }
142
143 /**
144 * Initializes the command map to be used for flushing.
145 *
146 * @param int $workspace
147 * @param int $language
148 * @return int
149 */
150 protected function initFlushData($workspace, $language = NULL) {
151 // workspace might be -98 a.k.a "All Workspaces but that's save here
152 $flushData = $this->getWorkspaceService()->getCmdArrayForFlushWS($workspace, TRUE, 0, $language);
153 $recordCount = 0;
154 foreach ($flushData as $table => $recs) {
155 $recordCount += count($recs);
156 }
157 if ($recordCount > 0) {
158 $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction', $flushData);
159 $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction_total', $recordCount);
160 $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction_processed', 0);
161 }
162 return $recordCount;
163 }
164
165 /**
166 * Processes the data.
167 *
168 * @param int $workspace
169 * @return int
170 */
171 protected function processData($workspace) {
172 $processData = $GLOBALS['BE_USER']->getSessionData('workspaceMassAction');
173 $recordsProcessed = $GLOBALS['BE_USER']->getSessionData('workspaceMassAction_processed');
174 $limitedCmd = array();
175 $numRecs = 0;
176 foreach ($processData as $table => $recs) {
177 foreach ($recs as $key => $value) {
178 $numRecs++;
179 $limitedCmd[$table][$key] = $value;
180 if ($numRecs == self::MAX_RECORDS_TO_PROCESS) {
181 break;
182 }
183 }
184 if ($numRecs == self::MAX_RECORDS_TO_PROCESS) {
185 break;
186 }
187 }
188 if ($numRecs == 0) {
189 // All done
190 $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction', NULL);
191 $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction_total', 0);
192 } else {
193 /** @var $tce \TYPO3\CMS\Core\DataHandling\DataHandler */
194 $tce = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\DataHandling\DataHandler::class);
195 $tce->stripslashes_values = 0;
196 // Execute the commands:
197 $tce->start(array(), $limitedCmd);
198 $tce->process_cmdmap();
199 $errors = $tce->errorLog;
200 if (count($errors) > 0) {
201 throw new \Exception(implode(', ', $errors));
202 } else {
203 // Unset processed records
204 foreach ($limitedCmd as $table => $recs) {
205 foreach ($recs as $key => $value) {
206 $recordsProcessed++;
207 unset($processData[$table][$key]);
208 }
209 }
210 $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction', $processData);
211 $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction_processed', $recordsProcessed);
212 }
213 }
214 return $recordsProcessed;
215 }
216
217 }