Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
6a488035 | 32 | * $Id$ |
6a488035 TO |
33 | */ |
34 | class CRM_Logging_ReportSummary extends CRM_Report_Form { | |
35 | protected $cid; | |
36 | ||
37 | protected $_logTables = array(); | |
38 | ||
39 | protected $loggingDB; | |
40 | ||
41c9c9e9 | 41 | /** |
42 | * The log table currently being processed. | |
43 | * | |
44 | * @var string | |
45 | */ | |
46 | protected $currentLogTable; | |
47 | ||
e0ef6999 | 48 | /** |
2fb1dd66 | 49 | * Class constructor. |
e0ef6999 | 50 | */ |
00be9182 | 51 | public function __construct() { |
694e78fd DS |
52 | // don’t display the ‘Add these Contacts to Group’ button |
53 | $this->_add2groupSupported = FALSE; | |
54 | ||
55 | $dsn = defined('CIVICRM_LOGGING_DSN') ? DB::parseDSN(CIVICRM_LOGGING_DSN) : DB::parseDSN(CIVICRM_DSN); | |
56 | $this->loggingDB = $dsn['database']; | |
57 | ||
58 | // used for redirect back to contact summary | |
41c9c9e9 | 59 | $this->cid = CRM_Utils_Request::retrieve('cid', 'Integer'); |
cbb9666e | 60 | |
ae5ffbb7 TO |
61 | $this->_logTables = array( |
62 | 'log_civicrm_contact' => array( | |
63 | 'fk' => 'id', | |
64 | ), | |
65 | 'log_civicrm_email' => array( | |
66 | 'fk' => 'contact_id', | |
67 | 'log_type' => 'Contact', | |
68 | ), | |
69 | 'log_civicrm_phone' => array( | |
70 | 'fk' => 'contact_id', | |
71 | 'log_type' => 'Contact', | |
72 | ), | |
73 | 'log_civicrm_address' => array( | |
74 | 'fk' => 'contact_id', | |
75 | 'log_type' => 'Contact', | |
76 | ), | |
77 | 'log_civicrm_note' => array( | |
78 | 'fk' => 'entity_id', | |
79 | 'entity_table' => TRUE, | |
80 | 'bracket_info' => array( | |
81 | 'table' => 'log_civicrm_note', | |
82 | 'column' => 'subject', | |
cbb9666e | 83 | ), |
ae5ffbb7 TO |
84 | ), |
85 | 'log_civicrm_note_comment' => array( | |
86 | 'fk' => 'entity_id', | |
87 | 'table_name' => 'log_civicrm_note', | |
88 | 'joins' => array( | |
89 | 'table' => 'log_civicrm_note', | |
90 | 'join' => "entity_log_civireport.entity_id = fk_table.id AND entity_log_civireport.entity_table = 'civicrm_note'", | |
cbb9666e | 91 | ), |
ae5ffbb7 TO |
92 | 'entity_table' => TRUE, |
93 | 'bracket_info' => array( | |
94 | 'table' => 'log_civicrm_note', | |
95 | 'column' => 'subject', | |
cbb9666e | 96 | ), |
ae5ffbb7 TO |
97 | ), |
98 | 'log_civicrm_group_contact' => array( | |
99 | 'fk' => 'contact_id', | |
100 | 'bracket_info' => array( | |
101 | 'entity_column' => 'group_id', | |
102 | 'table' => 'log_civicrm_group', | |
103 | 'column' => 'title', | |
cbb9666e | 104 | ), |
ae5ffbb7 TO |
105 | 'action_column' => 'status', |
106 | 'log_type' => 'Group', | |
107 | ), | |
108 | 'log_civicrm_entity_tag' => array( | |
109 | 'fk' => 'entity_id', | |
110 | 'bracket_info' => array( | |
111 | 'entity_column' => 'tag_id', | |
112 | 'table' => 'log_civicrm_tag', | |
113 | 'column' => 'name', | |
cbb9666e | 114 | ), |
ae5ffbb7 TO |
115 | 'entity_table' => TRUE, |
116 | ), | |
117 | 'log_civicrm_relationship' => array( | |
118 | 'fk' => 'contact_id_a', | |
119 | 'bracket_info' => array( | |
120 | 'entity_column' => 'relationship_type_id', | |
121 | 'table' => 'log_civicrm_relationship_type', | |
122 | 'column' => 'label_a_b', | |
cbb9666e | 123 | ), |
ae5ffbb7 | 124 | ), |
41c9c9e9 | 125 | 'log_civicrm_activity_contact' => array( |
ae5ffbb7 | 126 | 'fk' => 'contact_id', |
41c9c9e9 | 127 | 'table_name' => 'log_civicrm_activity_contact', |
ae5ffbb7 | 128 | 'log_type' => 'Activity', |
41c9c9e9 | 129 | 'field' => 'activity_id', |
b94abe71 | 130 | 'extra_joins' => array( |
131 | 'table' => 'log_civicrm_activity', | |
132 | 'join' => 'extra_table.id = entity_log_civireport.activity_id', | |
133 | ), | |
41c9c9e9 | 134 | |
ae5ffbb7 TO |
135 | 'bracket_info' => array( |
136 | 'entity_column' => 'activity_type_id', | |
137 | 'options' => CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE), | |
41c9c9e9 | 138 | 'lookup_table' => 'log_civicrm_activity', |
cbb9666e | 139 | ), |
ae5ffbb7 TO |
140 | ), |
141 | 'log_civicrm_case' => array( | |
142 | 'fk' => 'contact_id', | |
143 | 'joins' => array( | |
144 | 'table' => 'log_civicrm_case_contact', | |
145 | 'join' => 'entity_log_civireport.id = fk_table.case_id', | |
cbb9666e | 146 | ), |
ae5ffbb7 TO |
147 | 'bracket_info' => array( |
148 | 'entity_column' => 'case_type_id', | |
7a5c0c6c | 149 | 'options' => CRM_Case_BAO_Case::buildOptions('case_type_id', 'search'), |
ae5ffbb7 TO |
150 | ), |
151 | ), | |
152 | ); | |
6a488035 | 153 | |
ae5ffbb7 | 154 | $logging = new CRM_Logging_Schema(); |
694e78fd DS |
155 | |
156 | // build _logTables for contact custom tables | |
157 | $customTables = $logging->entityCustomDataLogTables('Contact'); | |
6a488035 | 158 | foreach ($customTables as $table) { |
9d72cede EM |
159 | $this->_logTables[$table] = array( |
160 | 'fk' => 'entity_id', | |
21dfd5f5 | 161 | 'log_type' => 'Contact', |
9d72cede | 162 | ); |
6a488035 TO |
163 | } |
164 | ||
694e78fd DS |
165 | // build _logTables for address custom tables |
166 | $customTables = $logging->entityCustomDataLogTables('Address'); | |
167 | foreach ($customTables as $table) { | |
ae5ffbb7 | 168 | $this->_logTables[$table] = array( |
2fb1dd66 EM |
169 | // For join of fk_table with contact table. |
170 | 'fk' => 'contact_id', | |
ae5ffbb7 | 171 | 'joins' => array( |
2fb1dd66 EM |
172 | // fk_table |
173 | 'table' => 'log_civicrm_address', | |
ae5ffbb7 TO |
174 | 'join' => 'entity_log_civireport.entity_id = fk_table.id', |
175 | ), | |
176 | 'log_type' => 'Contact', | |
177 | ); | |
694e78fd DS |
178 | } |
179 | ||
2fb1dd66 | 180 | // Allow log tables to be extended via report hooks. |
6b4b11c4 | 181 | CRM_Report_BAO_Hook::singleton()->alterLogTables($this, $this->_logTables); |
c3e3dd65 | 182 | |
6a488035 TO |
183 | parent::__construct(); |
184 | } | |
185 | ||
00be9182 | 186 | public function groupBy() { |
6a488035 TO |
187 | $this->_groupBy = 'GROUP BY entity_log_civireport.log_conn_id, entity_log_civireport.log_user_id, EXTRACT(DAY_MICROSECOND FROM entity_log_civireport.log_date), entity_log_civireport.id'; |
188 | } | |
189 | ||
41c9c9e9 | 190 | /** |
191 | * Adjust query for the activity_contact table. | |
192 | * | |
193 | * As this is just a join table the ID we REALLY care about is the activity id. | |
194 | * | |
195 | * @param string $tableName | |
196 | * @param string $tableKey | |
197 | * @param string $fieldName | |
198 | * @param string $field | |
199 | * | |
200 | * @return string | |
201 | */ | |
202 | public function selectClause(&$tableName, $tableKey, &$fieldName, &$field) { | |
203 | if ($this->currentLogTable == 'log_civicrm_activity_contact' && $fieldName == 'id') { | |
204 | $alias = "{$tableName}_{$fieldName}"; | |
205 | $select[] = "{$tableName}.activity_id as $alias"; | |
206 | $this->_selectAliases[] = $alias; | |
207 | return "activity_id"; | |
6a488035 | 208 | } |
b94abe71 | 209 | if ($fieldName == 'log_grouping') { |
210 | if ($this->currentLogTable != 'log_civicrm_activity_contact') { | |
211 | return 1; | |
212 | } | |
213 | $mergeActivityID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Contact Merged'); | |
214 | return " IF (entity_log_civireport.log_action = 'Insert' AND extra_table.activity_type_id = $mergeActivityID , GROUP_CONCAT(entity_log_civireport.contact_id), 1) "; | |
215 | } | |
6a488035 TO |
216 | } |
217 | ||
00be9182 | 218 | public function where() { |
8c544cd2 DS |
219 | // reset where clause as its called multiple times, every time insert sql is built. |
220 | $this->_whereClauses = array(); | |
42047ce4 | 221 | |
6a488035 TO |
222 | parent::where(); |
223 | $this->_where .= " AND (entity_log_civireport.log_action != 'Initialization')"; | |
224 | } | |
225 | ||
00be9182 | 226 | public function postProcess() { |
6a488035 TO |
227 | $this->beginPostProcess(); |
228 | $rows = array(); | |
eea16664 | 229 | |
b94abe71 | 230 | $tempColumns = "id int(10), log_civicrm_entity_log_grouping varchar(32)"; |
a7488080 | 231 | if (!empty($this->_params['fields']['log_action'])) { |
9c097caf DS |
232 | $tempColumns .= ", log_action varchar(64)"; |
233 | } | |
234 | $tempColumns .= ", log_type varchar(64), log_user_id int(10), log_date timestamp"; | |
a7488080 | 235 | if (!empty($this->_params['fields']['altered_contact'])) { |
9c097caf DS |
236 | $tempColumns .= ", altered_contact varchar(128)"; |
237 | } | |
3b45d110 | 238 | $tempColumns .= ", altered_contact_id int(10), log_conn_id varchar(17), is_deleted tinyint(4)"; |
a7488080 | 239 | if (!empty($this->_params['fields']['display_name'])) { |
9c097caf DS |
240 | $tempColumns .= ", display_name varchar(128)"; |
241 | } | |
242 | ||
6a488035 | 243 | // temp table to hold all altered contact-ids |
9c097caf | 244 | $sql = "CREATE TEMPORARY TABLE civicrm_temp_civireport_logsummary ( {$tempColumns} ) ENGINE=HEAP"; |
6a488035 TO |
245 | CRM_Core_DAO::executeQuery($sql); |
246 | ||
6a488035 TO |
247 | $logTypes = CRM_Utils_Array::value('log_type_value', $this->_params); |
248 | unset($this->_params['log_type_value']); | |
cbb9666e | 249 | if (empty($logTypes)) { |
250 | foreach (array_keys($this->_logTables) as $table) { | |
6a488035 TO |
251 | $type = $this->getLogType($table); |
252 | $logTypes[$type] = $type; | |
253 | } | |
254 | } | |
255 | ||
798878cc DS |
256 | $logTypeTableClause = '(1)'; |
257 | if ($logTypeTableValue = CRM_Utils_Array::value("log_type_table_value", $this->_params)) { | |
cbb9666e | 258 | $logTypeTableClause = $this->whereClause($this->_columns['log_civicrm_entity']['filters']['log_type_table'], |
259 | $this->_params['log_type_table_op'], $logTypeTableValue, NULL, NULL); | |
798878cc DS |
260 | unset($this->_params['log_type_table_value']); |
261 | } | |
cbb9666e | 262 | |
263 | foreach ($this->_logTables as $entity => $detail) { | |
6a488035 | 264 | if ((in_array($this->getLogType($entity), $logTypes) && |
cbb9666e | 265 | CRM_Utils_Array::value('log_type_op', $this->_params) == 'in') || |
6a488035 | 266 | (!in_array($this->getLogType($entity), $logTypes) && |
cbb9666e | 267 | CRM_Utils_Array::value('log_type_op', $this->_params) == 'notin') |
268 | ) { | |
41c9c9e9 | 269 | $this->currentLogTable = $entity; |
cbb9666e | 270 | $sql = $this->buildQuery(FALSE); |
6a488035 TO |
271 | $sql = str_replace("entity_log_civireport.log_type as", "'{$entity}' as", $sql); |
272 | $sql = "INSERT IGNORE INTO civicrm_temp_civireport_logsummary {$sql}"; | |
273 | CRM_Core_DAO::executeQuery($sql); | |
274 | } | |
275 | } | |
276 | ||
41c9c9e9 | 277 | $this->currentLogTable = ''; |
278 | ||
9a2a98cd DS |
279 | // add computed log_type column so that we can do a group by after that, which will help |
280 | // alterDisplay() counts sync with pager counts | |
281 | $sql = "SELECT DISTINCT log_type FROM civicrm_temp_civireport_logsummary"; | |
282 | $dao = CRM_Core_DAO::executeQuery($sql); | |
283 | $replaceWith = array(); | |
cbb9666e | 284 | while ($dao->fetch()) { |
9a2a98cd | 285 | $type = $this->getLogType($dao->log_type); |
cbb9666e | 286 | if (!array_key_exists($type, $replaceWith)) { |
9a2a98cd | 287 | $replaceWith[$type] = array(); |
cbb9666e | 288 | } |
9a2a98cd DS |
289 | $replaceWith[$type][] = $dao->log_type; |
290 | } | |
291 | foreach ($replaceWith as $type => $tables) { | |
292 | if (!empty($tables)) { | |
293 | $replaceWith[$type] = implode("','", $tables); | |
294 | } | |
295 | } | |
2efcf0c2 | 296 | |
9a2a98cd DS |
297 | $sql = "ALTER TABLE civicrm_temp_civireport_logsummary ADD COLUMN log_civicrm_entity_log_type_label varchar(64)"; |
298 | CRM_Core_DAO::executeQuery($sql); | |
299 | foreach ($replaceWith as $type => $in) { | |
300 | $sql = "UPDATE civicrm_temp_civireport_logsummary SET log_civicrm_entity_log_type_label='{$type}', log_date=log_date WHERE log_type IN('$in')"; | |
301 | CRM_Core_DAO::executeQuery($sql); | |
302 | } | |
303 | ||
304 | // note the group by columns are same as that used in alterDisplay as $newRows - $key | |
6a488035 TO |
305 | $this->limit(); |
306 | $sql = "{$this->_select} | |
307 | FROM civicrm_temp_civireport_logsummary entity_log_civireport | |
798878cc | 308 | WHERE {$logTypeTableClause} |
b94abe71 | 309 | GROUP BY log_civicrm_entity_log_date, log_civicrm_entity_log_type_label, log_civicrm_entity_log_conn_id, log_civicrm_entity_log_user_id, log_civicrm_entity_altered_contact_id, log_civicrm_entity_log_grouping |
9a2a98cd | 310 | ORDER BY log_civicrm_entity_log_date DESC {$this->_limit}"; |
cbb9666e | 311 | $sql = str_replace('modified_contact_civireport.display_name', 'entity_log_civireport.altered_contact', $sql); |
312 | $sql = str_replace('modified_contact_civireport.id', 'entity_log_civireport.altered_contact_id', $sql); | |
313 | $sql = str_replace(array( | |
314 | 'modified_contact_civireport.', | |
21dfd5f5 | 315 | 'altered_by_contact_civireport.', |
cbb9666e | 316 | ), 'entity_log_civireport.', $sql); |
6a488035 TO |
317 | $this->buildRows($sql, $rows); |
318 | ||
319 | // format result set. | |
320 | $this->formatDisplay($rows); | |
321 | ||
322 | // assign variables to templates | |
323 | $this->doTemplateAssignment($rows); | |
324 | ||
325 | // do print / pdf / instance stuff if needed | |
326 | $this->endPostProcess($rows); | |
327 | } | |
328 | ||
e0ef6999 | 329 | /** |
2fb1dd66 EM |
330 | * Get log type. |
331 | * | |
332 | * @param string $entity | |
e0ef6999 EM |
333 | * |
334 | * @return string | |
335 | */ | |
00be9182 | 336 | public function getLogType($entity) { |
a7488080 | 337 | if (!empty($this->_logTables[$entity]['log_type'])) { |
6a488035 TO |
338 | return $this->_logTables[$entity]['log_type']; |
339 | } | |
340 | $logType = ucfirst(substr($entity, strrpos($entity, '_') + 1)); | |
341 | return $logType; | |
342 | } | |
343 | ||
e0ef6999 | 344 | /** |
2fb1dd66 EM |
345 | * Get entity value. |
346 | * | |
100fef9d | 347 | * @param int $id |
e0ef6999 EM |
348 | * @param $entity |
349 | * @param $logDate | |
350 | * | |
351 | * @return mixed|null|string | |
352 | */ | |
00be9182 | 353 | public function getEntityValue($id, $entity, $logDate) { |
a7488080 CW |
354 | if (!empty($this->_logTables[$entity]['bracket_info'])) { |
355 | if (!empty($this->_logTables[$entity]['bracket_info']['entity_column'])) { | |
0d8afee2 | 356 | $logTable = !empty($this->_logTables[$entity]['table_name']) ? $this->_logTables[$entity]['table_name'] : $entity; |
41c9c9e9 | 357 | if (!empty($this->_logTables[$entity]['bracket_info']['lookup_table'])) { |
358 | $logTable = $this->_logTables[$entity]['bracket_info']['lookup_table']; | |
359 | } | |
6a488035 | 360 | $sql = " |
eea16664 | 361 | SELECT {$this->_logTables[$entity]['bracket_info']['entity_column']} |
362 | FROM `{$this->loggingDB}`.{$logTable} | |
6a488035 | 363 | WHERE log_date <= %1 AND id = %2 ORDER BY log_date DESC LIMIT 1"; |
41c9c9e9 | 364 | |
cbb9666e | 365 | $entityID = CRM_Core_DAO::singleValueQuery($sql, array( |
366 | 1 => array( | |
367 | CRM_Utils_Date::isoToMysql($logDate), | |
21dfd5f5 | 368 | 'Timestamp', |
cbb9666e | 369 | ), |
21dfd5f5 | 370 | 2 => array($id, 'Integer'), |
cbb9666e | 371 | )); |
372 | } | |
373 | else { | |
6a488035 TO |
374 | $entityID = $id; |
375 | } | |
376 | ||
9d72cede EM |
377 | if ($entityID && $logDate && |
378 | array_key_exists('table', $this->_logTables[$entity]['bracket_info']) | |
379 | ) { | |
6a488035 | 380 | $sql = " |
eea16664 | 381 | SELECT {$this->_logTables[$entity]['bracket_info']['column']} |
382 | FROM `{$this->loggingDB}`.{$this->_logTables[$entity]['bracket_info']['table']} | |
6a488035 | 383 | WHERE log_date <= %1 AND id = %2 ORDER BY log_date DESC LIMIT 1"; |
cbb9666e | 384 | return CRM_Core_DAO::singleValueQuery($sql, array( |
385 | 1 => array(CRM_Utils_Date::isoToMysql($logDate), 'Timestamp'), | |
21dfd5f5 | 386 | 2 => array($entityID, 'Integer'), |
cbb9666e | 387 | )); |
388 | } | |
389 | else { | |
9d72cede EM |
390 | if (array_key_exists('options', $this->_logTables[$entity]['bracket_info']) && |
391 | $entityID | |
392 | ) { | |
cbb9666e | 393 | return CRM_Utils_Array::value($entityID, $this->_logTables[$entity]['bracket_info']['options']); |
394 | } | |
6a488035 TO |
395 | } |
396 | } | |
cbb9666e | 397 | return NULL; |
6a488035 TO |
398 | } |
399 | ||
e0ef6999 | 400 | /** |
2fb1dd66 EM |
401 | * Get entity action. |
402 | * | |
100fef9d CW |
403 | * @param int $id |
404 | * @param int $connId | |
e0ef6999 EM |
405 | * @param $entity |
406 | * @param $oldAction | |
407 | * | |
408 | * @return null|string | |
409 | */ | |
00be9182 | 410 | public function getEntityAction($id, $connId, $entity, $oldAction) { |
a7488080 | 411 | if (!empty($this->_logTables[$entity]['action_column'])) { |
6a488035 | 412 | $sql = "select {$this->_logTables[$entity]['action_column']} from `{$this->loggingDB}`.{$entity} where id = %1 AND log_conn_id = %2"; |
cbb9666e | 413 | $newAction = CRM_Core_DAO::singleValueQuery($sql, array( |
414 | 1 => array($id, 'Integer'), | |
3b45d110 | 415 | 2 => array($connId, 'String'), |
cbb9666e | 416 | )); |
6a488035 TO |
417 | |
418 | switch ($entity) { | |
cbb9666e | 419 | case 'log_civicrm_group_contact': |
420 | if ($oldAction !== 'Update') { | |
421 | $newAction = $oldAction; | |
422 | } | |
423 | if ($oldAction == 'Insert') { | |
424 | $newAction = 'Added'; | |
425 | } | |
426 | break; | |
6a488035 TO |
427 | } |
428 | return $newAction; | |
429 | } | |
cbb9666e | 430 | return NULL; |
6a488035 | 431 | } |
96025800 | 432 | |
6a488035 | 433 | } |