| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 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-2014 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | class CRM_Logging_Reverter { |
| 36 | private $db; |
| 37 | private $log_conn_id; |
| 38 | private $log_date; |
| 39 | |
| 40 | /** |
| 41 | * @param int $log_conn_id |
| 42 | * @param $log_date |
| 43 | */ |
| 44 | function __construct($log_conn_id, $log_date) { |
| 45 | $dsn = defined('CIVICRM_LOGGING_DSN') ? DB::parseDSN(CIVICRM_LOGGING_DSN) : DB::parseDSN(CIVICRM_DSN); |
| 46 | $this->db = $dsn['database']; |
| 47 | $this->log_conn_id = $log_conn_id; |
| 48 | $this->log_date = $log_date; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @param $tables |
| 53 | */ |
| 54 | function revert($tables) { |
| 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)): |
| 119 | $dao = new $daos[$table]; |
| 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; |
| 135 | // custom data tables |
| 136 | |
| 137 | case in_array($table, array_keys($ctypes)): |
| 138 | foreach ($row as $id => $changes) { |
| 139 | $inserts = array('id' => '%1'); |
| 140 | $updates = array(); |
| 141 | $params = array(1 => array($id, 'Integer')); |
| 142 | $counter = 2; |
| 143 | foreach ($changes as $field => $value) { |
| 144 | // don’t try reverting a field that’s no longer there |
| 145 | if (!isset($ctypes[$table][$field])) { |
| 146 | continue; |
| 147 | } |
| 148 | switch ($ctypes[$table][$field]) { |
| 149 | case 'Date': |
| 150 | $value = substr(CRM_Utils_Date::isoToMysql($value), 0, 8); |
| 151 | break; |
| 152 | |
| 153 | case 'Timestamp': |
| 154 | $value = CRM_Utils_Date::isoToMysql($value); |
| 155 | break; |
| 156 | } |
| 157 | $inserts[$field] = "%$counter"; |
| 158 | $updates[] = "$field = %$counter"; |
| 159 | $params[$counter] = array($value, $ctypes[$table][$field]); |
| 160 | $counter++; |
| 161 | } |
| 162 | if ($changes['log_action'] == 'Delete') { |
| 163 | $sql = "INSERT INTO `$table` (" . implode(', ', array_keys($inserts)) . ') VALUES (' . implode(', ', $inserts) . ')'; |
| 164 | } |
| 165 | else { |
| 166 | $sql = "UPDATE `$table` SET " . implode(', ', $updates) . ' WHERE id = %1'; |
| 167 | } |
| 168 | CRM_Core_DAO::executeQuery($sql, $params); |
| 169 | } |
| 170 | break; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | // CRM-7353: if nothing altered civicrm_contact, touch it; this will |
| 175 | // make sure there’s an entry in log_civicrm_contact for this revert |
| 176 | if (empty($diffs['civicrm_contact'])) { |
| 177 | $query = " |
| 178 | SELECT id FROM `{$this->db}`.log_civicrm_contact |
| 179 | WHERE log_conn_id = %1 AND log_date BETWEEN DATE_SUB(%2, INTERVAL 10 SECOND) AND DATE_ADD(%2, INTERVAL 10 SECOND) |
| 180 | ORDER BY log_date DESC LIMIT 1 |
| 181 | "; |
| 182 | $params = array( |
| 183 | 1 => array($this->log_conn_id, 'Integer'), |
| 184 | 2 => array($this->log_date, 'String'), |
| 185 | ); |
| 186 | $cid = CRM_Core_DAO::singleValueQuery($query, $params); |
| 187 | if (!$cid) { |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | $dao = new CRM_Contact_DAO_Contact; |
| 192 | $dao->id = $cid; |
| 193 | if ($dao->find(TRUE)) { |
| 194 | // CRM-8102: MySQL can’t parse its own dates |
| 195 | $dao->birth_date = CRM_Utils_Date::isoToMysql($dao->birth_date); |
| 196 | $dao->deceased_date = CRM_Utils_Date::isoToMysql($dao->deceased_date); |
| 197 | $dao->save(); |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |