Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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-2013 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | class CRM_Logging_Differ { | |
36 | private $db; | |
37 | private $log_conn_id; | |
38 | private $log_date; | |
39 | private $interval; | |
40 | ||
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; | |
47 | } | |
48 | ||
49 | function diffsInTables($tables) { | |
50 | $diffs = array(); | |
51 | foreach ($tables as $table) { | |
52 | $diff = $this->diffsInTable($table); | |
53 | if (!empty($diff)) { | |
54 | $diffs[$table] = $diff; | |
55 | } | |
56 | } | |
57 | return $diffs; | |
58 | } | |
59 | ||
60 | function diffsInTable($table, $contactID = null) { | |
61 | $diffs = array(); | |
62 | ||
63 | $params = array( | |
64 | 1 => array($this->log_conn_id, 'Integer'), | |
65 | 2 => array($this->log_date, 'String'), | |
66 | ); | |
67 | ||
0b4c85c3 | 68 | $contactIdClause = $join = ''; |
6a488035 TO |
69 | if ( $contactID ) { |
70 | $params[3] = array($contactID, 'Integer'); | |
71 | switch ($table) { | |
72 | case 'civicrm_contact': | |
73 | $contactIdClause = "AND id = %3"; | |
74 | break; | |
75 | case 'civicrm_note': | |
b10fb376 | 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')"; |
6a488035 TO |
77 | break; |
78 | case 'civicrm_entity_tag': | |
79 | $contactIdClause = "AND entity_id = %3 AND entity_table = 'civicrm_contact'"; | |
80 | break; | |
81 | case 'civicrm_relationship': | |
82 | $contactIdClause = "AND (contact_id_a = %3 OR contact_id_b = %3)"; | |
83 | break; | |
84 | case 'civicrm_activity': | |
e7e657f0 | 85 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
9e74e3ce | 86 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
87 | $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); | |
88 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); | |
89 | ||
0b4c85c3 | 90 | $join = " |
9e74e3ce | 91 | LEFT JOIN civicrm_activity_contact at ON at.activity_id = lt.id AND at.contact_id = %3 AND at.record_type_id = {$targetID} |
92 | LEFT JOIN civicrm_activity_contact aa ON aa.activity_id = lt.id AND aa.contact_id = %3 AND aa.record_type_id = {$assigneeID} | |
93 | LEFT JOIN civicrm_activity_contact source ON source.activity_id = lt.id AND source.contact_id = %3 AND source.record_type_id = {$sourceID} "; | |
0b4c85c3 | 94 | $contactIdClause = "AND (at.id IS NOT NULL OR aa.id IS NOT NULL OR source.id IS NOT NULL)"; |
6a488035 TO |
95 | break; |
96 | case 'civicrm_case': | |
97 | $contactIdClause = "AND id = (select case_id FROM civicrm_case_contact WHERE contact_id = %3 LIMIT 1)"; | |
98 | break; | |
99 | default: | |
100 | $contactIdClause = "AND contact_id = %3"; | |
101 | if ( strpos($table, 'civicrm_value') !== false ) { | |
102 | $contactIdClause = "AND entity_id = %3"; | |
103 | } | |
104 | } | |
105 | } | |
106 | ||
107 | // 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) | |
0b4c85c3 | 108 | $sql = " |
8ef12e64 | 109 | SELECT DISTINCT lt.id FROM `{$this->db}`.`log_$table` lt |
110 | {$join} | |
111 | WHERE log_conn_id = %1 AND | |
112 | log_date BETWEEN DATE_SUB(%2, INTERVAL {$this->interval}) AND DATE_ADD(%2, INTERVAL {$this->interval}) | |
0b4c85c3 | 113 | {$contactIdClause}"; |
5a1bd70b | 114 | |
6a488035 TO |
115 | $dao = CRM_Core_DAO::executeQuery($sql, $params); |
116 | while ($dao->fetch()) { | |
117 | $diffs = array_merge($diffs, $this->diffsInTableForId($table, $dao->id)); | |
118 | } | |
119 | ||
120 | return $diffs; | |
121 | } | |
122 | ||
123 | private function diffsInTableForId($table, $id) { | |
124 | $diffs = array(); | |
125 | ||
126 | $params = array( | |
127 | 1 => array($this->log_conn_id, 'Integer'), | |
128 | 2 => array($this->log_date, 'String'), | |
129 | 3 => array($id, 'Integer'), | |
130 | ); | |
131 | ||
132 | // 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 | |
133 | $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"; | |
134 | ||
135 | $changedDAO = CRM_Core_DAO::executeQuery($changedSQL, $params); | |
136 | while ($changedDAO->fetch( )) { | |
137 | $changed = $changedDAO->toArray(); | |
138 | ||
139 | // return early if nothing found | |
140 | if (empty($changed)) { | |
141 | continue; | |
142 | } | |
143 | ||
144 | switch ($changed['log_action']) { | |
145 | case 'Delete': | |
146 | // the previous state is kept in the current state, current should keep the keys and clear the values | |
147 | $original = $changed; | |
148 | foreach ($changed as & $val) $val = NULL; | |
149 | $changed['log_action'] = 'Delete'; | |
150 | break; | |
151 | ||
152 | case 'Insert': | |
153 | // the previous state does not exist | |
154 | $original = array(); | |
155 | break; | |
156 | ||
157 | case 'Update': | |
158 | // look for the previous state (different log_conn_id) of the given id | |
159 | $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"; | |
160 | $original = $this->sqlToArray($originalSQL, $params); | |
161 | if (empty($original)) { | |
8ef12e64 | 162 | // A blank original array is not possible for Update action, otherwise we 'll end up displaying all information |
6a488035 TO |
163 | // in $changed variable as updated-info |
164 | $original = $changed; | |
165 | } | |
166 | ||
167 | break; | |
168 | } | |
169 | ||
170 | // populate $diffs with only the differences between $changed and $original | |
171 | $skipped = array('log_action', 'log_conn_id', 'log_date', 'log_user_id'); | |
172 | foreach (array_keys(array_diff_assoc($changed, $original)) as $diff) { | |
173 | if (in_array($diff, $skipped)) { | |
174 | continue; | |
175 | } | |
176 | ||
177 | if (CRM_Utils_Array::value($diff, $original) === CRM_Utils_Array::value($diff, $changed)) { | |
178 | continue; | |
179 | } | |
8ef12e64 | 180 | |
181 | // hack: case_type_id column is a varchar with separator. For proper mapping to type labels, | |
6a488035 TO |
182 | // we need to make sure separators are trimmed |
183 | if ($diff == 'case_type_id') { | |
184 | foreach (array('original', 'changed') as $var) { | |
185 | if (CRM_Utils_Array::value($diff, $$var)) { | |
186 | $holder =& $$var; | |
187 | $val = explode(CRM_Case_BAO_Case::VALUE_SEPARATOR, $holder[$diff]); | |
188 | $holder[$diff] = CRM_Utils_Array::value(1, $val); | |
189 | } | |
190 | } | |
191 | } | |
192 | ||
193 | $diffs[] = array( | |
194 | 'action' => $changed['log_action'], | |
195 | 'id' => $id, | |
196 | 'field' => $diff, | |
197 | 'from' => CRM_Utils_Array::value($diff, $original), | |
198 | 'to' => CRM_Utils_Array::value($diff, $changed), | |
199 | ); | |
200 | } | |
201 | } | |
202 | ||
203 | return $diffs; | |
204 | } | |
205 | ||
206 | function titlesAndValuesForTable($table) { | |
207 | // static caches for subsequent calls with the same $table | |
208 | static $titles = array(); | |
209 | static $values = array(); | |
210 | ||
211 | // FIXME: split off the table → DAO mapping to a GenCode-generated class | |
212 | static $daos = array( | |
213 | 'civicrm_address' => 'CRM_Core_DAO_Address', | |
214 | 'civicrm_contact' => 'CRM_Contact_DAO_Contact', | |
215 | 'civicrm_email' => 'CRM_Core_DAO_Email', | |
216 | 'civicrm_im' => 'CRM_Core_DAO_IM', | |
217 | 'civicrm_openid' => 'CRM_Core_DAO_OpenID', | |
218 | 'civicrm_phone' => 'CRM_Core_DAO_Phone', | |
219 | 'civicrm_website' => 'CRM_Core_DAO_Website', | |
220 | 'civicrm_contribution' => 'CRM_Contribute_DAO_Contribution', | |
221 | 'civicrm_note' => 'CRM_Core_DAO_Note', | |
222 | 'civicrm_relationship' => 'CRM_Contact_DAO_Relationship', | |
223 | 'civicrm_activity' => 'CRM_Activity_DAO_Activity', | |
224 | 'civicrm_case' => 'CRM_Case_DAO_Case', | |
225 | ); | |
226 | ||
227 | if (!isset($titles[$table]) or !isset($values[$table])) { | |
228 | ||
229 | if (in_array($table, array_keys($daos))) { | |
230 | // FIXME: these should be populated with pseudo constants as they | |
231 | // were at the time of logging rather than their current values | |
c0c9cd82 | 232 | // FIXME: Use *_BAO:buildOptions() method rather than pseudoconstants & fetch programmatically |
6a488035 TO |
233 | $values[$table] = array( |
234 | 'contribution_page_id' => CRM_Contribute_PseudoConstant::contributionPage(), | |
235 | 'contribution_status_id' => CRM_Contribute_PseudoConstant::contributionStatus(), | |
236 | 'financial_type_id' => CRM_Contribute_PseudoConstant::financialType(), | |
237 | 'country_id' => CRM_Core_PseudoConstant::country(), | |
26cf88b5 | 238 | 'gender_id' => CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'), |
b2b0530a | 239 | 'location_type_id' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'), |
6a488035 | 240 | 'payment_instrument_id' => CRM_Contribute_PseudoConstant::paymentInstrument(), |
b4f964d9 | 241 | 'phone_type_id' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'), |
c0c9cd82 CW |
242 | 'preferred_communication_method' => CRM_Contact_BAO_Contact::buildOptions('preferred_communication_method'), |
243 | 'preferred_language' => CRM_Contact_BAO_Contact::buildOptions('preferred_language'), | |
244 | 'prefix_id' => CRM_Contact_BAO_Contact::buildOptions('prefix_id'), | |
e7e657f0 | 245 | 'provider_id' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id'), |
6a488035 | 246 | 'state_province_id' => CRM_Core_PseudoConstant::stateProvince(), |
c0c9cd82 | 247 | 'suffix_id' => CRM_Contact_BAO_Contact::buildOptions('suffix_id'), |
cbf48754 | 248 | 'website_type_id' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_Website', 'website_type_id'), |
6a488035 TO |
249 | 'activity_type_id' => CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE), |
250 | 'case_type_id' => CRM_Case_PseudoConstant::caseType('label', FALSE), | |
cbf48754 | 251 | 'priority_id' => CRM_Core_PseudoConstant::get('CRM_Activity_DAO_Activity', 'priority_id'), |
6a488035 TO |
252 | ); |
253 | ||
254 | // for columns that appear in more than 1 table | |
255 | switch ($table) { | |
256 | case 'civicrm_case': | |
257 | $values[$table]['status_id'] = CRM_Case_PseudoConstant::caseStatus('label', FALSE); | |
258 | break; | |
259 | case 'civicrm_activity': | |
260 | $values[$table]['status_id'] = CRM_Core_PseudoConstant::activityStatus( ); | |
261 | break; | |
262 | } | |
263 | ||
4d5c2eb5 | 264 | $dao = new $daos[$table]; |
6a488035 TO |
265 | foreach ($dao->fields() as $field) { |
266 | $titles[$table][$field['name']] = CRM_Utils_Array::value('title', $field); | |
267 | ||
268 | if ($field['type'] == CRM_Utils_Type::T_BOOLEAN) { | |
269 | $values[$table][$field['name']] = array('0' => ts('false'), '1' => ts('true')); | |
270 | } | |
271 | } | |
272 | } | |
273 | elseif (substr($table, 0, 14) == 'civicrm_value_') { | |
274 | list($titles[$table], $values[$table]) = $this->titlesAndValuesForCustomDataTable($table); | |
275 | } | |
276 | } | |
277 | ||
278 | return array($titles[$table], $values[$table]); | |
279 | } | |
280 | ||
281 | private function sqlToArray($sql, $params) { | |
282 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
283 | $dao->fetch(); | |
284 | return $dao->toArray(); | |
285 | } | |
286 | ||
287 | private function titlesAndValuesForCustomDataTable($table) { | |
288 | $titles = array(); | |
289 | $values = array(); | |
290 | ||
291 | $params = array( | |
292 | 1 => array($this->log_conn_id, 'Integer'), | |
293 | 2 => array($this->log_date, 'String'), | |
294 | 3 => array($table, 'String'), | |
295 | ); | |
296 | ||
297 | $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"; | |
298 | $cgDao = CRM_Core_DAO::executeQuery($sql, $params); | |
299 | $cgDao->fetch(); | |
300 | ||
301 | $params[3] = array($cgDao->id, 'Integer'); | |
302 | $sql = " | |
303 | SELECT column_name, data_type, label, name, option_group_id | |
304 | FROM `{$this->db}`.log_civicrm_custom_field | |
305 | WHERE log_date <= %2 | |
306 | AND custom_group_id = %3 | |
307 | ORDER BY log_date | |
308 | "; | |
309 | $cfDao = CRM_Core_DAO::executeQuery($sql, $params); | |
310 | ||
311 | while ($cfDao->fetch()) { | |
312 | $titles[$cfDao->column_name] = "{$cgDao->title}: {$cfDao->label}"; | |
313 | ||
314 | switch ($cfDao->data_type) { | |
315 | case 'Boolean': | |
316 | $values[$cfDao->column_name] = array('0' => ts('false'), '1' => ts('true')); | |
317 | break; | |
318 | ||
319 | case 'String': | |
320 | $values[$cfDao->column_name] = array(); | |
321 | if (!empty($cfDao->option_group_id)) { | |
322 | $params[3] = array($cfDao->option_group_id, 'Integer'); | |
323 | $sql = " | |
324 | SELECT label, value | |
325 | FROM `{$this->db}`.log_civicrm_option_value | |
326 | WHERE log_date <= %2 | |
327 | AND option_group_id = %3 | |
328 | ORDER BY log_date | |
329 | "; | |
330 | $ovDao = CRM_Core_DAO::executeQuery($sql, $params); | |
331 | while ($ovDao->fetch()) { | |
332 | $values[$cfDao->column_name][$ovDao->value] = $ovDao->label; | |
333 | } | |
334 | } | |
335 | break; | |
336 | } | |
337 | } | |
338 | ||
339 | return array($titles, $values); | |
340 | } | |
341 | } | |
342 |