3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
35 class CRM_Logging_Differ
{
41 function __construct($log_conn_id, $log_date, $interval = '10 SECOND') {
42 $dsn = defined('CIVICRM_LOGGING_DSN') ? DB
::parseDSN(CIVICRM_LOGGING_DSN
) : DB
::parseDSN(CIVICRM_DSN
);
43 $this->db
= $dsn['database'];
44 $this->log_conn_id
= $log_conn_id;
45 $this->log_date
= $log_date;
46 $this->interval
= $interval;
49 function diffsInTables($tables) {
51 foreach ($tables as $table) {
52 $diff = $this->diffsInTable($table);
54 $diffs[$table] = $diff;
60 function diffsInTable($table, $contactID = null) {
64 1 => array($this->log_conn_id
, 'Integer'),
65 2 => array($this->log_date
, 'String'),
68 $logging = new CRM_Logging_Schema
;
69 $addressCustomTables = $logging->entityCustomDataLogTables('Address');
71 $contactIdClause = $join = '';
73 $params[3] = array($contactID, 'Integer');
75 case 'civicrm_contact':
76 $contactIdClause = "AND id = %3";
79 $contactIdClause = "AND ( entity_id = %3 AND entity_table = 'civicrm_contact' ) OR (entity_id IN (SELECT note.id FROM `{$this->db}`.log_civicrm_note note WHERE note.entity_id = %3 AND note.entity_table = 'civicrm_contact') AND entity_table = 'civicrm_note')";
81 case 'civicrm_entity_tag':
82 $contactIdClause = "AND entity_id = %3 AND entity_table = 'civicrm_contact'";
84 case 'civicrm_relationship':
85 $contactIdClause = "AND (contact_id_a = %3 OR contact_id_b = %3)";
87 case 'civicrm_activity':
88 $activityContacts = CRM_Core_OptionGroup
::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
89 $sourceID = CRM_Utils_Array
::key('Activity Source', $activityContacts);
90 $assigneeID = CRM_Utils_Array
::key('Activity Assignees', $activityContacts);
91 $targetID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
94 LEFT JOIN civicrm_activity_contact at ON at.activity_id = lt.id AND at.contact_id = %3 AND at.record_type_id = {$targetID}
95 LEFT JOIN civicrm_activity_contact aa ON aa.activity_id = lt.id AND aa.contact_id = %3 AND aa.record_type_id = {$assigneeID}
96 LEFT JOIN civicrm_activity_contact source ON source.activity_id = lt.id AND source.contact_id = %3 AND source.record_type_id = {$sourceID} ";
97 $contactIdClause = "AND (at.id IS NOT NULL OR aa.id IS NOT NULL OR source.id IS NOT NULL)";
100 $contactIdClause = "AND id = (select case_id FROM civicrm_case_contact WHERE contact_id = %3 LIMIT 1)";
103 if (array_key_exists($table, $addressCustomTables)) {
104 $join = "INNER JOIN `{$this->db}`.`log_civicrm_address` et ON et.id = lt.entity_id";
105 $contactIdClause = "AND contact_id = %3";
109 // allow tables to be extended by report hook query objects
110 list($contactIdClause, $join) = CRM_Report_BAO_Hook
::singleton()->logDiffClause($this, $table);
112 if (empty($contactIdClause)) {
113 $contactIdClause = "AND contact_id = %3";
115 if ( strpos($table, 'civicrm_value') !== false ) {
116 $contactIdClause = "AND entity_id = %3";
121 // find ids in this table that were affected in the given connection (based on connection id and a ±10 s time period around the date)
123 SELECT DISTINCT lt.id FROM `{$this->db}`.`log_$table` lt
125 WHERE lt.log_conn_id = %1 AND
126 lt.log_date BETWEEN DATE_SUB(%2, INTERVAL {$this->interval}) AND DATE_ADD(%2, INTERVAL {$this->interval})
129 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
130 while ($dao->fetch()) {
131 $diffs = array_merge($diffs, $this->diffsInTableForId($table, $dao->id
));
137 private function diffsInTableForId($table, $id) {
141 1 => array($this->log_conn_id
, 'Integer'),
142 2 => array($this->log_date
, 'String'),
143 3 => array($id, 'Integer'),
146 // look for all the changes in the given connection that happended less than {$this->interval} s later than log_date to the given id to catch multi-query changes
147 $changedSQL = "SELECT * FROM `{$this->db}`.`log_$table` WHERE log_conn_id = %1 AND log_date >= %2 AND log_date < DATE_ADD(%2, INTERVAL {$this->interval}) AND id = %3 ORDER BY log_date DESC LIMIT 1";
149 $changedDAO = CRM_Core_DAO
::executeQuery($changedSQL, $params);
150 while ($changedDAO->fetch( )) {
151 $changed = $changedDAO->toArray();
153 // return early if nothing found
154 if (empty($changed)) {
158 switch ($changed['log_action']) {
160 // the previous state is kept in the current state, current should keep the keys and clear the values
161 $original = $changed;
162 foreach ($changed as & $val) $val = NULL;
163 $changed['log_action'] = 'Delete';
167 // the previous state does not exist
172 // look for the previous state (different log_conn_id) of the given id
173 $originalSQL = "SELECT * FROM `{$this->db}`.`log_$table` WHERE log_conn_id != %1 AND log_date < %2 AND id = %3 ORDER BY log_date DESC LIMIT 1";
174 $original = $this->sqlToArray($originalSQL, $params);
175 if (empty($original)) {
176 // A blank original array is not possible for Update action, otherwise we 'll end up displaying all information
177 // in $changed variable as updated-info
178 $original = $changed;
184 // populate $diffs with only the differences between $changed and $original
185 $skipped = array('log_action', 'log_conn_id', 'log_date', 'log_user_id');
186 foreach (array_keys(array_diff_assoc($changed, $original)) as $diff) {
187 if (in_array($diff, $skipped)) {
191 if (CRM_Utils_Array
::value($diff, $original) === CRM_Utils_Array
::value($diff, $changed)) {
195 // hack: case_type_id column is a varchar with separator. For proper mapping to type labels,
196 // we need to make sure separators are trimmed
197 if ($diff == 'case_type_id') {
198 foreach (array('original', 'changed') as $var) {
199 if (!empty($
$var[$diff])) {
201 $val = explode(CRM_Case_BAO_Case
::VALUE_SEPARATOR
, $holder[$diff]);
202 $holder[$diff] = CRM_Utils_Array
::value(1, $val);
208 'action' => $changed['log_action'],
211 'from' => CRM_Utils_Array
::value($diff, $original),
212 'to' => CRM_Utils_Array
::value($diff, $changed),
220 function titlesAndValuesForTable($table) {
221 // static caches for subsequent calls with the same $table
222 static $titles = array();
223 static $values = array();
225 // FIXME: split off the table → DAO mapping to a GenCode-generated class
226 static $daos = array(
227 'civicrm_address' => 'CRM_Core_DAO_Address',
228 'civicrm_contact' => 'CRM_Contact_DAO_Contact',
229 'civicrm_email' => 'CRM_Core_DAO_Email',
230 'civicrm_im' => 'CRM_Core_DAO_IM',
231 'civicrm_openid' => 'CRM_Core_DAO_OpenID',
232 'civicrm_phone' => 'CRM_Core_DAO_Phone',
233 'civicrm_website' => 'CRM_Core_DAO_Website',
234 'civicrm_contribution' => 'CRM_Contribute_DAO_Contribution',
235 'civicrm_note' => 'CRM_Core_DAO_Note',
236 'civicrm_relationship' => 'CRM_Contact_DAO_Relationship',
237 'civicrm_activity' => 'CRM_Activity_DAO_Activity',
238 'civicrm_case' => 'CRM_Case_DAO_Case',
241 if (!isset($titles[$table]) or !isset($values[$table])) {
243 if (in_array($table, array_keys($daos))) {
244 // FIXME: these should be populated with pseudo constants as they
245 // were at the time of logging rather than their current values
246 // FIXME: Use *_BAO:buildOptions() method rather than pseudoconstants & fetch programmatically
247 $values[$table] = array(
248 'contribution_page_id' => CRM_Contribute_PseudoConstant
::contributionPage(),
249 'contribution_status_id' => CRM_Contribute_PseudoConstant
::contributionStatus(),
250 'financial_type_id' => CRM_Contribute_PseudoConstant
::financialType(),
251 'country_id' => CRM_Core_PseudoConstant
::country(),
252 'gender_id' => CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id'),
253 'location_type_id' => CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id'),
254 'payment_instrument_id' => CRM_Contribute_PseudoConstant
::paymentInstrument(),
255 'phone_type_id' => CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', 'phone_type_id'),
256 'preferred_communication_method' => CRM_Contact_BAO_Contact
::buildOptions('preferred_communication_method'),
257 'preferred_language' => CRM_Contact_BAO_Contact
::buildOptions('preferred_language'),
258 'prefix_id' => CRM_Contact_BAO_Contact
::buildOptions('prefix_id'),
259 'provider_id' => CRM_Core_PseudoConstant
::get('CRM_Core_DAO_IM', 'provider_id'),
260 'state_province_id' => CRM_Core_PseudoConstant
::stateProvince(),
261 'suffix_id' => CRM_Contact_BAO_Contact
::buildOptions('suffix_id'),
262 'website_type_id' => CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Website', 'website_type_id'),
263 'activity_type_id' => CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, FALSE, 'label', TRUE),
264 'case_type_id' => CRM_Case_PseudoConstant
::caseType('title', FALSE),
265 'priority_id' => CRM_Core_PseudoConstant
::get('CRM_Activity_DAO_Activity', 'priority_id'),
268 // for columns that appear in more than 1 table
271 $values[$table]['status_id'] = CRM_Case_PseudoConstant
::caseStatus('label', FALSE);
273 case 'civicrm_activity':
274 $values[$table]['status_id'] = CRM_Core_PseudoConstant
::activityStatus( );
278 $dao = new $daos[$table];
279 foreach ($dao->fields() as $field) {
280 $titles[$table][$field['name']] = CRM_Utils_Array
::value('title', $field);
282 if ($field['type'] == CRM_Utils_Type
::T_BOOLEAN
) {
283 $values[$table][$field['name']] = array('0' => ts('false'), '1' => ts('true'));
287 elseif (substr($table, 0, 14) == 'civicrm_value_') {
288 list($titles[$table], $values[$table]) = $this->titlesAndValuesForCustomDataTable($table);
290 $titles[$table] = $values[$table] = array();
294 return array($titles[$table], $values[$table]);
297 private function sqlToArray($sql, $params) {
298 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
300 return $dao->toArray();
303 private function titlesAndValuesForCustomDataTable($table) {
308 1 => array($this->log_conn_id
, 'Integer'),
309 2 => array($this->log_date
, 'String'),
310 3 => array($table, 'String'),
313 $sql = "SELECT id, title FROM `{$this->db}`.log_civicrm_custom_group WHERE log_date <= %2 AND table_name = %3 ORDER BY log_date DESC LIMIT 1";
314 $cgDao = CRM_Core_DAO
::executeQuery($sql, $params);
317 $params[3] = array($cgDao->id
, 'Integer');
319 SELECT column_name, data_type, label, name, option_group_id
320 FROM `{$this->db}`.log_civicrm_custom_field
322 AND custom_group_id = %3
325 $cfDao = CRM_Core_DAO
::executeQuery($sql, $params);
327 while ($cfDao->fetch()) {
328 $titles[$cfDao->column_name
] = "{$cgDao->title}: {$cfDao->label}";
330 switch ($cfDao->data_type
) {
332 $values[$cfDao->column_name
] = array('0' => ts('false'), '1' => ts('true'));
336 $values[$cfDao->column_name
] = array();
337 if (!empty($cfDao->option_group_id
)) {
338 $params[3] = array($cfDao->option_group_id
, 'Integer');
341 FROM `{$this->db}`.log_civicrm_option_value
343 AND option_group_id = %3
346 $ovDao = CRM_Core_DAO
::executeQuery($sql, $params);
347 while ($ovDao->fetch()) {
348 $values[$cfDao->column_name
][$ovDao->value
] = $ovDao->label
;
355 return array($titles, $values);