Merge pull request #4955 from atif-shaikh/master-cleanup
[civicrm-core.git] / CRM / Logging / ReportDetail.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Logging_ReportDetail extends CRM_Report_Form {
36 protected $cid;
37 protected $db;
38 protected $log_conn_id;
39 protected $log_date;
40 protected $raw;
41 protected $tables = array();
42 protected $interval = '10 SECOND';
43
02e02ac5
DS
44 protected $altered_name;
45 protected $altered_by;
46 protected $altered_by_id;
47
6a488035
TO
48 // detail/summary report ids
49 protected $detail;
50 protected $summary;
51
e0ef6999 52 /**
e0ef6999 53 */
00be9182 54 public function __construct() {
6a488035
TO
55 // don’t display the ‘Add these Contacts to Group’ button
56 $this->_add2groupSupported = FALSE;
57
58 $dsn = defined('CIVICRM_LOGGING_DSN') ? DB::parseDSN(CIVICRM_LOGGING_DSN) : DB::parseDSN(CIVICRM_DSN);
59 $this->db = $dsn['database'];
60
61 $this->log_conn_id = CRM_Utils_Request::retrieve('log_conn_id', 'Integer', CRM_Core_DAO::$_nullObject);
353ffa53
TO
62 $this->log_date = CRM_Utils_Request::retrieve('log_date', 'String', CRM_Core_DAO::$_nullObject);
63 $this->cid = CRM_Utils_Request::retrieve('cid', 'Integer', CRM_Core_DAO::$_nullObject);
64 $this->raw = CRM_Utils_Request::retrieve('raw', 'Boolean', CRM_Core_DAO::$_nullObject);
6a488035 65
353ffa53
TO
66 $this->altered_name = CRM_Utils_Request::retrieve('alteredName', 'String', CRM_Core_DAO::$_nullObject);
67 $this->altered_by = CRM_Utils_Request::retrieve('alteredBy', 'String', CRM_Core_DAO::$_nullObject);
02e02ac5 68 $this->altered_by_id = CRM_Utils_Request::retrieve('alteredById', 'Integer', CRM_Core_DAO::$_nullObject);
e0ef6999 69
6a488035
TO
70 parent::__construct();
71
72 CRM_Utils_System::resetBreadCrumb();
03e04002 73 $breadcrumb =
6a488035 74 array(
353ffa53
TO
75 array(
76 'title' => ts('Home'),
77 'url' => CRM_Utils_System::url()
78 ),
79 array(
80 'title' => ts('CiviCRM'),
81 'url' => CRM_Utils_System::url('civicrm', 'reset=1')
82 ),
83 array(
84 'title' => ts('View Contact'),
85 'url' => CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$this->cid}")
86 ),
87 array(
88 'title' => ts('Search Results'),
89 'url' => CRM_Utils_System::url('civicrm/contact/search', "force=1")
90 ),
91 );
6a488035
TO
92 CRM_Utils_System::appendBreadCrumb($breadcrumb);
93
94 if (CRM_Utils_Request::retrieve('revert', 'Boolean', CRM_Core_DAO::$_nullObject)) {
95 $reverter = new CRM_Logging_Reverter($this->log_conn_id, $this->log_date);
96 $reverter->revert($this->tables);
97 CRM_Core_Session::setStatus(ts('The changes have been reverted.'), ts('Reverted'), 'success');
98 if ($this->cid) {
99 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/view', "reset=1&selectedChild=log&cid={$this->cid}", FALSE, NULL, FALSE));
100 }
101 else {
102 CRM_Utils_System::redirect(CRM_Report_Utils_Report::getNextUrl($this->summary, 'reset=1', FALSE, TRUE));
103 }
104 }
105
106 // make sure the report works even without the params
107 if (!$this->log_conn_id or !$this->log_date) {
108 $dao = new CRM_Core_DAO;
109 $dao->query("SELECT log_conn_id, log_date FROM `{$this->db}`.log_{$this->tables[0]} WHERE log_action = 'Update' ORDER BY log_date DESC LIMIT 1");
110 $dao->fetch();
111 $this->log_conn_id = $dao->log_conn_id;
112 $this->log_date = $dao->log_date;
113 }
114
115 $this->_columnHeaders = array(
116 'field' => array('title' => ts('Field')),
117 'from' => array('title' => ts('Changed From')),
118 'to' => array('title' => ts('Changed To')),
119 );
120 }
121
e0ef6999
EM
122 /**
123 * @param bool $applyLimit
124 */
6ea503d4
TO
125 public function buildQuery($applyLimit = TRUE) {
126 }
6a488035 127
e0ef6999
EM
128 /**
129 * @param $sql
130 * @param $rows
131 */
00be9182 132 public function buildRows($sql, &$rows) {
6a488035
TO
133 // safeguard for when there aren’t any log entries yet
134 if (!$this->log_conn_id or !$this->log_date) {
135 return;
136 }
137
138 if (empty($rows)) {
139
140 $rows = array();
141
142 }
143
144 foreach ($this->tables as $table) {
145 $rows = array_merge($rows, $this->diffsInTable($table));
146 }
147 }
148
e0ef6999
EM
149 /**
150 * @param $table
151 *
152 * @return array
153 */
6a488035
TO
154 protected function diffsInTable($table) {
155 $rows = array();
156
157 $differ = new CRM_Logging_Differ($this->log_conn_id, $this->log_date, $this->interval);
158 $diffs = $differ->diffsInTable($table, $this->cid);
159
160 // return early if nothing found
161 if (empty($diffs)) {
162 return $rows;
163 }
164
165 list($titles, $values) = $differ->titlesAndValuesForTable($table);
166
167 // populate $rows with only the differences between $changed and $original (skipping certain columns and NULL ↔ empty changes unless raw requested)
168 $skipped = array('contact_id', 'entity_id', 'id');
169 foreach ($diffs as $diff) {
170 $field = $diff['field'];
353ffa53
TO
171 $from = $diff['from'];
172 $to = $diff['to'];
6a488035
TO
173
174 if ($this->raw) {
175 $field = "$table.$field";
176 }
177 else {
178 if (in_array($field, $skipped)) {
179 continue;
180 }
181 // $differ filters out === values; for presentation hide changes like 42 → '42'
182 if ($from == $to) {
183 continue;
184 }
185
40554aa3 186 // special-case for multiple values. Also works for CRM-7251: preferred_communication_method
e0ef6999
EM
187 if ((substr($from, 0, 1) == CRM_Core_DAO::VALUE_SEPARATOR &&
188 substr($from, -1, 1) == CRM_Core_DAO::VALUE_SEPARATOR) ||
189 (substr($to, 0, 1) == CRM_Core_DAO::VALUE_SEPARATOR &&
353ffa53
TO
190 substr($to, -1, 1) == CRM_Core_DAO::VALUE_SEPARATOR)
191 ) {
40554aa3
DS
192 $froms = $tos = array();
193 foreach (explode(CRM_Core_DAO::VALUE_SEPARATOR, trim($from, CRM_Core_DAO::VALUE_SEPARATOR)) as $val) {
194 $froms[] = CRM_Utils_Array::value($val, $values[$field]);
195 }
196 foreach (explode(CRM_Core_DAO::VALUE_SEPARATOR, trim($to, CRM_Core_DAO::VALUE_SEPARATOR)) as $val) {
197 $tos[] = CRM_Utils_Array::value($val, $values[$field]);
198 }
6a488035 199 $from = implode(', ', array_filter($froms));
353ffa53 200 $to = implode(', ', array_filter($tos));
6a488035
TO
201 }
202
203 if (isset($values[$field][$from])) {
6a488035 204 $from = $values[$field][$from];
6a488035
TO
205 }
206 if (isset($values[$field][$to])) {
207 $to = $values[$field][$to];
208 }
209 if (isset($titles[$field])) {
210 $field = $titles[$field];
211 }
212 if ($diff['action'] == 'Insert') {
213 $from = '';
214 }
215 if ($diff['action'] == 'Delete') {
216 $to = '';
217 }
218 }
219
220 $rows[] = array('field' => $field . " (id: {$diff['id']})", 'from' => $from, 'to' => $to);
221 }
222
223 return $rows;
224 }
225
00be9182 226 public function buildQuickForm() {
6a488035
TO
227 parent::buildQuickForm();
228
229 $params = array(
230 1 => array($this->log_conn_id, 'Integer'),
231 2 => array($this->log_date, 'String'),
232 );
233
02e02ac5 234 $this->assign('whom_url', CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$this->cid}"));
353ffa53 235 $this->assign('who_url', CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$this->altered_by_id}"));
02e02ac5 236 $this->assign('whom_name', $this->altered_name);
353ffa53 237 $this->assign('who_name', $this->altered_by);
02e02ac5 238
6a488035
TO
239 $this->assign('log_date', CRM_Utils_Date::mysqlToIso($this->log_date));
240
241 $q = "reset=1&log_conn_id={$this->log_conn_id}&log_date={$this->log_date}";
242 $this->assign('revertURL', CRM_Report_Utils_Report::getNextUrl($this->detail, "$q&revert=1", FALSE, TRUE));
243 $this->assign('revertConfirm', ts('Are you sure you want to revert all these changes?'));
244 }
6a488035 245}