function redhen_dedupe_get_duplicates in RedHen CRM 7
Same name and namespace in other branches
- 8 modules/redhen_dedupe/redhen_dedupe.module \redhen_dedupe_get_duplicates()
Get duplicate contacts.
Return value
array Array of objects containing first, last, and ids.
1 call to redhen_dedupe_get_duplicates()
- redhen_dedupe_list_page in modules/
redhen_dedupe/ redhen_dedupe.module - Page callback for listing duplicate contacts.
File
- modules/
redhen_dedupe/ redhen_dedupe.module, line 211
Code
function redhen_dedupe_get_duplicates($properties, $fields = array(), $active = TRUE) {
$query = db_select('redhen_contact', 'rc');
$query
->addTag('redhen_dedupe');
$query
->addExpression('COUNT(*)', 'count');
// PostGres doesn't support GROUP_CONCAT and aliases can't be used in
// conditional clauses so we need to repeat the aggregate function.
if (db_driver() == 'pgsql') {
$query
->addExpression('array_to_string(array_agg(contact_id), \',\')', 'ids');
$query
->having('COUNT(*) > 1');
}
else {
$query
->addExpression('GROUP_CONCAT(contact_id SEPARATOR \',\')', 'ids');
$query
->havingCondition('count', '1', '>');
}
if ($active) {
$query
->condition('redhen_state', '1');
}
foreach ($properties as $property) {
$query
->addField('rc', $property);
$query
->groupBy($property);
}
// To prevent adding the same field twice, we loop through all fields:
$join_fields = array();
foreach ($fields as $field) {
$field_pieces = explode(':', $field);
$field = array_shift($field_pieces);
$join_fields[$field] = $field;
}
foreach ($join_fields as $field) {
$query
->leftJoin('field_data_' . $field, $field, $field . '.entity_id = rc.contact_id');
}
foreach ($fields as $field) {
$field_pieces = explode(':', $field);
$field = array_shift($field_pieces);
$info = field_info_field($field);
if (count($field_pieces)) {
$column = reset($field_pieces);
}
else {
reset($info['columns']);
$column = key($info['columns']);
}
$query
->addField($field, $field . '_' . $column);
$query
->groupBy($field . '_' . $column);
}
$query
->orderBy('count', 'DESC');
return $query
->execute()
->fetchAll();
}