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