Merge pull request #16577 from civicrm/5.23
[civicrm-core.git] / CRM / Dedupe / BAO / Rule.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 * $Id$
17 *
18 */
19
20 /**
21 * The CiviCRM duplicate discovery engine is based on an
22 * algorithm designed by David Strauss <david@fourkitchens.com>.
23 */
24 class CRM_Dedupe_BAO_Rule extends CRM_Dedupe_DAO_Rule {
25
26 /**
27 * Ids of the contacts to limit the SQL queries (whole-database queries otherwise)
28 * @var array
29 */
30 public $contactIds = [];
31
32 /**
33 * Params to dedupe against (queries against the whole contact set otherwise)
34 * @var array
35 */
36 public $params = [];
37
38 /**
39 * Return the SQL query for the given rule - either for finding matching
40 * pairs of contacts, or for matching against the $params variable (if set).
41 *
42 * @return string
43 * SQL query performing the search
44 *
45 * @throws \CRM_Core_Exception
46 * @throws \CiviCRM_API3_Exception
47 */
48 public function sql() {
49 if ($this->params &&
50 (!array_key_exists($this->rule_table, $this->params) ||
51 !array_key_exists($this->rule_field, $this->params[$this->rule_table])
52 )
53 ) {
54 // if params is present and doesn't have an entry for a field, don't construct the clause.
55 return NULL;
56 }
57
58 // we need to initialise WHERE, ON and USING here, as some table types
59 // extend them; $where is an array of required conditions, $on and
60 // $using are arrays of required field matchings (for substring and
61 // full matches, respectively)
62 $where = [];
63 $on = ["SUBSTR(t1.{$this->rule_field}, 1, {$this->rule_length}) = SUBSTR(t2.{$this->rule_field}, 1, {$this->rule_length})"];
64
65 $innerJoinClauses = [
66 "t1.{$this->rule_field} IS NOT NULL",
67 "t2.{$this->rule_field} IS NOT NULL",
68 "t1.{$this->rule_field} = t2.{$this->rule_field}",
69 ];
70 if ($this->getFieldType($this->rule_field) === CRM_Utils_Type::T_DATE) {
71 $innerJoinClauses[] = "t1.{$this->rule_field} > '1000-01-01'";
72 $innerJoinClauses[] = "t2.{$this->rule_field} > '1000-01-01'";
73 }
74 else {
75 $innerJoinClauses[] = "t1.{$this->rule_field} <> ''";
76 $innerJoinClauses[] = "t2.{$this->rule_field} <> ''";
77 }
78
79 switch ($this->rule_table) {
80 case 'civicrm_contact':
81 $id = 'id';
82 //we should restrict by contact type in the first step
83 $sql = "SELECT contact_type FROM civicrm_dedupe_rule_group WHERE id = {$this->dedupe_rule_group_id};";
84 $ct = CRM_Core_DAO::singleValueQuery($sql);
85 if ($this->params) {
86 $where[] = "t1.contact_type = '{$ct}'";
87 }
88 else {
89 $where[] = "t1.contact_type = '{$ct}'";
90 $where[] = "t2.contact_type = '{$ct}'";
91 }
92 break;
93
94 case 'civicrm_address':
95 $id = 'contact_id';
96 $on[] = 't1.location_type_id = t2.location_type_id';
97 $innerJoinClauses[] = 't1.location_type_id = t2.location_type_id';
98 if (!empty($this->params['civicrm_address']['location_type_id'])) {
99 $locTypeId = CRM_Utils_Type::escape($this->params['civicrm_address']['location_type_id'], 'Integer', FALSE);
100 if ($locTypeId) {
101 $where[] = "t1.location_type_id = $locTypeId";
102 }
103 }
104 break;
105
106 case 'civicrm_email':
107 case 'civicrm_im':
108 case 'civicrm_openid':
109 case 'civicrm_phone':
110 $id = 'contact_id';
111 break;
112
113 case 'civicrm_note':
114 $id = 'entity_id';
115 if ($this->params) {
116 $where[] = "t1.entity_table = 'civicrm_contact'";
117 }
118 else {
119 $where[] = "t1.entity_table = 'civicrm_contact'";
120 $where[] = "t2.entity_table = 'civicrm_contact'";
121 }
122 break;
123
124 default:
125 // custom data tables
126 if (preg_match('/^civicrm_value_/', $this->rule_table) || preg_match('/^custom_value_/', $this->rule_table)) {
127 $id = 'entity_id';
128 }
129 else {
130 throw new CRM_Core_Exception("Unsupported rule_table for civicrm_dedupe_rule.id of {$this->id}");
131 }
132 break;
133 }
134
135 // build SELECT based on the field names containing contact ids
136 // if there are params provided, id1 should be 0
137 if ($this->params) {
138 $select = "t1.$id id1, {$this->rule_weight} weight";
139 $subSelect = 'id1, weight';
140 }
141 else {
142 $select = "t1.$id id1, t2.$id id2, {$this->rule_weight} weight";
143 $subSelect = 'id1, id2, weight';
144 }
145
146 // build FROM (and WHERE, if it's a parametrised search)
147 // based on whether the rule is about substrings or not
148 if ($this->params) {
149 $from = "{$this->rule_table} t1";
150 $str = 'NULL';
151 if (isset($this->params[$this->rule_table][$this->rule_field])) {
152 $str = CRM_Utils_Type::escape($this->params[$this->rule_table][$this->rule_field], 'String');
153 }
154 if ($this->rule_length) {
155 $where[] = "SUBSTR(t1.{$this->rule_field}, 1, {$this->rule_length}) = SUBSTR('$str', 1, {$this->rule_length})";
156 $where[] = "t1.{$this->rule_field} IS NOT NULL";
157 }
158 else {
159 $where[] = "t1.{$this->rule_field} = '$str'";
160 }
161 }
162 else {
163 if ($this->rule_length) {
164 $from = "{$this->rule_table} t1 JOIN {$this->rule_table} t2 ON (" . implode(' AND ', $on) . ")";
165 }
166 else {
167 $from = "{$this->rule_table} t1 INNER JOIN {$this->rule_table} t2 ON (" . implode(' AND ', $innerJoinClauses) . ")";
168 }
169 }
170
171 // finish building WHERE, also limit the results if requested
172 if (!$this->params) {
173 $where[] = "t1.$id < t2.$id";
174 }
175 $query = "SELECT $select FROM $from WHERE " . implode(' AND ', $where);
176 if ($this->contactIds) {
177 $cids = [];
178 foreach ($this->contactIds as $cid) {
179 $cids[] = CRM_Utils_Type::escape($cid, 'Integer');
180 }
181 if (count($cids) == 1) {
182 $query .= " AND (t1.$id = {$cids[0]}) UNION $query AND t2.$id = {$cids[0]}";
183 }
184 else {
185 $query .= " AND t1.$id IN (" . implode(',', $cids) . ")
186 UNION $query AND t2.$id IN (" . implode(',', $cids) . ")";
187 }
188 // The `weight` is ambiguous in the context of the union; put the whole
189 // thing in a subquery.
190 $query = "SELECT $subSelect FROM ($query) subunion";
191 }
192
193 return $query;
194 }
195
196 /**
197 * find fields related to a rule group.
198 *
199 * @param array $params contains the rule group property to identify rule group
200 *
201 * @return array
202 * rule fields array associated to rule group
203 */
204 public static function dedupeRuleFields($params) {
205 $rgBao = new CRM_Dedupe_BAO_RuleGroup();
206 $rgBao->used = $params['used'];
207 $rgBao->contact_type = $params['contact_type'];
208 $rgBao->find(TRUE);
209
210 $ruleBao = new CRM_Dedupe_BAO_Rule();
211 $ruleBao->dedupe_rule_group_id = $rgBao->id;
212 $ruleBao->find();
213 $ruleFields = [];
214 while ($ruleBao->fetch()) {
215 $field_name = $ruleBao->rule_field;
216 if ($field_name == 'phone_numeric') {
217 $field_name = 'phone';
218 }
219 $ruleFields[] = $field_name;
220 }
221 return $ruleFields;
222 }
223
224 /**
225 * @param int $cid
226 * @param int $oid
227 *
228 * @return bool
229 */
230 public static function validateContacts($cid, $oid) {
231 if (!$cid || !$oid) {
232 return NULL;
233 }
234 $exception = new CRM_Dedupe_DAO_Exception();
235 $exception->contact_id1 = $cid;
236 $exception->contact_id2 = $oid;
237 //make sure contact2 > contact1.
238 if ($cid > $oid) {
239 $exception->contact_id1 = $oid;
240 $exception->contact_id2 = $cid;
241 }
242
243 return $exception->find(TRUE) ? FALSE : TRUE;
244 }
245
246 /**
247 * Get the specification for the given field.
248 *
249 * @param string $fieldName
250 *
251 * @return array
252 * @throws \CiviCRM_API3_Exception
253 */
254 public function getFieldType($fieldName) {
255 $entity = CRM_Core_DAO_AllCoreTables::getBriefName(CRM_Core_DAO_AllCoreTables::getClassForTable($this->rule_table));
256 if (!$entity) {
257 // This means we have stored a custom field rather than an entity name in rule_table, figure out the entity.
258 $entity = civicrm_api3('CustomGroup', 'getvalue', ['table_name' => $this->rule_table, 'return' => 'extends']);
259 if (in_array($entity, ['Individual', 'Household', 'Organization'])) {
260 $entity = 'Contact';
261 }
262 $fieldName = 'custom_' . civicrm_api3('CustomField', 'getvalue', ['column_name' => $fieldName, 'return' => 'id']);
263 }
264 $fields = civicrm_api3($entity, 'getfields', ['action' => 'create'])['values'];
265 return $fields[$fieldName]['type'];
266 }
267
268 }