Merge pull request #3007 from GinkgoFJG/CRM-14283
[civicrm-core.git] / CRM / Logging / ReportSummary.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35 class CRM_Logging_ReportSummary extends CRM_Report_Form {
36 protected $cid;
37
38 protected $_logTables = array();
39
40 protected $loggingDB;
41
42 function __construct() {
43 // don’t display the ‘Add these Contacts to Group’ button
44 $this->_add2groupSupported = FALSE;
45
46 $dsn = defined('CIVICRM_LOGGING_DSN') ? DB::parseDSN(CIVICRM_LOGGING_DSN) : DB::parseDSN(CIVICRM_DSN);
47 $this->loggingDB = $dsn['database'];
48
49 // used for redirect back to contact summary
50 $this->cid = CRM_Utils_Request::retrieve('cid', 'Integer', CRM_Core_DAO::$_nullObject);
51
52 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
53 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
54 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
55 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
56
57 $this->_logTables =
58 array(
59 'log_civicrm_contact' =>
60 array(
61 'fk' => 'id',
62 ),
63 'log_civicrm_email' =>
64 array(
65 'fk' => 'contact_id',
66 'log_type' => 'Contact',
67 ),
68 'log_civicrm_phone' =>
69 array(
70 'fk' => 'contact_id',
71 'log_type' => 'Contact',
72 ),
73 'log_civicrm_address' =>
74 array(
75 'fk' => 'contact_id',
76 'log_type' => 'Contact',
77 ),
78 'log_civicrm_note' =>
79 array(
80 'fk' => 'entity_id',
81 'entity_table' => TRUE,
82 'bracket_info' => array('table' => 'log_civicrm_note', 'column' => 'subject'),
83 ),
84 'log_civicrm_note_comment' =>
85 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'"
91 ),
92 'entity_table' => TRUE,
93 'bracket_info' => array('table' => 'log_civicrm_note', 'column' => 'subject'),
94 ),
95 'log_civicrm_group_contact' =>
96 array(
97 'fk' => 'contact_id',
98 'bracket_info' => array('entity_column' => 'group_id', 'table' => 'log_civicrm_group', 'column' => 'title'),
99 'action_column' => 'status',
100 'log_type' => 'Group',
101 ),
102 'log_civicrm_entity_tag' =>
103 array(
104 'fk' => 'entity_id',
105 'bracket_info' => array('entity_column' => 'tag_id', 'table' => 'log_civicrm_tag', 'column' => 'name'),
106 'entity_table' => TRUE
107 ),
108 'log_civicrm_relationship' =>
109 array(
110 'fk' => 'contact_id_a',
111 'bracket_info' => array(
112 'entity_column' => 'relationship_type_id',
113 'table' => 'log_civicrm_relationship_type',
114 'column' => 'label_a_b'
115 ),
116 ),
117 'log_civicrm_activity_for_target' =>
118 array(
119 'fk' => 'contact_id',
120 'table_name' => 'log_civicrm_activity',
121 'joins' => array(
122 'table' => 'log_civicrm_activity_contact',
123 'join' => "(entity_log_civireport.id = fk_table.activity_id AND fk_table.record_type_id = {$targetID})"
124 ),
125 'bracket_info' => array(
126 'entity_column' => 'activity_type_id',
127 'options' => CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE)
128 ),
129 'log_type' => 'Activity',
130 ),
131 'log_civicrm_activity_for_assignee' =>
132 array(
133 'fk' => 'contact_id',
134 'table_name' => 'log_civicrm_activity',
135 'joins' => array(
136 'table' => 'log_civicrm_activity_contact',
137 'join' => "entity_log_civireport.id = fk_table.activity_id AND fk_table.record_type_id = {$assigneeID}"
138 ),
139 'bracket_info' => array(
140 'entity_column' => 'activity_type_id',
141 'options' => CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE)
142 ),
143 'log_type' => 'Activity',
144 ),
145 'log_civicrm_activity_for_source' =>
146 array(
147 'fk' => 'contact_id',
148 'table_name' => 'log_civicrm_activity',
149 'joins' => array(
150 'table' => 'log_civicrm_activity_contact',
151 'join' => "entity_log_civireport.id = fk_table.activity_id AND fk_table.record_type_id = {$sourceID}"
152 ),
153 'bracket_info' => array(
154 'entity_column' => 'activity_type_id',
155 'options' => CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE)
156 ),
157 'log_type' => 'Activity',
158 ),
159 'log_civicrm_case' =>
160 array(
161 'fk' => 'contact_id',
162 'joins' => array(
163 'table' => 'log_civicrm_case_contact',
164 'join' => 'entity_log_civireport.id = fk_table.case_id'
165 ),
166 'bracket_info' => array(
167 'entity_column' => 'case_type_id',
168 'options' => CRM_Case_PseudoConstant::caseType('label', FALSE)
169 ),
170 ),
171 );
172
173 $logging = new CRM_Logging_Schema;
174
175 // build _logTables for contact custom tables
176 $customTables = $logging->entityCustomDataLogTables('Contact');
177 foreach ($customTables as $table) {
178 $this->_logTables[$table] = array('fk' => 'entity_id', 'log_type' => 'Contact');
179 }
180
181 // build _logTables for address custom tables
182 $customTables = $logging->entityCustomDataLogTables('Address');
183 foreach ($customTables as $table) {
184 $this->_logTables[$table] =
185 array(
186 'fk' => 'contact_id',// for join of fk_table with contact table
187 'joins' => array(
188 'table' => 'log_civicrm_address', // fk_table
189 'join' => 'entity_log_civireport.entity_id = fk_table.id'
190 ),
191 'log_type' => 'Contact'
192 );
193 }
194
195 // allow log tables to be extended via report hooks
196 CRM_Report_BAO_Hook::singleton()->alterLogTables($this, $this->_logTables);
197
198 parent::__construct();
199 }
200
201 function groupBy() {
202 $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';
203 }
204
205 function select() {
206 $select = array();
207 $this->_columnHeaders = array();
208 foreach ($this->_columns as $tableName => $table) {
209 if (array_key_exists('fields', $table)) {
210 foreach ($table['fields'] as $fieldName => $field) {
211 if (CRM_Utils_Array::value('required', $field) or CRM_Utils_Array::value($fieldName, $this->_params['fields'])) {
212 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
213 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
214 $this->_columnHeaders["{$tableName}_{$fieldName}"]['no_display'] = CRM_Utils_Array::value('no_display', $field);
215 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
216 }
217 }
218 }
219 }
220 $this->_select = 'SELECT ' . implode(', ', $select) . ' ';
221 }
222
223 function where() {
224 // reset where clause as its called multiple times, every time insert sql is built.
225 $this->_whereClauses = array();
226
227 parent::where();
228 $this->_where .= " AND (entity_log_civireport.log_action != 'Initialization')";
229 }
230
231 function postProcess() {
232 $this->beginPostProcess();
233 $rows = array();
234
235 $tempColumns = "id int(10)";
236 if (!empty($this->_params['fields']['log_action'])) {
237 $tempColumns .= ", log_action varchar(64)";
238 }
239 $tempColumns .= ", log_type varchar(64), log_user_id int(10), log_date timestamp";
240 if (!empty($this->_params['fields']['altered_contact'])) {
241 $tempColumns .= ", altered_contact varchar(128)";
242 }
243 $tempColumns .= ", altered_contact_id int(10), log_conn_id int(11), is_deleted tinyint(4)";
244 if (!empty($this->_params['fields']['display_name'])) {
245 $tempColumns .= ", display_name varchar(128)";
246 }
247
248 // temp table to hold all altered contact-ids
249 $sql = "CREATE TEMPORARY TABLE civicrm_temp_civireport_logsummary ( {$tempColumns} ) ENGINE=HEAP";
250 CRM_Core_DAO::executeQuery($sql);
251
252 $logDateClause = $this->dateClause('log_date',
253 CRM_Utils_Array::value("log_date_relative", $this->_params),
254 CRM_Utils_Array::value("log_date_from", $this->_params),
255 CRM_Utils_Array::value("log_date_to", $this->_params),
256 CRM_Utils_Type::T_DATE,
257 CRM_Utils_Array::value("log_date_from_time", $this->_params),
258 CRM_Utils_Array::value("log_date_to_time", $this->_params));
259 $logDateClause = $logDateClause ? "AND {$logDateClause}" : NULL;
260
261 $logTypes = CRM_Utils_Array::value('log_type_value', $this->_params);
262 unset($this->_params['log_type_value']);
263 if (empty($logTypes)) {
264 foreach (array_keys($this->_logTables) as $table) {
265 $type = $this->getLogType($table);
266 $logTypes[$type] = $type;
267 }
268 }
269
270 $logTypeTableClause = '(1)';
271 if ($logTypeTableValue = CRM_Utils_Array::value("log_type_table_value", $this->_params)) {
272 $logTypeTableClause = $this->whereClause($this->_columns['log_civicrm_entity']['filters']['log_type_table'],
273 $this->_params['log_type_table_op'], $logTypeTableValue, NULL, NULL);
274 unset($this->_params['log_type_table_value']);
275 }
276
277 foreach ($this->_logTables as $entity => $detail) {
278 if ((in_array($this->getLogType($entity), $logTypes) &&
279 CRM_Utils_Array::value('log_type_op', $this->_params) == 'in') ||
280 (!in_array($this->getLogType($entity), $logTypes) &&
281 CRM_Utils_Array::value('log_type_op', $this->_params) == 'notin')
282 ) {
283 $this->from($entity);
284 $sql = $this->buildQuery(FALSE);
285 $sql = str_replace("entity_log_civireport.log_type as", "'{$entity}' as", $sql);
286 $sql = "INSERT IGNORE INTO civicrm_temp_civireport_logsummary {$sql}";
287 CRM_Core_DAO::executeQuery($sql);
288 }
289 }
290
291 // add computed log_type column so that we can do a group by after that, which will help
292 // alterDisplay() counts sync with pager counts
293 $sql = "SELECT DISTINCT log_type FROM civicrm_temp_civireport_logsummary";
294 $dao = CRM_Core_DAO::executeQuery($sql);
295 $replaceWith = array();
296 while ($dao->fetch()) {
297 $type = $this->getLogType($dao->log_type);
298 if (!array_key_exists($type, $replaceWith)) {
299 $replaceWith[$type] = array();
300 }
301 $replaceWith[$type][] = $dao->log_type;
302 }
303 foreach ($replaceWith as $type => $tables) {
304 if (!empty($tables)) {
305 $replaceWith[$type] = implode("','", $tables);
306 }
307 }
308
309 $sql = "ALTER TABLE civicrm_temp_civireport_logsummary ADD COLUMN log_civicrm_entity_log_type_label varchar(64)";
310 CRM_Core_DAO::executeQuery($sql);
311 foreach ($replaceWith as $type => $in) {
312 $sql = "UPDATE civicrm_temp_civireport_logsummary SET log_civicrm_entity_log_type_label='{$type}', log_date=log_date WHERE log_type IN('$in')";
313 CRM_Core_DAO::executeQuery($sql);
314 }
315
316 // note the group by columns are same as that used in alterDisplay as $newRows - $key
317 $this->limit();
318 $sql = "{$this->_select}
319 FROM civicrm_temp_civireport_logsummary entity_log_civireport
320 WHERE {$logTypeTableClause}
321 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
322 ORDER BY log_civicrm_entity_log_date DESC {$this->_limit}";
323 $sql = str_replace('modified_contact_civireport.display_name', 'entity_log_civireport.altered_contact', $sql);
324 $sql = str_replace('modified_contact_civireport.id', 'entity_log_civireport.altered_contact_id', $sql);
325 $sql = str_replace(array(
326 'modified_contact_civireport.',
327 'altered_by_contact_civireport.'
328 ), 'entity_log_civireport.', $sql);
329 $this->buildRows($sql, $rows);
330
331 // format result set.
332 $this->formatDisplay($rows);
333
334 // assign variables to templates
335 $this->doTemplateAssignment($rows);
336
337 // do print / pdf / instance stuff if needed
338 $this->endPostProcess($rows);
339 }
340
341 function getLogType($entity) {
342 if (!empty($this->_logTables[$entity]['log_type'])) {
343 return $this->_logTables[$entity]['log_type'];
344 }
345 $logType = ucfirst(substr($entity, strrpos($entity, '_') + 1));
346 return $logType;
347 }
348
349 function getEntityValue($id, $entity, $logDate) {
350 if (!empty($this->_logTables[$entity]['bracket_info'])) {
351 if (!empty($this->_logTables[$entity]['bracket_info']['entity_column'])) {
352 $logTable = !empty($this->_logTables[$entity]['table_name']) ? $this->_logTables[$entity]['table_name'] : $entity;
353 $sql = "
354 SELECT {$this->_logTables[$entity]['bracket_info']['entity_column']}
355 FROM `{$this->loggingDB}`.{$logTable}
356 WHERE log_date <= %1 AND id = %2 ORDER BY log_date DESC LIMIT 1";
357 $entityID = CRM_Core_DAO::singleValueQuery($sql, array(
358 1 => array(
359 CRM_Utils_Date::isoToMysql($logDate),
360 'Timestamp'
361 ),
362 2 => array($id, 'Integer')
363 ));
364 }
365 else {
366 $entityID = $id;
367 }
368
369 // since case_type_id is a varchar field with separator
370 if ($entity == 'log_civicrm_case') {
371 $entityID = explode(CRM_Case_BAO_Case::VALUE_SEPARATOR, $entityID);
372 $entityID = CRM_Utils_Array::value(1, $entityID);
373 }
374
375 if ($entityID && $logDate && array_key_exists('table', $this->_logTables[$entity]['bracket_info'])) {
376 $sql = "
377 SELECT {$this->_logTables[$entity]['bracket_info']['column']}
378 FROM `{$this->loggingDB}`.{$this->_logTables[$entity]['bracket_info']['table']}
379 WHERE log_date <= %1 AND id = %2 ORDER BY log_date DESC LIMIT 1";
380 return CRM_Core_DAO::singleValueQuery($sql, array(
381 1 => array(CRM_Utils_Date::isoToMysql($logDate), 'Timestamp'),
382 2 => array($entityID, 'Integer')
383 ));
384 }
385 else {
386 if (array_key_exists('options', $this->_logTables[$entity]['bracket_info']) && $entityID) {
387 return CRM_Utils_Array::value($entityID, $this->_logTables[$entity]['bracket_info']['options']);
388 }
389 }
390 }
391 return NULL;
392 }
393
394 function getEntityAction($id, $connId, $entity, $oldAction) {
395 if (!empty($this->_logTables[$entity]['action_column'])) {
396 $sql = "select {$this->_logTables[$entity]['action_column']} from `{$this->loggingDB}`.{$entity} where id = %1 AND log_conn_id = %2";
397 $newAction = CRM_Core_DAO::singleValueQuery($sql, array(
398 1 => array($id, 'Integer'),
399 2 => array($connId, 'Integer')
400 ));
401
402 switch ($entity) {
403 case 'log_civicrm_group_contact':
404 if ($oldAction !== 'Update') {
405 $newAction = $oldAction;
406 }
407 if ($oldAction == 'Insert') {
408 $newAction = 'Added';
409 }
410 break;
411 }
412 return $newAction;
413 }
414 return NULL;
415 }
416 }