Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
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 | */ | |
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() { | |
694e78fd DS |
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 | ||
cbb9666e | 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 | ||
6a488035 | 57 | $this->_logTables = |
cbb9666e | 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', | |
8507f5bb | 148 | 'table_name' => 'log_civicrm_activity', |
cbb9666e | 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', | |
42047ce4 | 168 | 'options' => CRM_Case_PseudoConstant::caseType('title', FALSE) |
cbb9666e | 169 | ), |
170 | ), | |
171 | ); | |
6a488035 | 172 | |
6a488035 | 173 | $logging = new CRM_Logging_Schema; |
694e78fd DS |
174 | |
175 | // build _logTables for contact custom tables | |
176 | $customTables = $logging->entityCustomDataLogTables('Contact'); | |
6a488035 TO |
177 | foreach ($customTables as $table) { |
178 | $this->_logTables[$table] = array('fk' => 'entity_id', 'log_type' => 'Contact'); | |
179 | } | |
180 | ||
694e78fd DS |
181 | // build _logTables for address custom tables |
182 | $customTables = $logging->entityCustomDataLogTables('Address'); | |
183 | foreach ($customTables as $table) { | |
42047ce4 | 184 | $this->_logTables[$table] = |
694e78fd DS |
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 | ||
c3e3dd65 | 195 | // allow log tables to be extended via report hooks |
6b4b11c4 | 196 | CRM_Report_BAO_Hook::singleton()->alterLogTables($this, $this->_logTables); |
c3e3dd65 | 197 | |
6a488035 TO |
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() { | |
8c544cd2 DS |
224 | // reset where clause as its called multiple times, every time insert sql is built. |
225 | $this->_whereClauses = array(); | |
42047ce4 | 226 | |
6a488035 TO |
227 | parent::where(); |
228 | $this->_where .= " AND (entity_log_civireport.log_action != 'Initialization')"; | |
229 | } | |
230 | ||
231 | function postProcess() { | |
232 | $this->beginPostProcess(); | |
233 | $rows = array(); | |
eea16664 | 234 | |
9c097caf | 235 | $tempColumns = "id int(10)"; |
a7488080 | 236 | if (!empty($this->_params['fields']['log_action'])) { |
9c097caf DS |
237 | $tempColumns .= ", log_action varchar(64)"; |
238 | } | |
239 | $tempColumns .= ", log_type varchar(64), log_user_id int(10), log_date timestamp"; | |
a7488080 | 240 | if (!empty($this->_params['fields']['altered_contact'])) { |
9c097caf DS |
241 | $tempColumns .= ", altered_contact varchar(128)"; |
242 | } | |
243 | $tempColumns .= ", altered_contact_id int(10), log_conn_id int(11), is_deleted tinyint(4)"; | |
a7488080 | 244 | if (!empty($this->_params['fields']['display_name'])) { |
9c097caf DS |
245 | $tempColumns .= ", display_name varchar(128)"; |
246 | } | |
247 | ||
6a488035 | 248 | // temp table to hold all altered contact-ids |
9c097caf | 249 | $sql = "CREATE TEMPORARY TABLE civicrm_temp_civireport_logsummary ( {$tempColumns} ) ENGINE=HEAP"; |
6a488035 TO |
250 | CRM_Core_DAO::executeQuery($sql); |
251 | ||
252 | $logDateClause = $this->dateClause('log_date', | |
cbb9666e | 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), | |
6a488035 TO |
256 | CRM_Utils_Type::T_DATE, |
257 | CRM_Utils_Array::value("log_date_from_time", $this->_params), | |
cbb9666e | 258 | CRM_Utils_Array::value("log_date_to_time", $this->_params)); |
259 | $logDateClause = $logDateClause ? "AND {$logDateClause}" : NULL; | |
6a488035 TO |
260 | |
261 | $logTypes = CRM_Utils_Array::value('log_type_value', $this->_params); | |
262 | unset($this->_params['log_type_value']); | |
cbb9666e | 263 | if (empty($logTypes)) { |
264 | foreach (array_keys($this->_logTables) as $table) { | |
6a488035 TO |
265 | $type = $this->getLogType($table); |
266 | $logTypes[$type] = $type; | |
267 | } | |
268 | } | |
269 | ||
798878cc DS |
270 | $logTypeTableClause = '(1)'; |
271 | if ($logTypeTableValue = CRM_Utils_Array::value("log_type_table_value", $this->_params)) { | |
cbb9666e | 272 | $logTypeTableClause = $this->whereClause($this->_columns['log_civicrm_entity']['filters']['log_type_table'], |
273 | $this->_params['log_type_table_op'], $logTypeTableValue, NULL, NULL); | |
798878cc DS |
274 | unset($this->_params['log_type_table_value']); |
275 | } | |
cbb9666e | 276 | |
277 | foreach ($this->_logTables as $entity => $detail) { | |
6a488035 | 278 | if ((in_array($this->getLogType($entity), $logTypes) && |
cbb9666e | 279 | CRM_Utils_Array::value('log_type_op', $this->_params) == 'in') || |
6a488035 | 280 | (!in_array($this->getLogType($entity), $logTypes) && |
cbb9666e | 281 | CRM_Utils_Array::value('log_type_op', $this->_params) == 'notin') |
282 | ) { | |
283 | $this->from($entity); | |
284 | $sql = $this->buildQuery(FALSE); | |
6a488035 TO |
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 | ||
9a2a98cd DS |
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(); | |
cbb9666e | 296 | while ($dao->fetch()) { |
9a2a98cd | 297 | $type = $this->getLogType($dao->log_type); |
cbb9666e | 298 | if (!array_key_exists($type, $replaceWith)) { |
9a2a98cd | 299 | $replaceWith[$type] = array(); |
cbb9666e | 300 | } |
9a2a98cd DS |
301 | $replaceWith[$type][] = $dao->log_type; |
302 | } | |
303 | foreach ($replaceWith as $type => $tables) { | |
304 | if (!empty($tables)) { | |
305 | $replaceWith[$type] = implode("','", $tables); | |
306 | } | |
307 | } | |
2efcf0c2 | 308 | |
9a2a98cd DS |
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 | |
6a488035 TO |
317 | $this->limit(); |
318 | $sql = "{$this->_select} | |
319 | FROM civicrm_temp_civireport_logsummary entity_log_civireport | |
798878cc | 320 | WHERE {$logTypeTableClause} |
9a2a98cd DS |
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}"; | |
cbb9666e | 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); | |
6a488035 TO |
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 | ||
cbb9666e | 341 | function getLogType($entity) { |
a7488080 | 342 | if (!empty($this->_logTables[$entity]['log_type'])) { |
6a488035 TO |
343 | return $this->_logTables[$entity]['log_type']; |
344 | } | |
345 | $logType = ucfirst(substr($entity, strrpos($entity, '_') + 1)); | |
346 | return $logType; | |
347 | } | |
348 | ||
cbb9666e | 349 | function getEntityValue($id, $entity, $logDate) { |
a7488080 CW |
350 | if (!empty($this->_logTables[$entity]['bracket_info'])) { |
351 | if (!empty($this->_logTables[$entity]['bracket_info']['entity_column'])) { | |
0d8afee2 | 352 | $logTable = !empty($this->_logTables[$entity]['table_name']) ? $this->_logTables[$entity]['table_name'] : $entity; |
6a488035 | 353 | $sql = " |
eea16664 | 354 | SELECT {$this->_logTables[$entity]['bracket_info']['entity_column']} |
355 | FROM `{$this->loggingDB}`.{$logTable} | |
6a488035 | 356 | WHERE log_date <= %1 AND id = %2 ORDER BY log_date DESC LIMIT 1"; |
cbb9666e | 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 { | |
6a488035 TO |
366 | $entityID = $id; |
367 | } | |
368 | ||
369 | // since case_type_id is a varchar field with separator | |
eea16664 | 370 | if ($entity == 'log_civicrm_case') { |
cbb9666e | 371 | $entityID = explode(CRM_Case_BAO_Case::VALUE_SEPARATOR, $entityID); |
6a488035 TO |
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 = " | |
eea16664 | 377 | SELECT {$this->_logTables[$entity]['bracket_info']['column']} |
378 | FROM `{$this->loggingDB}`.{$this->_logTables[$entity]['bracket_info']['table']} | |
6a488035 | 379 | WHERE log_date <= %1 AND id = %2 ORDER BY log_date DESC LIMIT 1"; |
cbb9666e | 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 | } | |
6a488035 TO |
389 | } |
390 | } | |
cbb9666e | 391 | return NULL; |
6a488035 TO |
392 | } |
393 | ||
cbb9666e | 394 | function getEntityAction($id, $connId, $entity, $oldAction) { |
a7488080 | 395 | if (!empty($this->_logTables[$entity]['action_column'])) { |
6a488035 | 396 | $sql = "select {$this->_logTables[$entity]['action_column']} from `{$this->loggingDB}`.{$entity} where id = %1 AND log_conn_id = %2"; |
cbb9666e | 397 | $newAction = CRM_Core_DAO::singleValueQuery($sql, array( |
398 | 1 => array($id, 'Integer'), | |
399 | 2 => array($connId, 'Integer') | |
400 | )); | |
6a488035 TO |
401 | |
402 | switch ($entity) { | |
cbb9666e | 403 | case 'log_civicrm_group_contact': |
404 | if ($oldAction !== 'Update') { | |
405 | $newAction = $oldAction; | |
406 | } | |
407 | if ($oldAction == 'Insert') { | |
408 | $newAction = 'Added'; | |
409 | } | |
410 | break; | |
6a488035 TO |
411 | } |
412 | return $newAction; | |
413 | } | |
cbb9666e | 414 | return NULL; |
6a488035 TO |
415 | } |
416 | } |