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