From 1719073dc0c86e3838888ebf8d13c1d3d1514a16 Mon Sep 17 00:00:00 2001 From: eileen Date: Tue, 12 Jul 2016 16:14:50 +1200 Subject: [PATCH] CRM-19035 Remove code duplication in the processing of duplicate matches --- CRM/Contact/Page/DedupeFind.php | 55 +------------------------------ CRM/Core/BAO/PrevNextCache.php | 38 +--------------------- CRM/Dedupe/Finder.php | 57 +++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 91 deletions(-) diff --git a/CRM/Contact/Page/DedupeFind.php b/CRM/Contact/Page/DedupeFind.php index 4bc94a6a1e..d9420b03b0 100644 --- a/CRM/Contact/Page/DedupeFind.php +++ b/CRM/Contact/Page/DedupeFind.php @@ -207,66 +207,14 @@ class CRM_Contact_Page_DedupeFind extends CRM_Core_Page_Basic { CRM_Utils_System::redirect($url); } else { - $cids = array(); - foreach ($foundDupes as $dupe) { - $cids[$dupe[0]] = 1; - $cids[$dupe[1]] = 1; - } - $cidString = implode(', ', array_keys($cids)); - $sql = "SELECT id, display_name FROM civicrm_contact WHERE id IN ($cidString) ORDER BY sort_name"; - $dao = new CRM_Core_DAO(); - $dao->query($sql); - $displayNames = array(); - while ($dao->fetch()) { - $displayNames[$dao->id] = $dao->display_name; - } - // FIXME: sort the contacts; $displayName // is already sort_name-sorted, so use that // (also, consider sorting by dupe count first) // lobo - change the sort to by threshold value // so the more likely dupes are sorted first - $userId = CRM_Core_Session::singleton()->getLoggedInContactID(); - $mainContacts = $permission = array(); - - foreach ($foundDupes as $dupes) { - $srcID = $dupes[0]; - $dstID = $dupes[1]; - if ($dstID == $userId) { - $srcID = $dupes[1]; - $dstID = $dupes[0]; - } - /*** - * Eliminate this since it introduces 3 queries PER merge row - * and hence is very expensive - * CRM-8822 - * if ( !array_key_exists( $srcID, $permission ) ) { - * $permission[$srcID] = CRM_Contact_BAO_Contact_Permission::allow( $srcID, CRM_Core_Permission::EDIT ); - * } - * if ( !array_key_exists( $dstID, $permission ) ) { - * $permission[$dstID] = CRM_Contact_BAO_Contact_Permission::allow( $dstID, CRM_Core_Permission::EDIT ); - * } - * - * $canMerge = ( $permission[$dstID] && $permission[$srcID] ); - * - */ + $mainContacts = CRM_Dedupe_Finder::parseAndStoreDupePairs($foundDupes, $cacheKeyString); - // we'll do permission checking during the merge process - $canMerge = TRUE; - - $mainContacts[] = $row = array( - 'srcID' => $srcID, - 'srcName' => $displayNames[$srcID], - 'dstID' => $dstID, - 'dstName' => $displayNames[$dstID], - 'weight' => $dupes[2], - 'canMerge' => $canMerge, - ); - - $data = CRM_Core_DAO::escapeString(serialize($row)); - $values[] = " ( 'civicrm_contact', $srcID, $dstID, '$cacheKeyString', '$data' ) "; - } if ($cid) { $this->_cid = $cid; } @@ -276,7 +224,6 @@ class CRM_Contact_Page_DedupeFind extends CRM_Core_Page_Basic { $this->_rgid = $rgid; $this->_mainContacts = $mainContacts; - CRM_Core_BAO_PrevNextCache::setItem($values); $session = CRM_Core_Session::singleton(); if ($this->_cid) { $session->pushUserContext(CRM_Utils_System::url('civicrm/contact/deduperules', diff --git a/CRM/Core/BAO/PrevNextCache.php b/CRM/Core/BAO/PrevNextCache.php index 3bfdd77385..8eb32921aa 100644 --- a/CRM/Core/BAO/PrevNextCache.php +++ b/CRM/Core/BAO/PrevNextCache.php @@ -384,43 +384,7 @@ WHERE (pn.cacheKey $op %1 OR pn.cacheKey $op %2) } if (!empty($foundDupes)) { - $cids = $displayNames = $values = array(); - foreach ($foundDupes as $dupe) { - $cids[$dupe[0]] = 1; - $cids[$dupe[1]] = 1; - } - $cidString = implode(', ', array_keys($cids)); - $sql = "SELECT id, display_name FROM civicrm_contact WHERE id IN ($cidString) ORDER BY sort_name"; - $dao = new CRM_Core_DAO(); - $dao->query($sql); - while ($dao->fetch()) { - $displayNames[$dao->id] = $dao->display_name; - } - - $session = CRM_Core_Session::singleton(); - $userId = $session->get('userID'); - - foreach ($foundDupes as $dupes) { - $srcID = $dupes[0]; - $dstID = $dupes[1]; - if ($dstID == $userId) { - $srcID = $dupes[1]; - $dstID = $dupes[0]; - } - - $row = array( - 'srcID' => $srcID, - 'srcName' => $displayNames[$srcID], - 'dstID' => $dstID, - 'dstName' => $displayNames[$dstID], - 'weight' => $dupes[2], - 'canMerge' => TRUE, - ); - - $data = CRM_Core_DAO::escapeString(serialize($row)); - $values[] = " ( 'civicrm_contact', $srcID, $dstID, '$cacheKeyString', '$data' ) "; - } - self::setItem($values); + CRM_Dedupe_Finder::parseAndStoreDupePairs($foundDupes, $cacheKeyString); } } diff --git a/CRM/Dedupe/Finder.php b/CRM/Dedupe/Finder.php index 2d8be85400..77e36cd1e9 100644 --- a/CRM/Dedupe/Finder.php +++ b/CRM/Dedupe/Finder.php @@ -348,4 +348,61 @@ class CRM_Dedupe_Finder { return $params; } + /** + * Parse duplicate pairs into a standardised array and store in the prev_next_cache. + * + * @param array $foundDupes + * @param string $cacheKeyString + * + * @return array Dupe pairs with the keys + * Dupe pairs with the keys + * -srcID + * -srcName + * -dstID + * -dstName + * -weight + * -canMerge + * + * @throws CRM_Core_Exception + */ + public static function parseAndStoreDupePairs($foundDupes, $cacheKeyString) { + $cids = array(); + foreach ($foundDupes as $dupe) { + $cids[$dupe[0]] = 1; + $cids[$dupe[1]] = 1; + } + $cidString = implode(', ', array_keys($cids)); + $sql = "SELECT id, display_name FROM civicrm_contact WHERE id IN ($cidString) ORDER BY sort_name"; + $dao = new CRM_Core_DAO(); + $dao->query($sql); + $displayNames = array(); + while ($dao->fetch()) { + $displayNames[$dao->id] = $dao->display_name; + } + + $userId = CRM_Core_Session::singleton()->getLoggedInContactID(); + foreach ($foundDupes as $dupes) { + $srcID = $dupes[0]; + $dstID = $dupes[1]; + if ($dstID == $userId) { + $srcID = $dupes[1]; + $dstID = $dupes[0]; + } + + $mainContacts[] = $row = array( + 'srcID' => $srcID, + 'srcName' => $displayNames[$srcID], + 'dstID' => $dstID, + 'dstName' => $displayNames[$dstID], + 'weight' => $dupes[2], + 'canMerge' => TRUE, + ); + + $data = CRM_Core_DAO::escapeString(serialize($row)); + $values[] = " ( 'civicrm_contact', $srcID, $dstID, '$cacheKeyString', '$data' ) "; + } + CRM_Core_BAO_PrevNextCache::setItem($values); + return $mainContacts; + } + } -- 2.25.1