3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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
35 class CRM_Logging_ReportSummary
extends CRM_Report_Form
{
38 protected $_logTables = array();
42 function __construct() {
43 // don’t display the ‘Add these Contacts to Group’ button
44 $this->_add2groupSupported
= FALSE;
46 $dsn = defined('CIVICRM_LOGGING_DSN') ? DB
::parseDSN(CIVICRM_LOGGING_DSN
) : DB
::parseDSN(CIVICRM_DSN
);
47 $this->loggingDB
= $dsn['database'];
49 // used for redirect back to contact summary
50 $this->cid
= CRM_Utils_Request
::retrieve('cid', 'Integer', CRM_Core_DAO
::$_nullObject);
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);
59 'log_civicrm_contact' =>
63 'log_civicrm_email' =>
66 'log_type' => 'Contact',
68 'log_civicrm_phone' =>
71 'log_type' => 'Contact',
73 'log_civicrm_address' =>
76 'log_type' => 'Contact',
81 'entity_table' => TRUE,
82 'bracket_info' => array('table' => 'log_civicrm_note', 'column' => 'subject'),
84 'log_civicrm_note_comment' =>
87 'table_name' => 'log_civicrm_note',
89 'table' => 'log_civicrm_note',
90 'join' => "entity_log_civireport.entity_id = fk_table.id AND entity_log_civireport.entity_table = 'civicrm_note'"
92 'entity_table' => TRUE,
93 'bracket_info' => array('table' => 'log_civicrm_note', 'column' => 'subject'),
95 'log_civicrm_group_contact' =>
98 'bracket_info' => array('entity_column' => 'group_id', 'table' => 'log_civicrm_group', 'column' => 'title'),
99 'action_column' => 'status',
100 'log_type' => 'Group',
102 'log_civicrm_entity_tag' =>
105 'bracket_info' => array('entity_column' => 'tag_id', 'table' => 'log_civicrm_tag', 'column' => 'name'),
106 'entity_table' => TRUE
108 'log_civicrm_relationship' =>
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'
117 'log_civicrm_activity_for_target' =>
119 'fk' => 'contact_id',
120 'table_name' => 'log_civicrm_activity',
122 'table' => 'log_civicrm_activity_contact',
123 'join' => "(entity_log_civireport.id = fk_table.activity_id AND fk_table.record_type_id = {$targetID})"
125 'bracket_info' => array(
126 'entity_column' => 'activity_type_id',
127 'options' => CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, FALSE, 'label', TRUE)
129 'log_type' => 'Activity',
131 'log_civicrm_activity_for_assignee' =>
133 'fk' => 'contact_id',
134 'table_name' => 'log_civicrm_activity',
136 'table' => 'log_civicrm_activity_contact',
137 'join' => "entity_log_civireport.id = fk_table.activity_id AND fk_table.record_type_id = {$assigneeID}"
139 'bracket_info' => array(
140 'entity_column' => 'activity_type_id',
141 'options' => CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, FALSE, 'label', TRUE)
143 'log_type' => 'Activity',
145 'log_civicrm_activity_for_source' =>
147 'fk' => 'contact_id',
148 'table_name' => 'log_civicrm_activity',
150 'table' => 'log_civicrm_activity_contact',
151 'join' => "entity_log_civireport.id = fk_table.activity_id AND fk_table.record_type_id = {$sourceID}"
153 'bracket_info' => array(
154 'entity_column' => 'activity_type_id',
155 'options' => CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, FALSE, 'label', TRUE)
157 'log_type' => 'Activity',
159 'log_civicrm_case' =>
161 'fk' => 'contact_id',
163 'table' => 'log_civicrm_case_contact',
164 'join' => 'entity_log_civireport.id = fk_table.case_id'
166 'bracket_info' => array(
167 'entity_column' => 'case_type_id',
168 'options' => CRM_Case_PseudoConstant
::caseType('title', FALSE)
173 $logging = new CRM_Logging_Schema
;
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');
181 // build _logTables for address custom tables
182 $customTables = $logging->entityCustomDataLogTables('Address');
183 foreach ($customTables as $table) {
184 $this->_logTables
[$table] =
186 'fk' => 'contact_id',// for join of fk_table with contact table
188 'table' => 'log_civicrm_address', // fk_table
189 'join' => 'entity_log_civireport.entity_id = fk_table.id'
191 'log_type' => 'Contact'
195 // allow log tables to be extended via report hooks
196 CRM_Report_BAO_Hook
::singleton()->alterLogTables($this, $this->_logTables
);
198 parent
::__construct();
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';
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);
220 $this->_select
= 'SELECT ' . implode(', ', $select) . ' ';
224 // reset where clause as its called multiple times, every time insert sql is built.
225 $this->_whereClauses
= array();
228 $this->_where
.= " AND (entity_log_civireport.log_action != 'Initialization')";
231 function postProcess() {
232 $this->beginPostProcess();
235 $tempColumns = "id int(10)";
236 if (!empty($this->_params
['fields']['log_action'])) {
237 $tempColumns .= ", log_action varchar(64)";
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)";
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)";
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);
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;
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;
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']);
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')
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);
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();
301 $replaceWith[$type][] = $dao->log_type
;
303 foreach ($replaceWith as $type => $tables) {
304 if (!empty($tables)) {
305 $replaceWith[$type] = implode("','", $tables);
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);
316 // note the group by columns are same as that used in alterDisplay as $newRows - $key
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);
331 // format result set.
332 $this->formatDisplay($rows);
334 // assign variables to templates
335 $this->doTemplateAssignment($rows);
337 // do print / pdf / instance stuff if needed
338 $this->endPostProcess($rows);
341 function getLogType($entity) {
342 if (!empty($this->_logTables
[$entity]['log_type'])) {
343 return $this->_logTables
[$entity]['log_type'];
345 $logType = ucfirst(substr($entity, strrpos($entity, '_') +
1));
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;
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(
359 CRM_Utils_Date
::isoToMysql($logDate),
362 2 => array($id, 'Integer')
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);
375 if ($entityID && $logDate && array_key_exists('table', $this->_logTables
[$entity]['bracket_info'])) {
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')
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']);
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')
403 case 'log_civicrm_group_contact':
404 if ($oldAction !== 'Update') {
405 $newAction = $oldAction;
407 if ($oldAction == 'Insert') {
408 $newAction = 'Added';