Merge pull request #11993 from jaapjansma/issue_66
[civicrm-core.git] / CRM / Report / Form / Contact / LoggingSummary.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
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
8c9251b3 31 * @copyright CiviCRM LLC (c) 2004-2018
6a488035
TO
32 */
33class CRM_Report_Form_Contact_LoggingSummary extends CRM_Logging_ReportSummary {
74cf4551 34 /**
2fb1dd66 35 * Class constructor.
74cf4551 36 */
00be9182 37 public function __construct() {
6a488035
TO
38 parent::__construct();
39
40 $logTypes = array();
481a74f4 41 foreach (array_keys($this->_logTables) as $table) {
6a488035
TO
42 $type = $this->getLogType($table);
43 $logTypes[$type] = $type;
44 }
45 asort($logTypes);
46
47 $this->_columns = array(
48 'log_civicrm_entity' => array(
49 'dao' => 'CRM_Contact_DAO_Contact',
50 'alias' => 'entity_log',
51 'fields' => array(
52 'id' => array(
53 'no_display' => TRUE,
54 'required' => TRUE,
55 ),
b94abe71 56 'log_grouping' => array(
57 'required' => TRUE,
58 'title' => ts('Extra information to control grouping'),
59 'no_display' => TRUE,
60 ),
6a488035
TO
61 'log_action' => array(
62 'default' => TRUE,
63 'title' => ts('Action'),
64 ),
65 'log_type' => array(
66 'required' => TRUE,
67 'title' => ts('Log Type'),
68 ),
69 'log_user_id' => array(
70 'no_display' => TRUE,
71 'required' => TRUE,
72 ),
73 'log_date' => array(
74 'default' => TRUE,
75 'required' => TRUE,
76 'type' => CRM_Utils_Type::T_TIME,
77 'title' => ts('When'),
78 ),
79 'altered_contact' => array(
80 'default' => TRUE,
81 'name' => 'display_name',
82 'title' => ts('Altered Contact'),
83 'alias' => 'modified_contact_civireport',
84 ),
85 'altered_contact_id' => array(
86 'name' => 'id',
87 'no_display' => TRUE,
88 'required' => TRUE,
353ffa53 89 'alias' => 'modified_contact_civireport',
6a488035
TO
90 ),
91 'log_conn_id' => array(
92 'no_display' => TRUE,
93 'required' => TRUE,
94 ),
95 'is_deleted' => array(
96 'no_display' => TRUE,
97 'required' => TRUE,
98 'alias' => 'modified_contact_civireport',
99 ),
100 ),
101 'filters' => array(
102 'log_date' => array(
103 'title' => ts('When'),
104 'operatorType' => CRM_Report_Form::OP_DATE,
105 'type' => CRM_Utils_Type::T_DATE,
106 ),
107 'altered_contact' => array(
108 'name' => 'display_name',
109 'title' => ts('Altered Contact'),
110 'type' => CRM_Utils_Type::T_STRING,
111 'alias' => 'modified_contact_civireport',
112 ),
113 'altered_contact_id' => array(
114 'name' => 'id',
115 'type' => CRM_Utils_Type::T_INT,
116 'alias' => 'modified_contact_civireport',
117 'no_display' => TRUE,
118 ),
119 'log_type' => array(
120 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
121 'options' => $logTypes,
122 'title' => ts('Log Type'),
123 'type' => CRM_Utils_Type::T_STRING,
124 ),
798878cc 125 'log_type_table' => array(
353ffa53 126 'name' => 'log_type',
798878cc
DS
127 'title' => ts('Log Type Table'),
128 'type' => CRM_Utils_Type::T_STRING,
129 ),
6a488035
TO
130 'log_action' => array(
131 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
2fb1dd66
EM
132 'options' => array(
133 'Insert' => ts('Insert'),
134 'Update' => ts('Update'),
135 'Delete' => ts('Delete'),
136 ),
6a488035
TO
137 'title' => ts('Action'),
138 'type' => CRM_Utils_Type::T_STRING,
139 ),
140 'id' => array(
141 'no_display' => TRUE,
142 'type' => CRM_Utils_Type::T_INT,
143 ),
144 ),
e85aa549 145 'order_bys' => array(
146 'log_date' => array(
147 'title' => ts('Log Date (When)'),
148 'default' => TRUE,
149 'default_weight' => '0',
150 'default_order' => 'DESC',
151 ),
152 'altered_contact' => array(
153 'name' => 'display_name',
154 'title' => ts('Altered Contact'),
155 'alias' => 'modified_contact_civireport',
156 ),
157 ),
6a488035
TO
158 ),
159 'altered_by_contact' => array(
353ffa53 160 'dao' => 'CRM_Contact_DAO_Contact',
6a488035
TO
161 'alias' => 'altered_by_contact',
162 'fields' => array(
163 'display_name' => array(
164 'default' => TRUE,
165 'name' => 'display_name',
166 'title' => ts('Altered By'),
167 ),
168 ),
169 'filters' => array(
170 'display_name' => array(
171 'name' => 'display_name',
172 'title' => ts('Altered By'),
173 'type' => CRM_Utils_Type::T_STRING,
174 ),
175 ),
e85aa549 176 'order_bys' => array(
177 'altered_by_contact' => array(
178 'name' => 'display_name',
179 'title' => ts('Altered by'),
180 ),
181 ),
6a488035
TO
182 ),
183 );
184 }
185
74cf4551 186 /**
ced9bfed
EM
187 * Alter display of rows.
188 *
189 * Iterate through the rows retrieved via SQL and make changes for display purposes,
190 * such as rendering contacts as links.
2fb1dd66
EM
191 *
192 * @param array $rows
ced9bfed 193 * Rows generated by SQL, with an array for each row.
74cf4551 194 */
00be9182 195 public function alterDisplay(&$rows) {
6a488035
TO
196 // cache for id → is_deleted mapping
197 $isDeleted = array();
353ffa53 198 $newRows = array();
6a488035
TO
199
200 foreach ($rows as $key => &$row) {
b94abe71 201 $isMerge = 0;
9bf49a0e 202 $baseQueryCriteria = "reset=1&log_conn_id={$row['log_civicrm_entity_log_conn_id']}";
203 if (!CRM_Logging_Differ::checkLogCanBeUsedWithNoLogDate($row['log_civicrm_entity_log_date'])) {
204 $baseQueryCriteria .= '&log_date=' . CRM_Utils_Date::isoToMysql($row['log_civicrm_entity_log_date']);
205 }
206 if ($this->cid) {
207 $baseQueryCriteria .= '&cid=' . $this->cid;
208 }
6a488035 209 if (!isset($isDeleted[$row['log_civicrm_entity_altered_contact_id']])) {
3bdca100 210 $isDeleted[$row['log_civicrm_entity_altered_contact_id']] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
211 $row['log_civicrm_entity_altered_contact_id'], 'is_deleted') !== '0';
6a488035
TO
212 }
213
a7488080 214 if (!empty($row['log_civicrm_entity_altered_contact']) &&
353ffa53
TO
215 !$isDeleted[$row['log_civicrm_entity_altered_contact_id']]
216 ) {
3bdca100 217 $row['log_civicrm_entity_altered_contact_link'] = CRM_Utils_System::url('civicrm/contact/view',
218 'reset=1&cid=' . $row['log_civicrm_entity_altered_contact_id']);
6a488035
TO
219 $row['log_civicrm_entity_altered_contact_hover'] = ts("Go to contact summary");
220 $entity = $this->getEntityValue($row['log_civicrm_entity_id'], $row['log_civicrm_entity_log_type'], $row['log_civicrm_entity_log_date']);
84178120 221 if ($entity) {
6a488035 222 $row['log_civicrm_entity_altered_contact'] = $row['log_civicrm_entity_altered_contact'] . " [{$entity}]";
84178120 223 }
b94abe71 224 if ($entity == 'Contact Merged') {
221fa9c7
EE
225 // We're looking at a merge activity created against the surviving
226 // contact record. There should be a single activity created against
227 // the deleted contact record, with this activity as parent.
549cd4ca 228 $deletedID = CRM_Core_DAO::singleValueQuery('
229 SELECT GROUP_CONCAT(contact_id) FROM civicrm_activity_contact ac
230 INNER JOIN civicrm_activity a
231 ON a.id = ac.activity_id AND a.parent_id = ' . $row['log_civicrm_entity_id'] . ' AND ac.record_type_id =
232 ' . CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_ActivityContact', 'record_type_id', 'Activity Targets')
233 );
234 if ($deletedID && !stristr($deletedID, ',')) {
235 $baseQueryCriteria .= '&oid=' . $deletedID;
236 }
237 $row['log_civicrm_entity_log_action'] = ts('Contact Merge');
238 $row = $this->addDetailReportLinksToRow($baseQueryCriteria, $row);
b94abe71 239 $isMerge = 1;
240 }
241
6a488035
TO
242 }
243 $row['altered_by_contact_display_name_link'] = CRM_Utils_System::url('civicrm/contact/view', 'reset=1&cid=' . $row['log_civicrm_entity_log_user_id']);
244 $row['altered_by_contact_display_name_hover'] = ts("Go to contact summary");
245
c9fb1753 246 if ($row['log_civicrm_entity_is_deleted'] and 'Update' == CRM_Utils_Array::value('log_civicrm_entity_log_action', $row)) {
6a488035
TO
247 $row['log_civicrm_entity_log_action'] = ts('Delete (to trash)');
248 }
249
f813f78e 250 if ('Contact' == CRM_Utils_Array::value('log_type', $this->_logTables[$row['log_civicrm_entity_log_type']]) &&
9bf49a0e 251 CRM_Utils_Array::value('log_civicrm_entity_log_action', $row) == ts('Insert')
353ffa53 252 ) {
6a488035
TO
253 $row['log_civicrm_entity_log_action'] = ts('Update');
254 }
255
221fa9c7
EE
256 // For certain tables, we may want to look at an alternate column to
257 // determine which action to display, determined by the 'action_column'
258 // key of the entry in $this->_logTables.
f813f78e 259 if ($newAction = $this->getEntityAction($row['log_civicrm_entity_id'],
353ffa53
TO
260 $row['log_civicrm_entity_log_conn_id'],
261 $row['log_civicrm_entity_log_type'],
262 CRM_Utils_Array::value('log_civicrm_entity_log_action', $row))
263 ) {
6a488035 264 $row['log_civicrm_entity_log_action'] = $newAction;
84178120 265 }
6a488035
TO
266
267 $row['log_civicrm_entity_log_type'] = $this->getLogType($row['log_civicrm_entity_log_type']);
268
269 $date = CRM_Utils_Date::isoToMysql($row['log_civicrm_entity_log_date']);
270
c9fb1753 271 if ('Update' == CRM_Utils_Array::value('log_civicrm_entity_log_action', $row)) {
9bf49a0e 272 $row = $this->addDetailReportLinksToRow($baseQueryCriteria, $row);
6a488035
TO
273 }
274
221fa9c7
EE
275 // In the summary, we only want to show one row per entity type,
276 // connection ID, contact ID, and user ID, rolling up multiple
277 // related actions against the same entity.
353ffa53 278 $key = $date . '_' .
f813f78e 279 $row['log_civicrm_entity_log_type'] . '_' .
b94abe71 280 // This ensures merge activities are not 'lost' by aggregation.
281 // I would prefer not to lose other entities either but it's a balancing act as
282 // described in https://issues.civicrm.org/jira/browse/CRM-12867 so adding this criteria
283 // while hackish saves us from figuring out if the original decision is still good.
221fa9c7 284 $isMerge . '_' .
f813f78e 285 $row['log_civicrm_entity_log_conn_id'] . '_' .
6a488035 286 $row['log_civicrm_entity_log_user_id'] . '_' .
9a2a98cd 287 $row['log_civicrm_entity_altered_contact_id'];
6a488035
TO
288 $newRows[$key] = $row;
289
290 unset($row['log_civicrm_entity_log_user_id']);
291 unset($row['log_civicrm_entity_log_conn_id']);
292 }
293
294 krsort($newRows);
295 $rows = $newRows;
296 }
297
74cf4551 298 /**
2fb1dd66 299 * Generate From Clause.
74cf4551 300 */
41c9c9e9 301 public function from() {
302 $entity = $this->currentLogTable;
6a488035 303
353ffa53 304 $detail = $this->_logTables[$entity];
6a488035
TO
305 $tableName = CRM_Utils_Array::value('table_name', $detail, $entity);
306 $clause = CRM_Utils_Array::value('entity_table', $detail);
84178120 307 $clause = $clause ? "AND entity_log_civireport.entity_table = 'civicrm_contact'" : NULL;
f813f78e 308
6a488035 309 $joinClause = "
f813f78e 310INNER JOIN civicrm_contact modified_contact_civireport
6a488035
TO
311 ON (entity_log_civireport.{$detail['fk']} = modified_contact_civireport.id {$clause})";
312
a7488080 313 if (!empty($detail['joins'])) {
6a488035 314 $clause = CRM_Utils_Array::value('entity_table', $detail);
84178120 315 $clause = $clause ? "AND fk_table.entity_table = 'civicrm_contact'" : NULL;
6a488035
TO
316 $joinClause = "
317INNER JOIN `{$this->loggingDB}`.{$detail['joins']['table']} fk_table ON {$detail['joins']['join']}
f813f78e 318INNER JOIN civicrm_contact modified_contact_civireport
6a488035
TO
319 ON (fk_table.{$detail['fk']} = modified_contact_civireport.id {$clause})";
320 }
321
b94abe71 322 if (!empty($detail['extra_joins'])) {
323 $joinClause .= "
324INNER JOIN `{$this->loggingDB}`.{$detail['extra_joins']['table']} extra_table ON {$detail['extra_joins']['join']}";
325 }
326
6a488035
TO
327 $this->_from = "
328FROM `{$this->loggingDB}`.$tableName entity_log_civireport
329{$joinClause}
f813f78e 330LEFT JOIN civicrm_contact altered_by_contact_civireport
6a488035
TO
331 ON (entity_log_civireport.log_user_id = altered_by_contact_civireport.id)";
332 }
96025800 333
9bf49a0e 334 /**
335 * Add links & hovers to the detailed report.
336 *
337 * @param $baseQueryCriteria
338 * @param $row
339 *
340 * @return mixed
341 */
342 protected function addDetailReportLinksToRow($baseQueryCriteria, $row) {
343 $q = $baseQueryCriteria;
344 $q .= (!empty($row['log_civicrm_entity_altered_contact'])) ? '&alteredName=' . $row['log_civicrm_entity_altered_contact'] : '';
345 $q .= (!empty($row['altered_by_contact_display_name'])) ? '&alteredBy=' . $row['altered_by_contact_display_name'] : '';
346 $q .= (!empty($row['log_civicrm_entity_log_user_id'])) ? '&alteredById=' . $row['log_civicrm_entity_log_user_id'] : '';
347
348 $url1 = CRM_Report_Utils_Report::getNextUrl('logging/contact/detail', "{$q}&snippet=4&section=2&layout=overlay", FALSE, TRUE);
349 $url2 = CRM_Report_Utils_Report::getNextUrl('logging/contact/detail', "{$q}&section=2", FALSE, TRUE);
350 $hoverTitle = ts('View details for this update');
351 $row['log_civicrm_entity_log_action'] = "<a href='{$url1}' class='crm-summary-link'><i class=\"crm-i fa-list-alt\"></i></a>&nbsp;<a title='{$hoverTitle}' href='{$url2}'>" . $row['log_civicrm_entity_log_action'] . '</a>';
352 return $row;
353 }
354
cb75c53a 355 /**
356 * Calculate section totals.
357 *
358 * Override to do nothing as this does not work / make sense on this report.
359 */
360 public function sectionTotals() {}
361
6a488035 362}