Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
f299f7db | 6 | | Copyright CiviCRM LLC (c) 2004-2020 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
f299f7db | 31 | * @copyright CiviCRM LLC (c) 2004-2020 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * The CiviCRM duplicate discovery engine is based on an | |
38 | * algorithm designed by David Strauss <david@fourkitchens.com>. | |
39 | */ | |
40 | class CRM_Dedupe_BAO_Rule extends CRM_Dedupe_DAO_Rule { | |
41 | ||
42 | /** | |
100fef9d | 43 | * Ids of the contacts to limit the SQL queries (whole-database queries otherwise) |
518fa0ee | 44 | * @var array |
6a488035 | 45 | */ |
518fa0ee | 46 | public $contactIds = []; |
6a488035 TO |
47 | |
48 | /** | |
100fef9d | 49 | * Params to dedupe against (queries against the whole contact set otherwise) |
518fa0ee | 50 | * @var array |
6a488035 | 51 | */ |
518fa0ee | 52 | public $params = []; |
6a488035 TO |
53 | |
54 | /** | |
55 | * Return the SQL query for the given rule - either for finding matching | |
56 | * pairs of contacts, or for matching against the $params variable (if set). | |
57 | * | |
a6c01b45 CW |
58 | * @return string |
59 | * SQL query performing the search | |
6a488035 | 60 | */ |
00be9182 | 61 | public function sql() { |
6a488035 TO |
62 | if ($this->params && |
63 | (!array_key_exists($this->rule_table, $this->params) || | |
64 | !array_key_exists($this->rule_field, $this->params[$this->rule_table]) | |
65 | ) | |
66 | ) { | |
67 | // if params is present and doesn't have an entry for a field, don't construct the clause. | |
68 | return NULL; | |
69 | } | |
70 | ||
71 | // we need to initialise WHERE, ON and USING here, as some table types | |
72 | // extend them; $where is an array of required conditions, $on and | |
73 | // $using are arrays of required field matchings (for substring and | |
74 | // full matches, respectively) | |
be2fb01f CW |
75 | $where = []; |
76 | $on = ["SUBSTR(t1.{$this->rule_field}, 1, {$this->rule_length}) = SUBSTR(t2.{$this->rule_field}, 1, {$this->rule_length})"]; | |
7965a7f1 PN |
77 | $entity = CRM_Core_DAO_AllCoreTables::getBriefName(CRM_Core_DAO_AllCoreTables::getClassForTable($this->rule_table)); |
78 | $fields = civicrm_api3($entity, 'getfields', ['action' => 'create'])['values']; | |
79 | ||
80 | $innerJoinClauses = [ | |
81 | "t1.{$this->rule_field} IS NOT NULL", | |
82 | "t2.{$this->rule_field} IS NOT NULL", | |
518fa0ee | 83 | "t1.{$this->rule_field} = t2.{$this->rule_field}", |
7965a7f1 PN |
84 | ]; |
85 | if ($fields[$this->rule_field]['type'] === CRM_Utils_Type::T_DATE) { | |
86 | $innerJoinClauses[] = "t1.{$this->rule_field} > '1000-01-01'"; | |
87 | $innerJoinClauses[] = "t2.{$this->rule_field} > '1000-01-01'"; | |
88 | } | |
89 | else { | |
90 | $innerJoinClauses[] = "t1.{$this->rule_field} <> ''"; | |
91 | $innerJoinClauses[] = "t2.{$this->rule_field} <> ''"; | |
92 | } | |
6a488035 TO |
93 | |
94 | switch ($this->rule_table) { | |
95 | case 'civicrm_contact': | |
96 | $id = 'id'; | |
97 | //we should restrict by contact type in the first step | |
98 | $sql = "SELECT contact_type FROM civicrm_dedupe_rule_group WHERE id = {$this->dedupe_rule_group_id};"; | |
99 | $ct = CRM_Core_DAO::singleValueQuery($sql); | |
100 | if ($this->params) { | |
101 | $where[] = "t1.contact_type = '{$ct}'"; | |
102 | } | |
103 | else { | |
104 | $where[] = "t1.contact_type = '{$ct}'"; | |
105 | $where[] = "t2.contact_type = '{$ct}'"; | |
106 | } | |
107 | break; | |
108 | ||
109 | case 'civicrm_address': | |
353ffa53 TO |
110 | $id = 'contact_id'; |
111 | $on[] = 't1.location_type_id = t2.location_type_id'; | |
2984eeb8 D |
112 | $innerJoinClauses[] = 't1.location_type_id = t2.location_type_id'; |
113 | if (!empty($this->params['civicrm_address']['location_type_id'])) { | |
6a488035 TO |
114 | $locTypeId = CRM_Utils_Type::escape($this->params['civicrm_address']['location_type_id'], 'Integer', FALSE); |
115 | if ($locTypeId) { | |
116 | $where[] = "t1.location_type_id = $locTypeId"; | |
117 | } | |
118 | } | |
119 | break; | |
120 | ||
121 | case 'civicrm_email': | |
122 | case 'civicrm_im': | |
123 | case 'civicrm_openid': | |
124 | case 'civicrm_phone': | |
125 | $id = 'contact_id'; | |
126 | break; | |
127 | ||
128 | case 'civicrm_note': | |
129 | $id = 'entity_id'; | |
130 | if ($this->params) { | |
131 | $where[] = "t1.entity_table = 'civicrm_contact'"; | |
132 | } | |
133 | else { | |
134 | $where[] = "t1.entity_table = 'civicrm_contact'"; | |
135 | $where[] = "t2.entity_table = 'civicrm_contact'"; | |
136 | } | |
137 | break; | |
138 | ||
139 | default: | |
140 | // custom data tables | |
141 | if (preg_match('/^civicrm_value_/', $this->rule_table) || preg_match('/^custom_value_/', $this->rule_table)) { | |
142 | $id = 'entity_id'; | |
143 | } | |
144 | else { | |
145 | CRM_Core_Error::fatal("Unsupported rule_table for civicrm_dedupe_rule.id of {$this->id}"); | |
146 | } | |
147 | break; | |
148 | } | |
149 | ||
150 | // build SELECT based on the field names containing contact ids | |
151 | // if there are params provided, id1 should be 0 | |
152 | if ($this->params) { | |
153 | $select = "t1.$id id1, {$this->rule_weight} weight"; | |
0226a9f3 | 154 | $subSelect = 'id1, weight'; |
6a488035 TO |
155 | } |
156 | else { | |
157 | $select = "t1.$id id1, t2.$id id2, {$this->rule_weight} weight"; | |
0226a9f3 | 158 | $subSelect = 'id1, id2, weight'; |
6a488035 TO |
159 | } |
160 | ||
161 | // build FROM (and WHERE, if it's a parametrised search) | |
162 | // based on whether the rule is about substrings or not | |
163 | if ($this->params) { | |
164 | $from = "{$this->rule_table} t1"; | |
165 | $str = 'NULL'; | |
166 | if (isset($this->params[$this->rule_table][$this->rule_field])) { | |
167 | $str = CRM_Utils_Type::escape($this->params[$this->rule_table][$this->rule_field], 'String'); | |
168 | } | |
169 | if ($this->rule_length) { | |
170 | $where[] = "SUBSTR(t1.{$this->rule_field}, 1, {$this->rule_length}) = SUBSTR('$str', 1, {$this->rule_length})"; | |
171 | $where[] = "t1.{$this->rule_field} IS NOT NULL"; | |
172 | } | |
173 | else { | |
174 | $where[] = "t1.{$this->rule_field} = '$str'"; | |
175 | } | |
176 | } | |
177 | else { | |
178 | if ($this->rule_length) { | |
179 | $from = "{$this->rule_table} t1 JOIN {$this->rule_table} t2 ON (" . implode(' AND ', $on) . ")"; | |
180 | } | |
181 | else { | |
7965a7f1 | 182 | $from = "{$this->rule_table} t1 INNER JOIN {$this->rule_table} t2 ON (" . implode(' AND ', $innerJoinClauses) . ")"; |
6a488035 TO |
183 | } |
184 | } | |
185 | ||
186 | // finish building WHERE, also limit the results if requested | |
187 | if (!$this->params) { | |
188 | $where[] = "t1.$id < t2.$id"; | |
6a488035 | 189 | } |
be61083d | 190 | $query = "SELECT $select FROM $from WHERE " . implode(' AND ', $where); |
6a488035 | 191 | if ($this->contactIds) { |
be2fb01f | 192 | $cids = []; |
6a488035 TO |
193 | foreach ($this->contactIds as $cid) { |
194 | $cids[] = CRM_Utils_Type::escape($cid, 'Integer'); | |
195 | } | |
196 | if (count($cids) == 1) { | |
be61083d | 197 | $query .= " AND (t1.$id = {$cids[0]}) UNION $query AND t2.$id = {$cids[0]}"; |
6a488035 TO |
198 | } |
199 | else { | |
be61083d | 200 | $query .= " AND t1.$id IN (" . implode(',', $cids) . ") |
201 | UNION $query AND t2.$id IN (" . implode(',', $cids) . ")"; | |
6a488035 | 202 | } |
0226a9f3 AH |
203 | // The `weight` is ambiguous in the context of the union; put the whole |
204 | // thing in a subquery. | |
205 | $query = "SELECT $subSelect FROM ($query) subunion"; | |
6a488035 TO |
206 | } |
207 | ||
be61083d | 208 | return $query; |
6a488035 TO |
209 | } |
210 | ||
211 | /** | |
dc195289 | 212 | * find fields related to a rule group. |
6a488035 | 213 | * |
389bcebf | 214 | * @param array $params contains the rule group property to identify rule group |
6a488035 | 215 | * |
72b3a70c CW |
216 | * @return array |
217 | * rule fields array associated to rule group | |
6a488035 | 218 | */ |
00be9182 | 219 | public static function dedupeRuleFields($params) { |
353ffa53 TO |
220 | $rgBao = new CRM_Dedupe_BAO_RuleGroup(); |
221 | $rgBao->used = $params['used']; | |
6a488035 TO |
222 | $rgBao->contact_type = $params['contact_type']; |
223 | $rgBao->find(TRUE); | |
224 | ||
225 | $ruleBao = new CRM_Dedupe_BAO_Rule(); | |
226 | $ruleBao->dedupe_rule_group_id = $rgBao->id; | |
227 | $ruleBao->find(); | |
be2fb01f | 228 | $ruleFields = []; |
6a488035 TO |
229 | while ($ruleBao->fetch()) { |
230 | $ruleFields[] = $ruleBao->rule_field; | |
231 | } | |
232 | return $ruleFields; | |
233 | } | |
234 | ||
e0ef6999 | 235 | /** |
100fef9d CW |
236 | * @param int $cid |
237 | * @param int $oid | |
e0ef6999 EM |
238 | * |
239 | * @return bool | |
240 | */ | |
00be9182 | 241 | public static function validateContacts($cid, $oid) { |
6a488035 | 242 | if (!$cid || !$oid) { |
389bcebf | 243 | return NULL; |
6a488035 TO |
244 | } |
245 | $exception = new CRM_Dedupe_DAO_Exception(); | |
246 | $exception->contact_id1 = $cid; | |
247 | $exception->contact_id2 = $oid; | |
248 | //make sure contact2 > contact1. | |
249 | if ($cid > $oid) { | |
250 | $exception->contact_id1 = $oid; | |
251 | $exception->contact_id2 = $cid; | |
252 | } | |
253 | ||
254 | return $exception->find(TRUE) ? FALSE : TRUE; | |
255 | } | |
96025800 | 256 | |
6a488035 | 257 | } |