TYPO3 CMS  TYPO3_8-7
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 
22 {
24 
30  private $pathToLocallang = 'LLL:EXT:workspaces/Resources/Private/Language/locallang.xlf';
31 
38  public function getMassStageActions($parameter)
39  {
40  $actions = [];
41  $currentWorkspace = $this->getCurrentWorkspace();
42  $massActionsEnabled = $GLOBALS['BE_USER']->getTSConfigVal('options.workspaces.enableMassActions') !== '0';
43  // in case we're working within "All Workspaces" we can't provide Mass Actions
44  if ($currentWorkspace != \TYPO3\CMS\Workspaces\Service\WorkspaceService::SELECT_ALL_WORKSPACES && $massActionsEnabled) {
45  $publishAccess = $GLOBALS['BE_USER']->workspacePublishAccess($currentWorkspace);
46  if ($publishAccess && !($GLOBALS['BE_USER']->workspaceRec['publish_access'] & 1)) {
47  $actions[] = ['action' => 'publish', 'title' => $GLOBALS['LANG']->sL($this->pathToLocallang . ':label_doaction_publish')];
48  if ($GLOBALS['BE_USER']->workspaceSwapAccess()) {
49  $actions[] = ['action' => 'swap', 'title' => $GLOBALS['LANG']->sL($this->pathToLocallang . ':label_doaction_swap')];
50  }
51  }
52  if ($currentWorkspace !== \TYPO3\CMS\Workspaces\Service\WorkspaceService::LIVE_WORKSPACE_ID) {
53  $actions[] = ['action' => 'discard', 'title' => $GLOBALS['LANG']->sL($this->pathToLocallang . ':label_doaction_discard')];
54  }
55  }
56  $result = [
57  'total' => count($actions),
58  'data' => $actions
59  ];
60  return $result;
61  }
62 
69  public function publishWorkspace(\stdClass $parameters)
70  {
71  $result = [
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 
98  public function flushWorkspace(\stdClass $parameters)
99  {
100  $result = [
101  'init' => false,
102  'total' => 0,
103  'processed' => 0,
104  'error' => false
105  ];
106  try {
107  if ($parameters->init) {
108  $language = $this->validateLanguageParameter($parameters);
109  $cnt = $this->initFlushData($this->getCurrentWorkspace(), $language);
110  $result['total'] = $cnt;
111  } else {
112  $result['processed'] = $this->processData($this->getCurrentWorkspace());
113  $result['total'] = $GLOBALS['BE_USER']->getSessionData('workspaceMassAction_total');
114  }
115  } catch (\Exception $e) {
116  $result['error'] = $e->getMessage();
117  }
118  return $result;
119  }
120 
129  protected function initPublishData($workspace, $swap, $language = null)
130  {
131  // workspace might be -98 a.k.a "All Workspaces but that's save here
132  $publishData = $this->getWorkspaceService()->getCmdArrayForPublishWS($workspace, $swap, 0, $language);
133  $recordCount = 0;
134  foreach ($publishData as $table => $recs) {
135  $recordCount += count($recs);
136  }
137  if ($recordCount > 0) {
138  $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction', $publishData);
139  $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction_total', $recordCount);
140  $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction_processed', 0);
141  }
142  return $recordCount;
143  }
144 
152  protected function initFlushData($workspace, $language = null)
153  {
154  // workspace might be -98 a.k.a "All Workspaces but that's save here
155  $flushData = $this->getWorkspaceService()->getCmdArrayForFlushWS($workspace, true, 0, $language);
156  $recordCount = 0;
157  foreach ($flushData as $table => $recs) {
158  $recordCount += count($recs);
159  }
160  if ($recordCount > 0) {
161  $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction', $flushData);
162  $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction_total', $recordCount);
163  $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction_processed', 0);
164  }
165  return $recordCount;
166  }
167 
174  protected function processData($workspace)
175  {
176  $processData = $GLOBALS['BE_USER']->getSessionData('workspaceMassAction');
177  $recordsProcessed = $GLOBALS['BE_USER']->getSessionData('workspaceMassAction_processed');
178  $limitedCmd = [];
179  $numRecs = 0;
180  foreach ($processData as $table => $recs) {
181  foreach ($recs as $key => $value) {
182  $numRecs++;
183  $limitedCmd[$table][$key] = $value;
184  if ($numRecs == self::MAX_RECORDS_TO_PROCESS) {
185  break;
186  }
187  }
188  if ($numRecs == self::MAX_RECORDS_TO_PROCESS) {
189  break;
190  }
191  }
192  if ($numRecs == 0) {
193  // All done
194  $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction', null);
195  $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction_total', 0);
196  } else {
198  $tce = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\DataHandling\DataHandler::class);
199  // Execute the commands:
200  $tce->start([], $limitedCmd);
201  $tce->process_cmdmap();
202  $errors = $tce->errorLog;
203  if (!empty($errors)) {
204  throw new \Exception(implode(', ', $errors), 1476048278);
205  }
206  // Unset processed records
207  foreach ($limitedCmd as $table => $recs) {
208  foreach ($recs as $key => $value) {
209  $recordsProcessed++;
210  unset($processData[$table][$key]);
211  }
212  }
213  $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction', $processData);
214  $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction_processed', $recordsProcessed);
215  }
216  return $recordsProcessed;
217  }
218 }
static makeInstance($className,... $constructorArguments)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']