[TASK] Move RecordHistory into DataHandling namespace
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / DataHandling / History / RecordHistoryStore.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\DataHandling\History;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Core\Database\Connection;
19 use TYPO3\CMS\Core\Database\ConnectionPool;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22 /**
23 * Used to save any history to a record
24 *
25 * @internal should only be used by the TYPO3 Core
26 */
27 class RecordHistoryStore
28 {
29 public const ACTION_ADD = 1;
30 public const ACTION_MODIFY = 2;
31 public const ACTION_MOVE = 3;
32 public const ACTION_DELETE = 4;
33 public const ACTION_UNDELETE = 5;
34
35 public const USER_BACKEND = 'BE';
36 public const USER_FRONTEND = 'FE';
37 public const USER_ANONYMOUS = '';
38
39 /**
40 * @var int|null
41 */
42 protected $userId;
43
44 /**
45 * @var string
46 */
47 protected $userType;
48
49 /**
50 * @var int|null
51 */
52 protected $originalUserId;
53
54 /**
55 * @var int|null
56 */
57 protected $tstamp;
58
59 /**
60 * @var int
61 */
62 protected $workspaceId;
63
64 /**
65 * @param int|null $userId
66 * @param string $userType
67 * @param int $originalUserId
68 * @param int $tstamp
69 * @param int $workspaceId
70 */
71 public function __construct(string $userType = self::USER_BACKEND, int $userId = null, int $originalUserId = null, int $tstamp = null, int $workspaceId = 0)
72 {
73 $this->userId = $userId;
74 $this->userType = $userType;
75 $this->originalUserId = $originalUserId;
76 $this->tstamp = $tstamp ?: $GLOBALS['EXEC_TIME'];
77 $this->workspaceId = $workspaceId;
78 }
79
80 /**
81 * @param string $table
82 * @param int $uid
83 * @param array $payload
84 * @return string
85 */
86 public function addRecord(string $table, int $uid, array $payload): string
87 {
88 $data = [
89 'actiontype' => self::ACTION_ADD,
90 'usertype' => $this->userType,
91 'userid' => $this->userId,
92 'originaluserid' => $this->originalUserId,
93 'tablename' => $table,
94 'recuid' => $uid,
95 'tstamp' => $this->tstamp,
96 'history_data' => json_encode($payload),
97 'workspace' => $this->workspaceId,
98 ];
99 $this->getDatabaseConnection()->insert('sys_history', $data);
100 return $this->getDatabaseConnection()->lastInsertId('sys_history');
101 }
102
103 /**
104 * @param string $table
105 * @param int $uid
106 * @param array $payload
107 * @return string
108 */
109 public function modifyRecord(string $table, int $uid, array $payload): string
110 {
111 $data = [
112 'actiontype' => self::ACTION_MODIFY,
113 'usertype' => $this->userType,
114 'userid' => $this->userId,
115 'originaluserid' => $this->originalUserId,
116 'tablename' => $table,
117 'recuid' => $uid,
118 'tstamp' => $this->tstamp,
119 'history_data' => json_encode($payload),
120 'workspace' => $this->workspaceId,
121 ];
122 $this->getDatabaseConnection()->insert('sys_history', $data);
123 return $this->getDatabaseConnection()->lastInsertId('sys_history');
124 }
125
126 /**
127 * @param string $table
128 * @param int $uid
129 * @return string
130 */
131 public function deleteRecord(string $table, int $uid): string
132 {
133 $data = [
134 'actiontype' => self::ACTION_DELETE,
135 'usertype' => $this->userType,
136 'userid' => $this->userId,
137 'originaluserid' => $this->originalUserId,
138 'tablename' => $table,
139 'recuid' => $uid,
140 'tstamp' => $this->tstamp,
141 'workspace' => $this->workspaceId,
142 ];
143 $this->getDatabaseConnection()->insert('sys_history', $data);
144 return $this->getDatabaseConnection()->lastInsertId('sys_history');
145 }
146
147 /**
148 * @param string $table
149 * @param int $uid
150 * @return string
151 */
152 public function undeleteRecord(string $table, int $uid): string
153 {
154 $data = [
155 'actiontype' => self::ACTION_UNDELETE,
156 'usertype' => $this->userType,
157 'userid' => $this->userId,
158 'originaluserid' => $this->originalUserId,
159 'tablename' => $table,
160 'recuid' => $uid,
161 'tstamp' => $this->tstamp,
162 'workspace' => $this->workspaceId,
163 ];
164 $this->getDatabaseConnection()->insert('sys_history', $data);
165 return $this->getDatabaseConnection()->lastInsertId('sys_history');
166 }
167
168 /**
169 * @param string $table
170 * @param int $uid
171 * @param array $payload
172 * @return string
173 */
174 public function moveRecord(string $table, int $uid, array $payload): string
175 {
176 $data = [
177 'actiontype' => self::ACTION_MOVE,
178 'usertype' => $this->userType,
179 'userid' => $this->userId,
180 'originaluserid' => $this->originalUserId,
181 'tablename' => $table,
182 'recuid' => $uid,
183 'tstamp' => $this->tstamp,
184 'history_data' => json_encode($payload),
185 'workspace' => $this->workspaceId,
186 ];
187 $this->getDatabaseConnection()->insert('sys_history', $data);
188 return $this->getDatabaseConnection()->lastInsertId('sys_history');
189 }
190
191 /**
192 * @return Connection
193 */
194 protected function getDatabaseConnection(): Connection
195 {
196 return GeneralUtility::makeInstance(ConnectionPool::class)
197 ->getConnectionForTable('sys_history');
198 }
199 }