Skip to content

Instantly share code, notes, and snippets.

@futurefirst
Created May 6, 2015 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save futurefirst/a4e1d4346fa99b50cb8e to your computer and use it in GitHub Desktop.
Save futurefirst/a4e1d4346fa99b50cb8e to your computer and use it in GitHub Desktop.
Extending a dedupe rule to change a condition to stipulate a specific value, and to add a condition on a subtype custom field
/**
* Implementation of hook_civicrm_dupeQuery
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_dupeQuery
*/
function extendeddedupe_civicrm_dupeQuery($obj, $type, &$query) {
if ($obj->id != STUDENT_DEDUPE_RULE_ID || $type != 'table') {
return;
}
watchdog(
'Extended Dedupe',
'dupeQuery before: obj :obj, type :type, query :query',
array(
':obj' => print_r($obj, TRUE),
':type' => $type,
':query' => print_r($query, TRUE),
),
WATCHDOG_DEBUG
);
// Change the contact subtype condition (if present) to say that they must be Students
$subtypeQuery = CRM_Utils_Array::value('civicrm_contact.contact_sub_type.10', $query);
if (!empty($subtypeQuery)) {
$subtypeQuery = str_replace(
"contact_sub_type IS NOT NULL",
"contact_sub_type LIKE '%Student%'",
$subtypeQuery
);
$query['civicrm_contact.contact_sub_type.10'] = $subtypeQuery;
}
// Add a condition that says their schools must match
$schoolQuery = "
SELECT t1.entity_id id1,
t2.entity_id id2,
10 weight
FROM civicrm_value_contact_reference_9 t1
JOIN civicrm_value_contact_reference_9 t2
USING (contact_reference_21)
WHERE t1.entity_id < t2.entity_id
AND t1.contact_reference_21 IS NOT NULL
";
// Was a group specified for this execution of the rule?
if (!empty($obj->contactIds)) {
foreach ($obj->contactIds as $cid) {
CRM_Utils_Type::validate($cid, 'Positive');
}
$implodedIds = implode(',', $obj->contactIds);
$schoolQuery .= "
AND (t1.entity_id IN ($implodedIds)
OR t2.entity_id IN ($implodedIds))
";
}
$query['civicrm_value_contact_reference_9.contact_reference_21.10'] = $schoolQuery;
watchdog(
'Extended Dedupe',
'dupeQuery after: obj :obj, type :type, query :query',
array(
':obj' => print_r($obj, TRUE),
':type' => $type,
':query' => print_r($query, TRUE),
),
WATCHDOG_DEBUG
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment