2 namespace TYPO3\CMS\Core\Tests\Functional\DataHandling\Framework
;
4 /***************************************************************
7 * (c) 2014 Oliver Hader <oliver.hader@typo3.org>
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
27 use TYPO3\CMS\Core\DataHandling\DataHandler
;
37 protected $dataHandler;
40 * @param DataHandler $dataHandler
42 public function __construct(DataHandler
$dataHandler) {
43 $this->setDataHandler($dataHandler);
47 * @param DataHandler $dataHandler
49 public function setDataHandler(DataHandler
$dataHandler) {
50 $this->dataHandler
= $dataHandler;
56 public function getDataHander() {
57 return $this->dataHandler
;
61 * @param string $tableName
62 * @param integer $pageId
63 * @param array $recordData
66 public function createNewRecord($tableName, $pageId, array $recordData) {
67 return $this->createNewRecords($pageId, array($tableName => $recordData));
71 * @param integer $pageId
72 * @param array $tableRecordData
75 public function createNewRecords($pageId, array $tableRecordData) {
77 $newTableIds = array();
79 foreach ($tableRecordData as $tableName => $recordData) {
80 $recordData = $this->resolvePreviousUid($recordData, $currentUid);
81 $recordData['pid'] = $pageId;
82 $currentUid = uniqid('NEW');
83 $newTableIds[$tableName][] = $currentUid;
84 $dataMap[$tableName][$currentUid] = $recordData;
86 $this->dataHandler
->start($dataMap, array());
87 $this->dataHandler
->process_datamap();
89 foreach ($newTableIds as $tableName => &$ids) {
90 foreach ($ids as &$id) {
91 if (!empty($this->dataHandler
->substNEWwithIDs
[$id])) {
92 $id = $this->dataHandler
->substNEWwithIDs
[$id];
101 * @param string $tableName
102 * @param integer $uid
103 * @param array $recordData
104 * @param NULL|array $deleteTableRecordIds
106 public function modifyRecord($tableName, $uid, array $recordData, array $deleteTableRecordIds = NULL) {
112 $commandMap = array();
113 if (!empty($deleteTableRecordIds)) {
114 foreach ($deleteTableRecordIds as $tableName => $recordIds) {
115 foreach ($recordIds as $recordId) {
116 $commandMap[$tableName][$recordId]['delete'] = TRUE;
120 $this->dataHandler
->start($dataMap, $commandMap);
121 $this->dataHandler
->process_datamap();
122 if (!empty($commandMap)) {
123 $this->dataHandler
->process_cmdmap();
128 * @param integer $pageId
129 * @param array $tableRecordData
131 public function modifyRecords($pageId, array $tableRecordData) {
134 foreach ($tableRecordData as $tableName => $recordData) {
135 if (empty($recordData['uid'])) {
138 $recordData = $this->resolvePreviousUid($recordData, $currentUid);
139 $currentUid = $recordData['uid'];
140 if ($recordData['uid'] === '__NEW') {
141 $recordData['pid'] = $pageId;
142 $currentUid = uniqid('NEW');
144 unset($recordData['uid']);
145 $dataMap[$tableName][$currentUid] = $recordData;
147 $this->dataHandler
->start($dataMap, array());
148 $this->dataHandler
->process_datamap();
152 * @param string $tableName
153 * @param integer $uid
156 public function deleteRecord($tableName, $uid) {
157 return $this->deleteRecords(
159 $tableName => array($uid),
165 * @param array $tableRecordIds
168 public function deleteRecords(array $tableRecordIds) {
169 $commandMap = array();
170 foreach ($tableRecordIds as $tableName => $ids) {
171 foreach ($ids as $uid) {
172 $commandMap[$tableName][$uid] = array(
177 $this->dataHandler
->start(array(), $commandMap);
178 $this->dataHandler
->process_cmdmap();
179 // Deleting workspace records is actually a copy(!)
180 return $this->dataHandler
->copyMappingArray
;
184 * @param string $tableName
185 * @param integer $uid
187 public function clearWorkspaceRecord($tableName, $uid) {
188 $this->clearWorkspaceRecords(
190 $tableName => array($uid),
196 * @param array $tableRecordIds
198 public function clearWorkspaceRecords(array $tableRecordIds) {
199 $commandMap = array();
200 foreach ($tableRecordIds as $tableName => $ids) {
201 foreach ($ids as $uid) {
202 $commandMap[$tableName][$uid] = array(
204 'action' => 'clearWSID',
209 $this->dataHandler
->start(array(), $commandMap);
210 $this->dataHandler
->process_cmdmap();
214 * @param string $tableName
215 * @param integer $uid
216 * @param integer $pageId
219 public function copyRecord($tableName, $uid, $pageId) {
227 $this->dataHandler
->start(array(), $commandMap);
228 $this->dataHandler
->process_cmdmap();
229 return $this->dataHandler
->copyMappingArray
;
233 * @param string $tableName
234 * @param integer $uid
235 * @param integer $pageId
237 public function moveRecord($tableName, $uid, $pageId) {
245 $this->dataHandler
->start(array(), $commandMap);
246 $this->dataHandler
->process_cmdmap();
250 * @param string $tableName
251 * @param integer $uid
252 * @param integer $languageId
255 public function localizeRecord($tableName, $uid, $languageId) {
259 'localize' => $languageId,
263 $this->dataHandler
->start(array(), $commandMap);
264 $this->dataHandler
->process_cmdmap();
265 return $this->dataHandler
->copyMappingArray
;
269 * @param string $tableName
270 * @param integer $uid
271 * @param string $fieldName
272 * @param array $referenceIds
274 public function modifyReferences($tableName, $uid, $fieldName, array $referenceIds) {
278 $fieldName => implode(',', $referenceIds),
282 $this->dataHandler
->start($dataMap, array());
283 $this->dataHandler
->process_datamap();
287 * @param string $tableName
288 * @param integer $uid
289 * @param string $fieldName
290 * @param integer $referenceId
292 public function addReference($tableName, $uid, $fieldName, $referenceId) {
293 $recordValues = $this->getRecordValues($tableName, $uid, $fieldName);
295 if (!in_array($referenceId, $recordValues)) {
296 $recordValues[] = $referenceId;
299 $this->modifyReferences($tableName, $uid, $fieldName, $recordValues);
303 * @param string $tableName
304 * @param integer $uid
305 * @param string $fieldName
306 * @param integer $referenceId
308 public function deleteReference($tableName, $uid, $fieldName, $referenceId) {
309 $recordValues = $this->getRecordValues($tableName, $uid, $fieldName);
311 if (($index = array_search($referenceId, $recordValues)) !== FALSE) {
312 unset($recordValues[$index]);
315 $this->modifyReferences($tableName, $uid, $fieldName, $recordValues);
319 * @param array $recordData
320 * @param NULL|int $previousUid
323 protected function resolvePreviousUid(array $recordData, $previousUid) {
324 if ($previousUid === NULL) {
327 foreach ($recordData as $fieldName => $fieldValue) {
328 if (strpos($fieldValue, '__previousUid') === FALSE) {
331 $recordData[$fieldName] = str_replace('__previousUid', $previousUid, $fieldValue);
337 * @param string $tableName
338 * @param integer $uid
339 * @param string $fieldName
342 protected function getRecordValues($tableName, $uid, $fieldName) {
343 $recordValues = array();
345 $recordValue = $this->getRecordValue($tableName, $uid, $fieldName);
346 if (!empty($recordValue)) {
347 $recordValues = explode(',', $recordValues);
350 return $recordValues;
354 * @param string $tableName
355 * @param integer $uid
356 * @param string $fieldName
357 * @return bool|string|NULL
359 protected function getRecordValue($tableName, $uid, $fieldName) {
360 $recordValue = FALSE;
362 $record = $this->getDatabaseConnection()->exec_SELECTgetSingleRow(
363 $fieldName, $tableName, 'uid=' . (int)$uid
366 if (isset($record[$fieldName])) {
367 $recordValue = $record[$fieldName];
374 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
376 protected function getDatabaseConnection() {
377 return $GLOBALS['TYPO3_DB'];