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