3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
34 class CRM_Logging_ReportSummary
extends CRM_Report_Form
{
37 protected $_logTables = array();
44 public function __construct() {
45 // don’t display the ‘Add these Contacts to Group’ button
46 $this->_add2groupSupported
= FALSE;
48 $dsn = defined('CIVICRM_LOGGING_DSN') ? DB
::parseDSN(CIVICRM_LOGGING_DSN
) : DB
::parseDSN(CIVICRM_DSN
);
49 $this->loggingDB
= $dsn['database'];
51 // used for redirect back to contact summary
52 $this->cid
= CRM_Utils_Request
::retrieve('cid', 'Integer', CRM_Core_DAO
::$_nullObject);
54 $activityContacts = CRM_Core_OptionGroup
::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
55 $sourceID = CRM_Utils_Array
::key('Activity Source', $activityContacts);
56 $assigneeID = CRM_Utils_Array
::key('Activity Assignees', $activityContacts);
57 $targetID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
59 $this->_logTables
= array(
60 'log_civicrm_contact' => array(
63 'log_civicrm_email' => array(
65 'log_type' => 'Contact',
67 'log_civicrm_phone' => array(
69 'log_type' => 'Contact',
71 'log_civicrm_address' => array(
73 'log_type' => 'Contact',
75 'log_civicrm_note' => array(
77 'entity_table' => TRUE,
78 'bracket_info' => array(
79 'table' => 'log_civicrm_note',
80 'column' => 'subject',
83 'log_civicrm_note_comment' => array(
85 'table_name' => 'log_civicrm_note',
87 'table' => 'log_civicrm_note',
88 'join' => "entity_log_civireport.entity_id = fk_table.id AND entity_log_civireport.entity_table = 'civicrm_note'",
90 'entity_table' => TRUE,
91 'bracket_info' => array(
92 'table' => 'log_civicrm_note',
93 'column' => 'subject',
96 'log_civicrm_group_contact' => array(
98 'bracket_info' => array(
99 'entity_column' => 'group_id',
100 'table' => 'log_civicrm_group',
103 'action_column' => 'status',
104 'log_type' => 'Group',
106 'log_civicrm_entity_tag' => array(
108 'bracket_info' => array(
109 'entity_column' => 'tag_id',
110 'table' => 'log_civicrm_tag',
113 'entity_table' => TRUE,
115 'log_civicrm_relationship' => array(
116 'fk' => 'contact_id_a',
117 'bracket_info' => array(
118 'entity_column' => 'relationship_type_id',
119 'table' => 'log_civicrm_relationship_type',
120 'column' => 'label_a_b',
123 'log_civicrm_activity_for_target' => array(
124 'fk' => 'contact_id',
125 'table_name' => 'log_civicrm_activity',
127 'table' => 'log_civicrm_activity_contact',
128 'join' => "(entity_log_civireport.id = fk_table.activity_id AND fk_table.record_type_id = {$targetID})",
130 'bracket_info' => array(
131 'entity_column' => 'activity_type_id',
132 'options' => CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, FALSE, 'label', TRUE),
134 'log_type' => 'Activity',
136 'log_civicrm_activity_for_assignee' => array(
137 'fk' => 'contact_id',
138 'table_name' => 'log_civicrm_activity',
140 'table' => 'log_civicrm_activity_contact',
141 'join' => "entity_log_civireport.id = fk_table.activity_id AND fk_table.record_type_id = {$assigneeID}",
143 'bracket_info' => array(
144 'entity_column' => 'activity_type_id',
145 'options' => CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, FALSE, 'label', TRUE),
147 'log_type' => 'Activity',
149 'log_civicrm_activity_for_source' => array(
150 'fk' => 'contact_id',
151 'table_name' => 'log_civicrm_activity',
153 'table' => 'log_civicrm_activity_contact',
154 'join' => "entity_log_civireport.id = fk_table.activity_id AND fk_table.record_type_id = {$sourceID}",
156 'bracket_info' => array(
157 'entity_column' => 'activity_type_id',
158 'options' => CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, FALSE, 'label', TRUE),
160 'log_type' => 'Activity',
162 'log_civicrm_case' => array(
163 'fk' => 'contact_id',
165 'table' => 'log_civicrm_case_contact',
166 'join' => 'entity_log_civireport.id = fk_table.case_id',
168 'bracket_info' => array(
169 'entity_column' => 'case_type_id',
170 'options' => CRM_Case_PseudoConstant
::caseType('title', FALSE),
175 $logging = new CRM_Logging_Schema();
177 // build _logTables for contact custom tables
178 $customTables = $logging->entityCustomDataLogTables('Contact');
179 foreach ($customTables as $table) {
180 $this->_logTables
[$table] = array(
182 'log_type' => 'Contact',
186 // build _logTables for address custom tables
187 $customTables = $logging->entityCustomDataLogTables('Address');
188 foreach ($customTables as $table) {
189 $this->_logTables
[$table] = array(
190 // For join of fk_table with contact table.
191 'fk' => 'contact_id',
194 'table' => 'log_civicrm_address',
195 'join' => 'entity_log_civireport.entity_id = fk_table.id',
197 'log_type' => 'Contact',
201 // Allow log tables to be extended via report hooks.
202 CRM_Report_BAO_Hook
::singleton()->alterLogTables($this, $this->_logTables
);
204 parent
::__construct();
207 public function groupBy() {
208 $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';
211 public function select() {
213 $this->_columnHeaders
= array();
214 foreach ($this->_columns
as $tableName => $table) {
215 if (array_key_exists('fields', $table)) {
216 foreach ($table['fields'] as $fieldName => $field) {
217 if (CRM_Utils_Array
::value('required', $field) or
218 CRM_Utils_Array
::value($fieldName, $this->_params
['fields'])
220 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
221 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
222 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['no_display'] = CRM_Utils_Array
::value('no_display', $field);
223 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
228 $this->_select
= 'SELECT ' . implode(', ', $select) . ' ';
231 public function where() {
232 // reset where clause as its called multiple times, every time insert sql is built.
233 $this->_whereClauses
= array();
236 $this->_where
.= " AND (entity_log_civireport.log_action != 'Initialization')";
239 public function postProcess() {
240 $this->beginPostProcess();
243 $tempColumns = "id int(10)";
244 if (!empty($this->_params
['fields']['log_action'])) {
245 $tempColumns .= ", log_action varchar(64)";
247 $tempColumns .= ", log_type varchar(64), log_user_id int(10), log_date timestamp";
248 if (!empty($this->_params
['fields']['altered_contact'])) {
249 $tempColumns .= ", altered_contact varchar(128)";
251 $tempColumns .= ", altered_contact_id int(10), log_conn_id int(11), is_deleted tinyint(4)";
252 if (!empty($this->_params
['fields']['display_name'])) {
253 $tempColumns .= ", display_name varchar(128)";
256 // temp table to hold all altered contact-ids
257 $sql = "CREATE TEMPORARY TABLE civicrm_temp_civireport_logsummary ( {$tempColumns} ) ENGINE=HEAP";
258 CRM_Core_DAO
::executeQuery($sql);
260 $logDateClause = $this->dateClause('log_date',
261 CRM_Utils_Array
::value("log_date_relative", $this->_params
),
262 CRM_Utils_Array
::value("log_date_from", $this->_params
),
263 CRM_Utils_Array
::value("log_date_to", $this->_params
),
264 CRM_Utils_Type
::T_DATE
,
265 CRM_Utils_Array
::value("log_date_from_time", $this->_params
),
266 CRM_Utils_Array
::value("log_date_to_time", $this->_params
));
267 $logDateClause = $logDateClause ?
"AND {$logDateClause}" : NULL;
269 $logTypes = CRM_Utils_Array
::value('log_type_value', $this->_params
);
270 unset($this->_params
['log_type_value']);
271 if (empty($logTypes)) {
272 foreach (array_keys($this->_logTables
) as $table) {
273 $type = $this->getLogType($table);
274 $logTypes[$type] = $type;
278 $logTypeTableClause = '(1)';
279 if ($logTypeTableValue = CRM_Utils_Array
::value("log_type_table_value", $this->_params
)) {
280 $logTypeTableClause = $this->whereClause($this->_columns
['log_civicrm_entity']['filters']['log_type_table'],
281 $this->_params
['log_type_table_op'], $logTypeTableValue, NULL, NULL);
282 unset($this->_params
['log_type_table_value']);
285 foreach ($this->_logTables
as $entity => $detail) {
286 if ((in_array($this->getLogType($entity), $logTypes) &&
287 CRM_Utils_Array
::value('log_type_op', $this->_params
) == 'in') ||
288 (!in_array($this->getLogType($entity), $logTypes) &&
289 CRM_Utils_Array
::value('log_type_op', $this->_params
) == 'notin')
291 $this->from($entity);
292 $sql = $this->buildQuery(FALSE);
293 $sql = str_replace("entity_log_civireport.log_type as", "'{$entity}' as", $sql);
294 $sql = "INSERT IGNORE INTO civicrm_temp_civireport_logsummary {$sql}";
295 CRM_Core_DAO
::executeQuery($sql);
299 // add computed log_type column so that we can do a group by after that, which will help
300 // alterDisplay() counts sync with pager counts
301 $sql = "SELECT DISTINCT log_type FROM civicrm_temp_civireport_logsummary";
302 $dao = CRM_Core_DAO
::executeQuery($sql);
303 $replaceWith = array();
304 while ($dao->fetch()) {
305 $type = $this->getLogType($dao->log_type
);
306 if (!array_key_exists($type, $replaceWith)) {
307 $replaceWith[$type] = array();
309 $replaceWith[$type][] = $dao->log_type
;
311 foreach ($replaceWith as $type => $tables) {
312 if (!empty($tables)) {
313 $replaceWith[$type] = implode("','", $tables);
317 $sql = "ALTER TABLE civicrm_temp_civireport_logsummary ADD COLUMN log_civicrm_entity_log_type_label varchar(64)";
318 CRM_Core_DAO
::executeQuery($sql);
319 foreach ($replaceWith as $type => $in) {
320 $sql = "UPDATE civicrm_temp_civireport_logsummary SET log_civicrm_entity_log_type_label='{$type}', log_date=log_date WHERE log_type IN('$in')";
321 CRM_Core_DAO
::executeQuery($sql);
324 // note the group by columns are same as that used in alterDisplay as $newRows - $key
326 $sql = "{$this->_select}
327 FROM civicrm_temp_civireport_logsummary entity_log_civireport
328 WHERE {$logTypeTableClause}
329 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
330 ORDER BY log_civicrm_entity_log_date DESC {$this->_limit}";
331 $sql = str_replace('modified_contact_civireport.display_name', 'entity_log_civireport.altered_contact', $sql);
332 $sql = str_replace('modified_contact_civireport.id', 'entity_log_civireport.altered_contact_id', $sql);
333 $sql = str_replace(array(
334 'modified_contact_civireport.',
335 'altered_by_contact_civireport.',
336 ), 'entity_log_civireport.', $sql);
337 $this->buildRows($sql, $rows);
339 // format result set.
340 $this->formatDisplay($rows);
342 // assign variables to templates
343 $this->doTemplateAssignment($rows);
345 // do print / pdf / instance stuff if needed
346 $this->endPostProcess($rows);
352 * @param string $entity
356 public function getLogType($entity) {
357 if (!empty($this->_logTables
[$entity]['log_type'])) {
358 return $this->_logTables
[$entity]['log_type'];
360 $logType = ucfirst(substr($entity, strrpos($entity, '_') +
1));
371 * @return mixed|null|string
373 public function getEntityValue($id, $entity, $logDate) {
374 if (!empty($this->_logTables
[$entity]['bracket_info'])) {
375 if (!empty($this->_logTables
[$entity]['bracket_info']['entity_column'])) {
376 $logTable = !empty($this->_logTables
[$entity]['table_name']) ?
$this->_logTables
[$entity]['table_name'] : $entity;
378 SELECT {$this->_logTables[$entity]['bracket_info']['entity_column']}
379 FROM `{$this->loggingDB}`.{$logTable}
380 WHERE log_date <= %1 AND id = %2 ORDER BY log_date DESC LIMIT 1";
381 $entityID = CRM_Core_DAO
::singleValueQuery($sql, array(
383 CRM_Utils_Date
::isoToMysql($logDate),
386 2 => array($id, 'Integer'),
393 if ($entityID && $logDate &&
394 array_key_exists('table', $this->_logTables
[$entity]['bracket_info'])
397 SELECT {$this->_logTables[$entity]['bracket_info']['column']}
398 FROM `{$this->loggingDB}`.{$this->_logTables[$entity]['bracket_info']['table']}
399 WHERE log_date <= %1 AND id = %2 ORDER BY log_date DESC LIMIT 1";
400 return CRM_Core_DAO
::singleValueQuery($sql, array(
401 1 => array(CRM_Utils_Date
::isoToMysql($logDate), 'Timestamp'),
402 2 => array($entityID, 'Integer'),
406 if (array_key_exists('options', $this->_logTables
[$entity]['bracket_info']) &&
409 return CRM_Utils_Array
::value($entityID, $this->_logTables
[$entity]['bracket_info']['options']);
424 * @return null|string
426 public function getEntityAction($id, $connId, $entity, $oldAction) {
427 if (!empty($this->_logTables
[$entity]['action_column'])) {
428 $sql = "select {$this->_logTables[$entity]['action_column']} from `{$this->loggingDB}`.{$entity} where id = %1 AND log_conn_id = %2";
429 $newAction = CRM_Core_DAO
::singleValueQuery($sql, array(
430 1 => array($id, 'Integer'),
431 2 => array($connId, 'Integer'),
435 case 'log_civicrm_group_contact':
436 if ($oldAction !== 'Update') {
437 $newAction = $oldAction;
439 if ($oldAction == 'Insert') {
440 $newAction = 'Added';