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