This is preliminary tidy up towards CRM-18443. There is no functional change, just the extraction of the definition
of the cacheKeyString & the joinClause. Extracting the join clause does not shorten it but it makes it
clear the clause is the same, ensures updates are done in all places & hopefully leads to the join being added
somewhere more sensible later - ie. instead of passing the join clause in call the same function, or a wrapper function
with $isExcludeDedupeExclusions as a paramter.
CRM_Core_Error::statusBounce(ts('The selected pair of contacts are marked as non duplicates. If these records should be merged, you can remove this exception on the <a href="%1">Dedupe Exceptions</a> page.', array(1 => CRM_Utils_System::url('civicrm/dedupe/exception', 'reset=1'))));
}
- //load cache mechanism
- $contactType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'contact_type');
- $cacheKey = "merge $contactType";
- $cacheKey .= $rgid ? "_{$rgid}" : '_0';
- $cacheKey .= $gid ? "_{$gid}" : '_0';
-
- $join = "LEFT JOIN civicrm_dedupe_exception de ON ( pn.entity_id1 = de.contact_id1 AND
- pn.entity_id2 = de.contact_id2 )";
+ $cacheKey = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid);
+
+ $join = CRM_Dedupe_Merger::getJoinOnDedupeTable();
$where = "de.id IS NULL";
$pos = CRM_Core_BAO_PrevNextCache::getPositions($cacheKey, $cid, $oid, $this->_mergeId, $join, $where, $flip);
}
if ($this->next && $this->_mergeId) {
- $cacheKey = "merge {$this->_contactType}";
- $cacheKey .= $this->_rgid ? "_{$this->_rgid}" : '_0';
- $cacheKey .= $this->_gid ? "_{$this->_gid}" : '_0';
+ $cacheKey = CRM_Dedupe_Merger::getMergeCacheKeyString($this->_rgid, $this->_gid);
- $join = "LEFT JOIN civicrm_dedupe_exception de ON ( pn.entity_id1 = de.contact_id1 AND
- pn.entity_id2 = de.contact_id2 )";
+ $join = CRM_Dedupe_Merger::getJoinOnDedupeTable();
$where = "de.id IS NULL";
$pos = CRM_Core_BAO_PrevNextCache::getPositions($cacheKey, NULL, NULL, $this->_mergeId, $join, $where);
$pnid = $_REQUEST['pnid'];
$isSelected = CRM_Utils_Type::escape($_REQUEST['is_selected'], 'Boolean');
- $contactType = CRM_Core_DAO::getFieldValue('CRM_Dedupe_DAO_RuleGroup', $rgid, 'contact_type');
- $cacheKeyString = "merge $contactType";
- $cacheKeyString .= $rgid ? "_{$rgid}" : '_0';
- $cacheKeyString .= $gid ? "_{$gid}" : '_0';
+ $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid);
$params = array(
1 => array($isSelected, 'Boolean'),
$rgid = CRM_Utils_Request::retrieve('rgid', 'Positive', $this, FALSE, 0);
if ($rgid) {
- $contactType = CRM_Core_DAO::getFieldValue('CRM_Dedupe_DAO_RuleGroup', $rgid, 'contact_type');
- $cacheKeyString = "merge $contactType";
- $cacheKeyString .= $rgid ? "_{$rgid}" : '_0';
- $cacheKeyString .= $gid ? "_{$gid}" : '_0';
- CRM_Core_BAO_PrevNextCache::deleteItem(NULL, $cacheKeyString);
+ CRM_Core_BAO_PrevNextCache::deleteItem(NULL, CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid));
}
$urlQry = "reset=1&action=update&rgid={$rgid}";
if ($gid) {
$rgid = CRM_Utils_Request::retrieve('rgid', 'Positive', $this, FALSE, 0);
$this->action = CRM_Core_Action::UPDATE;
- //calculate the $contactType
- if ($rgid) {
- $contactType = CRM_Core_DAO::getFieldValue('CRM_Dedupe_DAO_RuleGroup',
- $rgid,
- 'contact_type'
- );
- }
-
$sourceParams = 'snippet=4';
if ($gid) {
$sourceParams .= "&gid={$gid}";
$this->assign('sourceUrl', CRM_Utils_System::url('civicrm/ajax/dedupefind', $sourceParams, FALSE, NULL, FALSE));
//reload from cache table
- $cacheKeyString = "merge $contactType";
- $cacheKeyString .= $rgid ? "_{$rgid}" : '_0';
- $cacheKeyString .= $gid ? "_{$gid}" : '_0';
+ $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid);
$stats = CRM_Dedupe_Merger::getMergeStatsMsg($cacheKeyString);
if ($stats) {
// reset so we not displaying same message again
CRM_Dedupe_Merger::resetMergeStats($cacheKeyString);
}
- $join = "LEFT JOIN civicrm_dedupe_exception de ON ( pn.entity_id1 = de.contact_id1 AND
- pn.entity_id2 = de.contact_id2 )";
+ $join = CRM_Dedupe_Merger::getJoinOnDedupeTable();
$where = "de.id IS NULL";
if ($context == 'conflicts') {
$where .= " AND pn.is_selected = 1";
$action = CRM_Utils_Request::retrieve('action', 'String', CRM_Core_DAO::$_nullObject);
$mode = CRM_Utils_Request::retrieve('mode', 'String', CRM_Core_DAO::$_nullObject, FALSE, 'safe');
- $contactType = CRM_Core_DAO::getFieldValue('CRM_Dedupe_DAO_RuleGroup', $rgid, 'contact_type');
- $cacheKeyString = "merge {$contactType}";
- $cacheKeyString .= $rgid ? "_{$rgid}" : '_0';
- $cacheKeyString .= $gid ? "_{$gid}" : '_0';
+ $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid);
$urlQry = "reset=1&action=update&rgid={$rgid}";
$urlQry = $gid ? ($urlQry . "&gid={$gid}") : $urlQry;
*/
public static function refillCache($rgid = NULL, $gid = NULL, $cacheKeyString = NULL) {
if (!$cacheKeyString && $rgid) {
- $contactType = CRM_Core_DAO::getFieldValue('CRM_Dedupe_DAO_RuleGroup', $rgid, 'contact_type');
- $cacheKeyString = "merge {$contactType}";
- $cacheKeyString .= $rgid ? "_{$rgid}" : '_0';
- $cacheKeyString .= $gid ? "_{$gid}" : '_0';
+ $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid);
}
if (!$cacheKeyString) {
return $result;
}
+
+ /**
+ * Get the cached contact type for a particular rule group.
+ *
+ * @param int $rule_group_id
+ *
+ * @return string
+ */
+ public static function getContactTypeForRuleGroup($rule_group_id) {
+ if (!isset(\Civi::$statics[__CLASS__]) || !isset(\Civi::$statics[__CLASS__]['rule_groups'])) {
+ \Civi::$statics[__CLASS__]['rule_groups'] = array();
+ }
+ if (empty(\Civi::$statics[__CLASS__]['rule_groups'][$rule_group_id])) {
+ \Civi::$statics[__CLASS__]['rule_groups'][$rule_group_id]['contact_type'] = CRM_Core_DAO::getFieldValue(
+ 'CRM_Dedupe_DAO_RuleGroup',
+ $rule_group_id,
+ 'contact_type'
+ );
+ }
+
+ return \Civi::$statics[__CLASS__]['rule_groups'][$rule_group_id]['contact_type'];
+ }
+
}
* @return array|bool
*/
public static function batchMerge($rgid, $gid = NULL, $mode = 'safe', $autoFlip = TRUE, $batchLimit = 1, $isSelected = 2) {
- $contactType = CRM_Core_DAO::getFieldValue('CRM_Dedupe_DAO_RuleGroup', $rgid, 'contact_type');
- $cacheKeyString = "merge {$contactType}";
- $cacheKeyString .= $rgid ? "_{$rgid}" : '_0';
- $cacheKeyString .= $gid ? "_{$gid}" : '_0';
- $join = "LEFT JOIN civicrm_dedupe_exception de ON ( pn.entity_id1 = de.contact_id1 AND
- pn.entity_id2 = de.contact_id2 )";
+ $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid);
+ $join = CRM_Dedupe_Merger::getJoinOnDedupeTable();
$where = "de.id IS NULL";
if ($isSelected === 0 || $isSelected === 1) {
return CRM_Dedupe_Merger::merge($dupePairs, $cacheParams, $mode, $autoFlip, $redirectForPerformance);
}
+ /**
+ * Get the string to join the prevnext cache to the dedupe table.
+ *
+ * @return string
+ * The join string to join prevnext cache on the dedupe table.
+ */
+ public static function getJoinOnDedupeTable() {
+ return "
+ LEFT JOIN civicrm_dedupe_exception de
+ ON (
+ pn.entity_id1 = de.contact_id1
+ AND pn.entity_id2 = de.contact_id2 )
+ ";
+ }
+
public static function updateMergeStats($cacheKeyString, $result = array()) {
// gather latest stats
$merged = count($result['merged']);
}
}
+ /**
+ * Get the cache key string for the merge action.
+ *
+ * @param int $rule_group_id
+ * @param int $group_id
+ *
+ * @return string
+ */
+ public static function getMergeCacheKeyString($rule_group_id, $group_id) {
+ $contactType = CRM_Dedupe_BAO_RuleGroup::getContactTypeForRuleGroup($rule_group_id);
+ $cacheKeyString = "merge {$contactType}";
+ $cacheKeyString .= $rule_group_id ? "_{$rule_group_id}" : '_0';
+ $cacheKeyString .= $group_id ? "_{$group_id}" : '_0';
+ return $cacheKeyString;
+ }
+
}