From 5721d85e406e8e9c233135188c4bba4d21ee79b3 Mon Sep 17 00:00:00 2001 From: eileen Date: Wed, 23 Aug 2017 13:40:57 +1200 Subject: [PATCH] Towards CRM-20155 clean up form code in order to consolidate function use. The code in the Find and Merge Duplicate Contacts form has mystified me for along time. As a step towards extracting dedupe code into separate extension/s I have gone through & tidied up the finding of duplicates to re-use a function used elsewhere rather than duplicate it on the form. In the process I tried, and failed, to come up with a rationale for the duplicate form catching represented in setting form properties. Note that I made the parameter explicit. I am expecting this to be a URL paramter in the future (currently deploying that to our site as such in the context of being able to find matches for specfic contacts. I felt making it explicit now would aid in not missing instances of it when patching later --- CRM/Contact/Form/Task/Merge.php | 1 - CRM/Contact/Page/DedupeFind.php | 104 +++++++++++--------------------- CRM/Dedupe/Merger.php | 5 +- 3 files changed, 40 insertions(+), 70 deletions(-) diff --git a/CRM/Contact/Form/Task/Merge.php b/CRM/Contact/Form/Task/Merge.php index d0f0abd328..9bbdf4e223 100644 --- a/CRM/Contact/Form/Task/Merge.php +++ b/CRM/Contact/Form/Task/Merge.php @@ -33,7 +33,6 @@ /** * This class provides the functionality to Merge contacts. - * */ class CRM_Contact_Form_Task_Merge extends CRM_Contact_Form_Task { diff --git a/CRM/Contact/Page/DedupeFind.php b/CRM/Contact/Page/DedupeFind.php index 0646f0f7f7..5b09ef6641 100644 --- a/CRM/Contact/Page/DedupeFind.php +++ b/CRM/Contact/Page/DedupeFind.php @@ -62,6 +62,18 @@ class CRM_Contact_Page_DedupeFind extends CRM_Core_Page_Basic { $context = CRM_Utils_Request::retrieve('context', 'String', $this); $limit = CRM_Utils_Request::retrieve('limit', 'Integer', $this); $rgid = CRM_Utils_Request::retrieve('rgid', 'Positive', $this); + $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this, FALSE, 0); + // Using a placeholder for criteria as it is intended to be able to pass this later. + $criteria = array(); + $isConflictMode = ($context == 'conflicts'); + if ($cid) { + $this->_cid = $cid; + } + if ($gid) { + $this->_gid = $gid; + } + $this->_rgid = $rgid; + $urlQry = array( 'reset' => 1, 'rgid' => $rgid, @@ -78,14 +90,14 @@ class CRM_Contact_Page_DedupeFind extends CRM_Core_Page_Basic { if ($action & CRM_Core_Action::RENEW) { // empty cache if ($rgid) { - CRM_Core_BAO_PrevNextCache::deleteItem(NULL, CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid)); + CRM_Core_BAO_PrevNextCache::deleteItem(NULL, CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid, $criteria)); } $urlQry['action'] = 'update'; CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/dedupefind', $urlQry)); } elseif ($action & CRM_Core_Action::MAP) { // do a batch merge if requested - $result = CRM_Dedupe_Merger::batchMerge($rgid, $gid, 'safe', 75); + $result = CRM_Dedupe_Merger::batchMerge($rgid, $gid, 'safe', 75, 2, $criteria); $skippedCount = CRM_Utils_Request::retrieve('skipped', 'Positive', $this, FALSE, 0); $skippedCount = $skippedCount + count($result['skipped']); @@ -123,18 +135,17 @@ class CRM_Contact_Page_DedupeFind extends CRM_Core_Page_Basic { if ($action & CRM_Core_Action::UPDATE || $action & CRM_Core_Action::BROWSE ) { - $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this, FALSE, 0); $this->action = CRM_Core_Action::UPDATE; $urlQry['snippet'] = 4; - if ($context == 'conflicts') { + if ($isConflictMode) { $urlQry['selected'] = 1; } $this->assign('sourceUrl', CRM_Utils_System::url('civicrm/ajax/dedupefind', $urlQry, FALSE, NULL, FALSE)); //reload from cache table - $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid); + $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid, $criteria); $stats = CRM_Dedupe_Merger::getMergeStatsMsg($cacheKeyString); if ($stats) { @@ -142,80 +153,37 @@ class CRM_Contact_Page_DedupeFind extends CRM_Core_Page_Basic { // reset so we not displaying same message again CRM_Dedupe_Merger::resetMergeStats($cacheKeyString); } - $join = CRM_Dedupe_Merger::getJoinOnDedupeTable(); - $where = "de.id IS NULL"; - if ($context == 'conflicts') { - $where .= " AND pn.is_selected = 1"; - } - $this->_mainContacts = CRM_Core_BAO_PrevNextCache::retrieve($cacheKeyString, $join, $where); + + $this->_mainContacts = CRM_Dedupe_Merger::getDuplicatePairs($rgid, $gid, !$isConflictMode, 0, $isConflictMode, '', $isConflictMode, $criteria, TRUE); + if (empty($this->_mainContacts)) { - if ($context == 'conflicts') { + if ($isConflictMode) { // if the current screen was intended to list only selected contacts, move back to full dupe list $urlQry['action'] = 'update'; unset($urlQry['snippet']); CRM_Utils_System::redirect(CRM_Utils_System::url(CRM_Utils_System::currentPath(), $urlQry)); } - if ($gid) { - $foundDupes = $this->get("dedupe_dupes_$gid"); - if (!$foundDupes) { - $foundDupes = CRM_Dedupe_Finder::dupesInGroup($rgid, $gid, $limit); - } - $this->set("dedupe_dupes_$gid", $foundDupes); - } - else { - $foundDupes = $this->get('dedupe_dupes'); - if (!$foundDupes) { - $foundDupes = CRM_Dedupe_Finder::dupes($rgid, array(), TRUE, $limit); - } - $this->set('dedupe_dupes', $foundDupes); - } - if (!$foundDupes) { - $ruleGroup = new CRM_Dedupe_BAO_RuleGroup(); - $ruleGroup->id = $rgid; - $ruleGroup->find(TRUE); - - $session = CRM_Core_Session::singleton(); - $session->setStatus(ts('No possible duplicates were found using %1 rule.', array(1 => $ruleGroup->name)), ts('None Found'), 'info'); - $url = CRM_Utils_System::url('civicrm/contact/deduperules', 'reset=1'); - if ($context == 'search') { - $url = $session->readUserContext(); - } - CRM_Utils_System::redirect($url); - } - else { - $mainContacts = CRM_Dedupe_Finder::parseAndStoreDupePairs($foundDupes, $cacheKeyString); - - if ($cid) { - $this->_cid = $cid; - } - if ($gid) { - $this->_gid = $gid; - } - $this->_rgid = $rgid; - $this->_mainContacts = $mainContacts; - - $urlQry['action'] = 'update'; - if ($this->_cid) { - $urlQry['cid'] = $this->_cid; - CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/contact/deduperules', - $urlQry - )); - } - else { - CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/contact/dedupefind', - $urlQry - )); - } + $ruleGroupName = civicrm_api3('RuleGroup', 'getvalue', array('id' => $rgid, 'return' => 'name')); + CRM_Core_Session::singleton()->setStatus(ts('No possible duplicates were found using %1 rule.', array(1 => $ruleGroupName)), ts('None Found'), 'info'); + $url = CRM_Utils_System::url('civicrm/contact/deduperules', 'reset=1'); + if ($context == 'search') { + $url = CRM_Core_Session::singleton()->readUserContext(); } + CRM_Utils_System::redirect($url); } else { - if ($cid) { - $this->_cid = $cid; + $urlQry['action'] = 'update'; + if ($this->_cid) { + $urlQry['cid'] = $this->_cid; + CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/contact/deduperules', + $urlQry + )); } - if ($gid) { - $this->_gid = $gid; + else { + CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/contact/dedupefind', + $urlQry + )); } - $this->_rgid = $rgid; } $this->assign('action', $this->action); diff --git a/CRM/Dedupe/Merger.php b/CRM/Dedupe/Merger.php index 400d3364c6..a4770c4f27 100644 --- a/CRM/Dedupe/Merger.php +++ b/CRM/Dedupe/Merger.php @@ -645,7 +645,9 @@ INNER JOIN civicrm_membership membership2 ON membership1.membership_type_id = m } // else consider all dupe pairs // @todo Adding limit to Where??!! - $where .= " LIMIT {$batchLimit}"; + if ($batchLimit) { + $where .= " LIMIT {$batchLimit}"; + } return $where; } @@ -1868,6 +1870,7 @@ INNER JOIN civicrm_membership membership2 ON membership1.membership_type_id = m * @param bool $reloadCacheIfEmpty * @param int $batchLimit * @param bool $isSelected + * Limit to selected pairs. * @param array|string $orderByClause * @param bool $includeConflicts * @param array $criteria -- 2.25.1