TYPO3 CMS  TYPO3_7-6
ActionService.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 
19 
24 {
28  protected $dataHandler;
29 
33  public function getDataHandler()
34  {
35  return $this->dataHandler;
36  }
37 
44  public function createNewRecord($tableName, $pageId, array $recordData)
45  {
46  return $this->createNewRecords($pageId, [$tableName => $recordData]);
47  }
48 
54  public function createNewRecords($pageId, array $tableRecordData)
55  {
56  $dataMap = [];
57  $newTableIds = [];
58  $currentUid = null;
59  $previousTableName = null;
60  $previousUid = null;
61  foreach ($tableRecordData as $tableName => $recordData) {
62  $recordData = $this->resolvePreviousUid($recordData, $currentUid);
63  if (!isset($recordData['pid'])) {
64  $recordData['pid'] = $pageId;
65  }
66  $currentUid = StringUtility::getUniqueId('NEW');
67  $newTableIds[$tableName][] = $currentUid;
68  $dataMap[$tableName][$currentUid] = $recordData;
69  if ($previousTableName !== null && $previousUid !== null) {
70  $dataMap[$previousTableName][$previousUid] = $this->resolveNextUid(
71  $dataMap[$previousTableName][$previousUid],
72  $currentUid
73  );
74  }
75  $previousTableName = $tableName;
76  $previousUid = $currentUid;
77  }
78  $this->createDataHandler();
79  $this->dataHandler->start($dataMap, []);
80  $this->dataHandler->process_datamap();
81 
82  foreach ($newTableIds as $tableName => &$ids) {
83  foreach ($ids as &$id) {
84  if (!empty($this->dataHandler->substNEWwithIDs[$id])) {
85  $id = $this->dataHandler->substNEWwithIDs[$id];
86  }
87  }
88  }
89 
90  return $newTableIds;
91  }
92 
99  public function modifyRecord($tableName, $uid, array $recordData, array $deleteTableRecordIds = null)
100  {
101  $dataMap = [
102  $tableName => [
103  $uid => $recordData,
104  ],
105  ];
106  $commandMap = [];
107  if (!empty($deleteTableRecordIds)) {
108  foreach ($deleteTableRecordIds as $tableName => $recordIds) {
109  foreach ($recordIds as $recordId) {
110  $commandMap[$tableName][$recordId]['delete'] = true;
111  }
112  }
113  }
114  $this->createDataHandler();
115  $this->dataHandler->start($dataMap, $commandMap);
116  $this->dataHandler->process_datamap();
117  if (!empty($commandMap)) {
118  $this->dataHandler->process_cmdmap();
119  }
120  }
121 
126  public function modifyRecords($pageId, array $tableRecordData)
127  {
128  $dataMap = [];
129  $currentUid = null;
130  $previousTableName = null;
131  $previousUid = null;
132  foreach ($tableRecordData as $tableName => $recordData) {
133  if (empty($recordData['uid'])) {
134  continue;
135  }
136  $recordData = $this->resolvePreviousUid($recordData, $currentUid);
137  $currentUid = $recordData['uid'];
138  if ($recordData['uid'] === '__NEW') {
139  $currentUid = StringUtility::getUniqueId('NEW');
140  }
141  if (strpos($currentUid, 'NEW') === 0) {
142  $recordData['pid'] = $pageId;
143  }
144  unset($recordData['uid']);
145  $dataMap[$tableName][$currentUid] = $recordData;
146  if ($previousTableName !== null && $previousUid !== null) {
147  $dataMap[$previousTableName][$previousUid] = $this->resolveNextUid(
148  $dataMap[$previousTableName][$previousUid],
149  $currentUid
150  );
151  }
152  $previousTableName = $tableName;
153  $previousUid = $currentUid;
154  }
155  $this->createDataHandler();
156  $this->dataHandler->start($dataMap, []);
157  $this->dataHandler->process_datamap();
158  }
159 
165  public function deleteRecord($tableName, $uid)
166  {
167  return $this->deleteRecords(
168  [
169  $tableName => [$uid],
170  ]
171  );
172  }
173 
178  public function deleteRecords(array $tableRecordIds)
179  {
180  $commandMap = [];
181  foreach ($tableRecordIds as $tableName => $ids) {
182  foreach ($ids as $uid) {
183  $commandMap[$tableName][$uid] = [
184  'delete' => true,
185  ];
186  }
187  }
188  $this->createDataHandler();
189  $this->dataHandler->start([], $commandMap);
190  $this->dataHandler->process_cmdmap();
191  // Deleting workspace records is actually a copy(!)
192  return $this->dataHandler->copyMappingArray;
193  }
194 
199  public function clearWorkspaceRecord($tableName, $uid)
200  {
201  $this->clearWorkspaceRecords(
202  [
203  $tableName => [$uid],
204  ]
205  );
206  }
207 
211  public function clearWorkspaceRecords(array $tableRecordIds)
212  {
213  $commandMap = [];
214  foreach ($tableRecordIds as $tableName => $ids) {
215  foreach ($ids as $uid) {
216  $commandMap[$tableName][$uid] = [
217  'version' => [
218  'action' => 'clearWSID',
219  ]
220  ];
221  }
222  }
223  $this->createDataHandler();
224  $this->dataHandler->start([], $commandMap);
225  $this->dataHandler->process_cmdmap();
226  }
227 
235  public function copyRecord($tableName, $uid, $pageId, array $recordData = null)
236  {
237  $commandMap = [
238  $tableName => [
239  $uid => [
240  'copy' => $pageId,
241  ],
242  ],
243  ];
244  if ($recordData !== null) {
245  $commandMap[$tableName][$uid]['copy'] = [
246  'action' => 'paste',
247  'target' => $pageId,
248  'update' => $recordData,
249  ];
250  }
251  $this->createDataHandler();
252  $this->dataHandler->start([], $commandMap);
253  $this->dataHandler->process_cmdmap();
254  return $this->dataHandler->copyMappingArray;
255  }
256 
264  public function moveRecord($tableName, $uid, $pageId, array $recordData = null)
265  {
266  $commandMap = [
267  $tableName => [
268  $uid => [
269  'move' => $pageId,
270  ],
271  ],
272  ];
273  if ($recordData !== null) {
274  $commandMap[$tableName][$uid]['move'] = [
275  'action' => 'paste',
276  'target' => $pageId,
277  'update' => $recordData,
278  ];
279  }
280  $this->createDataHandler();
281  $this->dataHandler->start([], $commandMap);
282  $this->dataHandler->process_cmdmap();
283  return $this->dataHandler->copyMappingArray;
284  }
285 
292  public function localizeRecord($tableName, $uid, $languageId)
293  {
294  $commandMap = [
295  $tableName => [
296  $uid => [
297  'localize' => $languageId,
298  ],
299  ],
300  ];
301  $this->createDataHandler();
302  $this->dataHandler->start([], $commandMap);
303  $this->dataHandler->process_cmdmap();
304  return $this->dataHandler->copyMappingArray;
305  }
306 
313  public function copyRecordToLanguage($tableName, $uid, $languageId)
314  {
315  $commandMap = [
316  $tableName => [
317  $uid => [
318  'copyToLanguage' => $languageId,
319  ],
320  ],
321  ];
322  $this->createDataHandler();
323  $this->dataHandler->start([], $commandMap);
324  $this->dataHandler->process_cmdmap();
325  return $this->dataHandler->copyMappingArray;
326  }
327 
334  public function modifyReferences($tableName, $uid, $fieldName, array $referenceIds)
335  {
336  $dataMap = [
337  $tableName => [
338  $uid => [
339  $fieldName => implode(',', $referenceIds),
340  ],
341  ]
342  ];
343  $this->createDataHandler();
344  $this->dataHandler->start($dataMap, []);
345  $this->dataHandler->process_datamap();
346  }
347 
353  public function publishRecord($tableName, $liveUid, $throwException = true)
354  {
355  $this->publishRecords([$tableName => [$liveUid]], $throwException);
356  }
357 
363  public function publishRecords(array $tableLiveUids, $throwException = true)
364  {
365  $commandMap = [];
366  foreach ($tableLiveUids as $tableName => $liveUids) {
367  foreach ($liveUids as $liveUid) {
368  $versionedUid = $this->getVersionedId($tableName, $liveUid);
369  if (empty($versionedUid)) {
370  if ($throwException) {
371  throw new \TYPO3\CMS\Core\Tests\Exception('Versioned UID could not be determined');
372  } else {
373  continue;
374  }
375  }
376 
377  $commandMap[$tableName][$liveUid] = [
378  'version' => [
379  'action' => 'swap',
380  'swapWith' => $versionedUid,
381  'notificationAlternativeRecipients' => [],
382  ],
383  ];
384  }
385  }
386  $this->createDataHandler();
387  $this->dataHandler->start([], $commandMap);
388  $this->dataHandler->process_cmdmap();
389  }
390 
394  public function publishWorkspace($workspaceId)
395  {
396  $commandMap = $this->getWorkspaceService()->getCmdArrayForPublishWS($workspaceId, false);
397  $this->createDataHandler();
398  $this->dataHandler->start([], $commandMap);
399  $this->dataHandler->process_cmdmap();
400  }
401 
405  public function swapWorkspace($workspaceId)
406  {
407  $commandMap = $this->getWorkspaceService()->getCmdArrayForPublishWS($workspaceId, true);
408  $this->createDataHandler();
409  $this->dataHandler->start([], $commandMap);
410  $this->dataHandler->process_cmdmap();
411  }
412 
418  protected function resolvePreviousUid(array $recordData, $previousUid)
419  {
420  if ($previousUid === null) {
421  return $recordData;
422  }
423  foreach ($recordData as $fieldName => $fieldValue) {
424  if (strpos($fieldValue, '__previousUid') === false) {
425  continue;
426  }
427  $recordData[$fieldName] = str_replace('__previousUid', $previousUid, $fieldValue);
428  }
429  return $recordData;
430  }
431 
437  protected function resolveNextUid(array $recordData, $nextUid)
438  {
439  if ($nextUid === null) {
440  return $recordData;
441  }
442  foreach ($recordData as $fieldName => $fieldValue) {
443  if (strpos($fieldValue, '__nextUid') === false) {
444  continue;
445  }
446  $recordData[$fieldName] = str_replace('__nextUid', $nextUid, $fieldValue);
447  }
448  return $recordData;
449  }
450 
457  protected function getVersionedId($tableName, $liveUid, $useDeleteClause = false)
458  {
459  $versionedId = null;
460  $liveUid = (int)$liveUid;
461  $workspaceId = (int)$this->getBackendUser()->workspace;
462  $row = $this->getDatabaseConnection()->exec_SELECTgetSingleRow(
463  'uid',
464  $tableName,
465  'pid=-1 AND t3ver_oid=' . $liveUid . ' AND t3ver_wsid=' . $workspaceId .
466  ($useDeleteClause ? \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause($tableName) : '')
467  );
468  if (!empty($row['uid'])) {
469  $versionedId = (int)$row['uid'];
470  }
471  return $versionedId;
472  }
473 
477  protected function createDataHandler()
478  {
479  $this->dataHandler = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(DataHandler::class);
480  $backendUser = $this->getBackendUser();
481  if (isset($backendUser->uc['copyLevels'])) {
482  $this->dataHandler->copyTree = $backendUser->uc['copyLevels'];
483  }
484  return $this->dataHandler;
485  }
486 
490  protected function getWorkspaceService()
491  {
492  return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
493  \TYPO3\CMS\Workspaces\Service\WorkspaceService::class
494  );
495  }
496 
500  protected function getBackendUser()
501  {
502  return $GLOBALS['BE_USER'];
503  }
504 
508  protected function getDatabaseConnection()
509  {
510  return $GLOBALS['TYPO3_DB'];
511  }
512 }
getVersionedId($tableName, $liveUid, $useDeleteClause=false)
moveRecord($tableName, $uid, $pageId, array $recordData=null)
publishRecord($tableName, $liveUid, $throwException=true)
modifyRecord($tableName, $uid, array $recordData, array $deleteTableRecordIds=null)
copyRecord($tableName, $uid, $pageId, array $recordData=null)
$uid
Definition: server.php:38
publishRecords(array $tableLiveUids, $throwException=true)
modifyReferences($tableName, $uid, $fieldName, array $referenceIds)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']