Merge pull request #514 from adamwight/cache_acl_entityTable
[civicrm-core.git] / CRM / Report / Form / Contact / LoggingSummary.php
1 <?php
2 // $Id$
3
4 /*
5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
11 | |
12 | CiviCRM is free software; you can copy, modify, and distribute it |
13 | under the terms of the GNU Affero General Public License |
14 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | |
16 | CiviCRM is distributed in the hope that it will be useful, but |
17 | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | See the GNU Affero General Public License for more details. |
20 | |
21 | You should have received a copy of the GNU Affero General Public |
22 | License and the CiviCRM Licensing Exception along |
23 | with this program; if not, contact CiviCRM LLC |
24 | at info[AT]civicrm[DOT]org. If you have questions about the |
25 | GNU Affero General Public License or the licensing of CiviCRM, |
26 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
27 +--------------------------------------------------------------------+
28 */
29
30 /**
31 *
32 * @package CRM
33 * @copyright CiviCRM LLC (c) 2004-2013
34 * $Id$
35 *
36 */
37 class CRM_Report_Form_Contact_LoggingSummary extends CRM_Logging_ReportSummary {
38 function __construct() {
39 parent::__construct();
40
41 $logTypes = array();
42 foreach ( array_keys($this->_logTables) as $table ) {
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,
85 'alias' => 'modified_contact_civireport',
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 ),
121 'log_action' => array(
122 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
123 'options' => array('Insert' => ts('Insert'), 'Update' => ts('Update'), 'Delete' => ts('Delete')),
124 'title' => ts('Action'),
125 'type' => CRM_Utils_Type::T_STRING,
126 ),
127 'id' => array(
128 'no_display' => TRUE,
129 'type' => CRM_Utils_Type::T_INT,
130 ),
131 ),
132 ),
133 'altered_by_contact' => array(
134 'dao' => 'CRM_Contact_DAO_Contact',
135 'alias' => 'altered_by_contact',
136 'fields' => array(
137 'display_name' => array(
138 'default' => TRUE,
139 'name' => 'display_name',
140 'title' => ts('Altered By'),
141 ),
142 ),
143 'filters' => array(
144 'display_name' => array(
145 'name' => 'display_name',
146 'title' => ts('Altered By'),
147 'type' => CRM_Utils_Type::T_STRING,
148 ),
149 ),
150 ),
151 );
152 }
153
154 function alterDisplay(&$rows) {
155 // cache for id → is_deleted mapping
156 $isDeleted = array();
157 $newRows = array();
158
159 foreach ($rows as $key => &$row) {
160 if (!isset($isDeleted[$row['log_civicrm_entity_altered_contact_id']])) {
161 $isDeleted[$row['log_civicrm_entity_altered_contact_id']] =
162 CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $row['log_civicrm_entity_altered_contact_id'], 'is_deleted') !== '0';
163 }
164
165 if (CRM_Utils_Array::value('log_civicrm_entity_altered_contact', $row) &&
166 !$isDeleted[$row['log_civicrm_entity_altered_contact_id']]) {
167 $row['log_civicrm_entity_altered_contact_link'] =
168 CRM_Utils_System::url('civicrm/contact/view', 'reset=1&cid=' . $row['log_civicrm_entity_altered_contact_id']);
169 $row['log_civicrm_entity_altered_contact_hover'] = ts("Go to contact summary");
170 $entity = $this->getEntityValue($row['log_civicrm_entity_id'], $row['log_civicrm_entity_log_type'], $row['log_civicrm_entity_log_date']);
171 if ($entity)
172 $row['log_civicrm_entity_altered_contact'] = $row['log_civicrm_entity_altered_contact'] . " [{$entity}]";
173 }
174 $row['altered_by_contact_display_name_link'] = CRM_Utils_System::url('civicrm/contact/view', 'reset=1&cid=' . $row['log_civicrm_entity_log_user_id']);
175 $row['altered_by_contact_display_name_hover'] = ts("Go to contact summary");
176
177 if ($row['log_civicrm_entity_is_deleted'] and 'Update' == CRM_Utils_Array::value('log_civicrm_entity_log_action', $row)) {
178 $row['log_civicrm_entity_log_action'] = ts('Delete (to trash)');
179 }
180
181 if ('Contact' == CRM_Utils_Array::value('log_type', $this->_logTables[$row['log_civicrm_entity_log_type']]) &&
182 CRM_Utils_Array::value('log_civicrm_entity_log_action', $row) == 'Insert' ) {
183 $row['log_civicrm_entity_log_action'] = ts('Update');
184 }
185
186 if ($newAction = $this->getEntityAction($row['log_civicrm_entity_id'],
187 $row['log_civicrm_entity_log_conn_id'],
188 $row['log_civicrm_entity_log_type'],
189 CRM_Utils_Array::value('log_civicrm_entity_log_action', $row)))
190 $row['log_civicrm_entity_log_action'] = $newAction;
191
192 $row['log_civicrm_entity_log_type'] = $this->getLogType($row['log_civicrm_entity_log_type']);
193
194 $date = CRM_Utils_Date::isoToMysql($row['log_civicrm_entity_log_date']);
195
196 if ('Update' == CRM_Utils_Array::value('log_civicrm_entity_log_action', $row)) {
197 $q = "reset=1&log_conn_id={$row['log_civicrm_entity_log_conn_id']}&log_date=". $date;
198 if ($this->cid) {
199 $q .= '&cid=' . $this->cid;
200 }
201
202 $url1 = CRM_Report_Utils_Report::getNextUrl('logging/contact/detail', "{$q}&snippet=4&section=2&layout=overlay", FALSE, TRUE);
203 $url2 = CRM_Report_Utils_Report::getNextUrl('logging/contact/detail', "{$q}&section=2", FALSE, TRUE);
204 $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>';
205 }
206
207 $key = $date . '_' .
208 $row['log_civicrm_entity_log_type'] . '_' .
209 $row['log_civicrm_entity_log_conn_id'] . '_' .
210 $row['log_civicrm_entity_log_user_id'] . '_' .
211 $row['log_civicrm_entity_id'];
212 $newRows[$key] = $row;
213
214 unset($row['log_civicrm_entity_log_user_id']);
215 unset($row['log_civicrm_entity_log_conn_id']);
216 }
217
218 krsort($newRows);
219 $rows = $newRows;
220 }
221
222 function from( $logTable = null ) {
223 static $entity = null;
224 if ( $logTable ) {
225 $entity = $logTable;
226 }
227
228 $detail = $this->_logTables[$entity];
229 $tableName = CRM_Utils_Array::value('table_name', $detail, $entity);
230 $clause = CRM_Utils_Array::value('entity_table', $detail);
231 $clause = $clause ? "AND entity_log_civireport.entity_table = 'civicrm_contact'" : null;
232
233 $joinClause = "
234 INNER JOIN civicrm_contact modified_contact_civireport
235 ON (entity_log_civireport.{$detail['fk']} = modified_contact_civireport.id {$clause})";
236
237 if (CRM_Utils_Array::value('joins', $detail)) {
238 $clause = CRM_Utils_Array::value('entity_table', $detail);
239 $clause = $clause ? "AND fk_table.entity_table = 'civicrm_contact'" : null;
240 $joinClause = "
241 INNER JOIN `{$this->loggingDB}`.{$detail['joins']['table']} fk_table ON {$detail['joins']['join']}
242 INNER JOIN civicrm_contact modified_contact_civireport
243 ON (fk_table.{$detail['fk']} = modified_contact_civireport.id {$clause})";
244 }
245
246 $this->_from = "
247 FROM `{$this->loggingDB}`.$tableName entity_log_civireport
248 {$joinClause}
249 LEFT JOIN civicrm_contact altered_by_contact_civireport
250 ON (entity_log_civireport.log_user_id = altered_by_contact_civireport.id)";
251 }
252 }