From 03b40a7bb8b72da9ca3fa187e3f28f01509eaf30 Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 27 Mar 2017 14:06:27 +1300 Subject: [PATCH] Towards CRM-20328 remove duplicate code, towards taking extension from core --- CRM/Contribute/Form/Contribution/Confirm.php | 13 ++------ CRM/Contribute/Form/Contribution/Main.php | 6 +--- CRM/Core/BAO/UFGroup.php | 4 +-- CRM/Core/BAO/UFMatch.php | 4 +-- CRM/Event/Cart/Form/Cart.php | 10 +----- CRM/PCP/Form/PCPAccount.php | 8 ++--- CRM/Utils/DeprecatedUtils.php | 32 +++----------------- 7 files changed, 14 insertions(+), 63 deletions(-) diff --git a/CRM/Contribute/Form/Contribution/Confirm.php b/CRM/Contribute/Form/Contribution/Confirm.php index f6f1e657cc..61a44eadc1 100644 --- a/CRM/Contribute/Form/Contribution/Confirm.php +++ b/CRM/Contribute/Form/Contribution/Confirm.php @@ -1188,13 +1188,11 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr if (!$orgID) { // check if matching organization contact exists - $dedupeParams = CRM_Dedupe_Finder::formatParams($behalfOrganization, 'Organization'); - $dedupeParams['check_permission'] = FALSE; - $dupeIDs = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Organization', 'Unsupervised'); + $dupeID = CRM_Contact_BAO_Contact::getFirstDuplicateContact($behalfOrganization, 'Organization', 'Unsupervised', array(), FALSE); // CRM-6243 says to pick the first org even if more than one match if (count($dupeIDs) >= 1) { - $behalfOrganization['contact_id'] = $orgID = $dupeIDs[0]; + $behalfOrganization['contact_id'] = $orgID = $dupeID; // don't allow name edit unset($behalfOrganization['organization_name']); } @@ -2160,12 +2158,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr unset($dupeParams['honor']); } - $dedupeParams = CRM_Dedupe_Finder::formatParams($dupeParams, 'Individual'); - $dedupeParams['check_permission'] = FALSE; - $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual'); - - // if we find more than one contact, use the first one - $contactID = CRM_Utils_Array::value(0, $ids); + $contactID = CRM_Contact_BAO_Contact::getFirstDuplicateContact($dupeParams, 'Individual', 'Unsupervised', array(), FALSE); // Fetch default greeting id's if creating a contact if (!$contactID) { diff --git a/CRM/Contribute/Form/Contribution/Main.php b/CRM/Contribute/Form/Contribution/Main.php index f3cfc33270..fd23144e9b 100644 --- a/CRM/Contribute/Form/Contribution/Main.php +++ b/CRM/Contribute/Form/Contribution/Main.php @@ -727,11 +727,7 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu // For anonymous user check using dedupe rule // if user has Cancelled Membership if (!$memContactID) { - $dedupeParams = CRM_Dedupe_Finder::formatParams($fields, 'Individual'); - $dedupeParams['check_permission'] = FALSE; - $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual'); - // if we find more than one contact, use the first one - $memContactID = CRM_Utils_Array::value(0, $ids); + $memContactID = CRM_Contact_BAO_Contact::getFirstDuplicateContact($fields, 'Individual', 'Unsupervised', array(), FALSE); } $currentMemberships = CRM_Member_BAO_Membership::getContactsCancelledMembership($memContactID, $is_test diff --git a/CRM/Core/BAO/UFGroup.php b/CRM/Core/BAO/UFGroup.php index 3af88e2ffd..b6ddf7fa0a 100644 --- a/CRM/Core/BAO/UFGroup.php +++ b/CRM/Core/BAO/UFGroup.php @@ -967,9 +967,7 @@ class CRM_Core_BAO_UFGroup extends CRM_Core_DAO_UFGroup { * contact_id if found, null otherwise */ public static function findContact(&$params, $id = NULL, $contactType = 'Individual') { - $dedupeParams = CRM_Dedupe_Finder::formatParams($params, $contactType); - $dedupeParams['check_permission'] = CRM_Utils_Array::value('check_permission', $params, TRUE); - $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, $contactType, 'Supervised', array($id)); + $ids = CRM_Contact_BAO_Contact::getDuplicateContacts($params, $contactType, 'Supervised', array($id), CRM_Utils_Array::value('check_permission', $params, TRUE)); if (!empty($ids)) { return implode(',', $ids); } diff --git a/CRM/Core/BAO/UFMatch.php b/CRM/Core/BAO/UFMatch.php index f20be6680d..afd256805e 100644 --- a/CRM/Core/BAO/UFMatch.php +++ b/CRM/Core/BAO/UFMatch.php @@ -205,9 +205,7 @@ class CRM_Core_BAO_UFMatch extends CRM_Core_DAO_UFMatch { $params = $_POST; $params['email'] = $uniqId; - $dedupeParams = CRM_Dedupe_Finder::formatParams($params, 'Individual'); - $dedupeParams['check_permission'] = FALSE; - $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual'); + $ids = CRM_Contact_BAO_Contact::getDuplicateContacts($params, 'Individual', 'Unsupervised', array(), FALSE); if (!empty($ids) && Civi::settings()->get('uniq_email_per_site')) { // restrict dupeIds to ones that belong to current domain/site. diff --git a/CRM/Event/Cart/Form/Cart.php b/CRM/Event/Cart/Form/Cart.php index f7a8742030..3f4f23a319 100644 --- a/CRM/Event/Cart/Form/Cart.php +++ b/CRM/Event/Cart/Form/Cart.php @@ -128,15 +128,7 @@ class CRM_Event_Cart_Form_Cart extends CRM_Core_Form { * @return mixed|null */ public static function find_contact($fields) { - $dedupe_params = CRM_Dedupe_Finder::formatParams($fields, 'Individual'); - $dedupe_params['check_permission'] = FALSE; - $ids = CRM_Dedupe_Finder::dupesByParams($dedupe_params, 'Individual'); - if (is_array($ids)) { - return array_pop($ids); - } - else { - return NULL; - } + return CRM_Contact_BAO_Contact::getFirstDuplicateContact($fields, 'Individual', 'Unsupervised', array(), FALSE); } /** diff --git a/CRM/PCP/Form/PCPAccount.php b/CRM/PCP/Form/PCPAccount.php index 9e72f5ee06..d62abf8640 100644 --- a/CRM/PCP/Form/PCPAccount.php +++ b/CRM/PCP/Form/PCPAccount.php @@ -265,12 +265,8 @@ class CRM_PCP_Form_PCPAccount extends CRM_Core_Form { } } - $dedupeParams = CRM_Dedupe_Finder::formatParams($params, 'Individual'); - $dedupeParams['check_permission'] = FALSE; - $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual', 'Unsupervised'); - if ($ids) { - $this->_contactID = $ids['0']; - } + $this->_contactID = CRM_Contact_BAO_Contact::getFirstDuplicateContact($params, 'Individual', 'Unsupervised', array(), FALSE); + $contactID = CRM_Contact_BAO_Contact::createProfileContact($params, $this->_fields, $this->_contactID); $this->set('contactID', $contactID); diff --git a/CRM/Utils/DeprecatedUtils.php b/CRM/Utils/DeprecatedUtils.php index 9b65c3c68a..0fc7352b3e 100644 --- a/CRM/Utils/DeprecatedUtils.php +++ b/CRM/Utils/DeprecatedUtils.php @@ -1182,9 +1182,7 @@ function _civicrm_api3_deprecated_duplicate_formatted_contact($params) { } } else { - require_once 'CRM/Dedupe/Finder.php'; - $dedupeParams = CRM_Dedupe_Finder::formatParams($params, $params['contact_type']); - $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, $params['contact_type'], 'Unsupervised'); + $ids = CRM_Contact_BAO_Contact::getDuplicateContacts($params, $params['contact_type'], 'Unsupervised'); if (!empty($ids)) { $ids = implode(',', $ids); @@ -1455,24 +1453,10 @@ function _civicrm_api3_deprecated_contact_check_params( } if ($dupeCheck) { - // @todo switch to using api version by uncommenting these lines & removing following 11. - // Any change here is too scary for a stable release. + // @todo switch to using api version // $dupes = civicrm_api3('Contact', 'duplicatecheck', (array('match' => $params, 'dedupe_rule_id' => $dedupeRuleGroupID))); // $ids = $dupes['count'] ? implode(',', array_keys($dupes['values'])) : NULL; - // check for record already existing - require_once 'CRM/Dedupe/Finder.php'; - $dedupeParams = CRM_Dedupe_Finder::formatParams($params, $params['contact_type']); - - // CRM-6431 - // setting 'check_permission' here means that the dedupe checking will be carried out even if the - // person does not have permission to carry out de-dupes - // this is similar to the front end form - if (isset($params['check_permission'])) { - $dedupeParams['check_permission'] = $params['check_permission']; - } - - $ids = implode(',', CRM_Dedupe_Finder::dupesByParams($dedupeParams, $params['contact_type'], 'Unsupervised', array(), $dedupeRuleGroupID)); - + $ids = CRM_Contact_BAO_Contact::getDuplicateContacts($params, $params['contact_type'], 'Unsupervised', array(), CRM_Utils_Array::value('check_permissions', $params, $dedupeRuleGroupID)); if ($ids != NULL) { if ($dupeErrorArray) { $error = CRM_Core_Error::createError("Found matching contacts: $ids", @@ -1488,14 +1472,8 @@ function _civicrm_api3_deprecated_contact_check_params( // check for organisations with same name if (!empty($params['current_employer'])) { - $organizationParams = array(); - $organizationParams['organization_name'] = $params['current_employer']; - - require_once 'CRM/Dedupe/Finder.php'; - $dedupParams = CRM_Dedupe_Finder::formatParams($organizationParams, 'Organization'); - - $dedupParams['check_permission'] = FALSE; - $dupeIds = CRM_Dedupe_Finder::dupesByParams($dedupParams, 'Organization', 'Supervised'); + $organizationParams = array('organization_name' => $params['current_employer']); + $dupeIds = CRM_Contact_BAO_Contact::getDuplicateContacts($organizationParams, 'Organization', 'Supervised', array(), FALSE); // check for mismatch employer name and id if (!empty($params['employer_id']) && !in_array($params['employer_id'], $dupeIds) -- 2.25.1