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