3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 $contactIdClause = $join = '';
70 $params[3] = array($contactID, 'Integer');
72 case 'civicrm_contact':
73 $contactIdClause = "AND id = %3";
76 $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')";
78 case 'civicrm_entity_tag':
79 $contactIdClause = "AND entity_id = %3 AND entity_table = 'civicrm_contact'";
81 case 'civicrm_relationship':
82 $contactIdClause = "AND (contact_id_a = %3 OR contact_id_b = %3)";
84 case 'civicrm_activity':
86 LEFT JOIN civicrm_activity_target at ON at.activity_id = lt.id AND at.target_contact_id = %3
87 LEFT JOIN civicrm_activity_assignment aa ON aa.activity_id = lt.id AND aa.assignee_contact_id = %3
88 LEFT JOIN civicrm_activity source ON source.id = lt.id AND source.source_contact_id = %3";
89 $contactIdClause = "AND (at.id IS NOT NULL OR aa.id IS NOT NULL OR source.id IS NOT NULL)";
92 $contactIdClause = "AND id = (select case_id FROM civicrm_case_contact WHERE contact_id = %3 LIMIT 1)";
95 $contactIdClause = "AND contact_id = %3";
96 if ( strpos($table, 'civicrm_value') !== false ) {
97 $contactIdClause = "AND entity_id = %3";
102 // 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)
104 SELECT DISTINCT lt.id FROM `{$this->db}`.`log_$table` lt
106 WHERE log_conn_id = %1 AND
107 log_date BETWEEN DATE_SUB(%2, INTERVAL {$this->interval}) AND DATE_ADD(%2, INTERVAL {$this->interval})
109 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
110 while ($dao->fetch()) {
111 $diffs = array_merge($diffs, $this->diffsInTableForId($table, $dao->id
));
117 private function diffsInTableForId($table, $id) {
121 1 => array($this->log_conn_id
, 'Integer'),
122 2 => array($this->log_date
, 'String'),
123 3 => array($id, 'Integer'),
126 // 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
127 $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";
129 $changedDAO = CRM_Core_DAO
::executeQuery($changedSQL, $params);
130 while ($changedDAO->fetch( )) {
131 $changed = $changedDAO->toArray();
133 // return early if nothing found
134 if (empty($changed)) {
138 switch ($changed['log_action']) {
140 // the previous state is kept in the current state, current should keep the keys and clear the values
141 $original = $changed;
142 foreach ($changed as & $val) $val = NULL;
143 $changed['log_action'] = 'Delete';
147 // the previous state does not exist
152 // look for the previous state (different log_conn_id) of the given id
153 $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";
154 $original = $this->sqlToArray($originalSQL, $params);
155 if (empty($original)) {
156 // A blank original array is not possible for Update action, otherwise we 'll end up displaying all information
157 // in $changed variable as updated-info
158 $original = $changed;
164 // populate $diffs with only the differences between $changed and $original
165 $skipped = array('log_action', 'log_conn_id', 'log_date', 'log_user_id');
166 foreach (array_keys(array_diff_assoc($changed, $original)) as $diff) {
167 if (in_array($diff, $skipped)) {
171 if (CRM_Utils_Array
::value($diff, $original) === CRM_Utils_Array
::value($diff, $changed)) {
175 // hack: case_type_id column is a varchar with separator. For proper mapping to type labels,
176 // we need to make sure separators are trimmed
177 if ($diff == 'case_type_id') {
178 foreach (array('original', 'changed') as $var) {
179 if (CRM_Utils_Array
::value($diff, $
$var)) {
181 $val = explode(CRM_Case_BAO_Case
::VALUE_SEPARATOR
, $holder[$diff]);
182 $holder[$diff] = CRM_Utils_Array
::value(1, $val);
188 'action' => $changed['log_action'],
191 'from' => CRM_Utils_Array
::value($diff, $original),
192 'to' => CRM_Utils_Array
::value($diff, $changed),
200 function titlesAndValuesForTable($table) {
201 // static caches for subsequent calls with the same $table
202 static $titles = array();
203 static $values = array();
205 // FIXME: split off the table → DAO mapping to a GenCode-generated class
206 static $daos = array(
207 'civicrm_address' => 'CRM_Core_DAO_Address',
208 'civicrm_contact' => 'CRM_Contact_DAO_Contact',
209 'civicrm_email' => 'CRM_Core_DAO_Email',
210 'civicrm_im' => 'CRM_Core_DAO_IM',
211 'civicrm_openid' => 'CRM_Core_DAO_OpenID',
212 'civicrm_phone' => 'CRM_Core_DAO_Phone',
213 'civicrm_website' => 'CRM_Core_DAO_Website',
214 'civicrm_contribution' => 'CRM_Contribute_DAO_Contribution',
215 'civicrm_note' => 'CRM_Core_DAO_Note',
216 'civicrm_relationship' => 'CRM_Contact_DAO_Relationship',
217 'civicrm_activity' => 'CRM_Activity_DAO_Activity',
218 'civicrm_case' => 'CRM_Case_DAO_Case',
221 if (!isset($titles[$table]) or !isset($values[$table])) {
223 if (in_array($table, array_keys($daos))) {
224 // FIXME: these should be populated with pseudo constants as they
225 // were at the time of logging rather than their current values
226 $values[$table] = array(
227 'contribution_page_id' => CRM_Contribute_PseudoConstant
::contributionPage(),
228 'contribution_status_id' => CRM_Contribute_PseudoConstant
::contributionStatus(),
229 'financial_type_id' => CRM_Contribute_PseudoConstant
::financialType(),
230 'country_id' => CRM_Core_PseudoConstant
::country(),
231 'gender_id' => CRM_Core_PseudoConstant
::gender(),
232 'location_type_id' => CRM_Core_PseudoConstant
::locationType(),
233 'payment_instrument_id' => CRM_Contribute_PseudoConstant
::paymentInstrument(),
234 'phone_type_id' => CRM_Core_PseudoConstant
::phoneType(),
235 'preferred_communication_method' => CRM_Core_PseudoConstant
::pcm(),
236 'preferred_language' => CRM_Core_PseudoConstant
::languages(),
237 'prefix_id' => CRM_Core_PseudoConstant
::individualPrefix(),
238 'provider_id' => CRM_Core_PseudoConstant
::IMProvider(),
239 'state_province_id' => CRM_Core_PseudoConstant
::stateProvince(),
240 'suffix_id' => CRM_Core_PseudoConstant
::individualSuffix(),
241 'website_type_id' => CRM_Core_PseudoConstant
::websiteType(),
242 'activity_type_id' => CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, FALSE, 'label', TRUE),
243 'case_type_id' => CRM_Case_PseudoConstant
::caseType('label', FALSE),
244 'priority_id' => CRM_Core_PseudoConstant
::priority(),
247 // for columns that appear in more than 1 table
250 $values[$table]['status_id'] = CRM_Case_PseudoConstant
::caseStatus('label', FALSE);
252 case 'civicrm_activity':
253 $values[$table]['status_id'] = CRM_Core_PseudoConstant
::activityStatus( );
257 require_once str_replace('_', DIRECTORY_SEPARATOR
, $daos[$table]) . '.php';
258 eval("\$dao = new $daos[$table];");
259 foreach ($dao->fields() as $field) {
260 $titles[$table][$field['name']] = CRM_Utils_Array
::value('title', $field);
262 if ($field['type'] == CRM_Utils_Type
::T_BOOLEAN
) {
263 $values[$table][$field['name']] = array('0' => ts('false'), '1' => ts('true'));
267 elseif (substr($table, 0, 14) == 'civicrm_value_') {
268 list($titles[$table], $values[$table]) = $this->titlesAndValuesForCustomDataTable($table);
272 return array($titles[$table], $values[$table]);
275 private function sqlToArray($sql, $params) {
276 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
278 return $dao->toArray();
281 private function titlesAndValuesForCustomDataTable($table) {
286 1 => array($this->log_conn_id
, 'Integer'),
287 2 => array($this->log_date
, 'String'),
288 3 => array($table, 'String'),
291 $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";
292 $cgDao = CRM_Core_DAO
::executeQuery($sql, $params);
295 $params[3] = array($cgDao->id
, 'Integer');
297 SELECT column_name, data_type, label, name, option_group_id
298 FROM `{$this->db}`.log_civicrm_custom_field
300 AND custom_group_id = %3
303 $cfDao = CRM_Core_DAO
::executeQuery($sql, $params);
305 while ($cfDao->fetch()) {
306 $titles[$cfDao->column_name
] = "{$cgDao->title}: {$cfDao->label}";
308 switch ($cfDao->data_type
) {
310 $values[$cfDao->column_name
] = array('0' => ts('false'), '1' => ts('true'));
314 $values[$cfDao->column_name
] = array();
315 if (!empty($cfDao->option_group_id
)) {
316 $params[3] = array($cfDao->option_group_id
, 'Integer');
319 FROM `{$this->db}`.log_civicrm_option_value
321 AND option_group_id = %3
324 $ovDao = CRM_Core_DAO
::executeQuery($sql, $params);
325 while ($ovDao->fetch()) {
326 $values[$cfDao->column_name
][$ovDao->value
] = $ovDao->label
;
333 return array($titles, $values);