Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-02-03-14-48-25
[civicrm-core.git] / CRM / Logging / ReportSummary.php
index 203f336b2e0ba177edc2d52e3b6c494d72c17dc0..f72ee2ef1fea75473cc2a6ba0e0919e056c77b55 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
@@ -40,6 +40,15 @@ class CRM_Logging_ReportSummary extends CRM_Report_Form {
   protected $loggingDB;
 
   function __construct() {
+    // don’t display the ‘Add these Contacts to Group’ button
+    $this->_add2groupSupported = FALSE;
+
+    $dsn = defined('CIVICRM_LOGGING_DSN') ? DB::parseDSN(CIVICRM_LOGGING_DSN) : DB::parseDSN(CIVICRM_DSN);
+    $this->loggingDB = $dsn['database'];
+
+    // used for redirect back to contact summary
+    $this->cid = CRM_Utils_Request::retrieve('cid', 'Integer', CRM_Core_DAO::$_nullObject);
+
     $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
     $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
     $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
@@ -161,21 +170,31 @@ class CRM_Logging_ReportSummary extends CRM_Report_Form {
         ),
       );
 
-    // don’t display the ‘Add these Contacts to Group’ button
-    $this->_add2groupSupported = FALSE;
-
-    $dsn = defined('CIVICRM_LOGGING_DSN') ? DB::parseDSN(CIVICRM_LOGGING_DSN) : DB::parseDSN(CIVICRM_DSN);
-    $this->loggingDB = $dsn['database'];
-
-    // used for redirect back to contact summary
-    $this->cid = CRM_Utils_Request::retrieve('cid', 'Integer', CRM_Core_DAO::$_nullObject);
-
     $logging = new CRM_Logging_Schema;
-    $customTables = $logging->customDataLogTables();
+
+    // build _logTables for contact custom tables
+    $customTables = $logging->entityCustomDataLogTables('Contact');
     foreach ($customTables as $table) {
       $this->_logTables[$table] = array('fk' => 'entity_id', 'log_type' => 'Contact');
     }
 
+    // build _logTables for address custom tables
+    $customTables = $logging->entityCustomDataLogTables('Address');
+    foreach ($customTables as $table) {
+      $this->_logTables[$table] = 
+        array(
+          'fk' => 'contact_id',// for join of fk_table with contact table
+          'joins' => array(
+            'table' => 'log_civicrm_address', // fk_table
+            'join'  => 'entity_log_civireport.entity_id = fk_table.id'
+          ),
+          'log_type' => 'Contact'
+        );
+    }
+
+    // allow log tables to be extended via report hooks
+    CRM_Report_BAO_Hook::singleton()->alterLogTables($this, $this->_logTables);
+
     parent::__construct();
   }
 
@@ -211,15 +230,15 @@ class CRM_Logging_ReportSummary extends CRM_Report_Form {
     $rows = array();
 
     $tempColumns = "id int(10)";
-    if (CRM_Utils_Array::value('log_action', $this->_params['fields'])) {
+    if (!empty($this->_params['fields']['log_action'])) {
       $tempColumns .= ", log_action varchar(64)";
     }
     $tempColumns .= ", log_type varchar(64), log_user_id int(10), log_date timestamp";
-    if (CRM_Utils_Array::value('altered_contact', $this->_params['fields'])) {
+    if (!empty($this->_params['fields']['altered_contact'])) {
       $tempColumns .= ", altered_contact varchar(128)";
     }
     $tempColumns .= ", altered_contact_id int(10), log_conn_id int(11), is_deleted tinyint(4)";
-    if (CRM_Utils_Array::value('display_name', $this->_params['fields'])) {
+    if (!empty($this->_params['fields']['display_name'])) {
       $tempColumns .= ", display_name varchar(128)";
     }
 
@@ -317,7 +336,7 @@ ORDER BY log_civicrm_entity_log_date DESC {$this->_limit}";
   }
 
   function getLogType($entity) {
-    if (CRM_Utils_Array::value('log_type', $this->_logTables[$entity])) {
+    if (!empty($this->_logTables[$entity]['log_type'])) {
       return $this->_logTables[$entity]['log_type'];
     }
     $logType = ucfirst(substr($entity, strrpos($entity, '_') + 1));
@@ -325,9 +344,9 @@ ORDER BY log_civicrm_entity_log_date DESC {$this->_limit}";
   }
 
   function getEntityValue($id, $entity, $logDate) {
-    if (CRM_Utils_Array::value('bracket_info', $this->_logTables[$entity])) {
-      if (CRM_Utils_Array::value('entity_column', $this->_logTables[$entity]['bracket_info'])) {
-        $logTable = CRM_Utils_Array::value('table_name', $this->_logTables[$entity]) ? $this->_logTables[$entity]['table_name'] : $entity;
+    if (!empty($this->_logTables[$entity]['bracket_info'])) {
+      if (!empty($this->_logTables[$entity]['bracket_info']['entity_column'])) {
+        $logTable = !empty($this->_logTables[$entity]['table_name']) ? $this->_logTables[$entity]['table_name'] : $entity;
         $sql = "
 SELECT {$this->_logTables[$entity]['bracket_info']['entity_column']}
   FROM `{$this->loggingDB}`.{$logTable}
@@ -370,7 +389,7 @@ WHERE  log_date <= %1 AND id = %2 ORDER BY log_date DESC LIMIT 1";
   }
 
   function getEntityAction($id, $connId, $entity, $oldAction) {
-    if (CRM_Utils_Array::value('action_column', $this->_logTables[$entity])) {
+    if (!empty($this->_logTables[$entity]['action_column'])) {
       $sql = "select {$this->_logTables[$entity]['action_column']} from `{$this->loggingDB}`.{$entity} where id = %1 AND log_conn_id = %2";
       $newAction = CRM_Core_DAO::singleValueQuery($sql, array(
         1 => array($id, 'Integer'),