CRM-18010: Ensure type of each report filters
[civicrm-core.git] / CRM / Report / Form / Contact / LoggingSummary.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 */
33 class CRM_Report_Form_Contact_LoggingSummary extends CRM_Logging_ReportSummary {
34 /**
35 * Class constructor.
36 */
37 public function __construct() {
38 parent::__construct();
39
40 $logTypes = array();
41 foreach (array_keys($this->_logTables) as $table) {
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,
84 'alias' => 'modified_contact_civireport',
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 ),
120 'log_type_table' => array(
121 'name' => 'log_type',
122 'title' => ts('Log Type Table'),
123 'type' => CRM_Utils_Type::T_STRING,
124 ),
125 'log_action' => array(
126 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
127 'options' => array(
128 'Insert' => ts('Insert'),
129 'Update' => ts('Update'),
130 'Delete' => ts('Delete'),
131 ),
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(
142 'dao' => 'CRM_Contact_DAO_Contact',
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
162 /**
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.
167 *
168 * @param array $rows
169 * Rows generated by SQL, with an array for each row.
170 */
171 public function alterDisplay(&$rows) {
172 // cache for id → is_deleted mapping
173 $isDeleted = array();
174 $newRows = array();
175
176 foreach ($rows as $key => &$row) {
177 if (!isset($isDeleted[$row['log_civicrm_entity_altered_contact_id']])) {
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';
180 }
181
182 if (!empty($row['log_civicrm_entity_altered_contact']) &&
183 !$isDeleted[$row['log_civicrm_entity_altered_contact_id']]
184 ) {
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']);
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']);
189 if ($entity) {
190 $row['log_civicrm_entity_altered_contact'] = $row['log_civicrm_entity_altered_contact'] . " [{$entity}]";
191 }
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
196 if ($row['log_civicrm_entity_is_deleted'] and 'Update' == CRM_Utils_Array::value('log_civicrm_entity_log_action', $row)) {
197 $row['log_civicrm_entity_log_action'] = ts('Delete (to trash)');
198 }
199
200 if ('Contact' == CRM_Utils_Array::value('log_type', $this->_logTables[$row['log_civicrm_entity_log_type']]) &&
201 CRM_Utils_Array::value('log_civicrm_entity_log_action', $row) == 'Insert'
202 ) {
203 $row['log_civicrm_entity_log_action'] = ts('Update');
204 }
205
206 if ($newAction = $this->getEntityAction($row['log_civicrm_entity_id'],
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 ) {
211 $row['log_civicrm_entity_log_action'] = $newAction;
212 }
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
218 if ('Update' == CRM_Utils_Array::value('log_civicrm_entity_log_action', $row)) {
219 $q = "reset=1&log_conn_id={$row['log_civicrm_entity_log_conn_id']}&log_date=" . $date;
220 if ($this->cid) {
221 $q .= '&cid=' . $this->cid;
222 }
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'] : '';
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);
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>';
231 }
232
233 $key = $date . '_' .
234 $row['log_civicrm_entity_log_type'] . '_' .
235 $row['log_civicrm_entity_log_conn_id'] . '_' .
236 $row['log_civicrm_entity_log_user_id'] . '_' .
237 $row['log_civicrm_entity_altered_contact_id'];
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
248 /**
249 * Generate From Clause.
250 *
251 * @param string $logTable
252 */
253 public function from($logTable = NULL) {
254 static $entity = NULL;
255 if ($logTable) {
256 $entity = $logTable;
257 }
258
259 $detail = $this->_logTables[$entity];
260 $tableName = CRM_Utils_Array::value('table_name', $detail, $entity);
261 $clause = CRM_Utils_Array::value('entity_table', $detail);
262 $clause = $clause ? "AND entity_log_civireport.entity_table = 'civicrm_contact'" : NULL;
263
264 $joinClause = "
265 INNER JOIN civicrm_contact modified_contact_civireport
266 ON (entity_log_civireport.{$detail['fk']} = modified_contact_civireport.id {$clause})";
267
268 if (!empty($detail['joins'])) {
269 $clause = CRM_Utils_Array::value('entity_table', $detail);
270 $clause = $clause ? "AND fk_table.entity_table = 'civicrm_contact'" : NULL;
271 $joinClause = "
272 INNER JOIN `{$this->loggingDB}`.{$detail['joins']['table']} fk_table ON {$detail['joins']['join']}
273 INNER JOIN civicrm_contact modified_contact_civireport
274 ON (fk_table.{$detail['fk']} = modified_contact_civireport.id {$clause})";
275 }
276
277 $this->_from = "
278 FROM `{$this->loggingDB}`.$tableName entity_log_civireport
279 {$joinClause}
280 LEFT JOIN civicrm_contact altered_by_contact_civireport
281 ON (entity_log_civireport.log_user_id = altered_by_contact_civireport.id)";
282 }
283
284 }