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