Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Logging_Reverter { | |
36 | private $db; | |
37 | private $log_conn_id; | |
430ae6dd TO |
38 | private $log_date; |
39 | ||
e0ef6999 | 40 | /** |
100fef9d | 41 | * @param int $log_conn_id |
e0ef6999 EM |
42 | * @param $log_date |
43 | */ | |
00be9182 | 44 | public function __construct($log_conn_id, $log_date) { |
353ffa53 TO |
45 | $dsn = defined('CIVICRM_LOGGING_DSN') ? DB::parseDSN(CIVICRM_LOGGING_DSN) : DB::parseDSN(CIVICRM_DSN); |
46 | $this->db = $dsn['database']; | |
6a488035 | 47 | $this->log_conn_id = $log_conn_id; |
353ffa53 | 48 | $this->log_date = $log_date; |
6a488035 TO |
49 | } |
50 | ||
e0ef6999 EM |
51 | /** |
52 | * @param $tables | |
53 | */ | |
00be9182 | 54 | public function revert($tables) { |
6a488035 TO |
55 | // FIXME: split off the table → DAO mapping to a GenCode-generated class |
56 | $daos = array( | |
57 | 'civicrm_address' => 'CRM_Core_DAO_Address', | |
58 | 'civicrm_contact' => 'CRM_Contact_DAO_Contact', | |
59 | 'civicrm_email' => 'CRM_Core_DAO_Email', | |
60 | 'civicrm_im' => 'CRM_Core_DAO_IM', | |
61 | 'civicrm_openid' => 'CRM_Core_DAO_OpenID', | |
62 | 'civicrm_phone' => 'CRM_Core_DAO_Phone', | |
63 | 'civicrm_website' => 'CRM_Core_DAO_Website', | |
64 | 'civicrm_contribution' => 'CRM_Contribute_DAO_Contribution', | |
65 | 'civicrm_note' => 'CRM_Core_DAO_Note', | |
66 | 'civicrm_relationship' => 'CRM_Contact_DAO_Relationship', | |
67 | ); | |
68 | ||
69 | // get custom data tables, columns and types | |
70 | $ctypes = array(); | |
71 | $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)'); | |
72 | while ($dao->fetch()) { | |
73 | if (!isset($ctypes[$dao->table_name])) { | |
74 | $ctypes[$dao->table_name] = array('entity_id' => 'Integer'); | |
75 | } | |
76 | $ctypes[$dao->table_name][$dao->column_name] = $dao->data_type; | |
77 | } | |
78 | ||
79 | $differ = new CRM_Logging_Differ($this->log_conn_id, $this->log_date); | |
80 | $diffs = $differ->diffsInTables($tables); | |
81 | ||
82 | $deletes = array(); | |
83 | $reverts = array(); | |
84 | foreach ($diffs as $table => $changes) { | |
85 | foreach ($changes as $change) { | |
86 | switch ($change['action']) { | |
87 | case 'Insert': | |
88 | if (!isset($deletes[$table])) { | |
89 | $deletes[$table] = array(); | |
90 | } | |
91 | $deletes[$table][] = $change['id']; | |
92 | break; | |
93 | ||
94 | case 'Delete': | |
95 | case 'Update': | |
96 | if (!isset($reverts[$table])) { | |
97 | $reverts[$table] = array(); | |
98 | } | |
99 | if (!isset($reverts[$table][$change['id']])) { | |
100 | $reverts[$table][$change['id']] = array('log_action' => $change['action']); | |
101 | } | |
102 | $reverts[$table][$change['id']][$change['field']] = $change['from']; | |
103 | break; | |
104 | } | |
105 | } | |
106 | } | |
107 | ||
108 | // revert inserts by deleting | |
109 | foreach ($deletes as $table => $ids) { | |
110 | CRM_Core_DAO::executeQuery("DELETE FROM `$table` WHERE id IN (" . implode(', ', array_unique($ids)) . ')'); | |
111 | } | |
112 | ||
113 | // revert updates by updating to previous values | |
114 | foreach ($reverts as $table => $row) { | |
115 | switch (TRUE) { | |
116 | // DAO-based tables | |
117 | ||
118 | case in_array($table, array_keys($daos)): | |
a1a2a83d | 119 | $dao = new $daos[$table](); |
6a488035 TO |
120 | foreach ($row as $id => $changes) { |
121 | $dao->id = $id; | |
122 | foreach ($changes as $field => $value) { | |
123 | if ($field == 'log_action') { | |
124 | continue; | |
125 | } | |
126 | if (empty($value) and $value !== 0 and $value !== '0') { | |
127 | $value = 'null'; | |
128 | } | |
129 | $dao->$field = $value; | |
130 | } | |
131 | $changes['log_action'] == 'Delete' ? $dao->insert() : $dao->update(); | |
132 | $dao->reset(); | |
133 | } | |
134 | break; | |
ea100cb5 | 135 | |
6a488035 TO |
136 | // custom data tables |
137 | ||
138 | case in_array($table, array_keys($ctypes)): | |
139 | foreach ($row as $id => $changes) { | |
140 | $inserts = array('id' => '%1'); | |
141 | $updates = array(); | |
353ffa53 | 142 | $params = array(1 => array($id, 'Integer')); |
6a488035 TO |
143 | $counter = 2; |
144 | foreach ($changes as $field => $value) { | |
145 | // don’t try reverting a field that’s no longer there | |
146 | if (!isset($ctypes[$table][$field])) { | |
147 | continue; | |
148 | } | |
149 | switch ($ctypes[$table][$field]) { | |
150 | case 'Date': | |
151 | $value = substr(CRM_Utils_Date::isoToMysql($value), 0, 8); | |
152 | break; | |
153 | ||
154 | case 'Timestamp': | |
155 | $value = CRM_Utils_Date::isoToMysql($value); | |
156 | break; | |
157 | } | |
353ffa53 TO |
158 | $inserts[$field] = "%$counter"; |
159 | $updates[] = "$field = %$counter"; | |
6a488035 TO |
160 | $params[$counter] = array($value, $ctypes[$table][$field]); |
161 | $counter++; | |
162 | } | |
163 | if ($changes['log_action'] == 'Delete') { | |
164 | $sql = "INSERT INTO `$table` (" . implode(', ', array_keys($inserts)) . ') VALUES (' . implode(', ', $inserts) . ')'; | |
165 | } | |
166 | else { | |
167 | $sql = "UPDATE `$table` SET " . implode(', ', $updates) . ' WHERE id = %1'; | |
168 | } | |
169 | CRM_Core_DAO::executeQuery($sql, $params); | |
170 | } | |
171 | break; | |
172 | } | |
173 | } | |
174 | ||
175 | // CRM-7353: if nothing altered civicrm_contact, touch it; this will | |
176 | // make sure there’s an entry in log_civicrm_contact for this revert | |
177 | if (empty($diffs['civicrm_contact'])) { | |
178 | $query = " | |
179 | SELECT id FROM `{$this->db}`.log_civicrm_contact | |
180 | WHERE log_conn_id = %1 AND log_date BETWEEN DATE_SUB(%2, INTERVAL 10 SECOND) AND DATE_ADD(%2, INTERVAL 10 SECOND) | |
181 | ORDER BY log_date DESC LIMIT 1 | |
182 | "; | |
183 | $params = array( | |
184 | 1 => array($this->log_conn_id, 'Integer'), | |
185 | 2 => array($this->log_date, 'String'), | |
186 | ); | |
187 | $cid = CRM_Core_DAO::singleValueQuery($query, $params); | |
188 | if (!$cid) { | |
189 | return; | |
190 | } | |
191 | ||
2177d15c | 192 | $dao = new CRM_Contact_DAO_Contact(); |
6a488035 TO |
193 | $dao->id = $cid; |
194 | if ($dao->find(TRUE)) { | |
195 | // CRM-8102: MySQL can’t parse its own dates | |
196 | $dao->birth_date = CRM_Utils_Date::isoToMysql($dao->birth_date); | |
197 | $dao->deceased_date = CRM_Utils_Date::isoToMysql($dao->deceased_date); | |
198 | $dao->save(); | |
199 | } | |
200 | } | |
201 | } | |
96025800 | 202 | |
6a488035 | 203 | } |