Skip to content

Commit

Permalink
Clarify entityID types in Merger class
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Apr 4, 2023
1 parent d9231a1 commit a5fc5bb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
4 changes: 2 additions & 2 deletions CRM/Contact/Form/Merge.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public function preProcess() {
$cmsUser = $mainUfId && $otherUfId;
$this->assign('user', $cmsUser);

$rowsElementsAndInfo = CRM_Dedupe_Merger::getRowsElementsAndInfo($this->_cid, $this->_oid);
$rowsElementsAndInfo = CRM_Dedupe_Merger::getRowsElementsAndInfo((int) $this->_cid, (int) $this->_oid);
$main = $this->_mainDetails = $rowsElementsAndInfo['main_details'];
$other = $this->_otherDetails = $rowsElementsAndInfo['other_details'];

Expand Down Expand Up @@ -311,7 +311,7 @@ public function postProcess() {
$formValues['other_details'] = $this->_otherDetails;

// Check if any rel_tables checkboxes have been de-selected
$rowsElementsAndInfo = CRM_Dedupe_Merger::getRowsElementsAndInfo($this->_cid, $this->_oid);
$rowsElementsAndInfo = CRM_Dedupe_Merger::getRowsElementsAndInfo((int) $this->_cid, (int) $this->_oid);
// If rel_tables is not set then initialise with 0 value, required for the check which calls removeContactBelongings in moveAllBelongings
foreach (array_keys($rowsElementsAndInfo['rel_tables']) as $relTableElement) {
if (!array_key_exists($relTableElement, $formValues)) {
Expand Down
39 changes: 19 additions & 20 deletions CRM/Dedupe/Merger.php
Original file line number Diff line number Diff line change
Expand Up @@ -1483,9 +1483,9 @@ public static function getLocationBlockInfo() {
/**
* A function to build an array of information required by merge function and the merge UI.
*
* @param int $mainId
* @param int $mainID
* Main contact with whom merge has to happen.
* @param int $otherId
* @param int $otherID
* Duplicate contact which would be deleted after merge operation.
* @param bool $checkPermissions
* Should the logged in user's permissions be ignore. Setting this to false is
Expand Down Expand Up @@ -1523,13 +1523,15 @@ public static function getLocationBlockInfo() {
* though the form had been submitted with those options.
*
* @throws \CRM_Core_Exception
* @todo review permissions issue!
*
*/
public static function getRowsElementsAndInfo($mainId, $otherId, $checkPermissions = TRUE) {
public static function getRowsElementsAndInfo(int $mainID, int $otherID, bool $checkPermissions = TRUE) {
$qfZeroBug = 'e8cddb72-a257-11dc-b9cc-0016d3330ee9';
$fields = self::getMergeFieldsMetadata();

$main = self::getMergeContactDetails($mainId);
$other = self::getMergeContactDetails($otherId);
$main = self::getMergeContactDetails($mainID);
$other = self::getMergeContactDetails($otherID);

$compareFields = self::retrieveFields($main, $other);

Expand Down Expand Up @@ -1583,30 +1585,30 @@ public static function getRowsElementsAndInfo($mainId, $otherId, $checkPermissio
$locations = ['main' => [], 'other' => []];

foreach ($locationBlocks as $blockName => $blockInfo) {
[$locations, $rows, $elements, $migrationInfo] = self::addLocationFieldInfo($mainId, $otherId, $blockInfo, $blockName, $locations, $rows, $elements, $migrationInfo);
[$locations, $rows, $elements, $migrationInfo] = self::addLocationFieldInfo($mainID, $otherID, $blockInfo, $blockName, $locations, $rows, $elements, $migrationInfo);
} // End loop through each location block entity

// add the related tables and unset the ones that don't sport any of the duplicate contact's info
$mergeHandler = new CRM_Dedupe_MergeHandler((int) $mainId, (int) $otherId);
$mergeHandler = new CRM_Dedupe_MergeHandler((int) $mainID, (int) $otherID);
$relTables = $mergeHandler->getTablesRelatedToTheMergePair();
foreach ($relTables as $name => $null) {
$migrationInfo["move_$name"] = 1;

$relTables[$name]['main_url'] = str_replace('$cid', $mainId, $relTables[$name]['url']);
$relTables[$name]['other_url'] = str_replace('$cid', $otherId, $relTables[$name]['url']);
$relTables[$name]['main_url'] = str_replace('$cid', $mainID, $relTables[$name]['url']);
$relTables[$name]['other_url'] = str_replace('$cid', $otherID, $relTables[$name]['url']);
$relTables[$name]['has_operation'] = 0;

if ($name === 'rel_table_users') {
// @todo - this user url stuff is only needed for the form layer - move to CRM_Contact_Form_Merge
$relTables[$name]['main_url'] = str_replace('%ufid', CRM_Core_BAO_UFMatch::getUFId($mainId), $relTables[$name]['url']);
$relTables[$name]['other_url'] = str_replace('%ufid', CRM_Core_BAO_UFMatch::getUFId($otherId), $relTables[$name]['url']);
$relTables[$name]['main_url'] = str_replace('%ufid', CRM_Core_BAO_UFMatch::getUFId($mainID), $relTables[$name]['url']);
$relTables[$name]['other_url'] = str_replace('%ufid', CRM_Core_BAO_UFMatch::getUFId($otherID), $relTables[$name]['url']);
}
if ($name === 'rel_table_memberships') {
//Enable 'add new' checkbox if main contact does not contain any membership similar to duplicate contact.
$attributes = ['checked' => 'checked'];
$otherContactMemberships = CRM_Member_BAO_Membership::getAllContactMembership($otherId);
$otherContactMemberships = CRM_Member_BAO_Membership::getAllContactMembership($otherID);
foreach ($otherContactMemberships as $membership) {
$mainMembership = CRM_Member_BAO_Membership::getContactMembership($mainId, $membership['membership_type_id'], FALSE);
$mainMembership = CRM_Member_BAO_Membership::getContactMembership($mainID, $membership['membership_type_id'], FALSE);
if ($mainMembership) {
$attributes = [];
}
Expand All @@ -1628,11 +1630,11 @@ public static function getRowsElementsAndInfo($mainId, $otherId, $checkPermissio
}

// handle custom fields
$mainTree = self::getTree($main['contact_type'], $mainId,
$mainTree = self::getTree($main['contact_type'], $mainID,
$main['contact_sub_type'] ?? NULL,
$checkPermissions ? CRM_Core_Permission::EDIT : FALSE
);
$otherTree = self::getTree($main['contact_type'], $otherId,
$otherTree = self::getTree($main['contact_type'], $otherID,
$other['contact_sub_type'] ?? NULL,
$checkPermissions ? CRM_Core_Permission::EDIT : FALSE
);
Expand Down Expand Up @@ -1727,14 +1729,11 @@ public static function getRowsElementsAndInfo($mainId, $otherId, $checkPermissio
*/
private static function getTree(
$entityType,
$entityID,
int $entityID,
$subTypes,
$checkPermission
) {

if ($entityID) {
$entityID = CRM_Utils_Type::escape($entityID, 'Integer');
}
if (!is_array($subTypes)) {
if (empty($subTypes)) {
$subTypes = [];
Expand Down Expand Up @@ -2585,7 +2584,7 @@ public static function getConflicts(array &$migrationInfo, int $mainId, int $oth
$conflicts = [];
// Generate var $migrationInfo. The variable structure is exactly same as
// $formValues submitted during a UI merge for a pair of contacts.
$rowsElementsAndInfo = CRM_Dedupe_Merger::getRowsElementsAndInfo($mainId, $otherId, FALSE);
$rowsElementsAndInfo = CRM_Dedupe_Merger::getRowsElementsAndInfo((int) $mainId, (int) $otherId, FALSE);
// add additional details that we might need to resolve conflicts
$migrationInfo = $rowsElementsAndInfo['migration_info'];
$migrationInfo['main_details'] = &$rowsElementsAndInfo['main_details'];
Expand Down

0 comments on commit a5fc5bb

Please sign in to comment.