Skip to content

Commit

Permalink
Move DAO function to DAO class, call it from Merge class
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Jun 20, 2018
1 parent 7841d2e commit e3e87c7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 42 deletions.
48 changes: 48 additions & 0 deletions CRM/Core/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -2268,6 +2268,54 @@ public static function getReferencesToTable($tableName) {
return $refsFound;
}

/**
* Get all references to contact table.
*
* This includes core tables, custom group tables, tables added by the merge
* hook and the entity_tag table.
*
* Refer to CRM-17454 for information on the danger of querying the information
* schema to derive this.
*/
public static function getReferencesToContactTable() {
if (isset(\Civi::$statics[__CLASS__]) && isset(\Civi::$statics[__CLASS__]['contact_references'])) {
return \Civi::$statics[__CLASS__]['contact_references'];
}
$contactReferences = [];
$coreReferences = CRM_Core_DAO::getReferencesToTable('civicrm_contact');
foreach ($coreReferences as $coreReference) {
if (!is_a($coreReference, 'CRM_Core_Reference_Dynamic')) {
$contactReferences[$coreReference->getReferenceTable()][] = $coreReference->getReferenceKey();
}
}
self::appendCustomTablesExtendingContacts($contactReferences);

// FixME for time being adding below line statically as no Foreign key constraint defined for table 'civicrm_entity_tag'
$contactReferences['civicrm_entity_tag'][] = 'entity_id';
\Civi::$statics[__CLASS__]['contact_references'] = $contactReferences;
return \Civi::$statics[__CLASS__]['contact_references'];
}

/**
* Add custom tables that extend contacts to the list of contact references.
*
* CRM_Core_BAO_CustomGroup::getAllCustomGroupsByBaseEntity seems like a safe-ish
* function to be sure all are retrieved & we don't miss subtypes or inactive or multiples
* - the down side is it is not cached.
*
* Further changes should be include tests in the CRM_Core_MergerTest class
* to ensure that disabled, subtype, multiple etc groups are still captured.
*
* @param array $cidRefs
*/
public static function appendCustomTablesExtendingContacts(&$cidRefs) {
$customValueTables = CRM_Core_BAO_CustomGroup::getAllCustomGroupsByBaseEntity('Contact');
$customValueTables->find();
while ($customValueTables->fetch()) {
$cidRefs[$customValueTables->table_name] = array('entity_id');
}
}

/**
* Lookup the value of a MySQL global configuration variable.
*
Expand Down
51 changes: 10 additions & 41 deletions CRM/Dedupe/Merger.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,35 +203,23 @@ public static function getActiveRelTables($cid) {
/**
* Get array tables and fields that reference civicrm_contact.id.
*
* This includes core tables, custom group tables, tables added by the merge
* hook and (somewhat randomly) the entity_tag table.
* This function calls the merge hook and only exists to wrap the DAO function to support that deprecated call.
* The entityTypes hook is the recommended way to add tables to this result.
*
* Refer to CRM-17454 for information on the danger of querying the information
* schema to derive this.
*
* This function calls the merge hook but the entityTypes hook is the recommended
* way to add tables to this result.
* I thought about adding another hook to alter tableReferences but decided it was unclear if there
* are use cases not covered by entityTables and instead we should wait & see.
*/
public static function cidRefs() {
if (isset(\Civi::$statics[__CLASS__]) && isset(\Civi::$statics[__CLASS__]['contact_references'])) {
return \Civi::$statics[__CLASS__]['contact_references'];
}
$contactReferences = array();
$coreReferences = CRM_Core_DAO::getReferencesToTable('civicrm_contact');
foreach ($coreReferences as $coreReference) {
if (!is_a($coreReference, 'CRM_Core_Reference_Dynamic')) {
$contactReferences[$coreReference->getReferenceTable()][] = $coreReference->getReferenceKey();
}
}
self::addCustomTablesExtendingContactsToCidRefs($contactReferences);

// FixME for time being adding below line statically as no Foreign key constraint defined for table 'civicrm_entity_tag'
$contactReferences['civicrm_entity_tag'][] = 'entity_id';
$contactReferences = $coreReferences = CRM_Core_DAO::getReferencesToContactTable();

// Allow hook_civicrm_merge() to adjust $cidRefs.
// Note that if entities are registered using the entityTypes hook there
// is no need to use this hook.
CRM_Utils_Hook::merge('cidRefs', $contactReferences);
if ($contactReferences !== $coreReferences) {
Civi::log()->warning("Deprecated hook ::merge in context of 'cidRefs. Use entityTypes instead.", array('civi.tag' => 'deprecated'));
}
\Civi::$statics[__CLASS__]['contact_references'] = $contactReferences;
return \Civi::$statics[__CLASS__]['contact_references'];
}
Expand Down Expand Up @@ -486,7 +474,8 @@ public static function moveContactBelongings($mainId, $otherId, $tables = FALSE,
// getting all custom tables
$customTables = array();
if ($customTableToCopyFrom !== NULL) {
self::addCustomTablesExtendingContactsToCidRefs($customTables);
// @todo this duplicates cidRefs?
CRM_Core_DAO::appendCustomTablesExtendingContacts($customTables);
$customTables = array_keys($customTables);
}

Expand Down Expand Up @@ -1925,26 +1914,6 @@ public static function addMembershipToRealtedContacts($contactID) {
}
}

/**
* Add custom tables that extend contacts to the list of contact references.
*
* CRM_Core_BAO_CustomGroup::getAllCustomGroupsByBaseEntity seems like a safe-ish
* function to be sure all are retrieved & we don't miss subtypes or inactive or multiples
* - the down side is it is not cached.
*
* Further changes should be include tests in the CRM_Core_MergerTest class
* to ensure that disabled, subtype, multiple etc groups are still captured.
*
* @param array $cidRefs
*/
public static function addCustomTablesExtendingContactsToCidRefs(&$cidRefs) {
$customValueTables = CRM_Core_BAO_CustomGroup::getAllCustomGroupsByBaseEntity('Contact');
$customValueTables->find();
while ($customValueTables->fetch()) {
$cidRefs[$customValueTables->table_name] = array('entity_id');
}
}

/**
* Create activities tracking the merge on affected contacts.
*
Expand Down
2 changes: 1 addition & 1 deletion CRM/Logging/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@ public static function disableLoggingForThisConnection() {
* but this is the only entity currently available...
*/
public function getLogTablesForContact() {
$tables = array_keys(CRM_Dedupe_Merger::cidRefs());
$tables = array_keys(CRM_Core_DAO::getReferencesToContactTable());
return array_intersect($tables, $this->tables);
}

Expand Down

0 comments on commit e3e87c7

Please sign in to comment.