[TASK] Re-work/simplify copyright header in PHP files - Part 4
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Functional / DataHandling / Framework / ActionService.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Functional\DataHandling\Framework;
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
17 use TYPO3\CMS\Core\DataHandling\DataHandler;
18
19 /**
20 * DataHandler Actions
21 */
22 class ActionService {
23
24 /**
25 * @var DataHandler
26 */
27 protected $dataHandler;
28
29 /**
30 * @return DataHandler
31 */
32 public function getDataHander() {
33 return $this->dataHandler;
34 }
35
36 /**
37 * @param string $tableName
38 * @param integer $pageId
39 * @param array $recordData
40 * @return array
41 */
42 public function createNewRecord($tableName, $pageId, array $recordData) {
43 return $this->createNewRecords($pageId, array($tableName => $recordData));
44 }
45
46 /**
47 * @param integer $pageId
48 * @param array $tableRecordData
49 * @return array
50 */
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 $currentUid = uniqid('NEW', TRUE);
63 $newTableIds[$tableName][] = $currentUid;
64 $dataMap[$tableName][$currentUid] = $recordData;
65 if ($previousTableName !== NULL && $previousUid !== NULL) {
66 $dataMap[$previousTableName][$previousUid] = $this->resolveNextUid(
67 $dataMap[$previousTableName][$previousUid],
68 $currentUid
69 );
70 }
71 $previousTableName = $tableName;
72 $previousUid = $currentUid;
73 }
74 $this->createDataHandler();
75 $this->dataHandler->start($dataMap, array());
76 $this->dataHandler->process_datamap();
77
78 foreach ($newTableIds as $tableName => &$ids) {
79 foreach ($ids as &$id) {
80 if (!empty($this->dataHandler->substNEWwithIDs[$id])) {
81 $id = $this->dataHandler->substNEWwithIDs[$id];
82 }
83 }
84 }
85
86 return $newTableIds;
87 }
88
89 /**
90 * @param string $tableName
91 * @param integer $uid
92 * @param array $recordData
93 * @param NULL|array $deleteTableRecordIds
94 */
95 public function modifyRecord($tableName, $uid, array $recordData, array $deleteTableRecordIds = NULL) {
96 $dataMap = array(
97 $tableName => array(
98 $uid => $recordData,
99 ),
100 );
101 $commandMap = array();
102 if (!empty($deleteTableRecordIds)) {
103 foreach ($deleteTableRecordIds as $tableName => $recordIds) {
104 foreach ($recordIds as $recordId) {
105 $commandMap[$tableName][$recordId]['delete'] = TRUE;
106 }
107 }
108 }
109 $this->createDataHandler();
110 $this->dataHandler->start($dataMap, $commandMap);
111 $this->dataHandler->process_datamap();
112 if (!empty($commandMap)) {
113 $this->dataHandler->process_cmdmap();
114 }
115 }
116
117 /**
118 * @param integer $pageId
119 * @param array $tableRecordData
120 */
121 public function modifyRecords($pageId, array $tableRecordData) {
122 $dataMap = array();
123 $currentUid = NULL;
124 $previousTableName = NULL;
125 $previousUid = NULL;
126 foreach ($tableRecordData as $tableName => $recordData) {
127 if (empty($recordData['uid'])) {
128 continue;
129 }
130 $recordData = $this->resolvePreviousUid($recordData, $currentUid);
131 $currentUid = $recordData['uid'];
132 if ($recordData['uid'] === '__NEW') {
133 $recordData['pid'] = $pageId;
134 $currentUid = uniqid('NEW');
135 }
136 unset($recordData['uid']);
137 $dataMap[$tableName][$currentUid] = $recordData;
138 if ($previousTableName !== NULL && $previousUid !== NULL) {
139 $dataMap[$previousTableName][$previousUid] = $this->resolveNextUid(
140 $dataMap[$previousTableName][$previousUid],
141 $currentUid
142 );
143 }
144 $previousTableName = $tableName;
145 $previousUid = $currentUid;
146 }
147 $this->createDataHandler();
148 $this->dataHandler->start($dataMap, array());
149 $this->dataHandler->process_datamap();
150 }
151
152 /**
153 * @param string $tableName
154 * @param integer $uid
155 * @return array
156 */
157 public function deleteRecord($tableName, $uid) {
158 return $this->deleteRecords(
159 array(
160 $tableName => array($uid),
161 )
162 );
163 }
164
165 /**
166 * @param array $tableRecordIds
167 * @return array
168 */
169 public function deleteRecords(array $tableRecordIds) {
170 $commandMap = array();
171 foreach ($tableRecordIds as $tableName => $ids) {
172 foreach ($ids as $uid) {
173 $commandMap[$tableName][$uid] = array(
174 'delete' => TRUE,
175 );
176 }
177 }
178 $this->createDataHandler();
179 $this->dataHandler->start(array(), $commandMap);
180 $this->dataHandler->process_cmdmap();
181 // Deleting workspace records is actually a copy(!)
182 return $this->dataHandler->copyMappingArray;
183 }
184
185 /**
186 * @param string $tableName
187 * @param integer $uid
188 */
189 public function clearWorkspaceRecord($tableName, $uid) {
190 $this->clearWorkspaceRecords(
191 array(
192 $tableName => array($uid),
193 )
194 );
195 }
196
197 /**
198 * @param array $tableRecordIds
199 */
200 public function clearWorkspaceRecords(array $tableRecordIds) {
201 $commandMap = array();
202 foreach ($tableRecordIds as $tableName => $ids) {
203 foreach ($ids as $uid) {
204 $commandMap[$tableName][$uid] = array(
205 'version' => array(
206 'action' => 'clearWSID',
207 )
208 );
209 }
210 }
211 $this->createDataHandler();
212 $this->dataHandler->start(array(), $commandMap);
213 $this->dataHandler->process_cmdmap();
214 }
215
216 /**
217 * @param string $tableName
218 * @param integer $uid
219 * @param integer $pageId
220 * @param NULL|array $recordData
221 * @return array
222 */
223 public function copyRecord($tableName, $uid, $pageId, array $recordData = NULL) {
224 $commandMap = array(
225 $tableName => array(
226 $uid => array(
227 'copy' => $pageId,
228 ),
229 ),
230 );
231 if ($recordData !== NULL) {
232 $commandMap[$tableName][$uid]['copy'] = array(
233 'action' => 'paste',
234 'target' => $pageId,
235 'update' => $recordData,
236 );
237 }
238 $this->createDataHandler();
239 $this->dataHandler->start(array(), $commandMap);
240 $this->dataHandler->process_cmdmap();
241 return $this->dataHandler->copyMappingArray;
242 }
243
244 /**
245 * @param string $tableName
246 * @param integer $uid
247 * @param integer $pageId
248 * @param NULL|array $recordData
249 */
250 public function moveRecord($tableName, $uid, $pageId, array $recordData = NULL) {
251 $commandMap = array(
252 $tableName => array(
253 $uid => array(
254 'move' => $pageId,
255 ),
256 ),
257 );
258 if ($recordData !== NULL) {
259 $commandMap[$tableName][$uid]['move'] = array(
260 'action' => 'paste',
261 'target' => $pageId,
262 'update' => $recordData,
263 );
264 }
265 $this->createDataHandler();
266 $this->dataHandler->start(array(), $commandMap);
267 $this->dataHandler->process_cmdmap();
268 }
269
270 /**
271 * @param string $tableName
272 * @param integer $uid
273 * @param integer $languageId
274 * @return array
275 */
276 public function localizeRecord($tableName, $uid, $languageId) {
277 $commandMap = array(
278 $tableName => array(
279 $uid => array(
280 'localize' => $languageId,
281 ),
282 ),
283 );
284 $this->createDataHandler();
285 $this->dataHandler->start(array(), $commandMap);
286 $this->dataHandler->process_cmdmap();
287 return $this->dataHandler->copyMappingArray;
288 }
289
290 /**
291 * @param string $tableName
292 * @param integer $uid
293 * @param string $fieldName
294 * @param array $referenceIds
295 */
296 public function modifyReferences($tableName, $uid, $fieldName, array $referenceIds) {
297 $dataMap = array(
298 $tableName => array(
299 $uid => array(
300 $fieldName => implode(',', $referenceIds),
301 ),
302 )
303 );
304 $this->createDataHandler();
305 $this->dataHandler->start($dataMap, array());
306 $this->dataHandler->process_datamap();
307 }
308
309 /**
310 * @param string $tableName
311 * @param int $liveUid
312 * @param bool $throwException
313 */
314 public function publishRecord($tableName, $liveUid, $throwException = TRUE) {
315 $this->publishRecords(array($tableName => array($liveUid)), $throwException);
316 }
317
318 /**
319 * @param array $tableLiveUids
320 * @param bool $throwException
321 * @throws \TYPO3\CMS\Core\Tests\Exception
322 */
323 public function publishRecords(array $tableLiveUids, $throwException = TRUE) {
324 $commandMap = array();
325 foreach ($tableLiveUids as $tableName => $liveUids) {
326 foreach ($liveUids as $liveUid) {
327 $versionedUid = $this->getVersionedId($tableName, $liveUid);
328 if (empty($versionedUid)) {
329 if ($throwException) {
330 throw new \TYPO3\CMS\Core\Tests\Exception('Versioned UID could not be determined');
331 } else {
332 continue;
333 }
334 }
335
336 $commandMap[$tableName][$liveUid] = array(
337 'version' => array(
338 'action' => 'swap',
339 'swapWith' => $versionedUid,
340 'notificationAlternativeRecipients' => array(),
341 ),
342 );
343 }
344 }
345 $this->createDataHandler();
346 $this->dataHandler->start(array(), $commandMap);
347 $this->dataHandler->process_cmdmap();
348 }
349
350 /**
351 * @param int $workspaceId
352 */
353 public function publishWorkspace($workspaceId) {
354 $commandMap = $this->getWorkspaceService()->getCmdArrayForPublishWS($workspaceId, FALSE);
355 $this->createDataHandler();
356 $this->dataHandler->start(array(), $commandMap);
357 $this->dataHandler->process_cmdmap();
358 }
359
360 /**
361 * @param int $workspaceId
362 */
363 public function swapWorkspace($workspaceId) {
364 $commandMap = $this->getWorkspaceService()->getCmdArrayForPublishWS($workspaceId, TRUE);
365 $this->createDataHandler();
366 $this->dataHandler->start(array(), $commandMap);
367 $this->dataHandler->process_cmdmap();
368 }
369
370 /**
371 * @param array $recordData
372 * @param NULL|string|int $previousUid
373 * @return array
374 */
375 protected function resolvePreviousUid(array $recordData, $previousUid) {
376 if ($previousUid === NULL) {
377 return $recordData;
378 }
379 foreach ($recordData as $fieldName => $fieldValue) {
380 if (strpos($fieldValue, '__previousUid') === FALSE) {
381 continue;
382 }
383 $recordData[$fieldName] = str_replace('__previousUid', $previousUid, $fieldValue);
384 }
385 return $recordData;
386 }
387
388 /**
389 * @param array $recordData
390 * @param NULL|string|int $nextUid
391 * @return array
392 */
393 protected function resolveNextUid(array $recordData, $nextUid) {
394 if ($nextUid === NULL) {
395 return $recordData;
396 }
397 foreach ($recordData as $fieldName => $fieldValue) {
398 if (strpos($fieldValue, '__nextUid') === FALSE) {
399 continue;
400 }
401 $recordData[$fieldName] = str_replace('__nextUid', $nextUid, $fieldValue);
402 }
403 return $recordData;
404 }
405
406 /**
407 * @param string $tableName
408 * @param int $liveUid
409 * @param bool $useDeleteClause
410 * @return NULL|int
411 */
412 protected function getVersionedId($tableName, $liveUid, $useDeleteClause = FALSE) {
413 $versionedId = NULL;
414 $liveUid = (int)$liveUid;
415 $workspaceId = (int)$this->getBackendUser()->workspace;
416 $row = $this->getDatabaseConnection()->exec_SELECTgetSingleRow(
417 'uid',
418 $tableName,
419 'pid=-1 AND t3ver_oid=' . $liveUid . ' AND t3ver_wsid=' . $workspaceId .
420 ($useDeleteClause ? \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause($tableName) : '')
421 );
422 if (!empty($row['uid'])) {
423 $versionedId = (int)$row['uid'];
424 }
425 return $versionedId;
426 }
427
428 /**
429 * @return \TYPO3\CMS\Core\DataHandling\DataHandler
430 */
431 protected function createDataHandler() {
432 $dataHandler = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
433 'TYPO3\\CMS\\Core\\DataHandling\\DataHandler'
434 );
435 $this->dataHandler = $dataHandler;
436 return $dataHandler;
437 }
438
439 /**
440 * @return \TYPO3\CMS\Workspaces\Service\WorkspaceService
441 */
442 protected function getWorkspaceService() {
443 return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
444 'TYPO3\\CMS\\Workspaces\\Service\\WorkspaceService'
445 );
446 }
447
448 /**
449 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
450 */
451 protected function getBackendUser() {
452 return $GLOBALS['BE_USER'];
453 }
454
455 /**
456 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
457 */
458 protected function getDatabaseConnection() {
459 return $GLOBALS['TYPO3_DB'];
460 }
461
462 }