3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2017
33 class CRM_Logging_Reverter
{
39 * The diffs to be reverted.
43 private $diffs = array();
48 * @param string $log_conn_id
51 public function __construct($log_conn_id, $log_date) {
52 $dsn = defined('CIVICRM_LOGGING_DSN') ? DB
::parseDSN(CIVICRM_LOGGING_DSN
) : DB
::parseDSN(CIVICRM_DSN
);
53 $this->db
= $dsn['database'];
54 $this->log_conn_id
= $log_conn_id;
55 $this->log_date
= $log_date;
60 * Calculate a set of diffs based on the connection_id and changes at a close time.
62 * @param array $tables
64 public function calculateDiffsFromLogConnAndDate($tables) {
65 $differ = new CRM_Logging_Differ($this->log_conn_id
, $this->log_date
);
66 $this->diffs
= $differ->diffsInTables($tables);
74 public function setDiffs($diffs) {
75 $this->diffs
= $diffs;
79 * Revert changes in the array of diffs in $this->diffs.
81 public function revert() {
83 // get custom data tables, columns and types
85 $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)');
86 while ($dao->fetch()) {
87 if (!isset($ctypes[$dao->table_name
])) {
88 $ctypes[$dao->table_name
] = array('entity_id' => 'Integer');
90 $ctypes[$dao->table_name
][$dao->column_name
] = $dao->data_type
;
93 $diffs = $this->diffs
;
96 foreach ($diffs as $table => $changes) {
97 foreach ($changes as $change) {
98 switch ($change['action']) {
100 if (!isset($deletes[$table])) {
101 $deletes[$table] = array();
103 $deletes[$table][] = $change['id'];
108 if (!isset($reverts[$table])) {
109 $reverts[$table] = array();
111 if (!isset($reverts[$table][$change['id']])) {
112 $reverts[$table][$change['id']] = array('log_action' => $change['action']);
114 $reverts[$table][$change['id']][$change['field']] = $change['from'];
120 // revert inserts by deleting
121 foreach ($deletes as $table => $ids) {
122 CRM_Core_DAO
::executeQuery("DELETE FROM `$table` WHERE id IN (" . implode(', ', array_unique($ids)) . ')');
125 // revert updates by updating to previous values
126 foreach ($reverts as $table => $row) {
130 case (($tableDAO = CRM_Core_DAO_AllCoreTables
::getClassForTable($table)) != FALSE):
131 $dao = new $tableDAO ();
132 foreach ($row as $id => $changes) {
134 foreach ($changes as $field => $value) {
135 if ($field == 'log_action') {
138 if (empty($value) and $value !== 0 and $value !== '0') {
141 // Date reaches this point in ISO format (possibly) so strip out stuff
142 // if it does have hyphens of colons demarking the date & it regexes as being a date
143 // or datetime format.
144 if (preg_match('/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/', $value)) {
145 $value = str_replace('-', '', $value);
146 $value = str_replace(':', '', $value);
148 $dao->$field = $value;
150 $changes['log_action'] == 'Delete' ?
$dao->insert() : $dao->update();
156 // custom data tables
158 case in_array($table, array_keys($ctypes)):
159 foreach ($row as $id => $changes) {
160 $inserts = array('id' => '%1');
162 $params = array(1 => array($id, 'Integer'));
164 foreach ($changes as $field => $value) {
165 // don’t try reverting a field that’s no longer there
166 if (!isset($ctypes[$table][$field])) {
169 $fldVal = "%{$counter}";
170 switch ($ctypes[$table][$field]) {
172 $value = substr(CRM_Utils_Date
::isoToMysql($value), 0, 8);
176 $value = CRM_Utils_Date
::isoToMysql($value);
184 $inserts[$field] = "%$counter";
185 $updates[] = "{$field} = {$fldVal}";
186 if ($fldVal != 'DEFAULT') {
187 $params[$counter] = array($value, $ctypes[$table][$field]);
191 if ($changes['log_action'] == 'Delete') {
192 $sql = "INSERT INTO `$table` (" . implode(', ', array_keys($inserts)) . ') VALUES (' . implode(', ', $inserts) . ')';
195 $sql = "UPDATE `$table` SET " . implode(', ', $updates) . ' WHERE id = %1';
197 CRM_Core_DAO
::executeQuery($sql, $params);