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