Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | ||
3 | // TODO: How to handle NULL values/records? | |
4 | class CRM_Dedupe_BAO_QueryBuilder_IndividualGeneral extends CRM_Dedupe_BAO_QueryBuilder { | |
5 | static function record($rg) { | |
6 | $civicrm_contact = CRM_Utils_Array::value('civicrm_contact', $rg->params); | |
7 | $civicrm_address = CRM_Utils_Array::value('civicrm_address', $rg->params); | |
8 | ||
9 | // Since definitely have first and last name, escape them upfront. | |
10 | $first_name = CRM_Core_DAO::escapeString(CRM_Utils_Array::value('first_name', $civicrm_contact, '')); | |
11 | $last_name = CRM_Core_DAO::escapeString(CRM_Utils_Array::value('last_name', $civicrm_contact, '')); | |
12 | $street_address = CRM_Core_DAO::escapeString(CRM_Utils_Array::value('street_address', $civicrm_address, '')); | |
13 | ||
14 | $query = " | |
15 | SELECT contact1.id id1, {$rg->threshold} as weight | |
16 | FROM civicrm_contact AS contact1 | |
17 | JOIN civicrm_address AS address1 ON contact1.id=address1.contact_id | |
18 | WHERE contact1.contact_type = 'Individual' | |
19 | AND contact1.first_name = '$first_name' | |
20 | AND contact1.last_name = '$last_name' | |
21 | AND address1.street_address = '$street_address' | |
22 | "; | |
23 | ||
24 | if ($birth_date = CRM_Core_DAO::escapeString(CRM_Utils_Array::value('birth_date', $civicrm_contact, ''))) { | |
25 | $query .= " AND (contact1.birth_date IS NULL or contact1.birth_date = '$birth_date')\n"; | |
26 | } | |
27 | ||
28 | if ($suffix_id = CRM_Core_DAO::escapeString(CRM_Utils_Array::value('suffix_id', $civicrm_contact, ''))) { | |
29 | $query .= " AND (contact1.suffix_id IS NULL or contact1.suffix_id = $suffix_id)\n"; | |
30 | } | |
31 | ||
32 | if ($middle_name = CRM_Core_DAO::escapeString(CRM_Utils_Array::value('middle_name', $civicrm_contact, ''))) { | |
33 | $query .= " AND (contact1.middle_name IS NULL or contact1.middle_name = '$middle_name')\n"; | |
34 | } | |
35 | ||
36 | return array("civicrm_contact.{$rg->name}.{$rg->threshold}" => $query); | |
37 | } | |
38 | ||
39 | static function internal($rg) { | |
40 | $query = " | |
41 | SELECT contact1.id id1, contact2.id id2, {$rg->threshold} weight | |
42 | FROM civicrm_contact AS contact1 | |
43 | JOIN civicrm_contact AS contact2 ON ( | |
44 | contact1.first_name = contact2.first_name AND | |
45 | contact1.last_name = contact2.last_name AND | |
46 | contact1.contact_type = contact2.contact_type) | |
47 | JOIN civicrm_address AS address1 ON address1.contact_id = contact1.id | |
48 | JOIN civicrm_address AS address2 ON ( | |
49 | address2.contact_id = contact2.id AND | |
50 | address2.street_address = address1.street_address) | |
51 | WHERE contact1.contact_type = 'Individual' | |
52 | AND (contact1.suffix_id IS NULL OR contact2.suffix_id IS NULL OR contact1.suffix_id = contact2.suffix_id) | |
53 | AND (contact1.middle_name IS NULL OR contact2.middle_name IS NULL OR contact1.middle_name = contact2.middle_name) | |
54 | AND (contact1.birth_date IS NULL OR contact2.birth_date IS NULL OR contact1.birth_date = contact2.birth_date) | |
55 | AND " . self::internalFilters($rg); | |
56 | return array("civicrm_contact.{$rg->name}.{$rg->threshold}" => $query); | |
57 | } | |
58 | } | |
59 | ||
60 | ||
61 |