TYPO3 CMS  TYPO3_6-2
ActionService.php
Go to the documentation of this file.
1 <?php
3 
18 
23 
27  protected $dataHandler;
28 
32  public function getDataHandler() {
33  return $this->dataHandler;
34  }
35 
42  public function createNewRecord($tableName, $pageId, array $recordData) {
43  return $this->createNewRecords($pageId, array($tableName => $recordData));
44  }
45 
51  public function createNewRecords($pageId, array $tableRecordData) {
52  $dataMap = array();
53  $newTableIds = array();
54  $currentUid = NULL;
55  $previousTableName = NULL;
56  $previousUid = NULL;
57  foreach ($tableRecordData as $tableName => $recordData) {
58  $recordData = $this->resolvePreviousUid($recordData, $currentUid);
59  if (!isset($recordData['pid'])) {
60  $recordData['pid'] = $pageId;
61  }
62  // @see \TYPO3\CMS\Core\Tests\BaseTestCase->uniqid()
63  $currentUid = 'NEW' . str_replace('.', '', uniqid(mt_rand(), TRUE));
64  $newTableIds[$tableName][] = $currentUid;
65  $dataMap[$tableName][$currentUid] = $recordData;
66  if ($previousTableName !== NULL && $previousUid !== NULL) {
67  $dataMap[$previousTableName][$previousUid] = $this->resolveNextUid(
68  $dataMap[$previousTableName][$previousUid],
69  $currentUid
70  );
71  }
72  $previousTableName = $tableName;
73  $previousUid = $currentUid;
74  }
75  $this->createDataHandler();
76  $this->dataHandler->start($dataMap, array());
77  $this->dataHandler->process_datamap();
78 
79  foreach ($newTableIds as $tableName => &$ids) {
80  foreach ($ids as &$id) {
81  if (!empty($this->dataHandler->substNEWwithIDs[$id])) {
82  $id = $this->dataHandler->substNEWwithIDs[$id];
83  }
84  }
85  }
86 
87  return $newTableIds;
88  }
89 
96  public function modifyRecord($tableName, $uid, array $recordData, array $deleteTableRecordIds = NULL) {
97  $dataMap = array(
98  $tableName => array(
99  $uid => $recordData,
100  ),
101  );
102  $commandMap = array();
103  if (!empty($deleteTableRecordIds)) {
104  foreach ($deleteTableRecordIds as $tableName => $recordIds) {
105  foreach ($recordIds as $recordId) {
106  $commandMap[$tableName][$recordId]['delete'] = TRUE;
107  }
108  }
109  }
110  $this->createDataHandler();
111  $this->dataHandler->start($dataMap, $commandMap);
112  $this->dataHandler->process_datamap();
113  if (!empty($commandMap)) {
114  $this->dataHandler->process_cmdmap();
115  }
116  }
117 
122  public function modifyRecords($pageId, array $tableRecordData) {
123  $dataMap = array();
124  $currentUid = NULL;
125  $previousTableName = NULL;
126  $previousUid = NULL;
127  foreach ($tableRecordData as $tableName => $recordData) {
128  if (empty($recordData['uid'])) {
129  continue;
130  }
131  $recordData = $this->resolvePreviousUid($recordData, $currentUid);
132  $currentUid = $recordData['uid'];
133  if ($recordData['uid'] === '__NEW') {
134  $recordData['pid'] = $pageId;
135  // @see \TYPO3\CMS\Core\Tests\BaseTestCase->uniqid()
136  $currentUid = 'NEW' . str_replace('.', '', uniqid(mt_rand(), TRUE));
137  }
138  unset($recordData['uid']);
139  $dataMap[$tableName][$currentUid] = $recordData;
140  if ($previousTableName !== NULL && $previousUid !== NULL) {
141  $dataMap[$previousTableName][$previousUid] = $this->resolveNextUid(
142  $dataMap[$previousTableName][$previousUid],
143  $currentUid
144  );
145  }
146  $previousTableName = $tableName;
147  $previousUid = $currentUid;
148  }
149  $this->createDataHandler();
150  $this->dataHandler->start($dataMap, array());
151  $this->dataHandler->process_datamap();
152  }
153 
159  public function deleteRecord($tableName, $uid) {
160  return $this->deleteRecords(
161  array(
162  $tableName => array($uid),
163  )
164  );
165  }
166 
171  public function deleteRecords(array $tableRecordIds) {
172  $commandMap = array();
173  foreach ($tableRecordIds as $tableName => $ids) {
174  foreach ($ids as $uid) {
175  $commandMap[$tableName][$uid] = array(
176  'delete' => TRUE,
177  );
178  }
179  }
180  $this->createDataHandler();
181  $this->dataHandler->start(array(), $commandMap);
182  $this->dataHandler->process_cmdmap();
183  // Deleting workspace records is actually a copy(!)
184  return $this->dataHandler->copyMappingArray;
185  }
186 
191  public function clearWorkspaceRecord($tableName, $uid) {
192  $this->clearWorkspaceRecords(
193  array(
194  $tableName => array($uid),
195  )
196  );
197  }
198 
202  public function clearWorkspaceRecords(array $tableRecordIds) {
203  $commandMap = array();
204  foreach ($tableRecordIds as $tableName => $ids) {
205  foreach ($ids as $uid) {
206  $commandMap[$tableName][$uid] = array(
207  'version' => array(
208  'action' => 'clearWSID',
209  )
210  );
211  }
212  }
213  $this->createDataHandler();
214  $this->dataHandler->start(array(), $commandMap);
215  $this->dataHandler->process_cmdmap();
216  }
217 
225  public function copyRecord($tableName, $uid, $pageId, array $recordData = NULL) {
226  $commandMap = array(
227  $tableName => array(
228  $uid => array(
229  'copy' => $pageId,
230  ),
231  ),
232  );
233  if ($recordData !== NULL) {
234  $commandMap[$tableName][$uid]['copy'] = array(
235  'action' => 'paste',
236  'target' => $pageId,
237  'update' => $recordData,
238  );
239  }
240  $this->createDataHandler();
241  $this->dataHandler->start(array(), $commandMap);
242  $this->dataHandler->process_cmdmap();
243  return $this->dataHandler->copyMappingArray;
244  }
245 
253  public function moveRecord($tableName, $uid, $pageId, array $recordData = NULL) {
254  $commandMap = array(
255  $tableName => array(
256  $uid => array(
257  'move' => $pageId,
258  ),
259  ),
260  );
261  if ($recordData !== NULL) {
262  $commandMap[$tableName][$uid]['move'] = array(
263  'action' => 'paste',
264  'target' => $pageId,
265  'update' => $recordData,
266  );
267  }
268  $this->createDataHandler();
269  $this->dataHandler->start(array(), $commandMap);
270  $this->dataHandler->process_cmdmap();
271  return $this->dataHandler->copyMappingArray;
272  }
273 
280  public function localizeRecord($tableName, $uid, $languageId) {
281  $commandMap = array(
282  $tableName => array(
283  $uid => array(
284  'localize' => $languageId,
285  ),
286  ),
287  );
288  $this->createDataHandler();
289  $this->dataHandler->start(array(), $commandMap);
290  $this->dataHandler->process_cmdmap();
291  return $this->dataHandler->copyMappingArray;
292  }
293 
300  public function modifyReferences($tableName, $uid, $fieldName, array $referenceIds) {
301  $dataMap = array(
302  $tableName => array(
303  $uid => array(
304  $fieldName => implode(',', $referenceIds),
305  ),
306  )
307  );
308  $this->createDataHandler();
309  $this->dataHandler->start($dataMap, array());
310  $this->dataHandler->process_datamap();
311  }
312 
318  public function publishRecord($tableName, $liveUid, $throwException = TRUE) {
319  $this->publishRecords(array($tableName => array($liveUid)), $throwException);
320  }
321 
327  public function publishRecords(array $tableLiveUids, $throwException = TRUE) {
328  $commandMap = array();
329  foreach ($tableLiveUids as $tableName => $liveUids) {
330  foreach ($liveUids as $liveUid) {
331  $versionedUid = $this->getVersionedId($tableName, $liveUid);
332  if (empty($versionedUid)) {
333  if ($throwException) {
334  throw new \TYPO3\CMS\Core\Tests\Exception('Versioned UID could not be determined');
335  } else {
336  continue;
337  }
338  }
339 
340  $commandMap[$tableName][$liveUid] = array(
341  'version' => array(
342  'action' => 'swap',
343  'swapWith' => $versionedUid,
344  'notificationAlternativeRecipients' => array(),
345  ),
346  );
347  }
348  }
349  $this->createDataHandler();
350  $this->dataHandler->start(array(), $commandMap);
351  $this->dataHandler->process_cmdmap();
352  }
353 
357  public function publishWorkspace($workspaceId) {
358  $commandMap = $this->getWorkspaceService()->getCmdArrayForPublishWS($workspaceId, FALSE);
359  $this->createDataHandler();
360  $this->dataHandler->start(array(), $commandMap);
361  $this->dataHandler->process_cmdmap();
362  }
363 
367  public function swapWorkspace($workspaceId) {
368  $commandMap = $this->getWorkspaceService()->getCmdArrayForPublishWS($workspaceId, TRUE);
369  $this->createDataHandler();
370  $this->dataHandler->start(array(), $commandMap);
371  $this->dataHandler->process_cmdmap();
372  }
373 
379  protected function resolvePreviousUid(array $recordData, $previousUid) {
380  if ($previousUid === NULL) {
381  return $recordData;
382  }
383  foreach ($recordData as $fieldName => $fieldValue) {
384  if (strpos($fieldValue, '__previousUid') === FALSE) {
385  continue;
386  }
387  $recordData[$fieldName] = str_replace('__previousUid', $previousUid, $fieldValue);
388  }
389  return $recordData;
390  }
391 
397  protected function resolveNextUid(array $recordData, $nextUid) {
398  if ($nextUid === NULL) {
399  return $recordData;
400  }
401  foreach ($recordData as $fieldName => $fieldValue) {
402  if (strpos($fieldValue, '__nextUid') === FALSE) {
403  continue;
404  }
405  $recordData[$fieldName] = str_replace('__nextUid', $nextUid, $fieldValue);
406  }
407  return $recordData;
408  }
409 
416  protected function getVersionedId($tableName, $liveUid, $useDeleteClause = FALSE) {
417  $versionedId = NULL;
418  $liveUid = (int)$liveUid;
419  $workspaceId = (int)$this->getBackendUser()->workspace;
420  $row = $this->getDatabaseConnection()->exec_SELECTgetSingleRow(
421  'uid',
422  $tableName,
423  'pid=-1 AND t3ver_oid=' . $liveUid . ' AND t3ver_wsid=' . $workspaceId .
424  ($useDeleteClause ? \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause($tableName) : '')
425  );
426  if (!empty($row['uid'])) {
427  $versionedId = (int)$row['uid'];
428  }
429  return $versionedId;
430  }
431 
435  protected function createDataHandler() {
437  'TYPO3\\CMS\\Core\\DataHandling\\DataHandler'
438  );
439  $backendUser = $this->getBackendUser();
440  if (isset($backendUser->uc['copyLevels'])) {
441  $this->dataHandler->copyTree = $backendUser->uc['copyLevels'];
442  }
443  return $this->dataHandler;
444  }
445 
449  protected function getWorkspaceService() {
450  return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
451  'TYPO3\\CMS\\Workspaces\\Service\\WorkspaceService'
452  );
453  }
454 
458  protected function getBackendUser() {
459  return $GLOBALS['BE_USER'];
460  }
461 
465  protected function getDatabaseConnection() {
466  return $GLOBALS['TYPO3_DB'];
467  }
468 
469 }
getVersionedId($tableName, $liveUid, $useDeleteClause=FALSE)
$uid
Definition: server.php:36
publishRecords(array $tableLiveUids, $throwException=TRUE)
modifyRecord($tableName, $uid, array $recordData, array $deleteTableRecordIds=NULL)
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
copyRecord($tableName, $uid, $pageId, array $recordData=NULL)
modifyReferences($tableName, $uid, $fieldName, array $referenceIds)
moveRecord($tableName, $uid, $pageId, array $recordData=NULL)
publishRecord($tableName, $liveUid, $throwException=TRUE)