bdbdc2f49ca971b753e015e0b4a458cf89fc7fd4
[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 * Copyright notice
6 *
7 * (c) 2014 Oliver Hader <oliver.hader@typo3.org>
8 * All rights reserved
9 *
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.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
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.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 use TYPO3\CMS\Core\DataHandling\DataHandler;
28
29 /**
30 * DataHandler Actions
31 */
32 class ActionService {
33
34 /**
35 * @var DataHandler
36 */
37 protected $dataHandler;
38
39 /**
40 * @param DataHandler $dataHandler
41 */
42 public function __construct(DataHandler $dataHandler) {
43 $this->setDataHandler($dataHandler);
44 }
45
46 /**
47 * @param DataHandler $dataHandler
48 */
49 public function setDataHandler(DataHandler $dataHandler) {
50 $this->dataHandler = $dataHandler;
51 }
52
53 /**
54 * @return DataHandler
55 */
56 public function getDataHander() {
57 return $this->dataHandler;
58 }
59
60 /**
61 * @param string $tableName
62 * @param integer $pageId
63 * @param array $recordData
64 */
65 public function createNewRecord($tableName, $pageId, array $recordData) {
66 $this->createNewRecords($pageId, array($tableName => $recordData));
67 }
68
69 /**
70 * @param integer $pageId
71 * @param array $tableRecordData
72 * @return array
73 */
74 public function createNewRecords($pageId, array $tableRecordData) {
75 $dataMap = array();
76 $newTableIds = array();
77 $currentUid = NULL;
78 foreach ($tableRecordData as $tableName => $recordData) {
79 $recordData = $this->resolvePreviousUid($recordData, $currentUid);
80 $recordData['pid'] = $pageId;
81 $currentUid = uniqid('NEW');
82 $newTableIds[$tableName][] = $currentUid;
83 $dataMap[$tableName][$currentUid] = $recordData;
84 }
85 $this->dataHandler->start($dataMap, array());
86 $this->dataHandler->process_datamap();
87
88 foreach ($newTableIds as $tableName => &$ids) {
89 foreach ($ids as &$id) {
90 if (!empty($this->dataHandler->substNEWwithIDs[$id])) {
91 $id = $this->dataHandler->substNEWwithIDs[$id];
92 }
93 }
94 }
95
96 return $newTableIds;
97 }
98
99 /**
100 * @param string $tableName
101 * @param integer $uid
102 * @param array $recordData
103 * @param NULL|array $deleteTableRecordIds
104 */
105 public function modifyRecord($tableName, $uid, array $recordData, array $deleteTableRecordIds = NULL) {
106 $dataMap = array(
107 $tableName => array(
108 $uid => $recordData,
109 ),
110 );
111 $commandMap = array();
112 if (!empty($deleteTableRecordIds)) {
113 foreach ($deleteTableRecordIds as $tableName => $recordIds) {
114 foreach ($recordIds as $recordId) {
115 $commandMap[$tableName][$recordId]['delete'] = TRUE;
116 }
117 }
118 }
119 $this->dataHandler->start($dataMap, $commandMap);
120 $this->dataHandler->process_datamap();
121 if (!empty($commandMap)) {
122 $this->dataHandler->process_cmdmap();
123 }
124 }
125
126 /**
127 * @param integer $pageId
128 * @param array $tableRecordData
129 */
130 public function modifyRecords($pageId, array $tableRecordData) {
131 $dataMap = array();
132 $currentUid = NULL;
133 foreach ($tableRecordData as $tableName => $recordData) {
134 if (empty($recordData['uid'])) {
135 continue;
136 }
137 $recordData = $this->resolvePreviousUid($recordData, $currentUid);
138 $currentUid = $recordData['uid'];
139 if ($recordData['uid'] === '__NEW') {
140 $recordData['pid'] = $pageId;
141 $currentUid = uniqid('NEW');
142 }
143 unset($recordData['uid']);
144 $dataMap[$tableName][$currentUid] = $recordData;
145 }
146 $this->dataHandler->start($dataMap, array());
147 $this->dataHandler->process_datamap();
148 }
149
150 /**
151 * @param string $tableName
152 * @param integer $uid
153 */
154 public function deleteRecord($tableName, $uid) {
155 $commandMap = array(
156 $tableName => array(
157 $uid => array(
158 'delete' => TRUE,
159 ),
160 ),
161 );
162 $this->dataHandler->start(array(), $commandMap);
163 $this->dataHandler->process_cmdmap();
164 }
165
166 /**
167 * @param string $tableName
168 * @param integer $uid
169 * @param integer $pageId
170 */
171 public function copyRecord($tableName, $uid, $pageId) {
172 $commandMap = array(
173 $tableName => array(
174 $uid => array(
175 'copy' => $pageId,
176 ),
177 ),
178 );
179 $this->dataHandler->start(array(), $commandMap);
180 $this->dataHandler->process_cmdmap();
181 }
182
183 /**
184 * @param string $tableName
185 * @param integer $uid
186 * @param integer $pageId
187 */
188 public function moveRecord($tableName, $uid, $pageId) {
189 $commandMap = array(
190 $tableName => array(
191 $uid => array(
192 'move' => $pageId,
193 ),
194 ),
195 );
196 $this->dataHandler->start(array(), $commandMap);
197 $this->dataHandler->process_cmdmap();
198 }
199
200 /**
201 * @param string $tableName
202 * @param integer $uid
203 * @param integer $languageId
204 */
205 public function localizeRecord($tableName, $uid, $languageId) {
206 $commandMap = array(
207 $tableName => array(
208 $uid => array(
209 'localize' => $languageId,
210 ),
211 ),
212 );
213 $this->dataHandler->start(array(), $commandMap);
214 $this->dataHandler->process_cmdmap();
215 }
216
217 /**
218 * @param string $tableName
219 * @param integer $uid
220 * @param string $fieldName
221 * @param array $referenceIds
222 */
223 public function modifyReferences($tableName, $uid, $fieldName, array $referenceIds) {
224 $dataMap = array(
225 $tableName => array(
226 $uid => array(
227 $fieldName => implode(',', $referenceIds),
228 ),
229 )
230 );
231 $this->dataHandler->start($dataMap, array());
232 $this->dataHandler->process_datamap();
233 }
234
235 /**
236 * @param string $tableName
237 * @param integer $uid
238 * @param string $fieldName
239 * @param integer $referenceId
240 */
241 public function addReference($tableName, $uid, $fieldName, $referenceId) {
242 $recordValues = $this->getRecordValues($tableName, $uid, $fieldName);
243
244 if (!in_array($referenceId, $recordValues)) {
245 $recordValues[] = $referenceId;
246 }
247
248 $this->modifyReferences($tableName, $uid, $fieldName, $recordValues);
249 }
250
251 /**
252 * @param string $tableName
253 * @param integer $uid
254 * @param string $fieldName
255 * @param integer $referenceId
256 */
257 public function deleteReference($tableName, $uid, $fieldName, $referenceId) {
258 $recordValues = $this->getRecordValues($tableName, $uid, $fieldName);
259
260 if (($index = array_search($referenceId, $recordValues)) !== FALSE) {
261 unset($recordValues[$index]);
262 }
263
264 $this->modifyReferences($tableName, $uid, $fieldName, $recordValues);
265 }
266
267 /**
268 * @param array $recordData
269 * @param NULL|int $previousUid
270 * @return array
271 */
272 protected function resolvePreviousUid(array $recordData, $previousUid) {
273 if ($previousUid === NULL) {
274 return $recordData;
275 }
276 foreach ($recordData as $fieldName => $fieldValue) {
277 if (strpos($fieldValue, '__previousUid') === FALSE) {
278 continue;
279 }
280 $recordData[$fieldName] = str_replace('__previousUid', $previousUid, $fieldValue);
281 }
282 return $recordData;
283 }
284
285 /**
286 * @param string $tableName
287 * @param integer $uid
288 * @param string $fieldName
289 * @return array
290 */
291 protected function getRecordValues($tableName, $uid, $fieldName) {
292 $recordValues = array();
293
294 $recordValue = $this->getRecordValue($tableName, $uid, $fieldName);
295 if (!empty($recordValue)) {
296 $recordValues = explode(',', $recordValues);
297 }
298
299 return $recordValues;
300 }
301
302 /**
303 * @param string $tableName
304 * @param integer $uid
305 * @param string $fieldName
306 * @return bool|string|NULL
307 */
308 protected function getRecordValue($tableName, $uid, $fieldName) {
309 $recordValue = FALSE;
310
311 $record = $this->getDatabaseConnection()->exec_SELECTgetSingleRow(
312 $fieldName, $tableName, 'uid=' . (int)$uid
313 );
314
315 if (isset($record[$fieldName])) {
316 $recordValue = $record[$fieldName];
317 }
318
319 return $recordValue;
320 }
321
322 /**
323 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
324 */
325 protected function getDatabaseConnection() {
326 return $GLOBALS['TYPO3_DB'];
327 }
328
329 }