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