3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2016
33 class CRM_Logging_ReportDetail
extends CRM_Report_Form
{
39 * This would be set if we are viewing a merge of 2 contacts.
45 protected $log_conn_id;
48 protected $tables = array();
49 protected $interval = '10 SECOND';
51 protected $altered_name;
52 protected $altered_by;
53 protected $altered_by_id;
55 // detail/summary report ids
62 * @var CRM_Logging_Differ
67 * Array of changes made.
71 protected $diffs = array();
74 * Don't display the Add these contacts to Group button.
78 protected $_add2groupSupported = FALSE;
83 public function __construct() {
87 $this->parsePropertiesFromUrl();
89 parent
::__construct();
91 CRM_Utils_System
::resetBreadCrumb();
94 'title' => ts('Home'),
95 'url' => CRM_Utils_System
::url(),
98 'title' => ts('CiviCRM'),
99 'url' => CRM_Utils_System
::url('civicrm', 'reset=1'),
102 'title' => ts('View Contact'),
103 'url' => CRM_Utils_System
::url('civicrm/contact/view', "reset=1&cid={$this->cid}"),
106 'title' => ts('Search Results'),
107 'url' => CRM_Utils_System
::url('civicrm/contact/search', "force=1"),
110 CRM_Utils_System
::appendBreadCrumb($breadcrumb);
112 if (CRM_Utils_Request
::retrieve('revert', 'Boolean')) {
116 $this->_columnHeaders
= array(
117 'field' => array('title' => ts('Field')),
118 'from' => array('title' => ts('Changed From')),
119 'to' => array('title' => ts('Changed To')),
124 * Build query for report.
126 * We override this to be empty & calculate the rows in the buildRows function.
128 * @param bool $applyLimit
130 public function buildQuery($applyLimit = TRUE) {
134 * Build rows from query.
139 public function buildRows($sql, &$rows) {
140 // safeguard for when there aren’t any log entries yet
141 if (!$this->log_conn_id
&& !$this->log_date
) {
145 $rows = $this->convertDiffsToRows();
149 * Get the diffs for the report, calculating them if not already done.
151 * Note that contact details report now uses a more comprehensive method but
152 * the contribution logging details report still uses this.
156 protected function getDiffs() {
157 if (empty($this->diffs
)) {
158 foreach ($this->tables
as $table) {
159 $this->diffs
= array_merge($this->diffs
, $this->diffsInTable($table));
170 protected function diffsInTable($table) {
172 return $this->differ
->diffsInTable($table, $this->cid
);
176 * Convert the diffs to row format.
180 protected function convertDiffsToRows() {
181 // return early if nothing found
182 if (empty($this->diffs
)) {
186 // populate $rows with only the differences between $changed and $original (skipping certain columns and NULL ↔ empty changes unless raw requested)
187 $skipped = array('id');
188 foreach ($this->diffs
as $diff) {
189 $table = $diff['table'];
190 if (empty($metadata[$table])) {
191 list($metadata[$table]['titles'], $metadata[$table]['values']) = $this->differ
->titlesAndValuesForTable($table, $diff['log_date']);
193 $values = CRM_Utils_Array
::value('values', $metadata[$diff['table']], array());
194 $titles = $metadata[$diff['table']]['titles'];
195 $field = $diff['field'];
196 $from = $diff['from'];
200 $field = "$table.$field";
203 if (in_array($field, $skipped)) {
206 // $differ filters out === values; for presentation hide changes like 42 → '42'
211 // special-case for multiple values. Also works for CRM-7251: preferred_communication_method
212 if ((substr($from, 0, 1) == CRM_Core_DAO
::VALUE_SEPARATOR
&&
213 substr($from, -1, 1) == CRM_Core_DAO
::VALUE_SEPARATOR
) ||
214 (substr($to, 0, 1) == CRM_Core_DAO
::VALUE_SEPARATOR
&&
215 substr($to, -1, 1) == CRM_Core_DAO
::VALUE_SEPARATOR
)
217 $froms = $tos = array();
218 foreach (explode(CRM_Core_DAO
::VALUE_SEPARATOR
, trim($from, CRM_Core_DAO
::VALUE_SEPARATOR
)) as $val) {
219 $froms[] = CRM_Utils_Array
::value($val, $values[$field]);
221 foreach (explode(CRM_Core_DAO
::VALUE_SEPARATOR
, trim($to, CRM_Core_DAO
::VALUE_SEPARATOR
)) as $val) {
222 $tos[] = CRM_Utils_Array
::value($val, $values[$field]);
224 $from = implode(', ', array_filter($froms));
225 $to = implode(', ', array_filter($tos));
228 if (isset($values[$field][$from])) {
229 $from = $values[$field][$from];
231 if (isset($values[$field][$to])) {
232 $to = $values[$field][$to];
234 if (isset($titles[$field])) {
235 $field = $titles[$field];
237 if ($diff['action'] == 'Insert') {
240 if ($diff['action'] == 'Delete') {
245 $rows[] = array('field' => $field . " (id: {$diff['id']})", 'from' => $from, 'to' => $to);
251 public function buildQuickForm() {
252 parent
::buildQuickForm();
254 $this->assign('whom_url', CRM_Utils_System
::url('civicrm/contact/view', "reset=1&cid={$this->cid}"));
255 $this->assign('who_url', CRM_Utils_System
::url('civicrm/contact/view', "reset=1&cid={$this->altered_by_id}"));
256 $this->assign('whom_name', $this->altered_name
);
257 $this->assign('who_name', $this->altered_by
);
259 $this->assign('log_date', CRM_Utils_Date
::mysqlToIso($this->log_date
));
261 $q = "reset=1&log_conn_id={$this->log_conn_id}&log_date={$this->log_date}";
263 $q .= '&oid=' . $this->oid
;
265 $this->assign('revertURL', CRM_Report_Utils_Report
::getNextUrl($this->detail
, "$q&revert=1", FALSE, TRUE));
266 $this->assign('revertConfirm', ts('Are you sure you want to revert all changes?'));
270 * Store the dsn for the logging database in $this->db.
272 protected function storeDB() {
273 $dsn = defined('CIVICRM_LOGGING_DSN') ? DB
::parseDSN(CIVICRM_LOGGING_DSN
) : DB
::parseDSN(CIVICRM_DSN
);
274 $this->db
= $dsn['database'];
278 * Calculate all the contact related diffs for the change.
280 protected function calculateContactDiffs() {
281 $this->diffs
= $this->getAllContactChangesForConnection();
286 * Get an array of changes made in the mysql connection.
290 public function getAllContactChangesForConnection() {
291 if (empty($this->log_conn_id
)) {
296 return $this->differ
->getAllChangesForConnection($this->tables
);
298 catch (CRM_Core_Exception
$e) {
299 CRM_Core_Error
::statusBounce(ts($e->getMessage()));
304 * Make sure the differ is defined.
306 protected function setDiffer() {
307 if (empty($this->differ
)) {
308 $this->differ
= new CRM_Logging_Differ($this->log_conn_id
, $this->log_date
, $this->interval
);
313 * Set this tables to reflect tables changed in a merge.
315 protected function setTablesToContactRelatedTables() {
316 $schema = new CRM_Logging_Schema();
317 $this->tables
= $schema->getLogTablesForContact();
318 // allow tables to be extended by report hook query objects.
319 // This is a report specific hook. It's unclear how it interacts to / overlaps the main one.
320 // It probably precedes the main one and was never reconciled with it....
321 CRM_Report_BAO_Hook
::singleton()->alterLogTables($this, $this->tables
);
325 * Revert the changes defined by the parameters.
327 protected function revert() {
328 $reverter = new CRM_Logging_Reverter($this->log_conn_id
, $this->log_date
);
329 $reverter->calculateDiffsFromLogConnAndDate($this->tables
);
331 CRM_Core_Session
::setStatus(ts('The changes have been reverted.'), ts('Reverted'), 'success');
334 CRM_Utils_System
::redirect(CRM_Utils_System
::url(
335 'civicrm/contact/merge',
336 "reset=1&cid={$this->cid}&oid={$this->oid}",
343 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/contact/view', "reset=1&selectedChild=log&cid={$this->cid}", FALSE, NULL, FALSE));
347 CRM_Utils_System
::redirect(CRM_Report_Utils_Report
::getNextUrl($this->summary
, 'reset=1', FALSE, TRUE));
352 * Get the properties that might be in the URL.
354 protected function parsePropertiesFromUrl() {
355 $this->log_conn_id
= CRM_Utils_Request
::retrieve('log_conn_id', 'String', CRM_Core_DAO
::$_nullObject);
356 $this->log_date
= CRM_Utils_Request
::retrieve('log_date', 'String', CRM_Core_DAO
::$_nullObject);
357 $this->cid
= CRM_Utils_Request
::retrieve('cid', 'Integer', CRM_Core_DAO
::$_nullObject);
358 $this->raw
= CRM_Utils_Request
::retrieve('raw', 'Boolean', CRM_Core_DAO
::$_nullObject);
360 $this->altered_name
= CRM_Utils_Request
::retrieve('alteredName', 'String', CRM_Core_DAO
::$_nullObject);
361 $this->altered_by
= CRM_Utils_Request
::retrieve('alteredBy', 'String', CRM_Core_DAO
::$_nullObject);
362 $this->altered_by_id
= CRM_Utils_Request
::retrieve('alteredById', 'Integer', CRM_Core_DAO
::$_nullObject);