Merge pull request #11728 from JMAConsulting/CRM-21809
[civicrm-core.git] / CRM / Dedupe / Finder.php
index 4504447023001a2eb5a9898a77595e30953e2739..f3baeee76f6b3f54181ee7c7afc3f7c9ba0cbdd5 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.7                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2017                                |
+ | Copyright CiviCRM LLC (c) 2004-2018                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2017
+ * @copyright CiviCRM LLC (c) 2004-2018
  * $Id$
  *
  */
@@ -51,9 +51,10 @@ class CRM_Dedupe_Finder {
    * @param bool $checkPermissions
    *   Respect logged in user permissions.
    *
-   * @param int $limit
-   *   Optional limit. This limits the number of contacts for which the code will
-   *   attempt to find matches.
+   * @param int $searchLimit
+   *  Limit for the number of contacts to be used for comparison.
+   *  The search methodology finds all matches for the searchedContacts so this limits
+   *  the number of searched contacts, not the matches found.
    *
    * @return array
    *   Array of (cid1, cid2, weight) dupe triples
@@ -61,18 +62,18 @@ class CRM_Dedupe_Finder {
    * @throws CiviCRM_API3_Exception
    * @throws Exception
    */
-  public static function dupes($rgid, $cids = array(), $checkPermissions = TRUE, $limit = NULL) {
+  public static function dupes($rgid, $cids = array(), $checkPermissions = TRUE, $searchLimit = 0) {
     $rgBao = new CRM_Dedupe_BAO_RuleGroup();
     $rgBao->id = $rgid;
     $rgBao->contactIds = $cids;
     if (!$rgBao->find(TRUE)) {
       CRM_Core_Error::fatal("Dedupe rule not found for selected contacts");
     }
-    if (empty($rgBao->contactIds) && !empty($limit)) {
+    if (empty($rgBao->contactIds) && !empty($searchLimit)) {
       $limitedContacts = civicrm_api3('Contact', 'get', array(
         'return' => 'id',
         'contact_type' => $rgBao->contact_type,
-        'options' => array('limit' => $limit),
+        'options' => array('limit' => $searchLimit),
       ));
       $rgBao->contactIds = array_keys($limitedContacts['values']);
     }
@@ -145,6 +146,10 @@ class CRM_Dedupe_Finder {
     }
     $params['check_permission'] = CRM_Utils_Array::value('check_permission', $params, TRUE);
 
+    if (isset($params['civicrm_phone']['phone_numeric'])) {
+      $orig = $params['civicrm_phone']['phone_numeric'];
+      $params['civicrm_phone']['phone_numeric'] = preg_replace('/[^\d]/', '', $orig);
+    }
     $rgBao->params = $params;
     $rgBao->fillTable();
     $dao = new CRM_Core_DAO();
@@ -167,12 +172,16 @@ class CRM_Dedupe_Finder {
    * @param int $gid
    *   Contact group id (currently, works only with non-smart groups).
    *
-   * @param int $limit
+   * @param int $searchLimit
+   *  Limit for the number of contacts to be used for comparison.
+   *  The search methodology finds all matches for the searchedContacts so this limits
+   *  the number of searched contacts, not the matches found.
+   *
    * @return array
    *   array of (cid1, cid2, weight) dupe triples
    */
-  public static function dupesInGroup($rgid, $gid, $limit = NULL) {
-    $cids = array_keys(CRM_Contact_BAO_Group::getMember($gid, $limit));
+  public static function dupesInGroup($rgid, $gid, $searchLimit = 0) {
+    $cids = array_keys(CRM_Contact_BAO_Group::getMember($gid, $searchLimit));
     if (!empty($cids)) {
       return self::dupes($rgid, $cids);
     }
@@ -294,6 +303,16 @@ class CRM_Dedupe_Finder {
             }
           }
         }
+        if ($table == 'civicrm_phone') {
+          $fixes = array(
+            'phone' => 'phone_numeric',
+          );
+          foreach ($fixes as $orig => $target) {
+            if (!empty($flat[$orig])) {
+              $params[$table][$target] = $flat[$orig];
+            }
+          }
+        }
         foreach ($fields as $field => $title) {
           if (!empty($flat[$field])) {
             $params[$table][$field] = $flat[$field];