Merge pull request #5010 from eileenmcnaughton/rebase2
[civicrm-core.git] / CRM / Report / Form / Contact / LoggingSummary.php
CommitLineData
6a488035 1<?php
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035 32 * $Id$
6a488035
TO
33 */
34class CRM_Report_Form_Contact_LoggingSummary extends CRM_Logging_ReportSummary {
74cf4551 35 /**
2fb1dd66 36 * Class constructor.
74cf4551 37 */
00be9182 38 public function __construct() {
6a488035
TO
39 parent::__construct();
40
41 $logTypes = array();
481a74f4 42 foreach (array_keys($this->_logTables) as $table) {
6a488035
TO
43 $type = $this->getLogType($table);
44 $logTypes[$type] = $type;
45 }
46 asort($logTypes);
47
48 $this->_columns = array(
49 'log_civicrm_entity' => array(
50 'dao' => 'CRM_Contact_DAO_Contact',
51 'alias' => 'entity_log',
52 'fields' => array(
53 'id' => array(
54 'no_display' => TRUE,
55 'required' => TRUE,
56 ),
57 'log_action' => array(
58 'default' => TRUE,
59 'title' => ts('Action'),
60 ),
61 'log_type' => array(
62 'required' => TRUE,
63 'title' => ts('Log Type'),
64 ),
65 'log_user_id' => array(
66 'no_display' => TRUE,
67 'required' => TRUE,
68 ),
69 'log_date' => array(
70 'default' => TRUE,
71 'required' => TRUE,
72 'type' => CRM_Utils_Type::T_TIME,
73 'title' => ts('When'),
74 ),
75 'altered_contact' => array(
76 'default' => TRUE,
77 'name' => 'display_name',
78 'title' => ts('Altered Contact'),
79 'alias' => 'modified_contact_civireport',
80 ),
81 'altered_contact_id' => array(
82 'name' => 'id',
83 'no_display' => TRUE,
84 'required' => TRUE,
353ffa53 85 'alias' => 'modified_contact_civireport',
6a488035
TO
86 ),
87 'log_conn_id' => array(
88 'no_display' => TRUE,
89 'required' => TRUE,
90 ),
91 'is_deleted' => array(
92 'no_display' => TRUE,
93 'required' => TRUE,
94 'alias' => 'modified_contact_civireport',
95 ),
96 ),
97 'filters' => array(
98 'log_date' => array(
99 'title' => ts('When'),
100 'operatorType' => CRM_Report_Form::OP_DATE,
101 'type' => CRM_Utils_Type::T_DATE,
102 ),
103 'altered_contact' => array(
104 'name' => 'display_name',
105 'title' => ts('Altered Contact'),
106 'type' => CRM_Utils_Type::T_STRING,
107 'alias' => 'modified_contact_civireport',
108 ),
109 'altered_contact_id' => array(
110 'name' => 'id',
111 'type' => CRM_Utils_Type::T_INT,
112 'alias' => 'modified_contact_civireport',
113 'no_display' => TRUE,
114 ),
115 'log_type' => array(
116 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
117 'options' => $logTypes,
118 'title' => ts('Log Type'),
119 'type' => CRM_Utils_Type::T_STRING,
120 ),
798878cc 121 'log_type_table' => array(
353ffa53 122 'name' => 'log_type',
798878cc
DS
123 'title' => ts('Log Type Table'),
124 'type' => CRM_Utils_Type::T_STRING,
125 ),
6a488035
TO
126 'log_action' => array(
127 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
2fb1dd66
EM
128 'options' => array(
129 'Insert' => ts('Insert'),
130 'Update' => ts('Update'),
131 'Delete' => ts('Delete'),
132 ),
6a488035
TO
133 'title' => ts('Action'),
134 'type' => CRM_Utils_Type::T_STRING,
135 ),
136 'id' => array(
137 'no_display' => TRUE,
138 'type' => CRM_Utils_Type::T_INT,
139 ),
140 ),
141 ),
142 'altered_by_contact' => array(
353ffa53 143 'dao' => 'CRM_Contact_DAO_Contact',
6a488035
TO
144 'alias' => 'altered_by_contact',
145 'fields' => array(
146 'display_name' => array(
147 'default' => TRUE,
148 'name' => 'display_name',
149 'title' => ts('Altered By'),
150 ),
151 ),
152 'filters' => array(
153 'display_name' => array(
154 'name' => 'display_name',
155 'title' => ts('Altered By'),
156 'type' => CRM_Utils_Type::T_STRING,
157 ),
158 ),
159 ),
160 );
161 }
162
74cf4551 163 /**
2fb1dd66
EM
164 * Alter Report Display.
165 *
166 * @param array $rows
167 * Rows from report query.
74cf4551 168 */
00be9182 169 public function alterDisplay(&$rows) {
6a488035
TO
170 // cache for id → is_deleted mapping
171 $isDeleted = array();
353ffa53 172 $newRows = array();
6a488035
TO
173
174 foreach ($rows as $key => &$row) {
175 if (!isset($isDeleted[$row['log_civicrm_entity_altered_contact_id']])) {
3bdca100 176 $isDeleted[$row['log_civicrm_entity_altered_contact_id']] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
177 $row['log_civicrm_entity_altered_contact_id'], 'is_deleted') !== '0';
6a488035
TO
178 }
179
a7488080 180 if (!empty($row['log_civicrm_entity_altered_contact']) &&
353ffa53
TO
181 !$isDeleted[$row['log_civicrm_entity_altered_contact_id']]
182 ) {
3bdca100 183 $row['log_civicrm_entity_altered_contact_link'] = CRM_Utils_System::url('civicrm/contact/view',
184 'reset=1&cid=' . $row['log_civicrm_entity_altered_contact_id']);
6a488035
TO
185 $row['log_civicrm_entity_altered_contact_hover'] = ts("Go to contact summary");
186 $entity = $this->getEntityValue($row['log_civicrm_entity_id'], $row['log_civicrm_entity_log_type'], $row['log_civicrm_entity_log_date']);
84178120 187 if ($entity) {
6a488035 188 $row['log_civicrm_entity_altered_contact'] = $row['log_civicrm_entity_altered_contact'] . " [{$entity}]";
84178120 189 }
6a488035
TO
190 }
191 $row['altered_by_contact_display_name_link'] = CRM_Utils_System::url('civicrm/contact/view', 'reset=1&cid=' . $row['log_civicrm_entity_log_user_id']);
192 $row['altered_by_contact_display_name_hover'] = ts("Go to contact summary");
193
c9fb1753 194 if ($row['log_civicrm_entity_is_deleted'] and 'Update' == CRM_Utils_Array::value('log_civicrm_entity_log_action', $row)) {
6a488035
TO
195 $row['log_civicrm_entity_log_action'] = ts('Delete (to trash)');
196 }
197
f813f78e 198 if ('Contact' == CRM_Utils_Array::value('log_type', $this->_logTables[$row['log_civicrm_entity_log_type']]) &&
353ffa53
TO
199 CRM_Utils_Array::value('log_civicrm_entity_log_action', $row) == 'Insert'
200 ) {
6a488035
TO
201 $row['log_civicrm_entity_log_action'] = ts('Update');
202 }
203
f813f78e 204 if ($newAction = $this->getEntityAction($row['log_civicrm_entity_id'],
353ffa53
TO
205 $row['log_civicrm_entity_log_conn_id'],
206 $row['log_civicrm_entity_log_type'],
207 CRM_Utils_Array::value('log_civicrm_entity_log_action', $row))
208 ) {
6a488035 209 $row['log_civicrm_entity_log_action'] = $newAction;
84178120 210 }
6a488035
TO
211
212 $row['log_civicrm_entity_log_type'] = $this->getLogType($row['log_civicrm_entity_log_type']);
213
214 $date = CRM_Utils_Date::isoToMysql($row['log_civicrm_entity_log_date']);
215
c9fb1753 216 if ('Update' == CRM_Utils_Array::value('log_civicrm_entity_log_action', $row)) {
86bfa4f6 217 $q = "reset=1&log_conn_id={$row['log_civicrm_entity_log_conn_id']}&log_date=" . $date;
6a488035
TO
218 if ($this->cid) {
219 $q .= '&cid=' . $this->cid;
220 }
92fcb95f
TO
221 $q .= (!empty($row['log_civicrm_entity_altered_contact'])) ? '&alteredName=' . $row['log_civicrm_entity_altered_contact'] : '';
222 $q .= (!empty($row['altered_by_contact_display_name'])) ? '&alteredBy=' . $row['altered_by_contact_display_name'] : '';
223 $q .= (!empty($row['log_civicrm_entity_log_user_id'])) ? '&alteredById=' . $row['log_civicrm_entity_log_user_id'] : '';
6a488035
TO
224
225 $url1 = CRM_Report_Utils_Report::getNextUrl('logging/contact/detail', "{$q}&snippet=4&section=2&layout=overlay", FALSE, TRUE);
226 $url2 = CRM_Report_Utils_Report::getNextUrl('logging/contact/detail', "{$q}&section=2", FALSE, TRUE);
52604b19 227 $row['log_civicrm_entity_log_action'] = "<a href='{$url1}' class='crm-summary-link'><div class='icon ui-icon-zoomin'></div></a>&nbsp;<a title='View details for this update' href='{$url2}'>" . ts('Update') . '</a>';
6a488035
TO
228 }
229
353ffa53 230 $key = $date . '_' .
f813f78e 231 $row['log_civicrm_entity_log_type'] . '_' .
232 $row['log_civicrm_entity_log_conn_id'] . '_' .
6a488035 233 $row['log_civicrm_entity_log_user_id'] . '_' .
9a2a98cd 234 $row['log_civicrm_entity_altered_contact_id'];
6a488035
TO
235 $newRows[$key] = $row;
236
237 unset($row['log_civicrm_entity_log_user_id']);
238 unset($row['log_civicrm_entity_log_conn_id']);
239 }
240
241 krsort($newRows);
242 $rows = $newRows;
243 }
244
74cf4551 245 /**
2fb1dd66
EM
246 * Generate From Clause.
247 *
248 * @param string $logTable
74cf4551 249 */
84178120
TO
250 public function from($logTable = NULL) {
251 static $entity = NULL;
481a74f4 252 if ($logTable) {
6a488035
TO
253 $entity = $logTable;
254 }
255
353ffa53 256 $detail = $this->_logTables[$entity];
6a488035
TO
257 $tableName = CRM_Utils_Array::value('table_name', $detail, $entity);
258 $clause = CRM_Utils_Array::value('entity_table', $detail);
84178120 259 $clause = $clause ? "AND entity_log_civireport.entity_table = 'civicrm_contact'" : NULL;
f813f78e 260
6a488035 261 $joinClause = "
f813f78e 262INNER JOIN civicrm_contact modified_contact_civireport
6a488035
TO
263 ON (entity_log_civireport.{$detail['fk']} = modified_contact_civireport.id {$clause})";
264
a7488080 265 if (!empty($detail['joins'])) {
6a488035 266 $clause = CRM_Utils_Array::value('entity_table', $detail);
84178120 267 $clause = $clause ? "AND fk_table.entity_table = 'civicrm_contact'" : NULL;
6a488035
TO
268 $joinClause = "
269INNER JOIN `{$this->loggingDB}`.{$detail['joins']['table']} fk_table ON {$detail['joins']['join']}
f813f78e 270INNER JOIN civicrm_contact modified_contact_civireport
6a488035
TO
271 ON (fk_table.{$detail['fk']} = modified_contact_civireport.id {$clause})";
272 }
273
274 $this->_from = "
275FROM `{$this->loggingDB}`.$tableName entity_log_civireport
276{$joinClause}
f813f78e 277LEFT JOIN civicrm_contact altered_by_contact_civireport
6a488035
TO
278 ON (entity_log_civireport.log_user_id = altered_by_contact_civireport.id)";
279 }
96025800 280
6a488035 281}