Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
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 | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
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_Finder { | |
41 | ||
42 | /** | |
43 | * Return a contact_id-keyed array of arrays of possible dupes | |
44 | * (of the key contact_id) - limited to dupes of $cids if provided. | |
45 | * | |
98997235 TO |
46 | * @param int $rgid |
47 | * Rule group id. | |
48 | * @param array $cids | |
49 | * Contact ids to limit the search to. | |
6a488035 | 50 | * |
3058f4d9 | 51 | * @param bool $checkPermissions |
52 | * Respect logged in user permissions. | |
53 | * | |
a6c01b45 | 54 | * @return array |
3058f4d9 | 55 | * Array of (cid1, cid2, weight) dupe triples |
6c866f0c | 56 | * |
6c866f0c | 57 | * @throws Exception |
6a488035 | 58 | */ |
22912bef | 59 | public static function dupes($rgid, $cids = [], $checkPermissions = TRUE) { |
353ffa53 TO |
60 | $rgBao = new CRM_Dedupe_BAO_RuleGroup(); |
61 | $rgBao->id = $rgid; | |
6a488035 TO |
62 | $rgBao->contactIds = $cids; |
63 | if (!$rgBao->find(TRUE)) { | |
885de68e | 64 | throw new CRM_Core_Exception('Dedupe rule not found for selected contacts'); |
6a488035 TO |
65 | } |
66 | ||
67 | $rgBao->fillTable(); | |
68 | $dao = new CRM_Core_DAO(); | |
3058f4d9 | 69 | $dao->query($rgBao->thresholdQuery($checkPermissions)); |
be2fb01f | 70 | $dupes = []; |
6a488035 | 71 | while ($dao->fetch()) { |
be2fb01f | 72 | $dupes[] = [$dao->id1, $dao->id2, $dao->weight]; |
6a488035 TO |
73 | } |
74 | $dao->query($rgBao->tableDropQuery()); | |
75 | ||
76 | return $dupes; | |
77 | } | |
78 | ||
79 | /** | |
80 | * Return an array of possible dupes, based on the provided array of | |
81 | * params, using the default rule group for the given contact type and | |
82 | * usage. | |
83 | * | |
84 | * check_permission is a boolean flag to indicate if permission should be considered. | |
85 | * default is to always check permissioning but public pages for example might not want | |
6acc9d56 | 86 | * permission to be checked for anonymous users. Refer CRM-6211. We might be breaking |
6a488035 TO |
87 | * Multi-Site dedupe for public pages. |
88 | * | |
98997235 TO |
89 | * @param array $params |
90 | * Array of params of the form $params[$table][$field] == $value. | |
91 | * @param string $ctype | |
92 | * Contact type to match against. | |
93 | * @param string $used | |
94 | * Dedupe rule group usage ('Unsupervised' or 'Supervised' or 'General'). | |
95 | * @param array $except | |
96 | * Array of contacts that shouldn't be considered dupes. | |
97 | * @param int $ruleGroupID | |
98 | * The id of the dedupe rule we should be using. | |
6a488035 | 99 | * |
a6c01b45 CW |
100 | * @return array |
101 | * matching contact ids | |
885de68e | 102 | * @throws \CRM_Core_Exception |
6a488035 | 103 | */ |
389bcebf | 104 | public static function dupesByParams( |
57b29d67 | 105 | $params, |
6a488035 | 106 | $ctype, |
d58a19a1 | 107 | $used = 'Unsupervised', |
be2fb01f | 108 | $except = [], |
6a488035 TO |
109 | $ruleGroupID = NULL |
110 | ) { | |
111 | // If $params is empty there is zero reason to proceed. | |
112 | if (!$params) { | |
be2fb01f | 113 | return []; |
6a488035 | 114 | } |
a99b82c5 | 115 | $checkPermission = CRM_Utils_Array::value('check_permission', $params, TRUE); |
4f33e78b AS |
116 | // This may no longer be required - see https://github.com/civicrm/civicrm-core/pull/13176 |
117 | $params = array_filter($params); | |
6a488035 TO |
118 | |
119 | $foundByID = FALSE; | |
120 | if ($ruleGroupID) { | |
353ffa53 TO |
121 | $rgBao = new CRM_Dedupe_BAO_RuleGroup(); |
122 | $rgBao->id = $ruleGroupID; | |
6a488035 TO |
123 | $rgBao->contact_type = $ctype; |
124 | if ($rgBao->find(TRUE)) { | |
125 | $foundByID = TRUE; | |
126 | } | |
127 | } | |
128 | ||
129 | if (!$foundByID) { | |
353ffa53 | 130 | $rgBao = new CRM_Dedupe_BAO_RuleGroup(); |
6a488035 | 131 | $rgBao->contact_type = $ctype; |
353ffa53 | 132 | $rgBao->used = $used; |
6a488035 | 133 | if (!$rgBao->find(TRUE)) { |
885de68e | 134 | throw new CRM_Core_Exception("$used rule for $ctype does not exist"); |
6a488035 TO |
135 | } |
136 | } | |
6a488035 | 137 | |
ce83f203 JM |
138 | if (isset($params['civicrm_phone']['phone_numeric'])) { |
139 | $orig = $params['civicrm_phone']['phone_numeric']; | |
140 | $params['civicrm_phone']['phone_numeric'] = preg_replace('/[^\d]/', '', $orig); | |
141 | } | |
6a488035 TO |
142 | $rgBao->params = $params; |
143 | $rgBao->fillTable(); | |
144 | $dao = new CRM_Core_DAO(); | |
a99b82c5 | 145 | $dao->query($rgBao->thresholdQuery($checkPermission)); |
be2fb01f | 146 | $dupes = []; |
6a488035 TO |
147 | while ($dao->fetch()) { |
148 | if (isset($dao->id) && $dao->id) { | |
149 | $dupes[] = $dao->id; | |
150 | } | |
151 | } | |
152 | $dao->query($rgBao->tableDropQuery()); | |
153 | return array_diff($dupes, $except); | |
154 | } | |
155 | ||
156 | /** | |
157 | * Return a contact_id-keyed array of arrays of possible dupes in the given group. | |
158 | * | |
98997235 TO |
159 | * @param int $rgid |
160 | * Rule group id. | |
161 | * @param int $gid | |
329840ed | 162 | * Contact group id. |
6a488035 | 163 | * |
21a95d83 | 164 | * @param int $searchLimit |
165 | * Limit for the number of contacts to be used for comparison. | |
166 | * The search methodology finds all matches for the searchedContacts so this limits | |
167 | * the number of searched contacts, not the matches found. | |
168 | * | |
a6c01b45 CW |
169 | * @return array |
170 | * array of (cid1, cid2, weight) dupe triples | |
885de68e | 171 | * @throws \CiviCRM_API3_Exception |
6a488035 | 172 | */ |
21a95d83 | 173 | public static function dupesInGroup($rgid, $gid, $searchLimit = 0) { |
917acf6f | 174 | $cids = array_keys(CRM_Contact_BAO_Group::getMember($gid, TRUE, $searchLimit)); |
481a74f4 | 175 | if (!empty($cids)) { |
d58a19a1 TO |
176 | return self::dupes($rgid, $cids); |
177 | } | |
be2fb01f | 178 | return []; |
6a488035 TO |
179 | } |
180 | ||
6a488035 TO |
181 | /** |
182 | * A hackish function needed to massage CRM_Contact_Form_$ctype::formRule() | |
183 | * object into a valid $params array for dedupe | |
184 | * | |
98997235 TO |
185 | * @param array $fields |
186 | * Contact structure from formRule(). | |
187 | * @param string $ctype | |
188 | * Contact type of the given contact. | |
6a488035 | 189 | * |
a6c01b45 CW |
190 | * @return array |
191 | * valid $params array for dedupe | |
885de68e | 192 | * @throws \CRM_Core_Exception |
6a488035 | 193 | */ |
00be9182 | 194 | public static function formatParams($fields, $ctype) { |
be2fb01f | 195 | $flat = []; |
6a488035 TO |
196 | CRM_Utils_Array::flatten($fields, $flat); |
197 | ||
309a09df | 198 | // FIXME: This may no longer be necessary - check inputs |
be2fb01f | 199 | $replace_these = [ |
6a488035 TO |
200 | 'individual_prefix' => 'prefix_id', |
201 | 'individual_suffix' => 'suffix_id', | |
202 | 'gender' => 'gender_id', | |
be2fb01f CW |
203 | ]; |
204 | foreach (['individual_suffix', 'individual_prefix', 'gender'] as $name) { | |
a7488080 | 205 | if (!empty($fields[$name])) { |
6a488035 TO |
206 | $flat[$replace_these[$name]] = $flat[$name]; |
207 | unset($flat[$name]); | |
208 | } | |
209 | } | |
210 | ||
211 | // handle {birth,deceased}_date | |
be2fb01f | 212 | foreach ([ |
c5c263ca AH |
213 | 'birth_date', |
214 | 'deceased_date', | |
be2fb01f | 215 | ] as $date) { |
a7488080 | 216 | if (!empty($fields[$date])) { |
6a488035 TO |
217 | $flat[$date] = $fields[$date]; |
218 | if (is_array($flat[$date])) { | |
219 | $flat[$date] = CRM_Utils_Date::format($flat[$date]); | |
220 | } | |
221 | $flat[$date] = CRM_Utils_Date::processDate($flat[$date]); | |
222 | } | |
223 | } | |
224 | ||
a7488080 | 225 | if (!empty($flat['contact_source'])) { |
6a488035 TO |
226 | $flat['source'] = $flat['contact_source']; |
227 | unset($flat['contact_source']); | |
228 | } | |
229 | ||
230 | // handle preferred_communication_method | |
df5ad245 | 231 | if (!empty($fields['preferred_communication_method'])) { |
be2fb01f | 232 | $methods = array_intersect($fields['preferred_communication_method'], ['1']); |
6a488035 TO |
233 | $methods = array_keys($methods); |
234 | sort($methods); | |
235 | if ($methods) { | |
236 | $flat['preferred_communication_method'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $methods) . CRM_Core_DAO::VALUE_SEPARATOR; | |
237 | } | |
238 | } | |
239 | ||
240 | // handle custom data | |
0b330e6d | 241 | $tree = CRM_Core_BAO_CustomGroup::getTree($ctype, NULL, NULL, -1); |
6a488035 TO |
242 | CRM_Core_BAO_CustomGroup::postProcess($tree, $fields, TRUE); |
243 | foreach ($tree as $key => $cg) { | |
244 | if (!is_int($key)) { | |
245 | continue; | |
246 | } | |
247 | foreach ($cg['fields'] as $cf) { | |
248 | $flat[$cf['column_name']] = CRM_Utils_Array::value('data', $cf['customValue']); | |
249 | } | |
250 | } | |
251 | ||
252 | // if the key is dotted, keep just the last part of it | |
253 | foreach ($flat as $key => $value) { | |
254 | if (substr_count($key, '.')) { | |
255 | $last = explode('.', $key); | |
256 | $last = array_pop($last); | |
b44e3f84 | 257 | // make sure the first occurrence is kept, not the last |
6a488035 TO |
258 | if (!isset($flat[$last])) { |
259 | $flat[$last] = $value; | |
260 | } | |
261 | unset($flat[$key]); | |
262 | } | |
263 | } | |
264 | ||
265 | // drop the -digit (and -Primary, for CRM-3902) postfixes (so event registration's $flat['email-5'] becomes $flat['email']) | |
266 | // FIXME: CRM-5026 should be fixed here; the below clobbers all address info; we should split off address fields and match | |
267 | // the -digit to civicrm_address.location_type_id and -Primary to civicrm_address.is_primary | |
268 | foreach ($flat as $key => $value) { | |
be2fb01f | 269 | $matches = []; |
eba5ec3d | 270 | if (preg_match('/(.*)-(Primary-[\d+])$|(.*)-(\d+|Primary)$/', $key, $matches)) { |
520b28fe | 271 | $return = array_values(array_filter($matches)); |
367c9a2d JP |
272 | // make sure the first occurrence is kept, not the last |
273 | $flat[$return[1]] = empty($flat[$return[1]]) ? $value : $flat[$return[1]]; | |
6a488035 TO |
274 | unset($flat[$key]); |
275 | } | |
276 | } | |
277 | ||
be2fb01f | 278 | $params = []; |
6a488035 TO |
279 | $supportedFields = CRM_Dedupe_BAO_RuleGroup::supportedFields($ctype); |
280 | if (is_array($supportedFields)) { | |
281 | foreach ($supportedFields as $table => $fields) { | |
282 | if ($table == 'civicrm_address') { | |
283 | // for matching on civicrm_address fields, we also need the location_type_id | |
284 | $fields['location_type_id'] = ''; | |
285 | // FIXME: we also need to do some hacking for id and name fields, see CRM-3902’s comments | |
be2fb01f | 286 | $fixes = [ |
d58a19a1 | 287 | 'address_name' => 'name', |
353ffa53 | 288 | 'country' => 'country_id', |
d58a19a1 | 289 | 'state_province' => 'state_province_id', |
353ffa53 | 290 | 'county' => 'county_id', |
be2fb01f | 291 | ]; |
6a488035 | 292 | foreach ($fixes as $orig => $target) { |
59a67127 JM |
293 | if (!empty($flat[$orig])) { |
294 | $params[$table][$target] = $flat[$orig]; | |
295 | } | |
296 | } | |
297 | } | |
885de68e | 298 | if ($table === 'civicrm_phone') { |
be2fb01f | 299 | $fixes = [ |
0d7e59b0 | 300 | 'phone' => 'phone_numeric', |
be2fb01f | 301 | ]; |
59a67127 | 302 | foreach ($fixes as $orig => $target) { |
a7488080 | 303 | if (!empty($flat[$orig])) { |
6a488035 TO |
304 | $params[$table][$target] = $flat[$orig]; |
305 | } | |
306 | } | |
307 | } | |
308 | foreach ($fields as $field => $title) { | |
a7488080 | 309 | if (!empty($flat[$field])) { |
6a488035 TO |
310 | $params[$table][$field] = $flat[$field]; |
311 | } | |
312 | } | |
313 | } | |
314 | } | |
315 | return $params; | |
316 | } | |
96025800 | 317 | |
1719073d | 318 | /** |
319 | * Parse duplicate pairs into a standardised array and store in the prev_next_cache. | |
320 | * | |
321 | * @param array $foundDupes | |
322 | * @param string $cacheKeyString | |
323 | * | |
518fa0ee | 324 | * @return array |
1719073d | 325 | * Dupe pairs with the keys |
326 | * -srcID | |
327 | * -srcName | |
328 | * -dstID | |
329 | * -dstName | |
330 | * -weight | |
331 | * -canMerge | |
1719073d | 332 | */ |
333 | public static function parseAndStoreDupePairs($foundDupes, $cacheKeyString) { | |
be2fb01f | 334 | $cids = []; |
1719073d | 335 | foreach ($foundDupes as $dupe) { |
336 | $cids[$dupe[0]] = 1; | |
337 | $cids[$dupe[1]] = 1; | |
338 | } | |
339 | $cidString = implode(', ', array_keys($cids)); | |
1fe557c2 | 340 | |
341 | $dao = CRM_Core_DAO::executeQuery("SELECT id, display_name FROM civicrm_contact WHERE id IN ($cidString) ORDER BY sort_name"); | |
be2fb01f | 342 | $displayNames = []; |
1719073d | 343 | while ($dao->fetch()) { |
344 | $displayNames[$dao->id] = $dao->display_name; | |
345 | } | |
346 | ||
3bdcd4ec | 347 | $userId = CRM_Core_Session::getLoggedInContactID(); |
1719073d | 348 | foreach ($foundDupes as $dupes) { |
1fe557c2 | 349 | $srcID = $dupes[1]; |
350 | $dstID = $dupes[0]; | |
063ffcb7 | 351 | // The logged in user should never be the src (ie. the contact to be removed). |
1fe557c2 | 352 | if ($srcID == $userId) { |
353 | $srcID = $dstID; | |
354 | $dstID = $userId; | |
1719073d | 355 | } |
356 | ||
be2fb01f | 357 | $mainContacts[] = $row = [ |
1719073d | 358 | 'dstID' => $dstID, |
359 | 'dstName' => $displayNames[$dstID], | |
e67dcaf8 | 360 | 'srcID' => $srcID, |
361 | 'srcName' => $displayNames[$srcID], | |
1719073d | 362 | 'weight' => $dupes[2], |
363 | 'canMerge' => TRUE, | |
be2fb01f | 364 | ]; |
1719073d | 365 | |
366 | $data = CRM_Core_DAO::escapeString(serialize($row)); | |
e1c519d7 | 367 | CRM_Core_BAO_PrevNextCache::setItem('civicrm_contact', $dstID, $srcID, $cacheKeyString, $data); |
1719073d | 368 | } |
1719073d | 369 | return $mainContacts; |
370 | } | |
371 | ||
6a488035 | 372 | } |