‪TYPO3CMS  9.5
MassActionHandler.php
Go to the documentation of this file.
1 <?php
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 
23 
29 {
31 
37  private ‪$pathToLocallang = 'LLL:EXT:workspaces/Resources/Private/Language/locallang.xlf';
38 
43 
44  public function ‪__construct()
45  {
46  $this->workspaceService = GeneralUtility::makeInstance(WorkspaceService::class);
47  }
48 
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 
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 
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 
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 
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 
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 
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 
254  protected function ‪getCurrentWorkspace()
255  {
256  return $this->workspaceService->getCurrentWorkspace();
257  }
258 
262  protected function ‪getBackendUser()
263  {
264  return ‪$GLOBALS['BE_USER'];
265  }
266 
270  protected function ‪getLanguageService()
271  {
272  return ‪$GLOBALS['LANG'];
273  }
274 }
‪TYPO3\CMS\Core\DataHandling\DataHandler
Definition: DataHandler.php:81
‪TYPO3\CMS\Workspaces\Controller\Remote\MassActionHandler\getBackendUser
‪BackendUserAuthentication getBackendUser()
Definition: MassActionHandler.php:260
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger($var)
Definition: MathUtility.php:73
‪TYPO3\CMS\Workspaces\Controller\Remote\MassActionHandler\initFlushData
‪int initFlushData($workspace, $language=null)
Definition: MassActionHandler.php:166
‪TYPO3\CMS\Core\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Workspaces\Controller\Remote\MassActionHandler\publishWorkspace
‪array publishWorkspace(\stdClass $parameters)
Definition: MassActionHandler.php:84
‪TYPO3\CMS\Workspaces\Controller\Remote\MassActionHandler\__construct
‪__construct()
Definition: MassActionHandler.php:42
‪TYPO3\CMS\Workspaces\Controller\Remote\MassActionHandler\getCurrentWorkspace
‪int getCurrentWorkspace()
Definition: MassActionHandler.php:252
‪TYPO3\CMS\Workspaces\Controller\Remote\MassActionHandler\MAX_RECORDS_TO_PROCESS
‪const MAX_RECORDS_TO_PROCESS
Definition: MassActionHandler.php:30
‪TYPO3\CMS\Workspaces\Service\WorkspaceService\LIVE_WORKSPACE_ID
‪const LIVE_WORKSPACE_ID
Definition: WorkspaceService.php:45
‪TYPO3\CMS\Core\Localization\LanguageService\sL
‪string sL($input)
Definition: LanguageService.php:158
‪TYPO3\CMS\Workspaces\Controller\Remote\MassActionHandler\initPublishData
‪int initPublishData($workspace, $swap, $language=null)
Definition: MassActionHandler.php:143
‪TYPO3\CMS\Workspaces\Controller\Remote\MassActionHandler
Definition: MassActionHandler.php:29
‪TYPO3\CMS\Workspaces\Service\WorkspaceService\SELECT_ALL_WORKSPACES
‪const SELECT_ALL_WORKSPACES
Definition: WorkspaceService.php:44
‪TYPO3\CMS\Workspaces\Controller\Remote\MassActionHandler\$pathToLocallang
‪string $pathToLocallang
Definition: MassActionHandler.php:36
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication\getSessionData
‪mixed getSessionData($key)
Definition: AbstractUserAuthentication.php:1258
‪TYPO3\CMS\Workspaces\Controller\Remote
Definition: ActionHandler.php:2
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:45
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication\setAndSaveSessionData
‪setAndSaveSessionData($key, $data)
Definition: AbstractUserAuthentication.php:1285
‪TYPO3\CMS\Workspaces\Service\WorkspaceService
Definition: WorkspaceService.php:34
‪$errors
‪$errors
Definition: annotationChecker.php:115
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Workspaces\Controller\Remote\MassActionHandler\validateLanguageParameter
‪int null validateLanguageParameter(\stdClass $parameters)
Definition: MassActionHandler.php:238
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:21
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:29
‪TYPO3\CMS\Workspaces\Controller\Remote\MassActionHandler\processData
‪int processData()
Definition: MassActionHandler.php:187
‪TYPO3\CMS\Workspaces\Controller\Remote\MassActionHandler\getLanguageService
‪LanguageService getLanguageService()
Definition: MassActionHandler.php:268
‪TYPO3\CMS\Workspaces\Controller\Remote\MassActionHandler\$workspaceService
‪WorkspaceService $workspaceService
Definition: MassActionHandler.php:40
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Workspaces\Controller\Remote\MassActionHandler\getMassStageActions
‪array getMassStageActions()
Definition: MassActionHandler.php:52
‪TYPO3\CMS\Workspaces\Controller\Remote\MassActionHandler\flushWorkspace
‪array flushWorkspace(\stdClass $parameters)
Definition: MassActionHandler.php:113