[TASK] Fix typo in rst file
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / History / RecordHistoryStore.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\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 const ACTION_ADD = 1;
30 const ACTION_MODIFY = 2;
31 const ACTION_MOVE = 3;
32 const ACTION_DELETE = 4;
33 const ACTION_UNDELETE = 5;
34
35 const USER_BACKEND = 'BE';
36 const USER_FRONTEND = 'FE';
37 const USER_ANONYMOUS = '';
38
39 /**
40 * @var int|null
41 */
42 protected $userId;
43 protected $userType;
44 protected $originalUserId;
45 protected $tstamp;
46 protected $workspaceId;
47
48 /**
49 * @param int|null $userId
50 * @param string $userType
51 * @param int $originalUserId
52 * @param int $tstamp
53 * @param int $workspaceId
54 */
55 public function __construct(string $userType = self::USER_BACKEND, int $userId = null, int $originalUserId = null, int $tstamp = null, int $workspaceId = 0)
56 {
57 $this->userId = $userId;
58 $this->userType = $userType;
59 $this->originalUserId = $originalUserId;
60 $this->tstamp = $tstamp ?: $GLOBALS['EXEC_TIME'];
61 $this->workspaceId = $workspaceId;
62 }
63
64 /**
65 * @param string $table
66 * @param int $uid
67 * @param array $payload
68 * @return string
69 */
70 public function addRecord(string $table, int $uid, array $payload): string
71 {
72 $data = [
73 'actiontype' => self::ACTION_ADD,
74 'usertype' => $this->userType,
75 'userid' => $this->userId,
76 'originaluserid' => $this->originalUserId,
77 'tablename' => $table,
78 'recuid' => $uid,
79 'tstamp' => $this->tstamp,
80 'history_data' => json_encode($payload),
81 'workspace' => $this->workspaceId,
82 ];
83 $this->getDatabaseConnection()->insert('sys_history', $data);
84 return $this->getDatabaseConnection()->lastInsertId('sys_history');
85 }
86
87 /**
88 * @param string $table
89 * @param int $uid
90 * @param array $payload
91 * @return string
92 */
93 public function modifyRecord(string $table, int $uid, array $payload): string
94 {
95 $data = [
96 'actiontype' => self::ACTION_MODIFY,
97 'usertype' => $this->userType,
98 'userid' => $this->userId,
99 'originaluserid' => $this->originalUserId,
100 'tablename' => $table,
101 'recuid' => $uid,
102 'tstamp' => $this->tstamp,
103 'history_data' => json_encode($payload),
104 'workspace' => $this->workspaceId,
105 ];
106 $this->getDatabaseConnection()->insert('sys_history', $data);
107 return $this->getDatabaseConnection()->lastInsertId('sys_history');
108 }
109
110 /**
111 * @param string $table
112 * @param int $uid
113 * @return string
114 */
115 public function deleteRecord(string $table, int $uid): string
116 {
117 $data = [
118 'actiontype' => self::ACTION_DELETE,
119 'usertype' => $this->userType,
120 'userid' => $this->userId,
121 'originaluserid' => $this->originalUserId,
122 'tablename' => $table,
123 'recuid' => $uid,
124 'tstamp' => $this->tstamp,
125 'workspace' => $this->workspaceId,
126 ];
127 $this->getDatabaseConnection()->insert('sys_history', $data);
128 return $this->getDatabaseConnection()->lastInsertId('sys_history');
129 }
130
131 /**
132 * @param string $table
133 * @param int $uid
134 * @return string
135 */
136 public function undeleteRecord(string $table, int $uid): string
137 {
138 $data = [
139 'actiontype' => self::ACTION_UNDELETE,
140 'usertype' => $this->userType,
141 'userid' => $this->userId,
142 'originaluserid' => $this->originalUserId,
143 'tablename' => $table,
144 'recuid' => $uid,
145 'tstamp' => $this->tstamp,
146 'workspace' => $this->workspaceId,
147 ];
148 $this->getDatabaseConnection()->insert('sys_history', $data);
149 return $this->getDatabaseConnection()->lastInsertId('sys_history');
150 }
151
152 /**
153 * @param string $table
154 * @param int $uid
155 * @param array $payload
156 * @return string
157 */
158 public function moveRecord(string $table, int $uid, array $payload): string
159 {
160 $data = [
161 'actiontype' => self::ACTION_MOVE,
162 'usertype' => $this->userType,
163 'userid' => $this->userId,
164 'originaluserid' => $this->originalUserId,
165 'tablename' => $table,
166 'recuid' => $uid,
167 'tstamp' => $this->tstamp,
168 'history_data' => json_encode($payload),
169 'workspace' => $this->workspaceId,
170 ];
171 $this->getDatabaseConnection()->insert('sys_history', $data);
172 return $this->getDatabaseConnection()->lastInsertId('sys_history');
173 }
174
175 /**
176 * @return Connection
177 */
178 protected function getDatabaseConnection(): Connection
179 {
180 return GeneralUtility::makeInstance(ConnectionPool::class)
181 ->getConnectionForTable('sys_history');
182 }
183 }