/** * Class constructor. */ public 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); $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); $this->_logTables = array('log_civicrm_contact' => array('fk' => 'id'), 'log_civicrm_email' => array('fk' => 'contact_id', 'log_type' => 'Contact'), 'log_civicrm_phone' => array('fk' => 'contact_id', 'log_type' => 'Contact'), 'log_civicrm_address' => array('fk' => 'contact_id', 'log_type' => 'Contact'), 'log_civicrm_note' => array('fk' => 'entity_id', 'entity_table' => TRUE, 'bracket_info' => array('table' => 'log_civicrm_note', 'column' => 'subject')), 'log_civicrm_note_comment' => array('fk' => 'entity_id', 'table_name' => 'log_civicrm_note', 'joins' => array('table' => 'log_civicrm_note', 'join' => "entity_log_civireport.entity_id = fk_table.id AND entity_log_civireport.entity_table = 'civicrm_note'"), 'entity_table' => TRUE, 'bracket_info' => array('table' => 'log_civicrm_note', 'column' => 'subject')), 'log_civicrm_group_contact' => array('fk' => 'contact_id', 'bracket_info' => array('entity_column' => 'group_id', 'table' => 'log_civicrm_group', 'column' => 'title'), 'action_column' => 'status', 'log_type' => 'Group'), 'log_civicrm_entity_tag' => array('fk' => 'entity_id', 'bracket_info' => array('entity_column' => 'tag_id', 'table' => 'log_civicrm_tag', 'column' => 'name'), 'entity_table' => TRUE), 'log_civicrm_relationship' => array('fk' => 'contact_id_a', 'bracket_info' => array('entity_column' => 'relationship_type_id', 'table' => 'log_civicrm_relationship_type', 'column' => 'label_a_b')), 'log_civicrm_activity_for_target' => array('fk' => 'contact_id', 'table_name' => 'log_civicrm_activity', 'joins' => array('table' => 'log_civicrm_activity_contact', 'join' => "(entity_log_civireport.id = fk_table.activity_id AND fk_table.record_type_id = {$targetID})"), 'bracket_info' => array('entity_column' => 'activity_type_id', 'options' => CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE)), 'log_type' => 'Activity'), 'log_civicrm_activity_for_assignee' => array('fk' => 'contact_id', 'table_name' => 'log_civicrm_activity', 'joins' => array('table' => 'log_civicrm_activity_contact', 'join' => "entity_log_civireport.id = fk_table.activity_id AND fk_table.record_type_id = {$assigneeID}"), 'bracket_info' => array('entity_column' => 'activity_type_id', 'options' => CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE)), 'log_type' => 'Activity'), 'log_civicrm_activity_for_source' => array('fk' => 'contact_id', 'table_name' => 'log_civicrm_activity', 'joins' => array('table' => 'log_civicrm_activity_contact', 'join' => "entity_log_civireport.id = fk_table.activity_id AND fk_table.record_type_id = {$sourceID}"), 'bracket_info' => array('entity_column' => 'activity_type_id', 'options' => CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE)), 'log_type' => 'Activity'), 'log_civicrm_case' => array('fk' => 'contact_id', 'joins' => array('table' => 'log_civicrm_case_contact', 'join' => 'entity_log_civireport.id = fk_table.case_id'), 'bracket_info' => array('entity_column' => 'case_type_id', 'options' => CRM_Case_PseudoConstant::caseType('title', FALSE)))); $logging = new CRM_Logging_Schema(); // 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', 'joins' => array('table' => 'log_civicrm_address', '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(); }
/** * @param $table * @param int $contactID * * @return array */ public function diffsInTable($table, $contactID = NULL) { $diffs = array(); $params = array(1 => array($this->log_conn_id, 'Integer'), 2 => array($this->log_date, 'String')); $logging = new CRM_Logging_Schema(); $addressCustomTables = $logging->entityCustomDataLogTables('Address'); $contactIdClause = $join = ''; if ($contactID) { $params[3] = array($contactID, 'Integer'); switch ($table) { case 'civicrm_contact': $contactIdClause = "AND id = %3"; break; case 'civicrm_note': $contactIdClause = "AND (( entity_id = %3 AND entity_table = 'civicrm_contact' ) OR (entity_id IN (SELECT note.id FROM `{$this->db}`.log_civicrm_note note WHERE note.entity_id = %3 AND note.entity_table = 'civicrm_contact') AND entity_table = 'civicrm_note'))"; break; case 'civicrm_entity_tag': $contactIdClause = "AND entity_id = %3 AND entity_table = 'civicrm_contact'"; break; case 'civicrm_relationship': $contactIdClause = "AND (contact_id_a = %3 OR contact_id_b = %3)"; break; case 'civicrm_activity': $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); $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); $join = "\nLEFT JOIN civicrm_activity_contact at ON at.activity_id = lt.id AND at.contact_id = %3 AND at.record_type_id = {$targetID}\nLEFT JOIN civicrm_activity_contact aa ON aa.activity_id = lt.id AND aa.contact_id = %3 AND aa.record_type_id = {$assigneeID}\nLEFT JOIN civicrm_activity_contact source ON source.activity_id = lt.id AND source.contact_id = %3 AND source.record_type_id = {$sourceID} "; $contactIdClause = "AND (at.id IS NOT NULL OR aa.id IS NOT NULL OR source.id IS NOT NULL)"; break; case 'civicrm_case': $contactIdClause = "AND id = (select case_id FROM civicrm_case_contact WHERE contact_id = %3 LIMIT 1)"; break; default: if (array_key_exists($table, $addressCustomTables)) { $join = "INNER JOIN `{$this->db}`.`log_civicrm_address` et ON et.id = lt.entity_id"; $contactIdClause = "AND contact_id = %3"; break; } // allow tables to be extended by report hook query objects list($contactIdClause, $join) = CRM_Report_BAO_Hook::singleton()->logDiffClause($this, $table); if (empty($contactIdClause)) { $contactIdClause = "AND contact_id = %3"; } if (strpos($table, 'civicrm_value') !== FALSE) { $contactIdClause = "AND entity_id = %3"; } } } // find ids in this table that were affected in the given connection (based on connection id and a ±10 s time period around the date) $sql = "\nSELECT DISTINCT lt.id FROM `{$this->db}`.`log_{$table}` lt\n{$join}\nWHERE lt.log_conn_id = %1 AND\n lt.log_date BETWEEN DATE_SUB(%2, INTERVAL {$this->interval}) AND DATE_ADD(%2, INTERVAL {$this->interval})\n {$contactIdClause}"; $dao = CRM_Core_DAO::executeQuery($sql, $params); while ($dao->fetch()) { $diffs = array_merge($diffs, $this->diffsInTableForId($table, $dao->id)); } return $diffs; }