3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
35 class CRM_Logging_ReportDetail
extends CRM_Report_Form
{
38 protected $log_conn_id;
41 protected $tables = array();
42 protected $interval = '10 SECOND';
44 protected $altered_name;
45 protected $altered_by;
46 protected $altered_by_id;
48 // detail/summary report ids
54 public function __construct() {
55 // don’t display the ‘Add these Contacts to Group’ button
56 $this->_add2groupSupported
= FALSE;
58 $dsn = defined('CIVICRM_LOGGING_DSN') ? DB
::parseDSN(CIVICRM_LOGGING_DSN
) : DB
::parseDSN(CIVICRM_DSN
);
59 $this->db
= $dsn['database'];
61 $this->log_conn_id
= CRM_Utils_Request
::retrieve('log_conn_id', 'Integer', CRM_Core_DAO
::$_nullObject);
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);
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);
68 $this->altered_by_id
= CRM_Utils_Request
::retrieve('alteredById', 'Integer', CRM_Core_DAO
::$_nullObject);
70 parent
::__construct();
72 CRM_Utils_System
::resetBreadCrumb();
75 'title' => ts('Home'),
76 'url' => CRM_Utils_System
::url(),
79 'title' => ts('CiviCRM'),
80 'url' => CRM_Utils_System
::url('civicrm', 'reset=1'),
83 'title' => ts('View Contact'),
84 'url' => CRM_Utils_System
::url('civicrm/contact/view', "reset=1&cid={$this->cid}"),
87 'title' => ts('Search Results'),
88 'url' => CRM_Utils_System
::url('civicrm/contact/search', "force=1"),
91 CRM_Utils_System
::appendBreadCrumb($breadcrumb);
93 if (CRM_Utils_Request
::retrieve('revert', 'Boolean', CRM_Core_DAO
::$_nullObject)) {
94 $reverter = new CRM_Logging_Reverter($this->log_conn_id
, $this->log_date
);
95 $reverter->revert($this->tables
);
96 CRM_Core_Session
::setStatus(ts('The changes have been reverted.'), ts('Reverted'), 'success');
98 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/contact/view', "reset=1&selectedChild=log&cid={$this->cid}", FALSE, NULL, FALSE));
101 CRM_Utils_System
::redirect(CRM_Report_Utils_Report
::getNextUrl($this->summary
, 'reset=1', FALSE, TRUE));
105 // make sure the report works even without the params
106 if (!$this->log_conn_id
or !$this->log_date
) {
107 $dao = new CRM_Core_DAO();
108 $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 $this->log_conn_id
= $dao->log_conn_id
;
111 $this->log_date
= $dao->log_date
;
114 $this->_columnHeaders
= array(
115 'field' => array('title' => ts('Field')),
116 'from' => array('title' => ts('Changed From')),
117 'to' => array('title' => ts('Changed To')),
122 * @param bool $applyLimit
124 public function buildQuery($applyLimit = TRUE) {
131 public function buildRows($sql, &$rows) {
132 // safeguard for when there aren’t any log entries yet
133 if (!$this->log_conn_id
or !$this->log_date
) {
143 foreach ($this->tables
as $table) {
144 $rows = array_merge($rows, $this->diffsInTable($table));
153 protected function diffsInTable($table) {
156 $differ = new CRM_Logging_Differ($this->log_conn_id
, $this->log_date
, $this->interval
);
157 $diffs = $differ->diffsInTable($table, $this->cid
);
159 // return early if nothing found
164 list($titles, $values) = $differ->titlesAndValuesForTable($table);
166 // populate $rows with only the differences between $changed and $original (skipping certain columns and NULL ↔ empty changes unless raw requested)
167 $skipped = array('contact_id', 'entity_id', 'id');
168 foreach ($diffs as $diff) {
169 $field = $diff['field'];
170 $from = $diff['from'];
174 $field = "$table.$field";
177 if (in_array($field, $skipped)) {
180 // $differ filters out === values; for presentation hide changes like 42 → '42'
185 // special-case for multiple values. Also works for CRM-7251: preferred_communication_method
186 if ((substr($from, 0, 1) == CRM_Core_DAO
::VALUE_SEPARATOR
&&
187 substr($from, -1, 1) == CRM_Core_DAO
::VALUE_SEPARATOR
) ||
188 (substr($to, 0, 1) == CRM_Core_DAO
::VALUE_SEPARATOR
&&
189 substr($to, -1, 1) == CRM_Core_DAO
::VALUE_SEPARATOR
)
191 $froms = $tos = array();
192 foreach (explode(CRM_Core_DAO
::VALUE_SEPARATOR
, trim($from, CRM_Core_DAO
::VALUE_SEPARATOR
)) as $val) {
193 $froms[] = CRM_Utils_Array
::value($val, $values[$field]);
195 foreach (explode(CRM_Core_DAO
::VALUE_SEPARATOR
, trim($to, CRM_Core_DAO
::VALUE_SEPARATOR
)) as $val) {
196 $tos[] = CRM_Utils_Array
::value($val, $values[$field]);
198 $from = implode(', ', array_filter($froms));
199 $to = implode(', ', array_filter($tos));
202 if (isset($values[$field][$from])) {
203 $from = $values[$field][$from];
205 if (isset($values[$field][$to])) {
206 $to = $values[$field][$to];
208 if (isset($titles[$field])) {
209 $field = $titles[$field];
211 if ($diff['action'] == 'Insert') {
214 if ($diff['action'] == 'Delete') {
219 $rows[] = array('field' => $field . " (id: {$diff['id']})", 'from' => $from, 'to' => $to);
225 public function buildQuickForm() {
226 parent
::buildQuickForm();
229 1 => array($this->log_conn_id
, 'Integer'),
230 2 => array($this->log_date
, 'String'),
233 $this->assign('whom_url', CRM_Utils_System
::url('civicrm/contact/view', "reset=1&cid={$this->cid}"));
234 $this->assign('who_url', CRM_Utils_System
::url('civicrm/contact/view', "reset=1&cid={$this->altered_by_id}"));
235 $this->assign('whom_name', $this->altered_name
);
236 $this->assign('who_name', $this->altered_by
);
238 $this->assign('log_date', CRM_Utils_Date
::mysqlToIso($this->log_date
));
240 $q = "reset=1&log_conn_id={$this->log_conn_id}&log_date={$this->log_date}";
241 $this->assign('revertURL', CRM_Report_Utils_Report
::getNextUrl($this->detail
, "$q&revert=1", FALSE, TRUE));
242 $this->assign('revertConfirm', ts('Are you sure you want to revert all changes?'));