Merge pull request #15833 from yashodha/participant_edit
[civicrm-core.git] / CRM / Logging / Reverter.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
f299f7db 6 | Copyright CiviCRM LLC (c) 2004-2020 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
f299f7db 31 * @copyright CiviCRM LLC (c) 2004-2020
6a488035
TO
32 */
33class CRM_Logging_Reverter {
34 private $db;
35 private $log_conn_id;
430ae6dd
TO
36 private $log_date;
37
93afbc3a 38 /**
39 * The diffs to be reverted.
40 *
41 * @var array
42 */
be2fb01f 43 private $diffs = [];
93afbc3a 44
e0ef6999 45 /**
3b45d110 46 * Class constructor.
47 *
48 * @param string $log_conn_id
e0ef6999
EM
49 * @param $log_date
50 */
00be9182 51 public function __construct($log_conn_id, $log_date) {
353ffa53
TO
52 $dsn = defined('CIVICRM_LOGGING_DSN') ? DB::parseDSN(CIVICRM_LOGGING_DSN) : DB::parseDSN(CIVICRM_DSN);
53 $this->db = $dsn['database'];
6a488035 54 $this->log_conn_id = $log_conn_id;
353ffa53 55 $this->log_date = $log_date;
6a488035
TO
56 }
57
e0ef6999 58 /**
89f6247f 59 *
93afbc3a 60 * Calculate a set of diffs based on the connection_id and changes at a close time.
61 *
62 * @param array $tables
63 */
64 public function calculateDiffsFromLogConnAndDate($tables) {
65 $differ = new CRM_Logging_Differ($this->log_conn_id, $this->log_date);
66 $this->diffs = $differ->diffsInTables($tables);
67 }
68
8246bca4 69 /**
70 * Setter for diffs.
71 *
72 * @param array $diffs
73 */
93afbc3a 74 public function setDiffs($diffs) {
75 $this->diffs = $diffs;
76 }
77
78 /**
79 * Revert changes in the array of diffs in $this->diffs.
e0ef6999 80 */
93afbc3a 81 public function revert() {
6a488035
TO
82
83 // get custom data tables, columns and types
be2fb01f 84 $ctypes = [];
6a488035
TO
85 $dao = CRM_Core_DAO::executeQuery('SELECT table_name, column_name, data_type FROM civicrm_custom_group cg JOIN civicrm_custom_field cf ON (cf.custom_group_id = cg.id)');
86 while ($dao->fetch()) {
87 if (!isset($ctypes[$dao->table_name])) {
be2fb01f 88 $ctypes[$dao->table_name] = ['entity_id' => 'Integer'];
6a488035
TO
89 }
90 $ctypes[$dao->table_name][$dao->column_name] = $dao->data_type;
91 }
92
93afbc3a 93 $diffs = $this->diffs;
be2fb01f
CW
94 $deletes = [];
95 $reverts = [];
6a488035
TO
96 foreach ($diffs as $table => $changes) {
97 foreach ($changes as $change) {
98 switch ($change['action']) {
99 case 'Insert':
100 if (!isset($deletes[$table])) {
be2fb01f 101 $deletes[$table] = [];
6a488035
TO
102 }
103 $deletes[$table][] = $change['id'];
104 break;
105
106 case 'Delete':
107 case 'Update':
108 if (!isset($reverts[$table])) {
be2fb01f 109 $reverts[$table] = [];
6a488035
TO
110 }
111 if (!isset($reverts[$table][$change['id']])) {
be2fb01f 112 $reverts[$table][$change['id']] = ['log_action' => $change['action']];
6a488035
TO
113 }
114 $reverts[$table][$change['id']][$change['field']] = $change['from'];
115 break;
116 }
117 }
118 }
119
120 // revert inserts by deleting
121 foreach ($deletes as $table => $ids) {
122 CRM_Core_DAO::executeQuery("DELETE FROM `$table` WHERE id IN (" . implode(', ', array_unique($ids)) . ')');
123 }
809ed7ad 124
6a488035
TO
125 // revert updates by updating to previous values
126 foreach ($reverts as $table => $row) {
127 switch (TRUE) {
128 // DAO-based tables
129
89f6247f 130 case (($tableDAO = CRM_Core_DAO_AllCoreTables::getClassForTable($table)) != FALSE):
971e129b 131 $dao = new $tableDAO();
6a488035
TO
132 foreach ($row as $id => $changes) {
133 $dao->id = $id;
134 foreach ($changes as $field => $value) {
135 if ($field == 'log_action') {
136 continue;
137 }
138 if (empty($value) and $value !== 0 and $value !== '0') {
139 $value = 'null';
140 }
809ed7ad 141 // Date reaches this point in ISO format (possibly) so strip out stuff
142 // if it does have hyphens of colons demarking the date & it regexes as being a date
143 // or datetime format.
144 if (preg_match('/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/', $value)) {
145 $value = str_replace('-', '', $value);
146 $value = str_replace(':', '', $value);
147 }
6a488035
TO
148 $dao->$field = $value;
149 }
150 $changes['log_action'] == 'Delete' ? $dao->insert() : $dao->update();
809ed7ad 151
6a488035
TO
152 $dao->reset();
153 }
154 break;
ea100cb5 155
6a488035
TO
156 // custom data tables
157
158 case in_array($table, array_keys($ctypes)):
159 foreach ($row as $id => $changes) {
be2fb01f
CW
160 $inserts = ['id' => '%1'];
161 $updates = [];
162 $params = [1 => [$id, 'Integer']];
6a488035
TO
163 $counter = 2;
164 foreach ($changes as $field => $value) {
165 // don’t try reverting a field that’s no longer there
166 if (!isset($ctypes[$table][$field])) {
167 continue;
168 }
a6bb4cee 169 $fldVal = "%{$counter}";
6a488035
TO
170 switch ($ctypes[$table][$field]) {
171 case 'Date':
172 $value = substr(CRM_Utils_Date::isoToMysql($value), 0, 8);
173 break;
174
175 case 'Timestamp':
176 $value = CRM_Utils_Date::isoToMysql($value);
177 break;
a6bb4cee 178
179 case 'Boolean':
180 if ($value === '') {
181 $fldVal = 'DEFAULT';
182 }
6a488035 183 }
353ffa53 184 $inserts[$field] = "%$counter";
a6bb4cee 185 $updates[] = "{$field} = {$fldVal}";
186 if ($fldVal != 'DEFAULT') {
be2fb01f 187 $params[$counter] = [$value, $ctypes[$table][$field]];
a6bb4cee 188 }
6a488035
TO
189 $counter++;
190 }
191 if ($changes['log_action'] == 'Delete') {
192 $sql = "INSERT INTO `$table` (" . implode(', ', array_keys($inserts)) . ') VALUES (' . implode(', ', $inserts) . ')';
193 }
194 else {
195 $sql = "UPDATE `$table` SET " . implode(', ', $updates) . ' WHERE id = %1';
196 }
197 CRM_Core_DAO::executeQuery($sql, $params);
198 }
199 break;
200 }
201 }
202
6a488035 203 }
96025800 204
6a488035 205}