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