Eileen has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/282091

Change subject: CRM-18366 Fix contact logging detail to show all related 
changes & revert all related changes
......................................................................

CRM-18366 Fix contact logging detail to show all related changes & revert all 
related changes

This report switches from a hard-coded list of contact related tables to 
considering all tables with an FK to civicrm_contact, including entity_tag
group_subscription, batch, financial_item etc. The revert makes the same switch.

Note that with this change a full subset may still not be retrieved - in order 
to get that you need to enable the changes from CRM-18193 & apply all patches 
related to that issue (one will build on this)

Change-Id: I587b9140c188b23dfa8df8ebb70a43bff82cf874
---
M CRM/Logging/Differ.php
M CRM/Logging/ReportDetail.php
M CRM/Report/Form/Contact/LoggingDetail.php
3 files changed, 108 insertions(+), 35 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/crm/civicrm 
refs/changes/91/282091/1

diff --git a/CRM/Logging/Differ.php b/CRM/Logging/Differ.php
index 6dca866..5ed8dff 100644
--- a/CRM/Logging/Differ.php
+++ b/CRM/Logging/Differ.php
@@ -169,6 +169,7 @@
    * @param int $id
    *
    * @return array
+   * @throws \CRM_Core_Exception
    */
   private function diffsInTableForId($table, $id) {
     $diffs = array();
diff --git a/CRM/Logging/ReportDetail.php b/CRM/Logging/ReportDetail.php
index 974b7ca..edd24f1 100644
--- a/CRM/Logging/ReportDetail.php
+++ b/CRM/Logging/ReportDetail.php
@@ -48,6 +48,20 @@
   protected $summary;
 
   /**
+   * Instance of Differ.
+   *
+   * @var CRM_Logging_Differ
+   */
+  protected $differ;
+
+  /**
+   * Array of changes made.
+   *
+   * @var array
+   */
+  protected $diffs = array();
+
+  /**
    * Don't display the Add these contacts to Group button.
    *
    * @var bool
@@ -98,6 +112,10 @@
   }
 
   /**
+   * Build query for report.
+   *
+   * We override this to be empty & calculate the rows in the buildRows 
function.
+   *
    * @param bool $applyLimit
    */
   public function buildQuery($applyLimit = TRUE) {
@@ -112,16 +130,25 @@
     if (!$this->log_conn_id or !$this->log_date) {
       return;
     }
+    $this->getDiffs();
+    $rows = $this->convertDiffsToRows();
+  }
 
-    if (empty($rows)) {
-
-      $rows = array();
-
+  /**
+   * Get the diffs for the report, calculating them if not already done.
+   *
+   * Note that contact details report now uses a more comprehensive method but
+   * the contribution logging details report still uses this.
+   *
+   * @return array
+   */
+  protected function getDiffs() {
+    if (empty($this->diffs)) {
+      foreach ($this->tables as $table) {
+        $this->diffs = array_merge($this->diffs, $this->diffsInTable($table));
+      }
     }
-
-    foreach ($this->tables as $table) {
-      $rows = array_merge($rows, $this->diffsInTable($table));
-    }
+    return $this->diffs;
   }
 
   /**
@@ -130,21 +157,30 @@
    * @return array
    */
   protected function diffsInTable($table) {
-    $rows = array();
+    $this->setDiffer();
+    return $this->differ->diffsInTable($table, $this->cid);
+  }
 
-    $differ = new CRM_Logging_Differ($this->log_conn_id, $this->log_date, 
$this->interval);
-    $diffs = $differ->diffsInTable($table, $this->cid);
-
+  /**
+   * Convert the diffs to row format.
+   *
+   * @return array
+   */
+  protected function convertDiffsToRows() {
     // return early if nothing found
-    if (empty($diffs)) {
-      return $rows;
+    if (empty($this->diffs)) {
+      return array();
     }
 
-    list($titles, $values) = $differ->titlesAndValuesForTable($table);
-
     // populate $rows with only the differences between $changed and $original 
(skipping certain columns and NULL ↔ empty changes unless raw requested)
-    $skipped = array('contact_id', 'entity_id', 'id');
-    foreach ($diffs as $diff) {
+    $skipped = array('id');
+    foreach ($this->diffs as $diff) {
+      $table = $diff['table'];
+      if (empty($metadata[$table])) {
+        list($metadata[$table]['titles'], $metadata[$table]['values']) = 
$this->differ->titlesAndValuesForTable($table);
+      }
+      $values = CRM_Utils_Array::value('values', $metadata[$diff['table']], 
array());
+      $titles = $metadata[$diff['table']]['titles'];
       $field = $diff['field'];
       $from = $diff['from'];
       $to = $diff['to'];
@@ -239,6 +275,55 @@
   }
 
   /**
+   * Calculate all the contact related diffs for the change.
+   *
+   * @return array
+   */
+  protected function calculateContactDiffs(){
+    $this->diffs = $this->getAllContactChangesForConnection();
+  }
+
+
+  /**
+   * Get an array of changes made in the mysql connection.
+   *
+   * @return mixed
+   */
+  public function getAllContactChangesForConnection() {
+    if (empty($this->log_conn_id)) {
+      return array();
+    }
+    $this->setDiffer();
+    try {
+      return $this->differ->getAllChangesForConnection($this->tables);
+    }
+    catch (CRM_Core_Exception $e) {
+      CRM_Core_Error::statusBounce(ts($e->getMessage()));
+    }
+  }
+
+  /**
+   * Make sure the differ is defined.
+   */
+  protected function setDiffer() {
+    if (empty($this->differ)) {
+      $this->differ = new CRM_Logging_Differ($this->log_conn_id, 
$this->log_date, $this->interval);
+    }
+  }
+
+  /**
+   * Set this tables to reflect tables changed in a merge.
+   */
+  protected function setTablesToContactRelatedTables() {
+    $schema = new CRM_Logging_Schema();
+    $this->tables = $schema->getLogTablesForContact();
+    // allow tables to be extended by report hook query objects.
+    // This is a report specific hook. It's unclear how it interacts to / 
overlaps the main one.
+    // It probably precedes the main one and was never reconciled with it....
+    CRM_Report_BAO_Hook::singleton()->alterLogTables($this, $this->tables);
+  }
+
+  /**
    * Revert the changes defined by the parameters.
    */
   protected function revert() {
diff --git a/CRM/Report/Form/Contact/LoggingDetail.php 
b/CRM/Report/Form/Contact/LoggingDetail.php
index 08c4eee..3cf77c4 100644
--- a/CRM/Report/Form/Contact/LoggingDetail.php
+++ b/CRM/Report/Form/Contact/LoggingDetail.php
@@ -36,23 +36,10 @@
   /**
    */
   public function __construct() {
-    $logging = new CRM_Logging_Schema();
-    $this->tables[] = 'civicrm_contact';
-    $this->tables = array_merge($this->tables, 
array_keys($logging->customDataLogTables()));
-    $this->tables[] = 'civicrm_email';
-    $this->tables[] = 'civicrm_phone';
-    $this->tables[] = 'civicrm_im';
-    $this->tables[] = 'civicrm_openid';
-    $this->tables[] = 'civicrm_website';
-    $this->tables[] = 'civicrm_address';
-    $this->tables[] = 'civicrm_note';
-    $this->tables[] = 'civicrm_relationship';
-    $this->tables[] = 'civicrm_activity';
-    $this->tables[] = 'civicrm_case';
-
-    // allow tables to be extended by report hook query objects
-    CRM_Report_BAO_Hook::singleton()->alterLogTables($this, $this->tables);
-
+    $this->log_conn_id = CRM_Utils_Request::retrieve('log_conn_id', 'String');
+    $this->log_date = CRM_Utils_Request::retrieve('log_date', 'String');
+    $this->setTablesToContactRelatedTables();
+    $this->calculateContactDiffs();
     $this->detail = 'logging/contact/detail';
     $this->summary = 'logging/contact/summary';
 

-- 
To view, visit https://gerrit.wikimedia.org/r/282091
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I587b9140c188b23dfa8df8ebb70a43bff82cf874
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/crm/civicrm
Gerrit-Branch: master
Gerrit-Owner: Eileen <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to