Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
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 | |
fa938177 | 31 | * @copyright CiviCRM LLC (c) 2004-2016 |
6a488035 TO |
32 | */ |
33 | class 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 | */ | |
43 | private $diffs = array(); | |
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 | ||
69 | public function setDiffs($diffs) { | |
70 | $this->diffs = $diffs; | |
71 | } | |
72 | ||
73 | /** | |
74 | * Revert changes in the array of diffs in $this->diffs. | |
e0ef6999 | 75 | */ |
93afbc3a | 76 | public function revert() { |
6a488035 TO |
77 | |
78 | // get custom data tables, columns and types | |
79 | $ctypes = array(); | |
80 | $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)'); | |
81 | while ($dao->fetch()) { | |
82 | if (!isset($ctypes[$dao->table_name])) { | |
83 | $ctypes[$dao->table_name] = array('entity_id' => 'Integer'); | |
84 | } | |
85 | $ctypes[$dao->table_name][$dao->column_name] = $dao->data_type; | |
86 | } | |
87 | ||
93afbc3a | 88 | $diffs = $this->diffs; |
6a488035 TO |
89 | $deletes = array(); |
90 | $reverts = array(); | |
91 | foreach ($diffs as $table => $changes) { | |
92 | foreach ($changes as $change) { | |
93 | switch ($change['action']) { | |
94 | case 'Insert': | |
95 | if (!isset($deletes[$table])) { | |
96 | $deletes[$table] = array(); | |
97 | } | |
98 | $deletes[$table][] = $change['id']; | |
99 | break; | |
100 | ||
101 | case 'Delete': | |
102 | case 'Update': | |
103 | if (!isset($reverts[$table])) { | |
104 | $reverts[$table] = array(); | |
105 | } | |
106 | if (!isset($reverts[$table][$change['id']])) { | |
107 | $reverts[$table][$change['id']] = array('log_action' => $change['action']); | |
108 | } | |
109 | $reverts[$table][$change['id']][$change['field']] = $change['from']; | |
110 | break; | |
111 | } | |
112 | } | |
113 | } | |
114 | ||
115 | // revert inserts by deleting | |
116 | foreach ($deletes as $table => $ids) { | |
117 | CRM_Core_DAO::executeQuery("DELETE FROM `$table` WHERE id IN (" . implode(', ', array_unique($ids)) . ')'); | |
118 | } | |
6a488035 TO |
119 | // revert updates by updating to previous values |
120 | foreach ($reverts as $table => $row) { | |
121 | switch (TRUE) { | |
122 | // DAO-based tables | |
123 | ||
89f6247f | 124 | case (($tableDAO = CRM_Core_DAO_AllCoreTables::getClassForTable($table)) != FALSE): |
125 | $dao = new $tableDAO (); | |
6a488035 TO |
126 | foreach ($row as $id => $changes) { |
127 | $dao->id = $id; | |
128 | foreach ($changes as $field => $value) { | |
129 | if ($field == 'log_action') { | |
130 | continue; | |
131 | } | |
132 | if (empty($value) and $value !== 0 and $value !== '0') { | |
133 | $value = 'null'; | |
134 | } | |
135 | $dao->$field = $value; | |
136 | } | |
137 | $changes['log_action'] == 'Delete' ? $dao->insert() : $dao->update(); | |
138 | $dao->reset(); | |
139 | } | |
140 | break; | |
ea100cb5 | 141 | |
6a488035 TO |
142 | // custom data tables |
143 | ||
144 | case in_array($table, array_keys($ctypes)): | |
145 | foreach ($row as $id => $changes) { | |
146 | $inserts = array('id' => '%1'); | |
147 | $updates = array(); | |
353ffa53 | 148 | $params = array(1 => array($id, 'Integer')); |
6a488035 TO |
149 | $counter = 2; |
150 | foreach ($changes as $field => $value) { | |
151 | // don’t try reverting a field that’s no longer there | |
152 | if (!isset($ctypes[$table][$field])) { | |
153 | continue; | |
154 | } | |
a6bb4cee | 155 | $fldVal = "%{$counter}"; |
6a488035 TO |
156 | switch ($ctypes[$table][$field]) { |
157 | case 'Date': | |
158 | $value = substr(CRM_Utils_Date::isoToMysql($value), 0, 8); | |
159 | break; | |
160 | ||
161 | case 'Timestamp': | |
162 | $value = CRM_Utils_Date::isoToMysql($value); | |
163 | break; | |
a6bb4cee | 164 | |
165 | case 'Boolean': | |
166 | if ($value === '') { | |
167 | $fldVal = 'DEFAULT'; | |
168 | } | |
6a488035 | 169 | } |
353ffa53 | 170 | $inserts[$field] = "%$counter"; |
a6bb4cee | 171 | $updates[] = "{$field} = {$fldVal}"; |
172 | if ($fldVal != 'DEFAULT') { | |
173 | $params[$counter] = array($value, $ctypes[$table][$field]); | |
174 | } | |
6a488035 TO |
175 | $counter++; |
176 | } | |
177 | if ($changes['log_action'] == 'Delete') { | |
178 | $sql = "INSERT INTO `$table` (" . implode(', ', array_keys($inserts)) . ') VALUES (' . implode(', ', $inserts) . ')'; | |
179 | } | |
180 | else { | |
181 | $sql = "UPDATE `$table` SET " . implode(', ', $updates) . ' WHERE id = %1'; | |
182 | } | |
183 | CRM_Core_DAO::executeQuery($sql, $params); | |
184 | } | |
185 | break; | |
186 | } | |
187 | } | |
188 | ||
189 | // CRM-7353: if nothing altered civicrm_contact, touch it; this will | |
190 | // make sure there’s an entry in log_civicrm_contact for this revert | |
191 | if (empty($diffs['civicrm_contact'])) { | |
192 | $query = " | |
193 | SELECT id FROM `{$this->db}`.log_civicrm_contact | |
194 | WHERE log_conn_id = %1 AND log_date BETWEEN DATE_SUB(%2, INTERVAL 10 SECOND) AND DATE_ADD(%2, INTERVAL 10 SECOND) | |
195 | ORDER BY log_date DESC LIMIT 1 | |
196 | "; | |
197 | $params = array( | |
3b45d110 | 198 | 1 => array($this->log_conn_id, 'String'), |
6a488035 TO |
199 | 2 => array($this->log_date, 'String'), |
200 | ); | |
201 | $cid = CRM_Core_DAO::singleValueQuery($query, $params); | |
202 | if (!$cid) { | |
203 | return; | |
204 | } | |
205 | ||
2177d15c | 206 | $dao = new CRM_Contact_DAO_Contact(); |
6a488035 TO |
207 | $dao->id = $cid; |
208 | if ($dao->find(TRUE)) { | |
209 | // CRM-8102: MySQL can’t parse its own dates | |
210 | $dao->birth_date = CRM_Utils_Date::isoToMysql($dao->birth_date); | |
211 | $dao->deceased_date = CRM_Utils_Date::isoToMysql($dao->deceased_date); | |
212 | $dao->save(); | |
213 | } | |
214 | } | |
215 | } | |
96025800 | 216 | |
6a488035 | 217 | } |