Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-07-07-15-48-59
[civicrm-core.git] / CRM / Report / Form / Contact / LoggingSummary.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35 class CRM_Report_Form_Contact_LoggingSummary extends CRM_Logging_ReportSummary {
36 /**
37 *
38 */
39 function __construct() {
40 parent::__construct();
41
42 $logTypes = array();
43 foreach ( array_keys($this->_logTables) as $table ) {
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 ),
122 'log_type_table' => array(
123 'name' => 'log_type',
124 'title' => ts('Log Type Table'),
125 'type' => CRM_Utils_Type::T_STRING,
126 ),
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
160 /**
161 * @param $rows
162 */
163 function alterDisplay(&$rows) {
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']])) {
170 $isDeleted[$row['log_civicrm_entity_altered_contact_id']] =
171 CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $row['log_civicrm_entity_altered_contact_id'], 'is_deleted') !== '0';
172 }
173
174 if (!empty($row['log_civicrm_entity_altered_contact']) &&
175 !$isDeleted[$row['log_civicrm_entity_altered_contact_id']]) {
176 $row['log_civicrm_entity_altered_contact_link'] =
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']);
180 if ($entity)
181 $row['log_civicrm_entity_altered_contact'] = $row['log_civicrm_entity_altered_contact'] . " [{$entity}]";
182 }
183 $row['altered_by_contact_display_name_link'] = CRM_Utils_System::url('civicrm/contact/view', 'reset=1&cid=' . $row['log_civicrm_entity_log_user_id']);
184 $row['altered_by_contact_display_name_hover'] = ts("Go to contact summary");
185
186 if ($row['log_civicrm_entity_is_deleted'] and 'Update' == CRM_Utils_Array::value('log_civicrm_entity_log_action', $row)) {
187 $row['log_civicrm_entity_log_action'] = ts('Delete (to trash)');
188 }
189
190 if ('Contact' == CRM_Utils_Array::value('log_type', $this->_logTables[$row['log_civicrm_entity_log_type']]) &&
191 CRM_Utils_Array::value('log_civicrm_entity_log_action', $row) == 'Insert' ) {
192 $row['log_civicrm_entity_log_action'] = ts('Update');
193 }
194
195 if ($newAction = $this->getEntityAction($row['log_civicrm_entity_id'],
196 $row['log_civicrm_entity_log_conn_id'],
197 $row['log_civicrm_entity_log_type'],
198 CRM_Utils_Array::value('log_civicrm_entity_log_action', $row)))
199 $row['log_civicrm_entity_log_action'] = $newAction;
200
201 $row['log_civicrm_entity_log_type'] = $this->getLogType($row['log_civicrm_entity_log_type']);
202
203 $date = CRM_Utils_Date::isoToMysql($row['log_civicrm_entity_log_date']);
204
205 if ('Update' == CRM_Utils_Array::value('log_civicrm_entity_log_action', $row)) {
206 $q = "reset=1&log_conn_id={$row['log_civicrm_entity_log_conn_id']}&log_date=". $date;
207 if ($this->cid) {
208 $q .= '&cid=' . $this->cid;
209 }
210 $q .= (!empty($row['log_civicrm_entity_altered_contact'])) ?
211 '&alteredName='.$row['log_civicrm_entity_altered_contact'] : '';
212 $q .= (!empty($row['altered_by_contact_display_name'])) ?
213 '&alteredBy='.$row['altered_by_contact_display_name'] : '';
214 $q .= (!empty($row['log_civicrm_entity_log_user_id'])) ?
215 '&alteredById='.$row['log_civicrm_entity_log_user_id'] : '';
216
217 $url1 = CRM_Report_Utils_Report::getNextUrl('logging/contact/detail', "{$q}&snippet=4&section=2&layout=overlay", FALSE, TRUE);
218 $url2 = CRM_Report_Utils_Report::getNextUrl('logging/contact/detail', "{$q}&section=2", FALSE, TRUE);
219 $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>';
220 }
221
222 $key = $date . '_' .
223 $row['log_civicrm_entity_log_type'] . '_' .
224 $row['log_civicrm_entity_log_conn_id'] . '_' .
225 $row['log_civicrm_entity_log_user_id'] . '_' .
226 $row['log_civicrm_entity_altered_contact_id'];
227 $newRows[$key] = $row;
228
229 unset($row['log_civicrm_entity_log_user_id']);
230 unset($row['log_civicrm_entity_log_conn_id']);
231 }
232
233 krsort($newRows);
234 $rows = $newRows;
235 }
236
237 /**
238 * @param null $logTable
239 */
240 function from( $logTable = null ) {
241 static $entity = null;
242 if ( $logTable ) {
243 $entity = $logTable;
244 }
245
246 $detail = $this->_logTables[$entity];
247 $tableName = CRM_Utils_Array::value('table_name', $detail, $entity);
248 $clause = CRM_Utils_Array::value('entity_table', $detail);
249 $clause = $clause ? "AND entity_log_civireport.entity_table = 'civicrm_contact'" : null;
250
251 $joinClause = "
252 INNER JOIN civicrm_contact modified_contact_civireport
253 ON (entity_log_civireport.{$detail['fk']} = modified_contact_civireport.id {$clause})";
254
255 if (!empty($detail['joins'])) {
256 $clause = CRM_Utils_Array::value('entity_table', $detail);
257 $clause = $clause ? "AND fk_table.entity_table = 'civicrm_contact'" : null;
258 $joinClause = "
259 INNER JOIN `{$this->loggingDB}`.{$detail['joins']['table']} fk_table ON {$detail['joins']['join']}
260 INNER JOIN civicrm_contact modified_contact_civireport
261 ON (fk_table.{$detail['fk']} = modified_contact_civireport.id {$clause})";
262 }
263
264 $this->_from = "
265 FROM `{$this->loggingDB}`.$tableName entity_log_civireport
266 {$joinClause}
267 LEFT JOIN civicrm_contact altered_by_contact_civireport
268 ON (entity_log_civireport.log_user_id = altered_by_contact_civireport.id)";
269 }
270 }