Merge pull request #16591 from ixiam/dev/core#1575
[civicrm-core.git] / CRM / Logging / ReportDetail.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Logging_ReportDetail extends CRM_Report_Form {
18 protected $cid;
19
20 /**
21 * Other contact ID.
22 *
23 * This would be set if we are viewing a merge of 2 contacts.
24 *
25 * @var int
26 */
27 protected $oid;
28 protected $db;
29 protected $log_conn_id;
30 protected $log_date;
31 protected $raw;
32 protected $tables = [];
33 protected $interval = '10 SECOND';
34
35 protected $altered_name;
36 protected $altered_by;
37 protected $altered_by_id;
38
39 /**
40 * detail/summary report ids
41 * @var int
42 */
43 protected $detail;
44 protected $summary;
45
46 /**
47 * Instance of Differ.
48 *
49 * @var CRM_Logging_Differ
50 */
51 protected $differ;
52
53 /**
54 * Array of changes made.
55 *
56 * @var array
57 */
58 protected $diffs = [];
59
60 /**
61 * Don't display the Add these contacts to Group button.
62 *
63 * @var bool
64 */
65 protected $_add2groupSupported = FALSE;
66
67 /**
68 * Class constructor.
69 */
70 public function __construct() {
71
72 $this->storeDB();
73
74 $this->parsePropertiesFromUrl();
75
76 parent::__construct();
77
78 CRM_Utils_System::resetBreadCrumb();
79 $breadcrumb = [
80 [
81 'title' => ts('Home'),
82 'url' => CRM_Utils_System::url(),
83 ],
84 [
85 'title' => ts('CiviCRM'),
86 'url' => CRM_Utils_System::url('civicrm', 'reset=1'),
87 ],
88 [
89 'title' => ts('View Contact'),
90 'url' => CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$this->cid}"),
91 ],
92 [
93 'title' => ts('Search Results'),
94 'url' => CRM_Utils_System::url('civicrm/contact/search', "force=1"),
95 ],
96 ];
97 CRM_Utils_System::appendBreadCrumb($breadcrumb);
98
99 if (CRM_Utils_Request::retrieve('revert', 'Boolean')) {
100 $this->revert();
101 }
102
103 $this->_columnHeaders = [
104 'field' => ['title' => ts('Field')],
105 'from' => ['title' => ts('Changed From')],
106 'to' => ['title' => ts('Changed To')],
107 ];
108 }
109
110 /**
111 * Build query for report.
112 *
113 * We override this to be empty & calculate the rows in the buildRows function.
114 *
115 * @param bool $applyLimit
116 */
117 public function buildQuery($applyLimit = TRUE) {
118 }
119
120 /**
121 * Build rows from query.
122 *
123 * @param string $sql
124 * @param array $rows
125 */
126 public function buildRows($sql, &$rows) {
127 // safeguard for when there aren’t any log entries yet
128 if (!$this->log_conn_id && !$this->log_date) {
129 return;
130 }
131 $this->getDiffs();
132 $rows = $this->convertDiffsToRows();
133 }
134
135 /**
136 * Get the diffs for the report, calculating them if not already done.
137 *
138 * Note that contact details report now uses a more comprehensive method but
139 * the contribution logging details report still uses this.
140 *
141 * @return array
142 */
143 protected function getDiffs() {
144 if (empty($this->diffs)) {
145 foreach ($this->tables as $table) {
146 $this->diffs = array_merge($this->diffs, $this->diffsInTable($table));
147 }
148 }
149 return $this->diffs;
150 }
151
152 /**
153 * @param $table
154 *
155 * @return array
156 */
157 protected function diffsInTable($table) {
158 $this->setDiffer();
159 return $this->differ->diffsInTable($table, $this->cid);
160 }
161
162 /**
163 * Convert the diffs to row format.
164 *
165 * @return array
166 */
167 protected function convertDiffsToRows() {
168 // return early if nothing found
169 if (empty($this->diffs)) {
170 return [];
171 }
172
173 // populate $rows with only the differences between $changed and $original (skipping certain columns and NULL ↔ empty changes unless raw requested)
174 $skipped = ['id'];
175 foreach ($this->diffs as $diff) {
176 $table = $diff['table'];
177 if (empty($metadata[$table])) {
178 list($metadata[$table]['titles'], $metadata[$table]['values']) = $this->differ->titlesAndValuesForTable($table, $diff['log_date']);
179 }
180 $values = CRM_Utils_Array::value('values', $metadata[$diff['table']], []);
181 $titles = $metadata[$diff['table']]['titles'];
182 $field = $diff['field'];
183 $from = $diff['from'];
184 $to = $diff['to'];
185
186 if ($this->raw) {
187 $field = "$table.$field";
188 }
189 else {
190 if (in_array($field, $skipped)) {
191 continue;
192 }
193 // $differ filters out === values; for presentation hide changes like 42 → '42'
194 if ($from == $to) {
195 continue;
196 }
197
198 // special-case for multiple values. Also works for CRM-7251: preferred_communication_method
199 if ((substr($from, 0, 1) == CRM_Core_DAO::VALUE_SEPARATOR &&
200 substr($from, -1, 1) == CRM_Core_DAO::VALUE_SEPARATOR) ||
201 (substr($to, 0, 1) == CRM_Core_DAO::VALUE_SEPARATOR &&
202 substr($to, -1, 1) == CRM_Core_DAO::VALUE_SEPARATOR)
203 ) {
204 $froms = $tos = [];
205 foreach (explode(CRM_Core_DAO::VALUE_SEPARATOR, trim($from, CRM_Core_DAO::VALUE_SEPARATOR)) as $val) {
206 $froms[] = $values[$field][$val] ?? NULL;
207 }
208 foreach (explode(CRM_Core_DAO::VALUE_SEPARATOR, trim($to, CRM_Core_DAO::VALUE_SEPARATOR)) as $val) {
209 $tos[] = $values[$field][$val] ?? NULL;
210 }
211 $from = implode(', ', array_filter($froms));
212 $to = implode(', ', array_filter($tos));
213 }
214
215 if (isset($values[$field][$from])) {
216 $from = $values[$field][$from];
217 }
218 if (isset($values[$field][$to])) {
219 $to = $values[$field][$to];
220 }
221 if (isset($titles[$field])) {
222 $field = $titles[$field];
223 }
224 if ($diff['action'] == 'Insert') {
225 $from = '';
226 }
227 if ($diff['action'] == 'Delete') {
228 $to = '';
229 }
230 }
231
232 $rows[] = ['field' => $field . " (id: {$diff['id']})", 'from' => $from, 'to' => $to];
233 }
234
235 return $rows;
236 }
237
238 public function buildQuickForm() {
239 parent::buildQuickForm();
240
241 $this->assign('whom_url', CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$this->cid}"));
242 $this->assign('who_url', CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$this->altered_by_id}"));
243 $this->assign('whom_name', $this->altered_name);
244 $this->assign('who_name', $this->altered_by);
245
246 $this->assign('log_date', CRM_Utils_Date::mysqlToIso($this->log_date));
247
248 $q = "reset=1&log_conn_id={$this->log_conn_id}&log_date={$this->log_date}";
249 if ($this->oid) {
250 $q .= '&oid=' . $this->oid;
251 }
252 $this->assign('revertURL', CRM_Report_Utils_Report::getNextUrl($this->detail, "$q&revert=1", FALSE, TRUE));
253 $this->assign('revertConfirm', ts('Are you sure you want to revert all changes?'));
254 }
255
256 /**
257 * Store the dsn for the logging database in $this->db.
258 */
259 protected function storeDB() {
260 $dsn = defined('CIVICRM_LOGGING_DSN') ? DB::parseDSN(CIVICRM_LOGGING_DSN) : DB::parseDSN(CIVICRM_DSN);
261 $this->db = $dsn['database'];
262 }
263
264 /**
265 * Calculate all the contact related diffs for the change.
266 */
267 protected function calculateContactDiffs() {
268 $this->diffs = $this->getAllContactChangesForConnection();
269 }
270
271 /**
272 * Get an array of changes made in the mysql connection.
273 *
274 * @return mixed
275 */
276 public function getAllContactChangesForConnection() {
277 if (empty($this->log_conn_id)) {
278 return [];
279 }
280 $this->setDiffer();
281 try {
282 return $this->differ->getAllChangesForConnection($this->tables);
283 }
284 catch (CRM_Core_Exception $e) {
285 CRM_Core_Error::statusBounce(ts($e->getMessage()));
286 }
287 }
288
289 /**
290 * Make sure the differ is defined.
291 */
292 protected function setDiffer() {
293 if (empty($this->differ)) {
294 $this->differ = new CRM_Logging_Differ($this->log_conn_id, $this->log_date, $this->interval);
295 }
296 }
297
298 /**
299 * Set this tables to reflect tables changed in a merge.
300 */
301 protected function setTablesToContactRelatedTables() {
302 $schema = new CRM_Logging_Schema();
303 $this->tables = $schema->getLogTablesForContact();
304 // allow tables to be extended by report hook query objects.
305 // This is a report specific hook. It's unclear how it interacts to / overlaps the main one.
306 // It probably precedes the main one and was never reconciled with it....
307 CRM_Report_BAO_Hook::singleton()->alterLogTables($this, $this->tables);
308 }
309
310 /**
311 * Revert the changes defined by the parameters.
312 */
313 protected function revert() {
314 $reverter = new CRM_Logging_Reverter($this->log_conn_id, $this->log_date);
315 $reverter->calculateDiffsFromLogConnAndDate($this->tables);
316 $reverter->revert();
317 CRM_Core_Session::setStatus(ts('The changes have been reverted.'), ts('Reverted'), 'success');
318 if ($this->cid) {
319 if ($this->oid) {
320 CRM_Utils_System::redirect(CRM_Utils_System::url(
321 'civicrm/contact/merge',
322 "reset=1&cid={$this->cid}&oid={$this->oid}",
323 FALSE,
324 NULL,
325 FALSE)
326 );
327 }
328 else {
329 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/view', "reset=1&selectedChild=log&cid={$this->cid}", FALSE, NULL, FALSE));
330 }
331 }
332 else {
333 CRM_Utils_System::redirect(CRM_Report_Utils_Report::getNextUrl($this->summary, 'reset=1', FALSE, TRUE));
334 }
335 }
336
337 /**
338 * Get the properties that might be in the URL.
339 */
340 protected function parsePropertiesFromUrl() {
341 $this->log_conn_id = CRM_Utils_Request::retrieve('log_conn_id', 'String');
342 $this->log_date = CRM_Utils_Request::retrieve('log_date', 'String');
343 $this->cid = CRM_Utils_Request::retrieve('cid', 'Integer');
344 $this->raw = CRM_Utils_Request::retrieve('raw', 'Boolean');
345
346 $this->altered_name = CRM_Utils_Request::retrieve('alteredName', 'String');
347 $this->altered_by = CRM_Utils_Request::retrieve('alteredBy', 'String');
348 $this->altered_by_id = CRM_Utils_Request::retrieve('alteredById', 'Integer');
349 }
350
351 }