CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • dupe_match and updating the first contact
Pages: [1]

Author Topic: dupe_match and updating the first contact  (Read 1935 times)

Chris Burgess

  • Ask me questions
  • ****
  • Posts: 675
  • Karma: 59
dupe_match and updating the first contact
March 03, 2010, 01:25:41 pm
I've written a small wrapper which is intended to detect duplicate matches and update the first contact record in that case. (This is a questionable tactic by itself; assuming there's a duplicate match, you need to be certain that you actually want to handle it by updating the earliest contact record.)

Is there a better method to do this?

Code: [Select]
/**
 * Wrap civicrm_contact_add() with code which will detect dupe matches
 * and update the earliest created contact.
 *
 * Note that there are several places where we return $contact, but
 * the return statements are just for clarity. 
 *
 * You could remove all but the last one and it'd work the
 * same.
 *
 * @param array civicrm_contact_add() parameters
 * @return array civicrm error or civicrm contact array
 */
function my_civicrm_contact_add($params) {
  $params['dupe_check'] = TRUE ;
  civicrm_initialize() ;
  require_once 'api/v2/Contact.php' ;
  $contact = civicrm_contact_add($params) ;
  if ( !civicrm_error( $contact ) ) {
    // for clarity
    return $contact ;
  }
  else {
    // let's see if we have multiple matches
    if ( stristr($contact['error_message'],'Found matching contacts') ) {
      // if so, we'll get the lowest contact ID and update them
      $contact_ids = explode(',',$contact['error_data']) ;
      sort($contact_ids) ;
      $contact_id = array_shift($contact_ids) ;
      if ( (int)$contact_id > 0 ) {
        $params['contact_id'] = $contact_id ;
        $params['dupe_check'] = FALSE ;
        $contact = civicrm_contact_add($params);
        // for clarity
        return $contact ;
      }
      else {
        // some unlikely civicrm_error which gave us a non-numeric
        // contact_id
      }
    }
    else {
      // not multiple duplicates - some other civicrm_error
    }
  }
  // we didn't handle update to first dupe; this is either a
  // successful add of a non-dupe, or a civicrm_error
  return $contact ;
}
@xurizaemon ● www.fuzion.co.nz

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: dupe_match and updating the first contact
March 09, 2010, 01:32:27 am
The problem is that you don't know what dupe rule is applied (well, you know, but it might change because someone had to do an import...). Eg if the strict dupe rule is on email only and some users share their email (eg. the info@example.org), you're in for troubles. Talking about import, doesn't it override the rules (dupe check/always create duplicates) ?

Anyway, What I do is to add a small jQuery ajax check, and display the (potential) dupes on the contact form (blur on the last name). Doesn't prevent to create dupes via other.

@civicrew, shall I add an issue and put it in the trunk ?
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

xcf33

  • I post frequently
  • ***
  • Posts: 181
  • Karma: 7
  • CiviCRM version: 3.3.2
  • CMS version: Drupal 6.19/6.20
  • MySQL version: 5.x
  • PHP version: 5.2.6
Re: dupe_match and updating the first contact
May 25, 2010, 09:07:17 am
I just tested the API with dupe_check => TRUE

It seems all it's looking for is the email address, if the email address is the same it will not create the contact and give you an error code.

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: dupe_match and updating the first contact
May 25, 2010, 02:19:44 pm

hey chang:

the default strict dedupe rule is "email" only. hence u r seeing the email address check

the dedupe rule is configurable. if you change the strict dedupe rule, it will follow that rule.

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: dupe_match and updating the first contact
July 01, 2010, 01:11:28 am
Hi,

Just waking this thread up again because there is a problem with the 'check_dupe' option - which is that it relies on the person having permission to check duplicates.

I'm trying to figure out how to make it respect

    $params['check_permission'] = false;

(which is an option for the dupe finder function but it's getting stripped out before it gets there)
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: dupe_match and updating the first contact
July 01, 2010, 01:36:17 am
OK,

So following up on this - I applied the following to Contact.php and it allowed me to pass the 'check_permission' = False into the api call - thereby allowing me to use this api in a form used by anonymous user & have the same sort of functionality as the normal contribution form (for example) has.

It's possible that when 'dedupe_check' is set to True then 'check_permission' should default to FALSE? This seems like the 'expected' behaviour.



Code: [Select]
Index: Contact.php
===================================================================
--- Contact.php (revision 28469)
+++ Contact.php (working copy)
@@ -451,6 +451,11 @@
         // check for record already existing
         require_once 'CRM/Dedupe/Finder.php';
         $dedupeParams = CRM_Dedupe_Finder::formatParams($params, $params['contact_type']);
+        // 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'] = $fields['check_permission'];
+        }
         $ids = implode(',', CRM_Dedupe_Finder::dupesByParams($dedupeParams, $params['contact_type']));
        
         if ( $ids != null ) {
« Last Edit: July 01, 2010, 03:18:53 am by Eileen »
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: dupe_match and updating the first contact
July 01, 2010, 01:40:13 am
http://issues.civicrm.org/jira/browse/CRM-6431
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • dupe_match and updating the first contact

This forum was archived on 2017-11-26.