3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * The CiviCRM duplicate discovery engine is based on an
20 * algorithm designed by David Strauss <david@fourkitchens.com>.
22 class CRM_Dedupe_BAO_DedupeRule
extends CRM_Dedupe_DAO_DedupeRule
{
25 * Ids of the contacts to limit the SQL queries (whole-database queries otherwise)
28 public $contactIds = [];
31 * Params to dedupe against (queries against the whole contact set otherwise)
37 * Return the SQL query for the given rule - either for finding matching
38 * pairs of contacts, or for matching against the $params variable (if set).
41 * SQL query performing the search
43 * @throws \CRM_Core_Exception
44 * @throws \CiviCRM_API3_Exception
46 public function sql() {
48 (!array_key_exists($this->rule_table
, $this->params
) ||
49 !array_key_exists($this->rule_field
, $this->params
[$this->rule_table
])
52 // if params is present and doesn't have an entry for a field, don't construct the clause.
56 // we need to initialise WHERE, ON and USING here, as some table types
57 // extend them; $where is an array of required conditions, $on and
58 // $using are arrays of required field matchings (for substring and
59 // full matches, respectively)
61 $on = ["SUBSTR(t1.{$this->rule_field}, 1, {$this->rule_length}) = SUBSTR(t2.{$this->rule_field}, 1, {$this->rule_length})"];
64 "t1.{$this->rule_field} IS NOT NULL",
65 "t2.{$this->rule_field} IS NOT NULL",
66 "t1.{$this->rule_field} = t2.{$this->rule_field}",
69 if (in_array($this->getFieldType($this->rule_field
), CRM_Utils_Type
::getTextTypes(), TRUE)) {
70 $innerJoinClauses[] = "t1.{$this->rule_field} <> ''";
71 $innerJoinClauses[] = "t2.{$this->rule_field} <> ''";
74 switch ($this->rule_table
) {
75 case 'civicrm_contact':
77 //we should restrict by contact type in the first step
78 $sql = "SELECT contact_type FROM civicrm_dedupe_rule_group WHERE id = {$this->dedupe_rule_group_id};";
79 $ct = CRM_Core_DAO
::singleValueQuery($sql);
81 $where[] = "t1.contact_type = '{$ct}'";
84 $where[] = "t1.contact_type = '{$ct}'";
85 $where[] = "t2.contact_type = '{$ct}'";
89 case 'civicrm_address':
92 case 'civicrm_openid':
100 $where[] = "t1.entity_table = 'civicrm_contact'";
103 $where[] = "t1.entity_table = 'civicrm_contact'";
104 $where[] = "t2.entity_table = 'civicrm_contact'";
109 // custom data tables
110 if (preg_match('/^civicrm_value_/', $this->rule_table
) ||
preg_match('/^custom_value_/', $this->rule_table
)) {
114 throw new CRM_Core_Exception("Unsupported rule_table for civicrm_dedupe_rule.id of {$this->id}");
119 // build SELECT based on the field names containing contact ids
120 // if there are params provided, id1 should be 0
122 $select = "t1.$id id1, {$this->rule_weight} weight";
123 $subSelect = 'id1, weight';
126 $select = "t1.$id id1, t2.$id id2, {$this->rule_weight} weight";
127 $subSelect = 'id1, id2, weight';
130 // build FROM (and WHERE, if it's a parametrised search)
131 // based on whether the rule is about substrings or not
133 $from = "{$this->rule_table} t1";
135 if (isset($this->params
[$this->rule_table
][$this->rule_field
])) {
136 $str = trim(CRM_Utils_Type
::escape($this->params
[$this->rule_table
][$this->rule_field
], 'String'));
138 if ($this->rule_length
) {
139 $where[] = "SUBSTR(t1.{$this->rule_field}, 1, {$this->rule_length}) = SUBSTR('$str', 1, {$this->rule_length})";
140 $where[] = "t1.{$this->rule_field} IS NOT NULL";
143 $where[] = "t1.{$this->rule_field} = '$str'";
147 if ($this->rule_length
) {
148 $from = "{$this->rule_table} t1 JOIN {$this->rule_table} t2 ON (" . implode(' AND ', $on) . ")";
151 $from = "{$this->rule_table} t1 INNER JOIN {$this->rule_table} t2 ON (" . implode(' AND ', $innerJoinClauses) . ")";
155 // finish building WHERE, also limit the results if requested
156 if (!$this->params
) {
157 $where[] = "t1.$id < t2.$id";
159 $query = "SELECT $select FROM $from WHERE " . implode(' AND ', $where);
160 if ($this->contactIds
) {
162 foreach ($this->contactIds
as $cid) {
163 $cids[] = CRM_Utils_Type
::escape($cid, 'Integer');
165 if (count($cids) == 1) {
166 $query .= " AND (t1.$id = {$cids[0]}) UNION $query AND t2.$id = {$cids[0]}";
169 $query .= " AND t1.$id IN (" . implode(',', $cids) . ")
170 UNION $query AND t2.$id IN (" . implode(',', $cids) . ")";
172 // The `weight` is ambiguous in the context of the union; put the whole
173 // thing in a subquery.
174 $query = "SELECT $subSelect FROM ($query) subunion";
181 * find fields related to a rule group.
183 * @param array $params contains the rule group property to identify rule group
186 * rule fields array associated to rule group
188 public static function dedupeRuleFields($params) {
189 $rgBao = new CRM_Dedupe_BAO_DedupeRuleGroup();
190 $rgBao->used
= $params['used'];
191 $rgBao->contact_type
= $params['contact_type'];
194 $ruleBao = new CRM_Dedupe_BAO_DedupeRule();
195 $ruleBao->dedupe_rule_group_id
= $rgBao->id
;
198 while ($ruleBao->fetch()) {
199 $field_name = $ruleBao->rule_field
;
200 if ($field_name == 'phone_numeric') {
201 $field_name = 'phone';
203 $ruleFields[] = $field_name;
214 public static function validateContacts($cid, $oid) {
215 if (!$cid ||
!$oid) {
218 $exception = new CRM_Dedupe_DAO_DedupeException();
219 $exception->contact_id1
= $cid;
220 $exception->contact_id2
= $oid;
221 //make sure contact2 > contact1.
223 $exception->contact_id1
= $oid;
224 $exception->contact_id2
= $cid;
227 return !$exception->find(TRUE);
231 * Get the specification for the given field.
233 * @param string $fieldName
236 * @throws \CiviCRM_API3_Exception
238 public function getFieldType($fieldName) {
239 $entity = CRM_Core_DAO_AllCoreTables
::getBriefName(CRM_Core_DAO_AllCoreTables
::getClassForTable($this->rule_table
));
241 // This means we have stored a custom field rather than an entity name in rule_table, figure out the entity.
242 $entity = civicrm_api3('CustomGroup', 'getvalue', ['table_name' => $this->rule_table
, 'return' => 'extends']);
243 if (in_array($entity, ['Individual', 'Household', 'Organization'])) {
246 $fieldName = 'custom_' . civicrm_api3('CustomField', 'getvalue', ['column_name' => $fieldName, 'return' => 'id']);
248 $fields = civicrm_api3($entity, 'getfields', ['action' => 'create'])['values'];
249 return $fields[$fieldName]['type'];