Merge pull request #10466 from kryptothesuperdog/master
[civicrm-core.git] / CRM / Report / Form / Contact / LoggingSummary.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
32 */
33 class CRM_Report_Form_Contact_LoggingSummary extends CRM_Logging_ReportSummary {
34 /**
35 * Class constructor.
36 */
37 public function __construct() {
38 parent::__construct();
39
40 $logTypes = array();
41 foreach (array_keys($this->_logTables) as $table) {
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 ),
56 'log_grouping' => array(
57 'required' => TRUE,
58 'title' => ts('Extra information to control grouping'),
59 'no_display' => TRUE,
60 ),
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,
89 'alias' => 'modified_contact_civireport',
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 ),
125 'log_type_table' => array(
126 'name' => 'log_type',
127 'title' => ts('Log Type Table'),
128 'type' => CRM_Utils_Type::T_STRING,
129 ),
130 'log_action' => array(
131 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
132 'options' => array(
133 'Insert' => ts('Insert'),
134 'Update' => ts('Update'),
135 'Delete' => ts('Delete'),
136 ),
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 ),
145 ),
146 'altered_by_contact' => array(
147 'dao' => 'CRM_Contact_DAO_Contact',
148 'alias' => 'altered_by_contact',
149 'fields' => array(
150 'display_name' => array(
151 'default' => TRUE,
152 'name' => 'display_name',
153 'title' => ts('Altered By'),
154 ),
155 ),
156 'filters' => array(
157 'display_name' => array(
158 'name' => 'display_name',
159 'title' => ts('Altered By'),
160 'type' => CRM_Utils_Type::T_STRING,
161 ),
162 ),
163 ),
164 );
165 }
166
167 /**
168 * Alter display of rows.
169 *
170 * Iterate through the rows retrieved via SQL and make changes for display purposes,
171 * such as rendering contacts as links.
172 *
173 * @param array $rows
174 * Rows generated by SQL, with an array for each row.
175 */
176 public function alterDisplay(&$rows) {
177 // cache for id → is_deleted mapping
178 $isDeleted = array();
179 $newRows = array();
180
181 foreach ($rows as $key => &$row) {
182 $isMerge = 0;
183 $baseQueryCriteria = "reset=1&log_conn_id={$row['log_civicrm_entity_log_conn_id']}";
184 if (!CRM_Logging_Differ::checkLogCanBeUsedWithNoLogDate($row['log_civicrm_entity_log_date'])) {
185 $baseQueryCriteria .= '&log_date=' . CRM_Utils_Date::isoToMysql($row['log_civicrm_entity_log_date']);
186 }
187 if ($this->cid) {
188 $baseQueryCriteria .= '&cid=' . $this->cid;
189 }
190 if (!isset($isDeleted[$row['log_civicrm_entity_altered_contact_id']])) {
191 $isDeleted[$row['log_civicrm_entity_altered_contact_id']] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
192 $row['log_civicrm_entity_altered_contact_id'], 'is_deleted') !== '0';
193 }
194
195 if (!empty($row['log_civicrm_entity_altered_contact']) &&
196 !$isDeleted[$row['log_civicrm_entity_altered_contact_id']]
197 ) {
198 $row['log_civicrm_entity_altered_contact_link'] = CRM_Utils_System::url('civicrm/contact/view',
199 'reset=1&cid=' . $row['log_civicrm_entity_altered_contact_id']);
200 $row['log_civicrm_entity_altered_contact_hover'] = ts("Go to contact summary");
201 $entity = $this->getEntityValue($row['log_civicrm_entity_id'], $row['log_civicrm_entity_log_type'], $row['log_civicrm_entity_log_date']);
202 if ($entity) {
203 $row['log_civicrm_entity_altered_contact'] = $row['log_civicrm_entity_altered_contact'] . " [{$entity}]";
204 }
205 if ($entity == 'Contact Merged') {
206 // We're looking at a merge activity created against the surviving
207 // contact record. There should be a single activity created against
208 // the deleted contact record, with this activity as parent.
209 $deletedID = CRM_Core_DAO::singleValueQuery('
210 SELECT GROUP_CONCAT(contact_id) FROM civicrm_activity_contact ac
211 INNER JOIN civicrm_activity a
212 ON a.id = ac.activity_id AND a.parent_id = ' . $row['log_civicrm_entity_id'] . ' AND ac.record_type_id =
213 ' . CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_ActivityContact', 'record_type_id', 'Activity Targets')
214 );
215 if ($deletedID && !stristr($deletedID, ',')) {
216 $baseQueryCriteria .= '&oid=' . $deletedID;
217 }
218 $row['log_civicrm_entity_log_action'] = ts('Contact Merge');
219 $row = $this->addDetailReportLinksToRow($baseQueryCriteria, $row);
220 $isMerge = 1;
221 }
222
223 }
224 $row['altered_by_contact_display_name_link'] = CRM_Utils_System::url('civicrm/contact/view', 'reset=1&cid=' . $row['log_civicrm_entity_log_user_id']);
225 $row['altered_by_contact_display_name_hover'] = ts("Go to contact summary");
226
227 if ($row['log_civicrm_entity_is_deleted'] and 'Update' == CRM_Utils_Array::value('log_civicrm_entity_log_action', $row)) {
228 $row['log_civicrm_entity_log_action'] = ts('Delete (to trash)');
229 }
230
231 if ('Contact' == CRM_Utils_Array::value('log_type', $this->_logTables[$row['log_civicrm_entity_log_type']]) &&
232 CRM_Utils_Array::value('log_civicrm_entity_log_action', $row) == ts('Insert')
233 ) {
234 $row['log_civicrm_entity_log_action'] = ts('Update');
235 }
236
237 // For certain tables, we may want to look at an alternate column to
238 // determine which action to display, determined by the 'action_column'
239 // key of the entry in $this->_logTables.
240 if ($newAction = $this->getEntityAction($row['log_civicrm_entity_id'],
241 $row['log_civicrm_entity_log_conn_id'],
242 $row['log_civicrm_entity_log_type'],
243 CRM_Utils_Array::value('log_civicrm_entity_log_action', $row))
244 ) {
245 $row['log_civicrm_entity_log_action'] = $newAction;
246 }
247
248 $row['log_civicrm_entity_log_type'] = $this->getLogType($row['log_civicrm_entity_log_type']);
249
250 $date = CRM_Utils_Date::isoToMysql($row['log_civicrm_entity_log_date']);
251
252 if ('Update' == CRM_Utils_Array::value('log_civicrm_entity_log_action', $row)) {
253 $row = $this->addDetailReportLinksToRow($baseQueryCriteria, $row);
254 }
255
256 // In the summary, we only want to show one row per entity type,
257 // connection ID, contact ID, and user ID, rolling up multiple
258 // related actions against the same entity.
259 $key = $date . '_' .
260 $row['log_civicrm_entity_log_type'] . '_' .
261 // This ensures merge activities are not 'lost' by aggregation.
262 // I would prefer not to lose other entities either but it's a balancing act as
263 // described in https://issues.civicrm.org/jira/browse/CRM-12867 so adding this criteria
264 // while hackish saves us from figuring out if the original decision is still good.
265 $isMerge . '_' .
266 $row['log_civicrm_entity_log_conn_id'] . '_' .
267 $row['log_civicrm_entity_log_user_id'] . '_' .
268 $row['log_civicrm_entity_altered_contact_id'];
269 $newRows[$key] = $row;
270
271 unset($row['log_civicrm_entity_log_user_id']);
272 unset($row['log_civicrm_entity_log_conn_id']);
273 }
274
275 krsort($newRows);
276 $rows = $newRows;
277 }
278
279 /**
280 * Generate From Clause.
281 */
282 public function from() {
283 $entity = $this->currentLogTable;
284
285 $detail = $this->_logTables[$entity];
286 $tableName = CRM_Utils_Array::value('table_name', $detail, $entity);
287 $clause = CRM_Utils_Array::value('entity_table', $detail);
288 $clause = $clause ? "AND entity_log_civireport.entity_table = 'civicrm_contact'" : NULL;
289
290 $joinClause = "
291 INNER JOIN civicrm_contact modified_contact_civireport
292 ON (entity_log_civireport.{$detail['fk']} = modified_contact_civireport.id {$clause})";
293
294 if (!empty($detail['joins'])) {
295 $clause = CRM_Utils_Array::value('entity_table', $detail);
296 $clause = $clause ? "AND fk_table.entity_table = 'civicrm_contact'" : NULL;
297 $joinClause = "
298 INNER JOIN `{$this->loggingDB}`.{$detail['joins']['table']} fk_table ON {$detail['joins']['join']}
299 INNER JOIN civicrm_contact modified_contact_civireport
300 ON (fk_table.{$detail['fk']} = modified_contact_civireport.id {$clause})";
301 }
302
303 if (!empty($detail['extra_joins'])) {
304 $joinClause .= "
305 INNER JOIN `{$this->loggingDB}`.{$detail['extra_joins']['table']} extra_table ON {$detail['extra_joins']['join']}";
306 }
307
308 $this->_from = "
309 FROM `{$this->loggingDB}`.$tableName entity_log_civireport
310 {$joinClause}
311 LEFT JOIN civicrm_contact altered_by_contact_civireport
312 ON (entity_log_civireport.log_user_id = altered_by_contact_civireport.id)";
313 }
314
315 /**
316 * Add links & hovers to the detailed report.
317 *
318 * @param $baseQueryCriteria
319 * @param $row
320 *
321 * @return mixed
322 */
323 protected function addDetailReportLinksToRow($baseQueryCriteria, $row) {
324 $q = $baseQueryCriteria;
325 $q .= (!empty($row['log_civicrm_entity_altered_contact'])) ? '&alteredName=' . $row['log_civicrm_entity_altered_contact'] : '';
326 $q .= (!empty($row['altered_by_contact_display_name'])) ? '&alteredBy=' . $row['altered_by_contact_display_name'] : '';
327 $q .= (!empty($row['log_civicrm_entity_log_user_id'])) ? '&alteredById=' . $row['log_civicrm_entity_log_user_id'] : '';
328
329 $url1 = CRM_Report_Utils_Report::getNextUrl('logging/contact/detail', "{$q}&snippet=4&section=2&layout=overlay", FALSE, TRUE);
330 $url2 = CRM_Report_Utils_Report::getNextUrl('logging/contact/detail', "{$q}&section=2", FALSE, TRUE);
331 $hoverTitle = ts('View details for this update');
332 $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>';
333 return $row;
334 }
335
336 }