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