1<?php
2namespace TYPO3\CMS\Workspaces\Controller\Remote;
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
17use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
18use TYPO3\CMS\Core\DataHandling\DataHandler;
19use TYPO3\CMS\Core\Localization\LanguageService;
20use TYPO3\CMS\Core\Utility\GeneralUtility;
21use TYPO3\CMS\Core\Utility\MathUtility;
22use TYPO3\CMS\Workspaces\Service\WorkspaceService;
23
24/**
25 * Class encapsulates all actions which are triggered for all elements within the current workspace.
26 * @internal This is a specific Backend Controller implementation and is not considered part of the Public TYPO3 API.
27 */
28class MassActionHandler
29{
30    const MAX_RECORDS_TO_PROCESS = 30;
31
32    /**
33     * Path to the locallang file
34     *
35     * @var string
36     */
37    private $pathToLocallang = 'LLL:EXT:workspaces/Resources/Private/Language/locallang.xlf';
38
39    /**
40     * @var WorkspaceService
41     */
42    protected $workspaceService;
43
44    public function __construct()
45    {
46        $this->workspaceService = GeneralUtility::makeInstance(WorkspaceService::class);
47    }
48
49    /**
50     * Get list of available mass workspace actions.
51     *
52     * @return array $data
53     */
54    public function getMassStageActions()
55    {
56        $actions = [];
57        $currentWorkspace = $this->getCurrentWorkspace();
58        $backendUser = $this->getBackendUser();
59        $massActionsEnabled = (bool)($backendUser->getTSConfig()['options.']['workspaces.']['enableMassActions'] ?? true);
60        // in case we're working within "All Workspaces" we can't provide Mass Actions
61        if ($currentWorkspace != WorkspaceService::SELECT_ALL_WORKSPACES && $massActionsEnabled) {
62            $publishAccess = $backendUser->workspacePublishAccess($currentWorkspace);
63            if ($publishAccess && !($backendUser->workspaceRec['publish_access'] & 1)) {
64                $actions[] = ['action' => 'publish', 'title' => $this->getLanguageService()->sL($this->pathToLocallang . ':label_doaction_publish')];
65                if ($backendUser->workspaceSwapAccess()) {
66                    $actions[] = ['action' => 'swap', 'title' => $this->getLanguageService()->sL($this->pathToLocallang . ':label_doaction_swap')];
67                }
68            }
69            if ($currentWorkspace !== WorkspaceService::LIVE_WORKSPACE_ID) {
70                $actions[] = ['action' => 'discard', 'title' => $this->getLanguageService()->sL($this->pathToLocallang . ':label_doaction_discard')];
71            }
72        }
73        $result = [
74            'total' => count($actions),
75            'data' => $actions
76        ];
77        return $result;
78    }
79
80    /**
81     * Publishes the current workspace.
82     *
83     * @param \stdClass $parameters
84     * @return array
85     */
86    public function publishWorkspace(\stdClass $parameters)
87    {
88        $result = [
89            'init' => false,
90            'total' => 0,
91            'processed' => 0,
92            'error' => false
93        ];
94        try {
95            if ($parameters->init) {
96                $language = $this->validateLanguageParameter($parameters);
97                $cnt = $this->initPublishData($this->getCurrentWorkspace(), $parameters->swap, $language);
98                $result['total'] = $cnt;
99            } else {
100                $result['processed'] = $this->processData();
101                $result['total'] = $this->getBackendUser()->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    {
117        $result = [
118            'init' => false,
119            'total' => 0,
120            'processed' => 0,
121            'error' => false
122        ];
123        try {
124            if ($parameters->init) {
125                $language = $this->validateLanguageParameter($parameters);
126                $result['total'] = $this->initFlushData($this->getCurrentWorkspace(), $language);
127            } else {
128                $result['processed'] = $this->processData();
129                $result['total'] = $this->getBackendUser()->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 int $workspace
141     * @param bool $swap
142     * @param int $language
143     * @return int
144     */
145    protected function initPublishData($workspace, $swap, $language = null)
146    {
147        // workspace might be -98 a.k.a "All Workspaces but that's save here
148        $publishData = $this->workspaceService->getCmdArrayForPublishWS($workspace, $swap, 0, $language);
149        $recordCount = 0;
150        foreach ($publishData as $table => $recs) {
151            $recordCount += count($recs);
152        }
153        if ($recordCount > 0) {
154            $this->getBackendUser()->setAndSaveSessionData('workspaceMassAction', $publishData);
155            $this->getBackendUser()->setAndSaveSessionData('workspaceMassAction_total', $recordCount);
156            $this->getBackendUser()->setAndSaveSessionData('workspaceMassAction_processed', 0);
157        }
158        return $recordCount;
159    }
160
161    /**
162     * Initializes the command map to be used for flushing.
163     *
164     * @param int $workspace
165     * @param int $language
166     * @return int
167     */
168    protected function initFlushData($workspace, $language = null)
169    {
170        // workspace might be -98 a.k.a "All Workspaces but that's save here
171        $flushData = $this->workspaceService->getCmdArrayForFlushWS($workspace, true, 0, $language);
172        $recordCount = 0;
173        foreach ($flushData as $table => $recs) {
174            $recordCount += count($recs);
175        }
176        if ($recordCount > 0) {
177            $this->getBackendUser()->setAndSaveSessionData('workspaceMassAction', $flushData);
178            $this->getBackendUser()->setAndSaveSessionData('workspaceMassAction_total', $recordCount);
179            $this->getBackendUser()->setAndSaveSessionData('workspaceMassAction_processed', 0);
180        }
181        return $recordCount;
182    }
183
184    /**
185     * Processes the data.
186     *
187     * @return int
188     */
189    protected function processData()
190    {
191        $processData = $this->getBackendUser()->getSessionData('workspaceMassAction');
192        $recordsProcessed = $this->getBackendUser()->getSessionData('workspaceMassAction_processed');
193        $limitedCmd = [];
194        $numRecs = 0;
195        foreach ($processData as $table => $recs) {
196            foreach ($recs as $key => $value) {
197                $numRecs++;
198                $limitedCmd[$table][$key] = $value;
199                if ($numRecs == self::MAX_RECORDS_TO_PROCESS) {
200                    break;
201                }
202            }
203            if ($numRecs == self::MAX_RECORDS_TO_PROCESS) {
204                break;
205            }
206        }
207        if ($numRecs == 0) {
208            // All done
209            $this->getBackendUser()->setAndSaveSessionData('workspaceMassAction', null);
210            $this->getBackendUser()->setAndSaveSessionData('workspaceMassAction_total', 0);
211        } else {
212            $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
213            // Execute the commands:
214            $dataHandler->start([], $limitedCmd);
215            $dataHandler->process_cmdmap();
216            $errors = $dataHandler->errorLog;
217            if (!empty($errors)) {
218                throw new \Exception(implode(', ', $errors), 1476048278);
219            }
220            // Unset processed records
221            foreach ($limitedCmd as $table => $recs) {
222                foreach ($recs as $key => $value) {
223                    $recordsProcessed++;
224                    unset($processData[$table][$key]);
225                }
226            }
227            $this->getBackendUser()->setAndSaveSessionData('workspaceMassAction', $processData);
228            $this->getBackendUser()->setAndSaveSessionData('workspaceMassAction_processed', $recordsProcessed);
229        }
230        return $recordsProcessed;
231    }
232
233    /**
234     * Validates whether the submitted language parameter can be
235     * interpreted as integer value.
236     *
237     * @param \stdClass $parameters
238     * @return int|null
239     */
240    protected function validateLanguageParameter(\stdClass $parameters)
241    {
242        $language = null;
243        if (isset($parameters->language) && MathUtility::canBeInterpretedAsInteger($parameters->language)) {
244            $language = $parameters->language;
245        }
246        return $language;
247    }
248
249    /**
250     * Gets the current workspace ID.
251     *
252     * @return int The current workspace ID
253     */
254    protected function getCurrentWorkspace()
255    {
256        return $this->workspaceService->getCurrentWorkspace();
257    }
258
259    /**
260     * @return BackendUserAuthentication
261     */
262    protected function getBackendUser()
263    {
264        return $GLOBALS['BE_USER'];
265    }
266
267    /**
268     * @return LanguageService
269     */
270    protected function getLanguageService()
271    {
272        return $GLOBALS['LANG'];
273    }
274}
275