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