INFRA-132 - Batch #8
[civicrm-core.git] / CRM / Report / Form / Contact / LoggingSummary.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 public 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_type_table' => array(
122 'name' => 'log_type',
123 'title' => ts('Log Type Table'),
124 'type' => CRM_Utils_Type::T_STRING,
125 ),
126 'log_action' => array(
127 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
128 'options' => array('Insert' => ts('Insert'), 'Update' => ts('Update'), 'Delete' => ts('Delete')),
129 'title' => ts('Action'),
130 'type' => CRM_Utils_Type::T_STRING,
131 ),
132 'id' => array(
133 'no_display' => TRUE,
134 'type' => CRM_Utils_Type::T_INT,
135 ),
136 ),
137 ),
138 'altered_by_contact' => array(
139 'dao' => 'CRM_Contact_DAO_Contact',
140 'alias' => 'altered_by_contact',
141 'fields' => array(
142 'display_name' => array(
143 'default' => TRUE,
144 'name' => 'display_name',
145 'title' => ts('Altered By'),
146 ),
147 ),
148 'filters' => array(
149 'display_name' => array(
150 'name' => 'display_name',
151 'title' => ts('Altered By'),
152 'type' => CRM_Utils_Type::T_STRING,
153 ),
154 ),
155 ),
156 );
157 }
158
159 /**
160 * @param $rows
161 */
162 public function alterDisplay(&$rows) {
163 // cache for id → is_deleted mapping
164 $isDeleted = array();
165 $newRows = array();
166
167 foreach ($rows as $key => &$row) {
168 if (!isset($isDeleted[$row['log_civicrm_entity_altered_contact_id']])) {
169 $isDeleted[$row['log_civicrm_entity_altered_contact_id']] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
170 $row['log_civicrm_entity_altered_contact_id'], 'is_deleted') !== '0';
171 }
172
173 if (!empty($row['log_civicrm_entity_altered_contact']) &&
174 !$isDeleted[$row['log_civicrm_entity_altered_contact_id']]
175 ) {
176 $row['log_civicrm_entity_altered_contact_link'] = CRM_Utils_System::url('civicrm/contact/view',
177 '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 }
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
187 if ($row['log_civicrm_entity_is_deleted'] and 'Update' == CRM_Utils_Array::value('log_civicrm_entity_log_action', $row)) {
188 $row['log_civicrm_entity_log_action'] = ts('Delete (to trash)');
189 }
190
191 if ('Contact' == CRM_Utils_Array::value('log_type', $this->_logTables[$row['log_civicrm_entity_log_type']]) &&
192 CRM_Utils_Array::value('log_civicrm_entity_log_action', $row) == 'Insert'
193 ) {
194 $row['log_civicrm_entity_log_action'] = ts('Update');
195 }
196
197 if ($newAction = $this->getEntityAction($row['log_civicrm_entity_id'],
198 $row['log_civicrm_entity_log_conn_id'],
199 $row['log_civicrm_entity_log_type'],
200 CRM_Utils_Array::value('log_civicrm_entity_log_action', $row))
201 ) {
202 $row['log_civicrm_entity_log_action'] = $newAction;
203 }
204
205 $row['log_civicrm_entity_log_type'] = $this->getLogType($row['log_civicrm_entity_log_type']);
206
207 $date = CRM_Utils_Date::isoToMysql($row['log_civicrm_entity_log_date']);
208
209 if ('Update' == CRM_Utils_Array::value('log_civicrm_entity_log_action', $row)) {
210 $q = "reset=1&log_conn_id={$row['log_civicrm_entity_log_conn_id']}&log_date=" . $date;
211 if ($this->cid) {
212 $q .= '&cid=' . $this->cid;
213 }
214 $q .= (!empty($row['log_civicrm_entity_altered_contact'])) ? '&alteredName=' . $row['log_civicrm_entity_altered_contact'] : '';
215 $q .= (!empty($row['altered_by_contact_display_name'])) ? '&alteredBy=' . $row['altered_by_contact_display_name'] : '';
216 $q .= (!empty($row['log_civicrm_entity_log_user_id'])) ? '&alteredById=' . $row['log_civicrm_entity_log_user_id'] : '';
217
218 $url1 = CRM_Report_Utils_Report::getNextUrl('logging/contact/detail', "{$q}&snippet=4&section=2&layout=overlay", FALSE, TRUE);
219 $url2 = CRM_Report_Utils_Report::getNextUrl('logging/contact/detail', "{$q}&section=2", FALSE, TRUE);
220 $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>';
221 }
222
223 $key = $date . '_' .
224 $row['log_civicrm_entity_log_type'] . '_' .
225 $row['log_civicrm_entity_log_conn_id'] . '_' .
226 $row['log_civicrm_entity_log_user_id'] . '_' .
227 $row['log_civicrm_entity_altered_contact_id'];
228 $newRows[$key] = $row;
229
230 unset($row['log_civicrm_entity_log_user_id']);
231 unset($row['log_civicrm_entity_log_conn_id']);
232 }
233
234 krsort($newRows);
235 $rows = $newRows;
236 }
237
238 /**
239 * @param null $logTable
240 */
241 public function from($logTable = NULL) {
242 static $entity = NULL;
243 if ($logTable) {
244 $entity = $logTable;
245 }
246
247 $detail = $this->_logTables[$entity];
248 $tableName = CRM_Utils_Array::value('table_name', $detail, $entity);
249 $clause = CRM_Utils_Array::value('entity_table', $detail);
250 $clause = $clause ? "AND entity_log_civireport.entity_table = 'civicrm_contact'" : NULL;
251
252 $joinClause = "
253 INNER JOIN civicrm_contact modified_contact_civireport
254 ON (entity_log_civireport.{$detail['fk']} = modified_contact_civireport.id {$clause})";
255
256 if (!empty($detail['joins'])) {
257 $clause = CRM_Utils_Array::value('entity_table', $detail);
258 $clause = $clause ? "AND fk_table.entity_table = 'civicrm_contact'" : NULL;
259 $joinClause = "
260 INNER JOIN `{$this->loggingDB}`.{$detail['joins']['table']} fk_table ON {$detail['joins']['join']}
261 INNER JOIN civicrm_contact modified_contact_civireport
262 ON (fk_table.{$detail['fk']} = modified_contact_civireport.id {$clause})";
263 }
264
265 $this->_from = "
266 FROM `{$this->loggingDB}`.$tableName entity_log_civireport
267 {$joinClause}
268 LEFT JOIN civicrm_contact altered_by_contact_civireport
269 ON (entity_log_civireport.log_user_id = altered_by_contact_civireport.id)";
270 }
271 }