‪TYPO3CMS  ‪main
MassActionHandler.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
26 
32 {
33  public const ‪MAX_RECORDS_TO_PROCESS = 30;
34 
36 
37  public function ‪__construct()
38  {
39  $this->workspaceService = GeneralUtility::makeInstance(WorkspaceService::class);
40  }
41 
45  public function ‪publishWorkspace(\stdClass $parameters): array
46  {
47  $result = [
48  'init' => false,
49  'total' => 0,
50  'processed' => 0,
51  'error' => false,
52  ];
53  try {
54  if ($parameters->init) {
55  $language = $this->‪validateLanguageParameter($parameters);
56  $cnt = $this->‪initPublishData($this->‪getCurrentWorkspace(), $language);
57  $result['total'] = $cnt;
58  } else {
59  $result['processed'] = $this->‪processData();
60  $result['total'] = $this->‪getBackendUser()->getSessionData('workspaceMassAction_total');
61  }
62  } catch (\Exception $e) {
63  $result['error'] = $e->getMessage();
64  }
65  return $result;
66  }
67 
71  public function ‪flushWorkspace(\stdClass $parameters): array
72  {
73  $result = [
74  'init' => false,
75  'total' => 0,
76  'processed' => 0,
77  'error' => false,
78  ];
79  try {
80  if ($parameters->init) {
81  $language = $this->‪validateLanguageParameter($parameters);
82  $result['total'] = $this->‪initFlushData($this->‪getCurrentWorkspace(), $language);
83  } else {
84  $result['processed'] = $this->‪processData();
85  $result['total'] = $this->‪getBackendUser()->getSessionData('workspaceMassAction_total');
86  }
87  } catch (\Exception $e) {
88  $result['error'] = $e->getMessage();
89  }
90  return $result;
91  }
92 
96  protected function ‪initPublishData(int $workspace, int $language = null): int
97  {
98  // workspace might be -98 a.k.a "All Workspaces" but that's safe here
99  $publishData = $this->workspaceService->getCmdArrayForPublishWS($workspace, false, $language);
100  $recordCount = 0;
101  foreach ($publishData as $table => $recs) {
102  $recordCount += count($recs);
103  }
104  if ($recordCount > 0) {
105  $this->‪getBackendUser()->setAndSaveSessionData('workspaceMassAction', $publishData);
106  $this->‪getBackendUser()->setAndSaveSessionData('workspaceMassAction_total', $recordCount);
107  $this->‪getBackendUser()->setAndSaveSessionData('workspaceMassAction_processed', 0);
108  }
109  return $recordCount;
110  }
111 
115  protected function ‪initFlushData(int $workspace, int $language = null): int
116  {
117  // workspace might be -98 a.k.a "All Workspaces" but that's safe here
118  $flushData = $this->workspaceService->getCmdArrayForFlushWS($workspace, $language);
119  $recordCount = 0;
120  foreach ($flushData as $table => $recs) {
121  $recordCount += count($recs);
122  }
123  if ($recordCount > 0) {
124  $this->‪getBackendUser()->setAndSaveSessionData('workspaceMassAction', $flushData);
125  $this->‪getBackendUser()->setAndSaveSessionData('workspaceMassAction_total', $recordCount);
126  $this->‪getBackendUser()->setAndSaveSessionData('workspaceMassAction_processed', 0);
127  }
128  return $recordCount;
129  }
130 
134  protected function ‪processData(): int
135  {
136  $processData = $this->‪getBackendUser()->getSessionData('workspaceMassAction');
137  $recordsProcessed = $this->‪getBackendUser()->getSessionData('workspaceMassAction_processed');
138  $limitedCmd = [];
139  $numRecs = 0;
140  foreach ($processData as $table => $recs) {
141  foreach ($recs as $key => $value) {
142  $numRecs++;
143  $limitedCmd[$table][$key] = $value;
144  if ($numRecs == self::MAX_RECORDS_TO_PROCESS) {
145  break;
146  }
147  }
148  if ($numRecs == self::MAX_RECORDS_TO_PROCESS) {
149  break;
150  }
151  }
152  if ($numRecs == 0) {
153  // All done
154  $this->‪getBackendUser()->setAndSaveSessionData('workspaceMassAction', null);
155  $this->‪getBackendUser()->setAndSaveSessionData('workspaceMassAction_total', 0);
156  } else {
157  $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
158  // Execute the commands:
159  $dataHandler->start([], $limitedCmd);
160  $dataHandler->process_cmdmap();
161  ‪$errors = $dataHandler->errorLog;
162  if (!empty(‪$errors)) {
163  throw new \Exception(implode(', ', ‪$errors), 1476048278);
164  }
165  // Unset processed records
166  foreach ($limitedCmd as $table => $recs) {
167  foreach ($recs as $key => $value) {
168  $recordsProcessed++;
169  unset($processData[$table][$key]);
170  }
171  }
172  $this->‪getBackendUser()->setAndSaveSessionData('workspaceMassAction', $processData);
173  $this->‪getBackendUser()->setAndSaveSessionData('workspaceMassAction_processed', $recordsProcessed);
174  }
175  return $recordsProcessed;
176  }
177 
182  protected function ‪validateLanguageParameter(\stdClass $parameters): ?int
183  {
184  $language = null;
185  if (isset($parameters->language) && ‪MathUtility::canBeInterpretedAsInteger($parameters->language)) {
186  $language = $parameters->language;
187  }
188  return $language;
189  }
190 
194  protected function ‪getCurrentWorkspace(): int
195  {
196  return $this->workspaceService->getCurrentWorkspace();
197  }
198 
200  {
201  return ‪$GLOBALS['BE_USER'];
202  }
203 
205  {
206  return ‪$GLOBALS['LANG'];
207  }
208 }
‪TYPO3\CMS\Core\DataHandling\DataHandler
Definition: DataHandler.php:94
‪TYPO3\CMS\Workspaces\Controller\Remote\MassActionHandler\getBackendUser
‪getBackendUser()
Definition: MassActionHandler.php:199
‪TYPO3\CMS\Workspaces\Controller\Remote\MassActionHandler\processData
‪processData()
Definition: MassActionHandler.php:134
‪TYPO3\CMS\Workspaces\Controller\Remote\MassActionHandler\initFlushData
‪initFlushData(int $workspace, int $language=null)
Definition: MassActionHandler.php:115
‪TYPO3\CMS\Workspaces\Controller\Remote\MassActionHandler\__construct
‪__construct()
Definition: MassActionHandler.php:37
‪TYPO3\CMS\Workspaces\Controller\Remote\MassActionHandler\validateLanguageParameter
‪validateLanguageParameter(\stdClass $parameters)
Definition: MassActionHandler.php:182
‪TYPO3\CMS\Workspaces\Controller\Remote\MassActionHandler\MAX_RECORDS_TO_PROCESS
‪const MAX_RECORDS_TO_PROCESS
Definition: MassActionHandler.php:33
‪TYPO3\CMS\Workspaces\Controller\Remote\MassActionHandler\flushWorkspace
‪flushWorkspace(\stdClass $parameters)
Definition: MassActionHandler.php:71
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger(mixed $var)
Definition: MathUtility.php:69
‪TYPO3\CMS\Workspaces\Controller\Remote\MassActionHandler
Definition: MassActionHandler.php:32
‪TYPO3\CMS\Workspaces\Controller\Remote\MassActionHandler\initPublishData
‪initPublishData(int $workspace, int $language=null)
Definition: MassActionHandler.php:96
‪TYPO3\CMS\Workspaces\Controller\Remote\MassActionHandler\getLanguageService
‪getLanguageService()
Definition: MassActionHandler.php:204
‪TYPO3\CMS\Workspaces\Controller\Remote
Definition: ActionHandler.php:18
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Workspaces\Service\WorkspaceService
Definition: WorkspaceService.php:38
‪$errors
‪$errors
Definition: annotationChecker.php:116
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:46
‪TYPO3\CMS\Workspaces\Controller\Remote\MassActionHandler\$workspaceService
‪WorkspaceService $workspaceService
Definition: MassActionHandler.php:35
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Workspaces\Controller\Remote\MassActionHandler\publishWorkspace
‪publishWorkspace(\stdClass $parameters)
Definition: MassActionHandler.php:45
‪TYPO3\CMS\Workspaces\Controller\Remote\MassActionHandler\getCurrentWorkspace
‪getCurrentWorkspace()
Definition: MassActionHandler.php:194