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