Index: civicrm/trunk/sites/all/modules/civimerge/civimerge.module |
— | — | @@ -75,14 +75,15 @@ |
76 | 76 | |
77 | 77 | function civimerge_block_content_similar($cid, $raw = TRUE) { |
78 | 78 | civicrm_initialize(true); |
79 | | - |
80 | 79 | require_once 'CRM/Dedupe/Finder.php'; |
81 | 80 | require_once 'CRM/Contact/DAO/Contact.php'; |
82 | 81 | |
83 | 82 | $content = array(); |
84 | 83 | |
85 | 84 | $start = microtime(TRUE); |
86 | | - $similar = CRM_Dedupe_Finder::dupesOfContact($cid, 'Strict'); |
| 85 | + |
| 86 | + $similar = civicluster_get_similar($primary_cid); |
| 87 | + //$similar = CRM_Dedupe_Finder::dupesOfContact($cid, 'Strict'); |
87 | 88 | //$content[] = '<div>Similarity check: ' . round(microtime(TRUE) - $start, 2) . 's</div>'; |
88 | 89 | |
89 | 90 | if (!empty($similar)) { |
— | — | @@ -200,3 +201,102 @@ |
201 | 202 | 'query' => 'reset=1&cid=' . $cid . '&oid=' . $oid |
202 | 203 | ); |
203 | 204 | } |
| 205 | + |
| 206 | +function civicluster_get_similar($primary_cid) |
| 207 | +{ |
| 208 | + return _civicluster_get_similar_rough($primary_cid, 10); |
| 209 | +} |
| 210 | + |
| 211 | +function _civicluster_get_similar_rough($primary_cid, $min_score) |
| 212 | +{ |
| 213 | + $similar = array(); |
| 214 | + $similar = _civicluster_update_scores($similar, _civicluster_get_similar_location($primary_cid, 'civicrm_address', 'street_address'), 10); |
| 215 | + //$similar = _civicluster_update_scores($similar, _civicluster_get_similar_location($primary_cid, 'civicrm_address', 'city')); |
| 216 | + $similar = _civicluster_update_scores($similar, _civicluster_get_similar_location($primary_cid, 'civicrm_email', 'email'), 20); |
| 217 | + $similar = _civicluster_update_scores($similar, _civicluster_get_similar_location($primary_cid, 'civicrm_phone', 'phone'), 10); |
| 218 | + $similar = _civicluster_update_scores($similar, _civicluster_get_similar_name($primary_cid, 6), 2); |
| 219 | + $similar = _civicluster_update_scores($similar, _civicluster_get_similar_name($primary_cid, 8), 3); |
| 220 | + $similar = _civicluster_update_scores($similar, _civicluster_get_similar_name($primary_cid, 10), 4); |
| 221 | + $similar = _civicluster_update_scores($similar, _civicluster_get_similar_name($primary_cid, 12), 5); |
| 222 | + |
| 223 | + //drupal_set_message(print_r($similar, TRUE)); |
| 224 | + |
| 225 | + $similar_cids = array(); |
| 226 | + foreach($similar as $similar_cid => $score) |
| 227 | + { |
| 228 | + if ($score >= $min_score && $similar_cid != $primary_cid && !civicluster_are_separated($primary_cid, $similar_cid)) |
| 229 | + { |
| 230 | + $similar_cids[] = $similar_cid; |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + //drupal_set_message(print_r($similar_cids, TRUE)); |
| 235 | + |
| 236 | + return $similar_cids; |
| 237 | +} |
| 238 | + |
| 239 | +function _civicluster_get_similar_name($cid, $characters = 100) |
| 240 | +{ |
| 241 | + if (!is_numeric($cid)) |
| 242 | + return array(); |
| 243 | + |
| 244 | + $sql = 'SELECT sort_name FROM civicrm_contact WHERE id = ' . $cid; |
| 245 | + $res =& CRM_Core_DAO::executeQuery( $sql, CRM_Core_DAO::$_nullArray ); |
| 246 | + $row = $res->fetch(); |
| 247 | + $sort_name = $row['sort_name']; |
| 248 | + |
| 249 | + $sql = 'SELECT id FROM civicrm_contact far WHERE sort_name LIKE "%1%%"'; |
| 250 | + $sort_name = substr($sort_name, 0, $characters); |
| 251 | + $params = array(1 => array($sort_name, 'String')); |
| 252 | + $res =& CRM_Core_DAO::executeQuery($sql, $params); |
| 253 | + |
| 254 | + $ret = array(); |
| 255 | + while ($res->fetch()) |
| 256 | + { |
| 257 | + $ret[] = $res->id; |
| 258 | + } |
| 259 | + |
| 260 | + //drupal_set_message(print_r($ret, TRUE)); |
| 261 | + |
| 262 | + return $ret; |
| 263 | +} |
| 264 | + |
| 265 | +function _civicluster_get_similar_location($cid, $table, $field) |
| 266 | +{ |
| 267 | + if (!is_numeric($cid)) |
| 268 | + return array(); |
| 269 | + |
| 270 | + $sql = 'SELECT farl.entity_id AS contact_id FROM civicrm_location farl '; |
| 271 | + $sql .= 'INNER JOIN ' . $table . ' fara ON fara.location_id = farl.id '; |
| 272 | + $sql .= 'INNER JOIN ' . $table . ' neara ON fara.' . $field . ' = neara.' . $field . ' '; |
| 273 | + $sql .= 'INNER JOIN civicrm_location nearl ON neara.location_id = nearl.id '; |
| 274 | + $sql .= 'WHERE farl.entity_table = "civicrm_contact" AND nearl.entity_table = "civicrm_contact" AND nearl.entity_id = ' . $cid; |
| 275 | + //drupal_set_message($sql); |
| 276 | + $res =& CRM_Core_DAO::executeQuery( $sql, CRM_Core_DAO::$_nullArray ); |
| 277 | + $ret = array(); |
| 278 | + while ($res->fetch()) |
| 279 | + { |
| 280 | + $ret[] = $res->contact_id; |
| 281 | + } |
| 282 | + |
| 283 | + //drupal_set_message(print_r($ret, TRUE)); |
| 284 | + //echo 'Location ' . $table . '.' . $field . ':' . "\n"; |
| 285 | + //print_r($ret); |
| 286 | + |
| 287 | + return $ret; |
| 288 | +} |
| 289 | + |
| 290 | +function _civicluster_update_scores($existing, $new, $points = 1) |
| 291 | +{ |
| 292 | + foreach ($new as $cid) |
| 293 | + { |
| 294 | + if (array_key_exists($cid, $existing)) |
| 295 | + { |
| 296 | + $existing[$cid] += $points; |
| 297 | + } else { |
| 298 | + $existing[$cid] = $points; |
| 299 | + } |
| 300 | + } |
| 301 | + |
| 302 | + return $existing; |
| 303 | +} |
\ No newline at end of file |