Merge pull request #17719 from civicrm/5.27
[civicrm-core.git] / CRM / Dedupe / Merger.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035 11
fe3b8caf 12use Civi\Api4\CustomGroup;
13
6a488035
TO
14/**
15 *
16 * @package CRM
ca5cec67 17 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
18 */
19class CRM_Dedupe_Merger {
6a488035 20
e0ef6999 21 /**
4f1f1f2a
CW
22 * FIXME: consider creating a common structure with cidRefs() and eidRefs()
23 * FIXME: the sub-pages references by the URLs should
24 * be loaded dynamically on the merge form instead
723c5a81 25 *
e0ef6999 26 * @return array
723c5a81 27 * @throws \CiviCRM_API3_Exception
e0ef6999 28 */
00be9182 29 public static function relTables() {
6a488035 30
641a8cb5 31 if (!isset(Civi::$statics[__CLASS__]['relTables'])) {
12d73bba 32
641a8cb5
J
33 // Setting these merely prevents enotices - but it may be more appropriate not to add the user table below
34 // if the url can't be retrieved. A more standardised way to retrieve them is.
35 // CRM_Core_Config::singleton()->userSystem->getUserRecordUrl() - however that function takes a contact_id &
36 // we may need a different function when it is not known.
37 $title = $userRecordUrl = '';
38
39 $config = CRM_Core_Config::singleton();
8372e605 40 // @todo - this user url stuff is only needed for the form layer - move to CRM_Contact_Form_Merge
641a8cb5
J
41 if ($config->userSystem->is_drupal) {
42 $userRecordUrl = CRM_Utils_System::url('user/%ufid');
2c16165a 43 $title = ts('%1 User: %2; user id: %3', [
44 1 => $config->userFramework,
45 2 => '$ufname',
46 3 => '$ufid',
47 ]);
641a8cb5 48 }
723c5a81 49 elseif ($config->userFramework === 'Joomla') {
641a8cb5 50 $userRecordUrl = $config->userSystem->getVersion() > 1.5 ? $config->userFrameworkBaseURL . "index.php?option=com_users&view=user&task=user.edit&id=" . '%ufid' : $config->userFrameworkBaseURL . "index2.php?option=com_users&view=user&task=edit&id[]=" . '%ufid';
2c16165a 51 $title = ts('%1 User: %2; user id: %3', [
52 1 => $config->userFramework,
53 2 => '$ufname',
54 3 => '$ufid',
55 ]);
641a8cb5 56 }
6a488035 57
2c16165a 58 $relTables = [
59 'rel_table_contributions' => [
6a488035 60 'title' => ts('Contributions'),
2c16165a 61 'tables' => [
62 'civicrm_contribution',
63 'civicrm_contribution_recur',
64 'civicrm_contribution_soft',
65 ],
6a488035 66 'url' => CRM_Utils_System::url('civicrm/contact/view', 'reset=1&force=1&cid=$cid&selectedChild=contribute'),
2c16165a 67 ],
68 'rel_table_contribution_page' => [
6a488035 69 'title' => ts('Contribution Pages'),
2c16165a 70 'tables' => ['civicrm_contribution_page'],
6a488035 71 'url' => CRM_Utils_System::url('civicrm/admin/contribute', 'reset=1&cid=$cid'),
2c16165a 72 ],
73 'rel_table_memberships' => [
6a488035 74 'title' => ts('Memberships'),
2c16165a 75 'tables' => [
76 'civicrm_membership',
77 'civicrm_membership_log',
78 'civicrm_membership_type',
79 ],
6a488035 80 'url' => CRM_Utils_System::url('civicrm/contact/view', 'reset=1&force=1&cid=$cid&selectedChild=member'),
2c16165a 81 ],
82 'rel_table_participants' => [
6a488035 83 'title' => ts('Participants'),
2c16165a 84 'tables' => ['civicrm_participant'],
6a488035 85 'url' => CRM_Utils_System::url('civicrm/contact/view', 'reset=1&force=1&cid=$cid&selectedChild=participant'),
2c16165a 86 ],
87 'rel_table_events' => [
6a488035 88 'title' => ts('Events'),
2c16165a 89 'tables' => ['civicrm_event'],
6a488035 90 'url' => CRM_Utils_System::url('civicrm/event/manage', 'reset=1&cid=$cid'),
2c16165a 91 ],
92 'rel_table_activities' => [
6a488035 93 'title' => ts('Activities'),
2c16165a 94 'tables' => ['civicrm_activity', 'civicrm_activity_contact'],
6a488035 95 'url' => CRM_Utils_System::url('civicrm/contact/view', 'reset=1&force=1&cid=$cid&selectedChild=activity'),
2c16165a 96 ],
97 'rel_table_relationships' => [
6a488035 98 'title' => ts('Relationships'),
2c16165a 99 'tables' => ['civicrm_relationship'],
6a488035 100 'url' => CRM_Utils_System::url('civicrm/contact/view', 'reset=1&force=1&cid=$cid&selectedChild=rel'),
2c16165a 101 ],
102 'rel_table_custom_groups' => [
6a488035 103 'title' => ts('Custom Groups'),
2c16165a 104 'tables' => ['civicrm_custom_group'],
6a488035 105 'url' => CRM_Utils_System::url('civicrm/admin/custom/group', 'reset=1'),
2c16165a 106 ],
107 'rel_table_uf_groups' => [
6a488035 108 'title' => ts('Profiles'),
2c16165a 109 'tables' => ['civicrm_uf_group'],
6a488035 110 'url' => CRM_Utils_System::url('civicrm/admin/uf/group', 'reset=1'),
2c16165a 111 ],
112 'rel_table_groups' => [
6a488035 113 'title' => ts('Groups'),
2c16165a 114 'tables' => ['civicrm_group_contact'],
6a488035 115 'url' => CRM_Utils_System::url('civicrm/contact/view', 'reset=1&force=1&cid=$cid&selectedChild=group'),
2c16165a 116 ],
117 'rel_table_notes' => [
6a488035 118 'title' => ts('Notes'),
2c16165a 119 'tables' => ['civicrm_note'],
6a488035 120 'url' => CRM_Utils_System::url('civicrm/contact/view', 'reset=1&force=1&cid=$cid&selectedChild=note'),
2c16165a 121 ],
122 'rel_table_tags' => [
6a488035 123 'title' => ts('Tags'),
2c16165a 124 'tables' => ['civicrm_entity_tag'],
6a488035 125 'url' => CRM_Utils_System::url('civicrm/contact/view', 'reset=1&force=1&cid=$cid&selectedChild=tag'),
2c16165a 126 ],
127 'rel_table_mailings' => [
6a488035 128 'title' => ts('Mailings'),
2c16165a 129 'tables' => [
130 'civicrm_mailing',
131 'civicrm_mailing_event_queue',
132 'civicrm_mailing_event_subscribe',
133 ],
f521f375 134 'url' => CRM_Utils_System::url('civicrm/contact/view', 'reset=1&force=1&cid=$cid&selectedChild=mailing'),
2c16165a 135 ],
136 'rel_table_cases' => [
6a488035 137 'title' => ts('Cases'),
2c16165a 138 'tables' => ['civicrm_case_contact'],
6a488035 139 'url' => CRM_Utils_System::url('civicrm/contact/view', 'reset=1&force=1&cid=$cid&selectedChild=case'),
2c16165a 140 ],
141 'rel_table_grants' => [
6a488035 142 'title' => ts('Grants'),
2c16165a 143 'tables' => ['civicrm_grant'],
6a488035 144 'url' => CRM_Utils_System::url('civicrm/contact/view', 'reset=1&force=1&cid=$cid&selectedChild=grant'),
2c16165a 145 ],
146 'rel_table_pcp' => [
6a488035 147 'title' => ts('PCPs'),
2c16165a 148 'tables' => ['civicrm_pcp'],
6a488035 149 'url' => CRM_Utils_System::url('civicrm/contribute/pcp/manage', 'reset=1'),
2c16165a 150 ],
151 'rel_table_pledges' => [
6a488035 152 'title' => ts('Pledges'),
2c16165a 153 'tables' => ['civicrm_pledge', 'civicrm_pledge_payment'],
6a488035 154 'url' => CRM_Utils_System::url('civicrm/contact/view', 'reset=1&force=1&cid=$cid&selectedChild=pledge'),
2c16165a 155 ],
156 'rel_table_users' => [
6a488035 157 'title' => $title,
2c16165a 158 'tables' => ['civicrm_uf_match'],
6a488035 159 'url' => $userRecordUrl,
2c16165a 160 ],
161 ];
6a488035 162
76c53278
CW
163 $relTables += self::getMultiValueCustomSets('relTables');
164
6a488035
TO
165 // Allow hook_civicrm_merge() to adjust $relTables
166 CRM_Utils_Hook::merge('relTables', $relTables);
641a8cb5
J
167
168 // Cache the results in a static variable
169 Civi::$statics[__CLASS__]['relTables'] = $relTables;
6a488035 170 }
641a8cb5
J
171
172 return Civi::$statics[__CLASS__]['relTables'];
6a488035
TO
173 }
174
175 /**
fe482240 176 * Returns the related tables groups for which a contact has any info entered.
ad37ac8e 177 *
178 * @param int $cid
179 *
180 * @return array
723c5a81 181 * @throws \CiviCRM_API3_Exception
6a488035 182 */
00be9182 183 public static function getActiveRelTables($cid) {
6a488035 184 $cid = (int) $cid;
2c16165a 185 $groups = [];
6a488035
TO
186
187 $relTables = self::relTables();
188 $cidRefs = self::cidRefs();
189 $eidRefs = self::eidRefs();
190 foreach ($relTables as $group => $params) {
2c16165a 191 $sqls = [];
6a488035
TO
192 foreach ($params['tables'] as $table) {
193 if (isset($cidRefs[$table])) {
194 foreach ($cidRefs[$table] as $field) {
195 $sqls[] = "SELECT COUNT(*) AS count FROM $table WHERE $field = $cid";
196 }
197 }
198 if (isset($eidRefs[$table])) {
199 foreach ($eidRefs[$table] as $entityTable => $entityId) {
200 $sqls[] = "SELECT COUNT(*) AS count FROM $table WHERE $entityId = $cid AND $entityTable = 'civicrm_contact'";
201 }
202 }
203 foreach ($sqls as $sql) {
204 if (CRM_Core_DAO::singleValueQuery($sql) > 0) {
205 $groups[] = $group;
206 }
207 }
208 }
209 }
210 return array_unique($groups);
211 }
212
213 /**
3ae0ae3d 214 * Get array tables and fields that reference civicrm_contact.id.
215 *
e3e87c73 216 * This function calls the merge hook and only exists to wrap the DAO function to support that deprecated call.
217 * The entityTypes hook is the recommended way to add tables to this result.
3ae0ae3d 218 *
e3e87c73 219 * I thought about adding another hook to alter tableReferences but decided it was unclear if there
220 * are use cases not covered by entityTables and instead we should wait & see.
6a488035 221 */
00be9182 222 public static function cidRefs() {
e31c3de1 223 if (isset(\Civi::$statics[__CLASS__]) && isset(\Civi::$statics[__CLASS__]['contact_references'])) {
224 return \Civi::$statics[__CLASS__]['contact_references'];
225 }
64b3569d 226
e3e87c73 227 $contactReferences = $coreReferences = CRM_Core_DAO::getReferencesToContactTable();
6a488035 228
e31c3de1 229 CRM_Utils_Hook::merge('cidRefs', $contactReferences);
e3e87c73 230 if ($contactReferences !== $coreReferences) {
2c16165a 231 Civi::log()
232 ->warning("Deprecated hook ::merge in context of 'cidRefs. Use entityTypes instead.", ['civi.tag' => 'deprecated']);
e3e87c73 233 }
e31c3de1 234 \Civi::$statics[__CLASS__]['contact_references'] = $contactReferences;
235 return \Civi::$statics[__CLASS__]['contact_references'];
6a488035
TO
236 }
237
238 /**
239 * Return tables and their fields referencing civicrm_contact.contact_id with entity_id
240 */
00be9182 241 public static function eidRefs() {
6a488035
TO
242 static $eidRefs;
243 if (!$eidRefs) {
244 // FIXME: this should be generated dynamically from the schema
245 // tables that reference contacts with entity_{id,table}
2c16165a 246 $eidRefs = [
247 'civicrm_acl' => ['entity_table' => 'entity_id'],
248 'civicrm_acl_entity_role' => ['entity_table' => 'entity_id'],
249 'civicrm_entity_file' => ['entity_table' => 'entity_id'],
250 'civicrm_log' => ['entity_table' => 'entity_id'],
251 'civicrm_mailing_group' => ['entity_table' => 'entity_id'],
252 'civicrm_note' => ['entity_table' => 'entity_id'],
253 ];
6a488035
TO
254
255 // Allow hook_civicrm_merge() to adjust $eidRefs
256 CRM_Utils_Hook::merge('eidRefs', $eidRefs);
257 }
258 return $eidRefs;
259 }
260
9da04f20 261 /**
fe482240 262 * Return tables using locations.
9da04f20 263 */
00be9182 264 public static function locTables() {
9da04f20
AS
265 static $locTables;
266 if (!$locTables) {
2c16165a 267 $locTables = ['civicrm_email', 'civicrm_address', 'civicrm_phone'];
9da04f20
AS
268
269 // Allow hook_civicrm_merge() to adjust $locTables
270 CRM_Utils_Hook::merge('locTables', $locTables);
271 }
272 return $locTables;
273 }
274
76c53278
CW
275 /**
276 * We treat multi-valued custom sets as "related tables" similar to activities, contributions, etc.
6d277089 277 *
98997235
TO
278 * @param string $request
279 * 'relTables' or 'cidRefs'.
6d277089 280 *
641a8cb5 281 * @return array
6d277089 282 * @throws \CiviCRM_API3_Exception
76c53278
CW
283 * @see CRM-13836
284 */
00be9182 285 public static function getMultiValueCustomSets($request) {
641a8cb5
J
286
287 if (!isset(Civi::$statics[__CLASS__]['multiValueCustomSets'])) {
2c16165a 288 $data = [
289 'relTables' => [],
290 'cidRefs' => [],
291 ];
292 $result = civicrm_api3('custom_group', 'get', [
76c53278 293 'is_multiple' => 1,
2c16165a 294 'extends' => [
295 'IN' => [
296 'Individual',
297 'Organization',
298 'Household',
299 'Contact',
300 ],
301 ],
302 'return' => ['id', 'title', 'table_name', 'style'],
303 ]);
22e263ad 304 foreach ($result['values'] as $custom) {
2c16165a 305 $data['cidRefs'][$custom['table_name']] = ['entity_id'];
723c5a81 306 $urlSuffix = $custom['style'] === 'Tab' ? '&selectedChild=custom_' . $custom['id'] : '';
2c16165a 307 $data['relTables']['rel_table_custom_' . $custom['id']] = [
76c53278 308 'title' => $custom['title'],
2c16165a 309 'tables' => [$custom['table_name']],
76c53278 310 'url' => CRM_Utils_System::url('civicrm/contact/view', 'reset=1&force=1&cid=$cid' . $urlSuffix),
2c16165a 311 ];
76c53278 312 }
641a8cb5
J
313
314 // Store the result in a static variable cache
315 Civi::$statics[__CLASS__]['multiValueCustomSets'] = $data;
76c53278 316 }
641a8cb5
J
317
318 return Civi::$statics[__CLASS__]['multiValueCustomSets'][$request];
76c53278
CW
319 }
320
6a488035
TO
321 /**
322 * Tables which require custom processing should declare functions to call here.
323 * Doing so will override normal processing.
324 */
00be9182 325 public static function cpTables() {
6a488035
TO
326 static $tables;
327 if (!$tables) {
2c16165a 328 $tables = [
329 'civicrm_case_contact' => ['CRM_Case_BAO_Case' => 'mergeContacts'],
330 'civicrm_group_contact' => ['CRM_Contact_BAO_GroupContact' => 'mergeGroupContact'],
6a488035 331 // Empty array == do nothing - this table is handled by mergeGroupContact
2c16165a 332 'civicrm_subscription_history' => [],
333 'civicrm_relationship' => ['CRM_Contact_BAO_Relationship' => 'mergeRelationships'],
334 'civicrm_membership' => ['CRM_Member_BAO_Membership' => 'mergeMemberships'],
335 ];
6a488035
TO
336 }
337 return $tables;
338 }
339
340 /**
100fef9d 341 * Return payment related table.
6a488035 342 */
00be9182 343 public static function paymentTables() {
6a488035
TO
344 static $tables;
345 if (!$tables) {
2c16165a 346 $tables = ['civicrm_pledge', 'civicrm_membership', 'civicrm_participant'];
6a488035 347 }
6a488035
TO
348 return $tables;
349 }
350
351 /**
100fef9d 352 * Return payment update Query.
54957108 353 *
354 * @param string $tableName
355 * @param int $mainContactId
356 * @param int $otherContactId
357 *
358 * @return array
6a488035 359 */
00be9182 360 public static function paymentSql($tableName, $mainContactId, $otherContactId) {
2c16165a 361 $sqls = [];
6a488035
TO
362 if (!$tableName || !$mainContactId || !$otherContactId) {
363 return $sqls;
364 }
365
366 $paymentTables = self::paymentTables();
367 if (!in_array($tableName, $paymentTables)) {
368 return $sqls;
369 }
370
371 switch ($tableName) {
372 case 'civicrm_pledge':
373 $sqls[] = "
374 UPDATE IGNORE civicrm_contribution contribution
375INNER JOIN civicrm_pledge_payment payment ON ( payment.contribution_id = contribution.id )
376INNER JOIN civicrm_pledge pledge ON ( pledge.id = payment.pledge_id )
377 SET contribution.contact_id = $mainContactId
378 WHERE pledge.contact_id = $otherContactId";
379 break;
380
381 case 'civicrm_membership':
382 $sqls[] = "
383 UPDATE IGNORE civicrm_contribution contribution
384INNER JOIN civicrm_membership_payment payment ON ( payment.contribution_id = contribution.id )
385INNER JOIN civicrm_membership membership ON ( membership.id = payment.membership_id )
386 SET contribution.contact_id = $mainContactId
387 WHERE membership.contact_id = $otherContactId";
388 break;
389
390 case 'civicrm_participant':
391 $sqls[] = "
392 UPDATE IGNORE civicrm_contribution contribution
393INNER JOIN civicrm_participant_payment payment ON ( payment.contribution_id = contribution.id )
394INNER JOIN civicrm_participant participant ON ( participant.id = payment.participant_id )
395 SET contribution.contact_id = $mainContactId
396 WHERE participant.contact_id = $otherContactId";
397 break;
398 }
399
400 return $sqls;
401 }
402
e0ef6999 403 /**
100fef9d
CW
404 * @param int $mainId
405 * @param int $otherId
406 * @param string $tableName
e0ef6999
EM
407 * @param array $tableOperations
408 * @param string $mode
409 *
410 * @return array
411 */
2c16165a 412 public static function operationSql($mainId, $otherId, $tableName, $tableOperations = [], $mode = 'add') {
413 $sqls = [];
6a488035
TO
414 if (!$tableName || !$mainId || !$otherId) {
415 return $sqls;
416 }
417
6a488035
TO
418 switch ($tableName) {
419 case 'civicrm_membership':
d58a19a1
TO
420 if (array_key_exists($tableName, $tableOperations) && $tableOperations[$tableName]['add']) {
421 break;
422 }
723c5a81 423 if ($mode === 'add') {
d58a19a1 424 $sqls[] = "
6a488035
TO
425DELETE membership1.* FROM civicrm_membership membership1
426 INNER JOIN civicrm_membership membership2 ON membership1.membership_type_id = membership2.membership_type_id
427 AND membership1.contact_id = {$mainId}
428 AND membership2.contact_id = {$otherId} ";
d58a19a1 429 }
723c5a81 430 if ($mode === 'payment') {
d58a19a1 431 $sqls[] = "
6a488035
TO
432DELETE contribution.* FROM civicrm_contribution contribution
433INNER JOIN civicrm_membership_payment payment ON payment.contribution_id = contribution.id
434INNER JOIN civicrm_membership membership1 ON membership1.id = payment.membership_id
435 AND membership1.contact_id = {$mainId}
436INNER JOIN civicrm_membership membership2 ON membership1.membership_type_id = membership2.membership_type_id
437 AND membership2.contact_id = {$otherId}";
d58a19a1
TO
438 }
439 break;
6a488035
TO
440
441 case 'civicrm_uf_match':
442 // normal queries won't work for uf_match since that will lead to violation of unique constraint,
b44e3f84 443 // failing to meet intended result. Therefore we introduce this additional query:
6a488035
TO
444 $sqls[] = "DELETE FROM civicrm_uf_match WHERE contact_id = {$mainId}";
445 break;
446 }
447
448 return $sqls;
449 }
450
4099a9c5
AP
451 /**
452 * Based on the provided two contact_ids and a set of tables, remove the
453 * belongings of the other contact and of their relations.
454 *
455 * @param int $otherID
6d277089 456 * @param array $tables
457 *
458 * @throws \CiviCRM_API3_Exception
4099a9c5
AP
459 */
460 public static function removeContactBelongings($otherID, $tables) {
461 // CRM-20421: Removing Inherited memberships when memberships of parent are not migrated to new contact.
6d277089 462 if (in_array('civicrm_membership', $tables, TRUE)) {
4099a9c5
AP
463 $membershipIDs = CRM_Utils_Array::collect('id',
464 CRM_Utils_Array::value('values',
6d277089 465 civicrm_api3('Membership', "get", [
466 'contact_id' => $otherID,
467 'return' => 'id',
518fa0ee 468 ])
e122e55d
TO
469 )
470 );
4099a9c5
AP
471
472 if (!empty($membershipIDs)) {
6d277089 473 civicrm_api3('Membership', 'get', [
2c16165a 474 'owner_membership_id' => ['IN' => $membershipIDs],
475 'api.Membership.delete' => ['id' => '$value.id'],
476 ]);
4099a9c5
AP
477 }
478 }
479 }
480
6a488035
TO
481 /**
482 * Based on the provided two contact_ids and a set of tables, move the
483 * belongings of the other contact to the main one.
484 *
eca28463 485 * @param CRM_Dedupe_MergeHandler $mergeHandler
fe3b8caf 486 * @param array $tables
54957108 487 * @param array $tableOperations
6d277089 488 *
fe3b8caf 489 * @throws \API_Exception
6d277089 490 * @throws \CiviCRM_API3_Exception
fe3b8caf 491 * @throws \Civi\API\Exception\UnauthorizedException
6a488035 492 */
eca28463 493 public static function moveContactBelongings($mergeHandler, $tables, $tableOperations) {
494 $mainId = $mergeHandler->getToKeepID();
495 $otherId = $mergeHandler->getToRemoveID();
6a488035 496 $cidRefs = self::cidRefs();
eca28463 497 $eidRefs = $mergeHandler->getTablesDynamicallyRelatedToContactTable();
9811efd4 498 $dynamicRefs = CRM_Core_DAO::getDynamicReferencesToTable('civicrm_contact');
6a488035
TO
499 $cpTables = self::cpTables();
500 $paymentTables = self::paymentTables();
fe3b8caf 501 self::filterRowBasedCustomDataFromCustomTables($cidRefs);
674920ca
ES
502
503 $affected = array_merge(array_keys($cidRefs), array_keys($eidRefs));
0556db5d
CR
504
505 // if there aren't any specific tables, don't affect the ones handled by relTables()
506 // also don't affect tables in locTables() CRM-15658
507 $relTables = self::relTables();
eca28463 508 // These arrays don't make a lot of sense. For now ensure the tested handling of tags works...
509 // it is moved over further down....
510 unset($relTables['rel_table_tags']);
0556db5d
CR
511 $handled = self::locTables();
512
513 foreach ($relTables as $params) {
514 $handled = array_merge($handled, $params['tables']);
6a488035 515 }
0556db5d 516 $affected = array_diff($affected, $handled);
e3159b07 517 $affected = array_unique(array_merge($affected, $tables));
6a488035
TO
518
519 $mainId = (int) $mainId;
520 $otherId = (int) $otherId;
521
2c16165a 522 $sqls = [];
6a488035
TO
523 foreach ($affected as $table) {
524 // Call custom processing function for objects that require it
525 if (isset($cpTables[$table])) {
526 foreach ($cpTables[$table] as $className => $fnName) {
43d4ad93 527 $className::$fnName($mainId, $otherId, $sqls, $tables, $tableOperations);
6a488035
TO
528 }
529 // Skip normal processing
530 continue;
531 }
b3fdbf3d 532
6a488035
TO
533 // use UPDATE IGNORE + DELETE query pair to skip on situations when
534 // there's a UNIQUE restriction on ($field, some_other_field) pair
535 if (isset($cidRefs[$table])) {
536 foreach ($cidRefs[$table] as $field) {
537 // carry related contributions CRM-5359
538 if (in_array($table, $paymentTables)) {
6a488035
TO
539 $paymentSqls = self::paymentSql($table, $mainId, $otherId);
540 $sqls = array_merge($sqls, $paymentSqls);
71560cf3
C
541
542 if (!empty($tables) && !in_array('civicrm_contribution', $tables)) {
543 $payOprSqls = self::operationSql($mainId, $otherId, $table, $tableOperations, 'payment');
544 $sqls = array_merge($sqls, $payOprSqls);
545 }
6a488035
TO
546 }
547
548 $preOperationSqls = self::operationSql($mainId, $otherId, $table, $tableOperations);
549 $sqls = array_merge($sqls, $preOperationSqls);
bbc11d19 550 $sqls[] = "UPDATE $table SET $field = $mainId WHERE $field = $otherId";
6a488035
TO
551 }
552 }
ae2346d4 553
6a488035 554 if (isset($eidRefs[$table])) {
9811efd4
JG
555 foreach ($dynamicRefs[$table] as $dynamicRef) {
556 $sqls[] = "UPDATE IGNORE $table SET {$dynamicRef[0]}= $mainId WHERE {$dynamicRef[0]} = $otherId AND {$dynamicRef[1]} = 'civicrm_contact'";
557 $sqls[] = "DELETE FROM $table WHERE {$dynamicRef[0]} = $otherId AND {$dynamicRef[1]} = 'civicrm_contact'";
558 }
6a488035
TO
559 }
560 }
561
562 // Allow hook_civicrm_merge() to add SQL statements for the merge operation.
563 CRM_Utils_Hook::merge('sqls', $sqls, $mainId, $otherId, $tables);
564
6a488035 565 foreach ($sqls as $sql) {
2c16165a 566 CRM_Core_DAO::executeQuery($sql, [], TRUE, NULL, TRUE);
6a488035 567 }
0556db5d 568 CRM_Dedupe_Merger::addMembershipToRealtedContacts($mainId);
6a488035
TO
569 }
570
fe3b8caf 571 /**
572 * Filter out custom tables from cidRefs unless they are there due to a contact reference or are a multiple set.
573 *
574 * The only fields where we want to move the data by sql is where entity reference fields
575 * on another contact refer to the contact being merged, or it is a multiple record set.
576 * The transference of custom data from one contact to another is done in 2 other places in the dedupe process but should
577 * not be done in moveAllContactData.
578 *
579 * Note it's a bit silly the way we build & then cull cidRefs - however, poor hook placement means that
580 * until we fully deprecate calling the hook from cidRefs we are stuck.
581 *
582 * It was deprecated in code (via deprecation notices if people altered it) in Mar 2019 but in docs only in Apri 2020.
583 *
584 * @param array $cidRefs
585 *
586 * @throws \API_Exception
587 * @throws \Civi\API\Exception\UnauthorizedException
588 */
589 protected static function filterRowBasedCustomDataFromCustomTables(array &$cidRefs) {
590 $customTables = (array) CustomGroup::get()
591 ->setCheckPermissions(FALSE)
592 ->setSelect(['table_name'])
593 ->addWhere('is_multiple', '=', 0)
96a118e6 594 ->addWhere('extends', 'IN', array_merge(['Contact'], CRM_Contact_BAO_ContactType::contactTypes()))
fe3b8caf 595 ->execute()
596 ->indexBy('table_name');
597 foreach (array_intersect_key($cidRefs, $customTables) as $tableName => $cidSpec) {
598 if (in_array('entity_id', $cidSpec, TRUE)) {
599 unset($cidRefs[$tableName][array_search('entity_id', $cidSpec, TRUE)]);
600 }
601 if (empty($cidRefs[$tableName])) {
602 unset($cidRefs[$tableName]);
603 }
604 }
605 }
606
4f751978
CR
607 /**
608 * Given a contact ID, will check if a record exists in given table.
609 *
723c5a81 610 * @param int $contactID
611 * @param string $table
612 * @param string $idField
4f751978
CR
613 * Field where the contact's ID is stored in the table
614 *
615 * @return bool
616 * True if a record is found for the given contact ID, false otherwise
617 */
ae2346d4
CR
618 private static function customRecordExists($contactID, $table, $idField) {
619 $sql = "
620 SELECT COUNT(*) AS count
621 FROM $table
622 WHERE $idField = $contactID
623 ";
624 $dbResult = CRM_Core_DAO::executeQuery($sql);
625 $dbResult->fetch();
626
627 if ($dbResult->count > 0) {
ee3b1d86 628 return TRUE;
ae2346d4
CR
629 }
630
ee3b1d86 631 return FALSE;
ae2346d4
CR
632 }
633
6a488035 634 /**
4eea8606 635 * Load all non-empty fields for the contacts
b3fdbf3d 636 *
98997235
TO
637 * @param array $main
638 * Contact details.
639 * @param array $other
640 * Contact details.
6a488035 641 *
77b97be7 642 * @return array
723c5a81 643 *
644 * @throws \CRM_Core_Exception
6a488035 645 */
4eea8606 646 public static function retrieveFields($main, $other) {
2c16165a 647 $result = [
648 'contact' => [],
649 'custom' => [],
650 ];
16254ae1 651 foreach (self::getContactFields() as $validField) {
eb61dc07
J
652 // CRM-17556 Get all non-empty fields, to make comparison easier
653 if (!empty($main[$validField]) || !empty($other[$validField])) {
6a488035
TO
654 $result['contact'][] = $validField;
655 }
656 }
657
658 $mainEvs = CRM_Core_BAO_CustomValueTable::getEntityValues($main['id']);
659 $otherEvs = CRM_Core_BAO_CustomValueTable::getEntityValues($other['id']);
660 $keys = array_unique(array_merge(array_keys($mainEvs), array_keys($otherEvs)));
661 foreach ($keys as $key) {
76c53278
CW
662 // Exclude multi-value fields CRM-13836
663 if (strpos($key, '_')) {
664 continue;
665 }
9c1bc317
CW
666 $key1 = $mainEvs[$key] ?? NULL;
667 $key2 = $otherEvs[$key] ?? NULL;
26b7ff28 668 // We wish to retain '0' as it has a different meaning than NULL on a checkbox.
669 // However I can't think of a case where an empty string is more meaningful than null
670 // or where it would be FALSE or something else nullish.
671 $valuesToIgnore = [NULL, '', []];
672 if (!in_array($key1, $valuesToIgnore, TRUE) || !in_array($key2, $valuesToIgnore, TRUE)) {
6a488035
TO
673 $result['custom'][] = $key;
674 }
675 }
676 return $result;
677 }
678
679 /**
100fef9d 680 * Batch merge a set of contacts based on rule-group and group.
6a488035 681 *
98997235
TO
682 * @param int $rgid
683 * Rule group id.
684 * @param int $gid
685 * Group id.
686 * @param string $mode
687 * Helps decide how to behave when there are conflicts.
15a793bb
EM
688 * A 'safe' value skips the merge if there are any un-resolved conflicts, wheras 'aggressive'
689 * mode does a force merge.
f931b74c 690 * @param int $batchLimit number of merges to carry out in one batch.
691 * @param int $isSelected if records with is_selected column needs to be processed.
bb22928b 692 * Note the option of '2' is only used in conjunction with $redirectForPerformance
b5a6c6c5 693 * to determine when to reload the cache (!). The use of anything other than a boolean is being grandfathered
694 * out in favour of explicitly passing in $reloadCacheIfEmpty
6a488035 695 *
e23e26ec 696 * @param array $criteria
697 * Criteria to use in the filter.
698 *
3058f4d9 699 * @param bool $checkPermissions
700 * Respect logged in user permissions.
bb22928b 701 * @param bool|NULL $reloadCacheIfEmpty
702 * If not set explicitly this is calculated but it is preferred that it be set
703 * per comments on isSelected above.
3058f4d9 704 *
997a03fe 705 * @param int $searchLimit
706 * Limit on number of contacts to search for duplicates for.
707 * This means that if the limit is 1000 then only duplicates for the first 1000 contacts
708 * matching criteria will be found and batchMerged (the number of merges could be less than or greater than 100)
709 *
77b97be7 710 * @return array|bool
997a03fe 711 *
712 * @throws \CRM_Core_Exception
713 * @throws \CiviCRM_API3_Exception
96a118e6 714 * @throws \API_Exception
6a488035 715 */
997a03fe 716 public static function batchMerge($rgid, $gid = NULL, $mode = 'safe', $batchLimit = 1, $isSelected = 2, $criteria = [], $checkPermissions = TRUE, $reloadCacheIfEmpty = NULL, $searchLimit = 0) {
f7dbf5d9 717 $redirectForPerformance = $batchLimit > 1;
418ffc5b 718 if ($mode === 'aggressive' && $checkPermissions && !CRM_Core_Permission::check('force merge duplicate contacts')) {
719 throw new CRM_Core_Exception(ts('Insufficient permissions for aggressive mode batch merge'));
720 }
bb22928b 721 if (!isset($reloadCacheIfEmpty)) {
722 $reloadCacheIfEmpty = (!$redirectForPerformance && $isSelected == 2);
723 }
724 if ($isSelected !== 0 && $isSelected !== 1) {
725 // explicitly set to NULL if not 1 or 0 as part of grandfathering out the mystical '2' value.
726 $isSelected = NULL;
727 }
723c5a81 728 $dupePairs = self::getDuplicatePairs($rgid, $gid, $reloadCacheIfEmpty, $batchLimit, $isSelected, ($mode === 'aggressive'), $criteria, $checkPermissions, $searchLimit);
6a488035 729
2c16165a 730 $cacheParams = [
997a03fe 731 'cache_key_string' => self::getMergeCacheKeyString($rgid, $gid, $criteria, $checkPermissions, $searchLimit),
2988f5c7 732 // @todo stop passing these parameters in & instead calculate them in the merge function based
733 // on the 'real' params like $isRespectExclusions $batchLimit and $isSelected.
734 'join' => self::getJoinOnDedupeTable(),
ed3c1599 735 'where' => self::getWhereString($isSelected),
736 'limit' => (int) $batchLimit,
2c16165a 737 ];
d238756a 738 return CRM_Dedupe_Merger::merge($dupePairs, $cacheParams, $mode, $redirectForPerformance, $checkPermissions);
6a488035
TO
739 }
740
2ae26001 741 /**
742 * Get the string to join the prevnext cache to the dedupe table.
743 *
744 * @return string
745 * The join string to join prevnext cache on the dedupe table.
746 */
747 public static function getJoinOnDedupeTable() {
723c5a81 748 return '
2ae26001 749 LEFT JOIN civicrm_dedupe_exception de
750 ON (
751 pn.entity_id1 = de.contact_id1
752 AND pn.entity_id2 = de.contact_id2 )
723c5a81 753 ';
2ae26001 754 }
755
2988f5c7 756 /**
757 * Get where string for dedupe join.
758 *
2988f5c7 759 * @param bool $isSelected
760 *
761 * @return string
762 */
ed3c1599 763 protected static function getWhereString($isSelected) {
723c5a81 764 $where = 'de.id IS NULL';
2988f5c7 765 if ($isSelected === 0 || $isSelected === 1) {
766 $where .= " AND pn.is_selected = {$isSelected}";
767 }
2988f5c7 768 return $where;
769 }
770
bc854509 771 /**
772 * Update the statistics for the merge set.
773 *
774 * @param string $cacheKeyString
775 * @param array $result
723c5a81 776 *
777 * @throws \CiviCRM_API3_Exception
bc854509 778 */
2c16165a 779 public static function updateMergeStats($cacheKeyString, $result = []) {
63ef778e 780 // gather latest stats
2c16165a 781 $merged = count($result['merged']);
63ef778e 782 $skipped = count($result['skipped']);
783
784 if ($merged <= 0 && $skipped <= 0) {
785 return;
786 }
787
788 // get previous stats
e13fa54b 789 $previousStats = CRM_Dedupe_Merger::getMergeStats($cacheKeyString);
63ef778e 790 if (!empty($previousStats)) {
e13fa54b 791 if ($previousStats['merged']) {
792 $merged = $merged + $previousStats['merged'];
63ef778e 793 }
e13fa54b 794 if ($previousStats['skipped']) {
795 $skipped = $skipped + $previousStats['skipped'];
63ef778e 796 }
797 }
798
799 // delete old stats
800 CRM_Dedupe_Merger::resetMergeStats($cacheKeyString);
801
802 // store the updated stats
2c16165a 803 $data = [
3bcde7f1 804 'merged' => (int) $merged,
805 'skipped' => (int) $skipped,
2c16165a 806 ];
63ef778e 807
3bcde7f1 808 CRM_Core_DAO::executeQuery("INSERT INTO civicrm_prevnext_cache (entity_table, entity_id1, entity_id2, cacheKey, data) VALUES
2e09a60f 809 ('civicrm_contact', 0, 0, %1, %2)", [1 => [$cacheKeyString . '_stats', 'String'], 2 => [serialize($data), 'String']]);
63ef778e 810 }
811
ae6455b2 812 /**
813 * Delete information about merges for the given string.
814 *
815 * @param $cacheKeyString
816 */
f931b74c 817 public static function resetMergeStats($cacheKeyString) {
ae6455b2 818 CRM_Core_BAO_PrevNextCache::deleteItem(NULL, "{$cacheKeyString}_stats");
63ef778e 819 }
820
bc854509 821 /**
822 * Get merge outcome statistics.
823 *
824 * @param string $cacheKeyString
825 *
826 * @return array
827 * Array of how many were merged and how many were skipped.
e13fa54b 828 *
829 * @throws \CiviCRM_API3_Exception
bc854509 830 */
f931b74c 831 public static function getMergeStats($cacheKeyString) {
e13fa54b 832 $stats = civicrm_api3('Dedupe', 'get', ['cachekey' => "{$cacheKeyString}_stats", 'sequential' => 1])['values'];
63ef778e 833 if (!empty($stats)) {
e13fa54b 834 return $stats[0]['data'];
63ef778e 835 }
e13fa54b 836 return [];
63ef778e 837 }
838
bc854509 839 /**
840 * Get merge statistics message.
841 *
3743fd3d 842 * @param array $stats
bc854509 843 *
844 * @return string
845 */
3743fd3d
CW
846 public static function getMergeStatsMsg($stats) {
847 $msg = '';
63ef778e 848 if (!empty($stats['merged'])) {
2c16165a 849 $msg = '<p>' . ts('One contact merged.', [
518fa0ee
SL
850 'count' => $stats['merged'],
851 'plural' => '%count contacts merged.',
852 ]) . '</p>';
63ef778e 853 }
854 if (!empty($stats['skipped'])) {
2c16165a 855 $msg .= '<p>' . ts('One contact was skipped.', [
518fa0ee
SL
856 'count' => $stats['skipped'],
857 'plural' => '%count contacts were skipped.',
858 ]) . '</p>';
63ef778e 859 }
860 return $msg;
861 }
862
6a488035 863 /**
100fef9d 864 * Merge given set of contacts. Performs core operation.
6a488035 865 *
98997235
TO
866 * @param array $dupePairs
867 * Set of pair of contacts for whom merge is to be done.
868 * @param array $cacheParams
869 * Prev-next-cache params based on which next pair of contacts are computed.
6a488035 870 * Generally used with batch-merge.
98997235
TO
871 * @param string $mode
872 * Helps decide how to behave when there are conflicts.
6a488035
TO
873 * A 'safe' value skips the merge if there are any un-resolved conflicts.
874 * Does a force merge otherwise (aggressive mode).
6a488035 875 *
77b97be7 876 * @param bool $redirectForPerformance
3058f4d9 877 * Redirect to a url for batch processing.
878 *
879 * @param bool $checkPermissions
880 * Respect logged in user permissions.
77b97be7
EM
881 *
882 * @return array|bool
b068bfde 883 *
884 * @throws \API_Exception
885 * @throws \CRM_Core_Exception
886 * @throws \CiviCRM_API3_Exception
6a488035 887 */
2c16165a 888 public static function merge($dupePairs = [], $cacheParams = [], $mode = 'safe',
889 $redirectForPerformance = FALSE, $checkPermissions = TRUE
6a488035 890 ) {
9c1bc317 891 $cacheKeyString = $cacheParams['cache_key_string'] ?? NULL;
2c16165a 892 $resultStats = ['merged' => [], 'skipped' => []];
6a488035
TO
893
894 // we don't want dupe caching to get reset after every-merge, and therefore set the
0626851e 895 CRM_Core_Config::setPermitCacheFlushMode(FALSE);
2c16165a 896 $deletedContacts = [];
6a488035
TO
897
898 while (!empty($dupePairs)) {
78272750 899 foreach ($dupePairs as $index => $dupes) {
900 if (in_array($dupes['dstID'], $deletedContacts) || in_array($dupes['srcID'], $deletedContacts)) {
901 unset($dupePairs[$index]);
902 continue;
903 }
b068bfde 904 if (($result = self::dedupePair($dupes, $mode, $checkPermissions, $cacheKeyString)) === FALSE) {
905 unset($dupePairs[$index]);
906 continue;
907 }
908 if (!empty($result['merged'])) {
909 $deletedContacts[] = $result['merged'][0]['other_id'];
910 $resultStats['merged'][] = ($result['merged'][0]);
911 }
912 else {
913 $resultStats['skipped'][] = ($result['skipped'][0]);
6a488035 914 }
6a488035
TO
915 }
916
917 if ($cacheKeyString && !$redirectForPerformance) {
918 // retrieve next pair of dupes
2988f5c7 919 // @todo call getDuplicatePairs.
6a488035
TO
920 $dupePairs = CRM_Core_BAO_PrevNextCache::retrieve($cacheKeyString,
921 $cacheParams['join'],
66eceb0b 922 $cacheParams['where'],
923 0,
ed3c1599 924 $cacheParams['limit'],
2c16165a 925 [],
66eceb0b 926 '',
927 FALSE
6a488035
TO
928 );
929 }
930 else {
931 // do not proceed. Terminate the loop
932 unset($dupePairs);
933 }
934 }
63ef778e 935
936 CRM_Dedupe_Merger::updateMergeStats($cacheKeyString, $resultStats);
6a488035
TO
937 return $resultStats;
938 }
939
940 /**
941 * A function which uses various rules / algorithms for choosing which contact to bias to
942 * when there's a conflict (to handle "gotchas"). Plus the safest route to merge.
943 *
98997235
TO
944 * @param int $mainId
945 * Main contact with whom merge has to happen.
946 * @param int $otherId
947 * Duplicate contact which would be deleted after merge operation.
948 * @param array $migrationInfo
949 * Array of information about which elements to merge.
950 * @param string $mode
951 * Helps decide how to behave when there are conflicts.
13919cf8
J
952 * - A 'safe' value skips the merge if there are any un-resolved conflicts.
953 * - Does a force merge otherwise (aggressive mode).
6a488035 954 *
ad37ac8e 955 * @param array $conflicts
a2abc387 956 * An empty array to be filed with conflict information.
ad37ac8e 957 *
77b97be7 958 * @return bool
ffa59d18 959 *
960 * @throws \CRM_Core_Exception
961 * @throws \CiviCRM_API3_Exception
962 * @throws \API_Exception
6a488035 963 */
2c16165a 964 public static function skipMerge($mainId, $otherId, &$migrationInfo, $mode = 'safe', &$conflicts = []) {
0971143c 965
403400d9 966 $conflicts = self::getConflicts($migrationInfo, $mainId, $otherId, $mode)['conflicts'];
d73a022b 967 // A hook could have set skip_merge in order to alter merge behaviour.
968 // This is a something we might ideally deprecate since they really 'should'
969 // mess with the conflicts array instead.
970 return (bool) ($migrationData['skip_merge'] ?? !empty($conflicts));
6a488035
TO
971 }
972
d48ad2c0 973 /**
974 * Compare 2 addresses to see if they are the same.
975 *
976 * @param array $mainAddress
977 * @param array $comparisonAddress
978 *
979 * @return bool
980 */
518fa0ee 981 public static function locationIsSame($mainAddress, $comparisonAddress) {
ffa59d18 982 $keysToIgnore = self::ignoredFields();
d48ad2c0 983 foreach ($comparisonAddress as $field => $value) {
984 if (in_array($field, $keysToIgnore)) {
985 continue;
986 }
00443529 987 if ((!empty($value) || $value === '0') && isset($mainAddress[$field]) && $mainAddress[$field] != $value) {
d48ad2c0 988 return FALSE;
989 }
990 }
991 return TRUE;
992 }
993
34917198
J
994 /**
995 * A function to build an array of information about location blocks that is
996 * required when merging location fields
997 *
998 * @return array
999 */
1000 public static function getLocationBlockInfo() {
723c5a81 1001 return [
2c16165a 1002 'address' => [
34917198
J
1003 'label' => 'Address',
1004 'displayField' => 'display',
1005 'sortString' => 'location_type_id',
1006 'hasLocation' => TRUE,
1007 'hasType' => FALSE,
2c16165a 1008 ],
1009 'email' => [
34917198 1010 'label' => 'Email',
2a7e1ddc 1011 'displayField' => 'display',
34917198
J
1012 'sortString' => 'location_type_id',
1013 'hasLocation' => TRUE,
1014 'hasType' => FALSE,
2c16165a 1015 ],
1016 'im' => [
34917198
J
1017 'label' => 'IM',
1018 'displayField' => 'name',
1019 'sortString' => 'location_type_id,provider_id',
1020 'hasLocation' => TRUE,
1021 'hasType' => 'provider_id',
2c16165a 1022 ],
1023 'phone' => [
34917198
J
1024 'label' => 'Phone',
1025 'displayField' => 'phone',
1026 'sortString' => 'location_type_id,phone_type_id',
1027 'hasLocation' => TRUE,
1028 'hasType' => 'phone_type_id',
2c16165a 1029 ],
1030 'website' => [
34917198
J
1031 'label' => 'Website',
1032 'displayField' => 'url',
1033 'sortString' => 'website_type_id',
1034 'hasLocation' => FALSE,
1035 'hasType' => 'website_type_id',
2c16165a 1036 ],
1037 ];
34917198
J
1038 }
1039
6a488035
TO
1040 /**
1041 * A function to build an array of information required by merge function and the merge UI.
1042 *
98997235
TO
1043 * @param int $mainId
1044 * Main contact with whom merge has to happen.
1045 * @param int $otherId
1046 * Duplicate contact which would be deleted after merge operation.
201057c0 1047 * @param bool $checkPermissions
1048 * Should the logged in user's permissions be ignore. Setting this to false is
1049 * highly risky as it could cause data to be lost due to conflicts not showing up.
1050 * OTOH there is a risk a merger might view custom data they do not have permission to.
1051 * Hence for now only making this really explicit and making it reflect perms in
1052 * an api call.
1053 *
1054 * @todo review permissions issue!
6a488035 1055 *
77b97be7 1056 * @return array|bool|int
1e3e0eed
J
1057 *
1058 * rows => An array of arrays, each is row of merge information for the table
1059 * Format: move_fieldname, eg: move_contact_type
1060 * main => Value associated with the main contact
1061 * other => Value associated with the other contact
1062 * title => The title of the field to display in the merge table
1063 *
1064 * elements => An array of form elements for the merge UI
1065 *
1066 * rel_table_elements => An array of form elements for the merge UI for
1067 * entities related to the contact (eg: checkbox to move 'mailings')
1068 *
1e3e0eed
J
1069 * rel_tables => Stores the tables that have related entities for the contact
1070 * for example mailings, groups
1071 *
1072 * main_details => An array of core contact field values, eg: first_name, etc.
1073 * location_blocks => An array of location block data for the main contact
1074 * stored as the 'result' of an API call.
1075 * eg: main_details['location_blocks']['address'][0]['id']
1076 * eg: main_details['location_blocks']['email'][1]['id']
1077 *
1078 * other_details => As above, but for the 'other' contact
1079 *
1080 * migration_info => Stores the 'default' merge actions for each field which
1081 * is used when programatically merging contacts. It contains instructions
1082 * to move all fields from the 'other' contact to the 'main' contact, as
1083 * though the form had been submitted with those options.
1084 *
3058f4d9 1085 * @throws \CRM_Core_Exception
1086 * @throws \CiviCRM_API3_Exception
1087 * @throws \Exception
6a488035 1088 */
201057c0 1089 public static function getRowsElementsAndInfo($mainId, $otherId, $checkPermissions = TRUE) {
6a488035 1090 $qfZeroBug = 'e8cddb72-a257-11dc-b9cc-0016d3330ee9';
74a6069d 1091 $fields = self::getMergeFieldsMetadata();
6a488035 1092
fcc3d8ee 1093 $main = self::getMergeContactDetails($mainId);
1094 $other = self::getMergeContactDetails($otherId);
6a488035 1095
4eea8606 1096 $compareFields = self::retrieveFields($main, $other);
6a488035 1097
2c16165a 1098 $rows = $elements = $relTableElements = $migrationInfo = [];
6a488035 1099
4eea8606 1100 foreach ($compareFields['contact'] as $field) {
723c5a81 1101 if ($field === 'contact_sub_type') {
4d5f18cc 1102 // CRM-15681 don't display sub-types in UI
1103 continue;
1104 }
1ce87307 1105 $rows["move_$field"] = [
1106 'main' => self::getFieldValueAndLabel($field, $main)['label'],
1107 'other' => self::getFieldValueAndLabel($field, $other)['label'],
1108 'title' => $fields[$field]['title'],
1109 ];
2fd72602 1110
bdc6e6ee 1111 $value = self::getFieldValueAndLabel($field, $other)['value'];
1112 //CRM-14334
1113 if ($value === NULL || $value == '') {
1114 $value = 'null';
1115 }
1116 if ($value === 0 or $value === '0') {
1117 $value = $qfZeroBug;
1118 }
1119 if (is_array($value) && empty($value[1])) {
1120 $value[1] = NULL;
1121 }
13919cf8 1122
bdc6e6ee 1123 // Display a checkbox to migrate, only if the values are different
1124 if ($value != $main[$field]) {
1125 $elements[] = [
3d1d395a 1126 0 => 'advcheckbox',
1127 1 => "move_$field",
1128 2 => NULL,
1129 3 => NULL,
1130 4 => NULL,
1131 5 => $value,
1132 'is_checked' => (!isset($main[$field]) || $main[$field] === ''),
bdc6e6ee 1133 ];
1134 }
13919cf8 1135
bdc6e6ee 1136 $migrationInfo["move_$field"] = $value;
6a488035
TO
1137 }
1138
eb61dc07
J
1139 // Handle location blocks.
1140 // @todo OpenID not in API yet, so is not supported here.
65a35632 1141
087f00ec 1142 // Set up useful information about the location blocks
34917198 1143 $locationBlocks = self::getLocationBlockInfo();
087f00ec 1144
1a90ac89 1145 $locations = ['main' => [], 'other' => []];
6a488035 1146
087f00ec 1147 foreach ($locationBlocks as $blockName => $blockInfo) {
18226102 1148 list($locations, $rows, $elements, $migrationInfo) = self::addLocationFieldInfo($mainId, $otherId, $blockInfo, $blockName, $locations, $rows, $elements, $migrationInfo);
34917198 1149 } // End loop through each location block entity
6a488035
TO
1150
1151 // add the related tables and unset the ones that don't sport any of the duplicate contact's info
9287a0b7 1152 $mergeHandler = new CRM_Dedupe_MergeHandler((int) $mainId, (int) $otherId);
1153 $relTables = $mergeHandler->getTablesRelatedToTheMergePair();
6a488035 1154 foreach ($relTables as $name => $null) {
2c16165a 1155 $relTableElements[] = ['checkbox', "move_$name"];
6a488035
TO
1156 $migrationInfo["move_$name"] = 1;
1157
1158 $relTables[$name]['main_url'] = str_replace('$cid', $mainId, $relTables[$name]['url']);
1159 $relTables[$name]['other_url'] = str_replace('$cid', $otherId, $relTables[$name]['url']);
8372e605 1160 if ($name === 'rel_table_users') {
1161 // @todo - this user url stuff is only needed for the form layer - move to CRM_Contact_Form_Merge
1162 $relTables[$name]['main_url'] = str_replace('%ufid', CRM_Core_BAO_UFMatch::getUFId($otherId), $relTables[$name]['url']);
1163 $relTables[$name]['other_url'] = str_replace('%ufid', CRM_Core_BAO_UFMatch::getUFId($otherId), $relTables[$name]['url']);
6a488035 1164 }
723c5a81 1165 if ($name === 'rel_table_memberships') {
7d84a47d
JP
1166 //Enable 'add new' checkbox if main contact does not contain any membership similar to duplicate contact.
1167 $attributes = ['checked' => 'checked'];
1168 $otherContactMemberships = CRM_Member_BAO_Membership::getAllContactMembership($otherId);
1169 foreach ($otherContactMemberships as $membership) {
1170 $mainMembership = CRM_Member_BAO_Membership::getContactMembership($mainId, $membership['membership_type_id'], FALSE);
1171 if ($mainMembership) {
1172 $attributes = [];
1173 }
1174 }
2c16165a 1175 $elements[] = [
1176 'checkbox',
1177 "operation[move_{$name}][add]",
1178 NULL,
1179 ts('add new'),
1180 $attributes,
1181 ];
6a488035
TO
1182 $migrationInfo["operation"]["move_{$name}"]['add'] = 1;
1183 }
1184 }
1185 foreach ($relTables as $name => $null) {
1186 $relTables["move_$name"] = $relTables[$name];
1187 unset($relTables[$name]);
1188 }
1189
1190 // handle custom fields
201057c0 1191 $mainTree = CRM_Core_BAO_CustomGroup::getTree($main['contact_type'], NULL, $mainId, -1,
1192 CRM_Utils_Array::value('contact_sub_type', $main), NULL, TRUE, NULL, TRUE, $checkPermissions
6a488035 1193 );
0b330e6d 1194 $otherTree = CRM_Core_BAO_CustomGroup::getTree($main['contact_type'], NULL, $otherId, -1,
201057c0 1195 CRM_Utils_Array::value('contact_sub_type', $other), NULL, TRUE, NULL, TRUE, $checkPermissions
6a488035 1196 );
6a488035
TO
1197
1198 foreach ($otherTree as $gid => $group) {
6a488035
TO
1199 if (!isset($group['fields'])) {
1200 continue;
1201 }
1202
1203 foreach ($group['fields'] as $fid => $field) {
b1ff97eb 1204 $mainContactValue = $mainTree[$gid]['fields'][$fid]['customValue'] ?? NULL;
1205 $otherContactValue = $otherTree[$gid]['fields'][$fid]['customValue'] ?? NULL;
4eea8606 1206 if (in_array($fid, $compareFields['custom'])) {
b1ff97eb 1207 $rows["custom_group_$gid"]['title'] = $rows["custom_group_$gid"]['title'] ?? $group['title'];
1208
1209 if ($mainContactValue) {
1210 foreach ($mainContactValue as $valueId => $values) {
51a3717f 1211 $rows["move_custom_$fid"]['main'] = CRM_Core_BAO_CustomField::displayValue($values['data'], $fid);
6a488035
TO
1212 }
1213 }
b1ff97eb 1214 $value = 'null';
1215 if ($otherContactValue) {
1216 foreach ($otherContactValue as $valueId => $values) {
51a3717f 1217 $rows["move_custom_$fid"]['other'] = CRM_Core_BAO_CustomField::displayValue($values['data'], $fid);
6a488035
TO
1218 if ($values['data'] === 0 || $values['data'] === '0') {
1219 $values['data'] = $qfZeroBug;
d58a19a1 1220 }
6a488035 1221 $value = ($values['data']) ? $values['data'] : $value;
d58a19a1 1222 }
6a488035
TO
1223 }
1224 $rows["move_custom_$fid"]['title'] = $field['label'];
1225
2c16165a 1226 $elements[] = [
97be1890 1227 0 => 'advcheckbox',
1228 1 => "move_custom_$fid",
1229 2 => NULL,
1230 3 => NULL,
1231 4 => NULL,
1232 5 => $value,
1233 'is_checked' => (!isset($rows["move_custom_$fid"]['main']) || $rows["move_custom_$fid"]['main'] === ''),
2c16165a 1234 ];
6a488035
TO
1235 $migrationInfo["move_custom_$fid"] = $value;
1236 }
1237 }
1238 }
abb422f9 1239
2c16165a 1240 $result = [
6a488035
TO
1241 'rows' => $rows,
1242 'elements' => $elements,
1243 'rel_table_elements' => $relTableElements,
6a488035
TO
1244 'rel_tables' => $relTables,
1245 'main_details' => $main,
1246 'other_details' => $other,
1247 'migration_info' => $migrationInfo,
2c16165a 1248 ];
6a488035 1249
34917198
J
1250 $result['main_details']['location_blocks'] = $locations['main'];
1251 $result['other_details']['location_blocks'] = $locations['other'];
6a488035
TO
1252
1253 return $result;
1254 }
1255
1256 /**
1257 * Based on the provided two contact_ids and a set of tables, move the belongings of the
1258 * other contact to the main one - be it Location / CustomFields or Contact .. related info.
1259 * A superset of moveContactBelongings() function.
1260 *
98997235
TO
1261 * @param int $mainId
1262 * Main contact with whom merge has to happen.
1263 * @param int $otherId
1264 * Duplicate contact which would be deleted after merge operation.
77b97be7 1265 *
1600a9c0 1266 * @param array $migrationInfo
6a488035 1267 *
3058f4d9 1268 * @param bool $checkPermissions
1269 * Respect logged in user permissions.
1270 *
77b97be7 1271 * @return bool
fe3b8caf 1272 *
1273 * @throws \API_Exception
1274 * @throws \CRM_Core_Exception
1600a9c0 1275 * @throws \CiviCRM_API3_Exception
fe3b8caf 1276 * @throws \Civi\API\Exception\UnauthorizedException
6a488035 1277 */
3058f4d9 1278 public static function moveAllBelongings($mainId, $otherId, $migrationInfo, $checkPermissions = TRUE) {
6a488035
TO
1279 if (empty($migrationInfo)) {
1280 return FALSE;
1281 }
119664d6 1282 // Encapsulate in a transaction to avoid half-merges.
1283 $transaction = new CRM_Core_Transaction();
6a488035 1284
1acd98ec 1285 $contactType = $migrationInfo['main_details']['contact_type'];
6a488035 1286 $relTables = CRM_Dedupe_Merger::relTables();
35e0e03e 1287 $submittedCustomFields = $moveTables = $tableOperations = $removeTables = [];
ae2346d4 1288
54ed517e 1289 self::swapOutFieldsAffectedByQFZeroBug($migrationInfo);
6a488035 1290 foreach ($migrationInfo as $key => $value) {
08b00099 1291
723c5a81 1292 if (substr($key, 0, 12) === 'move_custom_' && $value != NULL) {
6a488035 1293 $submitted[substr($key, 5)] = $value;
ae2346d4 1294 $submittedCustomFields[] = substr($key, 12);
6a488035 1295 }
08b00099 1296 elseif (in_array(substr($key, 5), CRM_Dedupe_Merger::getContactFields()) && $value != NULL) {
6a488035
TO
1297 $submitted[substr($key, 5)] = $value;
1298 }
723c5a81 1299 elseif (substr($key, 0, 15) === 'move_rel_table_' and $value == '1') {
6a488035
TO
1300 $moveTables = array_merge($moveTables, $relTables[substr($key, 5)]['tables']);
1301 if (array_key_exists('operation', $migrationInfo)) {
1302 foreach ($relTables[substr($key, 5)]['tables'] as $table) {
1303 if (array_key_exists($key, $migrationInfo['operation'])) {
1304 $tableOperations[$table] = $migrationInfo['operation'][$key];
1305 }
1306 }
1307 }
1308 }
723c5a81 1309 elseif (substr($key, 0, 15) === 'move_rel_table_' and $value == '0') {
4099a9c5
AP
1310 $removeTables = array_merge($moveTables, $relTables[substr($key, 5)]['tables']);
1311 }
6a488035 1312 }
35e0e03e 1313 self::mergeLocations($mainId, $otherId, $migrationInfo);
6a488035 1314
6a488035 1315 // **** Do contact related migrations
1600a9c0 1316 // @todo - move all custom field processing to the move class & eventually have an
1317 // overridable DAO class for it.
1318 $customFieldBAO = new CRM_Core_BAO_CustomField();
1319 $customFieldBAO->move($otherId, $mainId, $submittedCustomFields);
eca28463 1320 // add the related tables and unset the ones that don't sport any of the duplicate contact's info
1321 $mergeHandler = new CRM_Dedupe_MergeHandler((int) $mainId, (int) $otherId);
1322 CRM_Dedupe_Merger::moveContactBelongings($mergeHandler, $moveTables, $tableOperations);
0556db5d 1323 unset($moveTables, $tableOperations);
6a488035 1324
4099a9c5
AP
1325 // **** Do table related removals
1326 if (!empty($removeTables)) {
1327 // **** CRM-20421
1328 CRM_Dedupe_Merger::removeContactBelongings($otherId, $removeTables);
2c16165a 1329 $removeTables = [];
4099a9c5
AP
1330 }
1331
6a488035 1332 // FIXME: fix gender, prefix and postfix, so they're edible by createProfileContact()
2c16165a 1333 $names['gender'] = ['newName' => 'gender_id', 'groupName' => 'gender'];
1334 $names['individual_prefix'] = [
1335 'newName' => 'prefix_id',
1336 'groupName' => 'individual_prefix',
1337 ];
1338 $names['individual_suffix'] = [
1339 'newName' => 'suffix_id',
1340 'groupName' => 'individual_suffix',
1341 ];
1342 $names['communication_style'] = [
1343 'newName' => 'communication_style_id',
1344 'groupName' => 'communication_style',
1345 ];
1346 $names['addressee'] = [
1347 'newName' => 'addressee_id',
1348 'groupName' => 'addressee',
1349 ];
1350 $names['email_greeting'] = [
1351 'newName' => 'email_greeting_id',
1352 'groupName' => 'email_greeting',
1353 ];
1354 $names['postal_greeting'] = [
1355 'newName' => 'postal_greeting_id',
1356 'groupName' => 'postal_greeting',
1357 ];
6a488035 1358 CRM_Core_OptionGroup::lookupValues($submitted, $names, TRUE);
6a488035 1359 // fix custom fields so they're edible by createProfileContact()
f941d73a 1360 $cFields = self::getCustomFieldMetadata($contactType);
6a488035
TO
1361
1362 if (!isset($submitted)) {
2c16165a 1363 $submitted = [];
6a488035
TO
1364 }
1365 foreach ($submitted as $key => $value) {
1600a9c0 1366 list($cFields, $submitted) = self::processCustomFields($mainId, $key, $cFields, $submitted, $value);
6a488035
TO
1367 }
1368
6a488035 1369 // move view only custom fields CRM-5362
2c16165a 1370 $viewOnlyCustomFields = [];
6a488035 1371 foreach ($submitted as $key => $value) {
ca38a45b 1372 $fid = CRM_Core_BAO_CustomField::getKeyID($key);
1373 if ($fid && array_key_exists($fid, $cFields) && !empty($cFields[$fid]['attributes']['is_view'])
1374 ) {
6a488035
TO
1375 $viewOnlyCustomFields[$key] = $value;
1376 }
1377 }
6a488035
TO
1378 // special case to set values for view only, CRM-5362
1379 if (!empty($viewOnlyCustomFields)) {
1380 $viewOnlyCustomFields['entityID'] = $mainId;
1381 CRM_Core_BAO_CustomValueTable::setValues($viewOnlyCustomFields);
1382 }
1383
1f0138dd
SL
1384 // dev/core#996 Ensure that the earliest created date is stored against the kept contact id
1385 $mainCreatedDate = civicrm_api3('Contact', 'getsingle', [
1386 'id' => $mainId,
1387 'return' => ['created_date'],
1388 ])['created_date'];
1389 $otherCreatedDate = civicrm_api3('Contact', 'getsingle', [
1390 'id' => $otherId,
1391 'return' => ['created_date'],
1392 ])['created_date'];
978b982d 1393 if ($otherCreatedDate < $mainCreatedDate && !empty($otherCreatedDate)) {
1f0138dd
SL
1394 CRM_Core_DAO::executeQuery("UPDATE civicrm_contact SET created_date = %1 WHERE id = %2", [
1395 1 => [$otherCreatedDate, 'String'],
1396 2 => [$mainId, 'Positive'],
1397 ]);
1398 }
1399
3058f4d9 1400 if (!$checkPermissions || (CRM_Core_Permission::check('merge duplicate contacts') &&
2c16165a 1401 CRM_Core_Permission::check('delete contacts'))
6a488035
TO
1402 ) {
1403 // if ext id is submitted then set it null for contact to be deleted
a7488080 1404 if (!empty($submitted['external_identifier'])) {
6a488035
TO
1405 $query = "UPDATE civicrm_contact SET external_identifier = null WHERE id = {$otherId}";
1406 CRM_Core_DAO::executeQuery($query);
1407 }
2c16165a 1408 civicrm_api3('contact', 'delete', ['id' => $otherId]);
6a488035 1409 }
6a488035 1410
4d5f18cc 1411 // CRM-15681 merge sub_types
35522279
J
1412 if ($other_sub_types = CRM_Utils_Array::value('contact_sub_type', $migrationInfo['other_details'])) {
1413 if ($main_sub_types = CRM_Utils_Array::value('contact_sub_type', $migrationInfo['main_details'])) {
481a74f4 1414 $submitted['contact_sub_type'] = array_unique(array_merge($main_sub_types, $other_sub_types));
0db6c3e1
TO
1415 }
1416 else {
4d5f18cc 1417 $submitted['contact_sub_type'] = $other_sub_types;
1418 }
1419 }
1420
6a488035
TO
1421 // **** Update contact related info for the main contact
1422 if (!empty($submitted)) {
1423 $submitted['contact_id'] = $mainId;
1424
1425 //update current employer field
1426 if ($currentEmloyerId = CRM_Utils_Array::value('current_employer_id', $submitted)) {
1427 if (!CRM_Utils_System::isNull($currentEmloyerId)) {
1428 $submitted['current_employer'] = $submitted['current_employer_id'];
0db6c3e1
TO
1429 }
1430 else {
6a488035
TO
1431 $submitted['current_employer'] = '';
1432 }
1433 unset($submitted['current_employer_id']);
1434 }
1435
00f3da2e 1436 //CRM-14312 include prefix/suffix from mainId if not overridden for proper construction of display/sort name
481a74f4 1437 if (!isset($submitted['prefix_id']) && !empty($migrationInfo['main_details']['prefix_id'])) {
00f3da2e
BS
1438 $submitted['prefix_id'] = $migrationInfo['main_details']['prefix_id'];
1439 }
481a74f4 1440 if (!isset($submitted['suffix_id']) && !empty($migrationInfo['main_details']['suffix_id'])) {
00f3da2e
BS
1441 $submitted['suffix_id'] = $migrationInfo['main_details']['suffix_id'];
1442 }
9b0e7e51 1443 $null = [];
1444 CRM_Contact_BAO_Contact::createProfileContact($submitted, $null, $mainId);
6a488035 1445 }
119664d6 1446 $transaction->commit();
1273d77c 1447 CRM_Utils_Hook::post('merge', 'Contact', $mainId);
f00283b5 1448 self::createMergeActivities($mainId, $otherId);
5ea06a7b 1449
6a488035
TO
1450 return TRUE;
1451 }
16254ae1
ARW
1452
1453 /**
f00283b5 1454 * Get fields in the contact table suitable for merging.
1455 *
a6c01b45 1456 * @return array
f00283b5 1457 * Array of field names to be potentially merged.
16254ae1 1458 */
00be9182 1459 public static function getContactFields() {
16254ae1 1460 $contactFields = CRM_Contact_DAO_Contact::fields();
2c16165a 1461 $invalidFields = [
353ffa53 1462 'api_key',
353ffa53
TO
1463 'created_date',
1464 'display_name',
1465 'hash',
1466 'id',
1467 'modified_date',
1468 'primary_contact_id',
1469 'sort_name',
c301f76e 1470 'user_unique_id',
2c16165a 1471 ];
bdd7870e
RN
1472 foreach ($contactFields as $field => $value) {
1473 if (in_array($field, $invalidFields)) {
1474 unset($contactFields[$field]);
1475 }
1476 }
16254ae1
ARW
1477 return array_keys($contactFields);
1478 }
ada104d5
AW
1479
1480 /**
1481 * Added for CRM-12695
c301f76e 1482 * Based on the contactID provided
ada104d5
AW
1483 * add/update membership(s) to related contacts
1484 *
c301f76e 1485 * @param int $contactID
723c5a81 1486 *
1487 * @throws \CRM_Core_Exception
1488 * @throws \CiviCRM_API3_Exception
ada104d5 1489 */
00be9182 1490 public static function addMembershipToRealtedContacts($contactID) {
ada104d5
AW
1491 $dao = new CRM_Member_DAO_Membership();
1492 $dao->contact_id = $contactID;
1493 $dao->is_test = 0;
1494 $dao->find();
1495
1496 //checks membership of contact itself
1497 while ($dao->fetch()) {
1498 $relationshipTypeId = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $dao->membership_type_id, 'relationship_type_id', 'id');
1499 if ($relationshipTypeId) {
2c16165a 1500 $membershipParams = [
ada104d5
AW
1501 'id' => $dao->id,
1502 'contact_id' => $dao->contact_id,
1503 'membership_type_id' => $dao->membership_type_id,
1504 'join_date' => CRM_Utils_Date::isoToMysql($dao->join_date),
1505 'start_date' => CRM_Utils_Date::isoToMysql($dao->start_date),
1506 'end_date' => CRM_Utils_Date::isoToMysql($dao->end_date),
1507 'source' => $dao->source,
21dfd5f5 1508 'status_id' => $dao->status_id,
2c16165a 1509 ];
ada104d5
AW
1510 // create/update membership(s) for related contact(s)
1511 CRM_Member_BAO_Membership::createRelatedMemberships($membershipParams, $dao);
1512 } // end of if relationshipTypeId
1513 }
1514 }
96025800 1515
f00283b5 1516 /**
1517 * Create activities tracking the merge on affected contacts.
1518 *
1519 * @param int $mainId
1520 * @param int $otherId
1521 *
1522 * @throws \CiviCRM_API3_Exception
1523 */
1524 public static function createMergeActivities($mainId, $otherId) {
2c16165a 1525 $params = [
f00283b5 1526 1 => $otherId,
1527 2 => $mainId,
2c16165a 1528 ];
1529 $activity = civicrm_api3('activity', 'create', [
dc33398e 1530 'source_contact_id' => CRM_Core_Session::getLoggedInContactID() ? CRM_Core_Session::getLoggedInContactID() :
518fa0ee 1531 $mainId,
f00283b5 1532 'subject' => ts('Contact ID %1 has been merged and deleted.', $params),
1533 'target_contact_id' => $mainId,
1534 'activity_type_id' => 'Contact Merged',
1535 'status_id' => 'Completed',
2c16165a 1536 ]);
1537 if (civicrm_api3('Setting', 'getvalue', [
1538 'name' => 'contact_undelete',
1539 'group' => 'CiviCRM Preferences',
1540 ])) {
1541 civicrm_api3('activity', 'create', [
dc33398e 1542 'source_contact_id' => CRM_Core_Session::getLoggedInContactID() ? CRM_Core_Session::getLoggedInContactID() :
518fa0ee 1543 $otherId,
4d834a99 1544 'subject' => ts('Contact ID %1 has been merged into Contact ID %2 and deleted.', $params),
1545 'target_contact_id' => $otherId,
1546 'activity_type_id' => 'Contact Deleted by Merge',
1547 'parent_id' => $activity['id'],
1548 'status_id' => 'Completed',
2c16165a 1549 ]);
4d834a99 1550 }
f00283b5 1551 }
1552
2988f5c7 1553 /**
1554 * Get Duplicate Pairs based on a rule for a group.
1555 *
1556 * @param int $rule_group_id
1557 * @param int $group_id
1558 * @param bool $reloadCacheIfEmpty
917acf6f 1559 * Should the cache be reloaded if empty - this must be false when in a dedupe action!
2988f5c7 1560 * @param int $batchLimit
1561 * @param bool $isSelected
5721d85e 1562 * Limit to selected pairs.
66eceb0b 1563 * @param bool $includeConflicts
e23e26ec 1564 * @param array $criteria
1565 * Additional criteria to narrow down the merge group.
2988f5c7 1566 *
3058f4d9 1567 * @param bool $checkPermissions
1568 * Respect logged in user permissions.
1569 *
21a95d83 1570 * @param int $searchLimit
1571 * Limit to searching for matches against this many contacts.
1572 *
c41f68a5 1573 * @param int $isForceNewSearch
1574 * Should a new search be forced, bypassing any cache retrieval.
1575 *
2988f5c7 1576 * @return array
518fa0ee 1577 * Array of matches meeting the criteria.
9c5fe572 1578 *
1579 * @throws \CRM_Core_Exception
1580 * @throws \CiviCRM_API3_Exception
2988f5c7 1581 */
c41f68a5 1582 public static function getDuplicatePairs($rule_group_id, $group_id, $reloadCacheIfEmpty, $batchLimit, $isSelected, $includeConflicts = TRUE, $criteria = [], $checkPermissions = TRUE, $searchLimit = 0, $isForceNewSearch = 0) {
1583 $dupePairs = $isForceNewSearch ? [] : self::getCachedDuplicateMatches($rule_group_id, $group_id, $batchLimit, $isSelected, $includeConflicts, $criteria, $checkPermissions, $searchLimit);
2988f5c7 1584 if (empty($dupePairs) && $reloadCacheIfEmpty) {
1585 // If we haven't found any dupes, probably cache is empty.
66eceb0b 1586 // Try filling cache and give another try. We don't need to specify include conflicts here are there will not be any
1587 // until we have done some processing.
e1e24a57 1588 CRM_Core_BAO_PrevNextCache::refillCache($rule_group_id, $group_id, $criteria, $checkPermissions, $searchLimit);
997a03fe 1589 return self::getCachedDuplicateMatches($rule_group_id, $group_id, $batchLimit, $isSelected, FALSE, $criteria, $checkPermissions, $searchLimit);
2988f5c7 1590 }
1591 return $dupePairs;
1592 }
1593
2ae26001 1594 /**
1595 * Get the cache key string for the merge action.
1596 *
1597 * @param int $rule_group_id
1598 * @param int $group_id
e23e26ec 1599 * @param array $criteria
1600 * Additional criteria to narrow down the merge group.
1601 * Currently we are only supporting the key 'contact' within it.
3058f4d9 1602 * @param bool $checkPermissions
1603 * Respect the users permissions.
997a03fe 1604 * @param int $searchLimit
1605 * Number of contacts to seek dupes for (we need this because if
1606 * we change it the results won't be refreshed otherwise. Changing the limit
1607 * from 100 to 1000 SHOULD result in a new dedupe search).
3058f4d9 1608 *
2ae26001 1609 * @return string
1610 */
997a03fe 1611 public static function getMergeCacheKeyString($rule_group_id, $group_id, $criteria, $checkPermissions, $searchLimit) {
2ae26001 1612 $contactType = CRM_Dedupe_BAO_RuleGroup::getContactTypeForRuleGroup($rule_group_id);
9d2f6d53 1613 $cacheKeyString = "merge_{$contactType}";
2ae26001 1614 $cacheKeyString .= $rule_group_id ? "_{$rule_group_id}" : '_0';
1615 $cacheKeyString .= $group_id ? "_{$group_id}" : '_0';
997a03fe 1616 $cacheKeyString .= '_' . (int) $searchLimit;
629dd4f3 1617 $cacheKeyString .= !empty($criteria) ? md5(serialize($criteria)) : '_0';
3058f4d9 1618 if ($checkPermissions) {
1619 $contactID = CRM_Core_Session::getLoggedInContactID();
1620 if (!$contactID) {
1621 // Distinguish between no permission check & no logged in user.
1622 $contactID = 'null';
1623 }
1624 $cacheKeyString .= '_' . $contactID;
1625 }
1626 else {
1627 $cacheKeyString .= '_0';
1628 }
2ae26001 1629 return $cacheKeyString;
1630 }
1631
74a6069d 1632 /**
1633 * Get the metadata for the merge fields.
1634 *
1635 * This is basically the contact metadata, augmented with fields to
1636 * represent email greeting, postal greeting & addressee.
1637 *
1638 * @return array
1639 */
1640 public static function getMergeFieldsMetadata() {
1641 if (isset(\Civi::$statics[__CLASS__]) && isset(\Civi::$statics[__CLASS__]['merge_fields_metadata'])) {
1642 return \Civi::$statics[__CLASS__]['merge_fields_metadata'];
1643 }
1644 $fields = CRM_Contact_DAO_Contact::fields();
2c16165a 1645 static $optionValueFields = [];
74a6069d 1646 if (empty($optionValueFields)) {
1647 $optionValueFields = CRM_Core_OptionValue::getFields();
1648 }
1649 foreach ($optionValueFields as $field => $params) {
1650 $fields[$field]['title'] = $params['title'];
1651 }
1652 \Civi::$statics[__CLASS__]['merge_fields_metadata'] = $fields;
1653 return \Civi::$statics[__CLASS__]['merge_fields_metadata'];
1654 }
1655
1656 /**
1657 * Get the details of the contact to be merged.
1658 *
1659 * @param int $contact_id
74a6069d 1660 *
1661 * @return array
1662 *
1663 * @throws CRM_Core_Exception
1664 */
fcc3d8ee 1665 public static function getMergeContactDetails($contact_id) {
2c16165a 1666 $params = [
74a6069d 1667 'contact_id' => $contact_id,
1668 'version' => 3,
2c16165a 1669 'return' => array_merge(['display_name'], self::getContactFields()),
1670 ];
74a6069d 1671 $result = civicrm_api('contact', 'get', $params);
1672
1673 // CRM-18480: Cancel the process if the contact is already deleted
1674 if (isset($result['values'][$contact_id]['contact_is_deleted']) && !empty($result['values'][$contact_id]['contact_is_deleted'])) {
2c16165a 1675 throw new CRM_Core_Exception(ts('Cannot merge because one contact (ID %1) has been deleted.', [
fcc3d8ee 1676 1 => $contact_id,
2c16165a 1677 ]));
74a6069d 1678 }
1679
1680 return $result['values'][$contact_id];
1681 }
1682
92a77772 1683 /**
1684 * Merge location.
1685 *
1686 * Based on the data in the $locationMigrationInfo merge the locations for 2 contacts.
1687 *
1688 * The data is in the format received from the merge form (which is a fairly confusing format).
1689 *
1690 * It is converted into an array of DAOs which is passed to the alterLocationMergeData hook
1691 * before saving or deleting the DAOs. A new hook is added to allow these to be altered after they have
1692 * been calculated and before saving because
1693 * - the existing format & hook combo is so confusing it is hard for developers to change & inherently fragile
1694 * - passing to a hook right before save means calculations only have to be done once
1695 * - the existing pattern of passing dissimilar data to the same (merge) hook with a different 'type' is just
1696 * ugly.
1697 *
1698 * The use of the new hook is tested, including the fact it is called before contributions are merged, as this
16d4e0b7 1699 * is likely to be significant data in merge hooks.
92a77772 1700 *
1701 * @param int $mainId
1702 * @param int $otherId
92a77772 1703 *
1704 * @param array $migrationInfo
1705 * Migration info for the merge. This is passed to the hook as informational only.
1706 */
35e0e03e 1707 public static function mergeLocations($mainId, $otherId, $migrationInfo) {
1708 foreach ($migrationInfo as $key => $value) {
723c5a81 1709 $isLocationField = (substr($key, 0, 14) === 'move_location_' and $value != NULL);
35e0e03e 1710 if (!$isLocationField) {
1711 continue;
1712 }
92a77772 1713 $locField = explode('_', $key);
1714 $fieldName = $locField[2];
1715 $fieldCount = $locField[3];
1716
1717 // Set up the operation type (add/overwrite)
1718 // Ignore operation for websites
1719 // @todo Tidy this up
1720 $operation = 0;
723c5a81 1721 if ($fieldName !== 'website') {
9c1bc317 1722 $operation = $migrationInfo['location_blocks'][$fieldName][$fieldCount]['operation'] ?? NULL;
92a77772 1723 }
1724 // default operation is overwrite.
1725 if (!$operation) {
1726 $operation = 2;
1727 }
1728 $locBlocks[$fieldName][$fieldCount]['operation'] = $operation;
1729 }
2c16165a 1730 $blocksDAO = [];
92a77772 1731
1732 // @todo Handle OpenID (not currently in API).
1733 if (!empty($locBlocks)) {
1734 $locationBlocks = self::getLocationBlockInfo();
1735
2c16165a 1736 $primaryBlockIds = CRM_Contact_BAO_Contact::getLocBlockIds($mainId, ['is_primary' => 1]);
1737 $billingBlockIds = CRM_Contact_BAO_Contact::getLocBlockIds($mainId, ['is_billing' => 1]);
92a77772 1738
1739 foreach ($locBlocks as $name => $block) {
2c16165a 1740 $blocksDAO[$name] = ['delete' => [], 'update' => []];
92a77772 1741 if (!is_array($block) || CRM_Utils_System::isNull($block)) {
1742 continue;
1743 }
1744 $daoName = 'CRM_Core_DAO_' . $locationBlocks[$name]['label'];
abd0b852 1745 $changePrimary = FALSE;
92a77772 1746 $primaryDAOId = (array_key_exists($name, $primaryBlockIds)) ? array_pop($primaryBlockIds[$name]) : NULL;
1747 $billingDAOId = (array_key_exists($name, $billingBlockIds)) ? array_pop($billingBlockIds[$name]) : NULL;
1748
1749 foreach ($block as $blkCount => $values) {
9c1bc317 1750 $otherBlockId = $migrationInfo['other_details']['location_blocks'][$name][$blkCount]['id'] ?? NULL;
92a77772 1751 $mainBlockId = CRM_Utils_Array::value('mainContactBlockId', $migrationInfo['location_blocks'][$name][$blkCount], 0);
1752 if (!$otherBlockId) {
1753 continue;
1754 }
1755
1756 // For the block which belongs to other-contact, link the location block to main-contact
1757 $otherBlockDAO = new $daoName();
1758 $otherBlockDAO->contact_id = $mainId;
1759
1760 // Get the ID of this block on the 'other' contact, otherwise skip
1761 $otherBlockDAO->id = $otherBlockId;
1762
1763 // Add/update location and type information from the form, if applicable
1764 if ($locationBlocks[$name]['hasLocation']) {
9c1bc317 1765 $locTypeId = $migrationInfo['location_blocks'][$name][$blkCount]['locTypeId'] ?? NULL;
92a77772 1766 $otherBlockDAO->location_type_id = $locTypeId;
1767 }
1768 if ($locationBlocks[$name]['hasType']) {
9c1bc317 1769 $typeTypeId = $migrationInfo['location_blocks'][$name][$blkCount]['typeTypeId'] ?? NULL;
92a77772 1770 $otherBlockDAO->{$locationBlocks[$name]['hasType']} = $typeTypeId;
1771 }
1772
abd0b852
J
1773 // If we're deliberately setting this as primary then add the flag
1774 // and remove it from the current primary location (if there is one).
1775 // But only once for each entity.
9c1bc317 1776 $set_primary = $migrationInfo['location_blocks'][$name][$blkCount]['set_other_primary'] ?? NULL;
9e848fc4 1777 if (!$changePrimary && $set_primary == "1") {
abd0b852
J
1778 $otherBlockDAO->is_primary = 1;
1779 if ($primaryDAOId) {
1780 $removePrimaryDAO = new $daoName();
1781 $removePrimaryDAO->id = $primaryDAOId;
1782 $removePrimaryDAO->is_primary = 0;
1783 $blocksDAO[$name]['update'][$primaryDAOId] = $removePrimaryDAO;
1784 }
1785 $changePrimary = TRUE;
1786 }
1787 // Otherwise, if main contact already has primary, set it to 0.
1788 elseif ($primaryDAOId) {
92a77772 1789 $otherBlockDAO->is_primary = 0;
1790 }
abd0b852
J
1791
1792 // If the main contact already has a billing location, set this to 0.
92a77772 1793 if ($billingDAOId) {
1794 $otherBlockDAO->is_billing = 0;
1795 }
1796
1797 $operation = CRM_Utils_Array::value('operation', $values, 2);
1798 // overwrite - need to delete block which belongs to main-contact.
1799 if (!empty($mainBlockId) && ($operation == 2)) {
1800 $deleteDAO = new $daoName();
1801 $deleteDAO->id = $mainBlockId;
1802 $deleteDAO->find(TRUE);
1803
1804 // if we about to delete a primary / billing block, set the flags for new block
1805 // that we going to assign to main-contact
1806 if ($primaryDAOId && ($primaryDAOId == $deleteDAO->id)) {
1807 $otherBlockDAO->is_primary = 1;
1808 }
1809 if ($billingDAOId && ($billingDAOId == $deleteDAO->id)) {
1810 $otherBlockDAO->is_billing = 1;
1811 }
1812 $blocksDAO[$name]['delete'][$deleteDAO->id] = $deleteDAO;
1813 }
1814 $blocksDAO[$name]['update'][$otherBlockDAO->id] = $otherBlockDAO;
1815 }
1816 }
1817 }
1818
1819 CRM_Utils_Hook::alterLocationMergeData($blocksDAO, $mainId, $otherId, $migrationInfo);
1820 foreach ($blocksDAO as $blockDAOs) {
1821 if (!empty($blockDAOs['update'])) {
1822 foreach ($blockDAOs['update'] as $blockDAO) {
1823 $blockDAO->save();
1824 }
1825 }
1826 if (!empty($blockDAOs['delete'])) {
1827 foreach ($blockDAOs['delete'] as $blockDAO) {
1828 $blockDAO->delete();
1829 }
1830 }
1831 }
1832 }
1833
e4e1db4b 1834 /**
1835 * Dedupe a pair of contacts.
1836 *
b068bfde 1837 * @param array $dupes
e4e1db4b 1838 * @param string $mode
1839 * @param bool $checkPermissions
e4e1db4b 1840 * @param string $cacheKeyString
37cd9336 1841 *
b068bfde 1842 * @return bool|array
37cd9336 1843 * @throws \CRM_Core_Exception
1844 * @throws \CiviCRM_API3_Exception
ffa59d18 1845 * @throws \API_Exception
e4e1db4b 1846 */
b068bfde 1847 protected static function dedupePair($dupes, $mode = 'safe', $checkPermissions = TRUE, $cacheKeyString = NULL) {
1848 CRM_Utils_Hook::merge('flip', $dupes, $dupes['dstID'], $dupes['srcID']);
1849 $mainId = $dupes['dstID'];
1850 $otherId = $dupes['srcID'];
1851 $resultStats = [];
1852
1853 if (!$mainId || !$otherId) {
1854 // return error
1855 return FALSE;
1856 }
aefc291e 1857 $migrationInfo = [];
2c16165a 1858 $conflicts = [];
c9f05fb3 1859 // Try to lock the contacts before we load the data as we don't want it changing under us.
1860 // https://lab.civicrm.org/dev/core/issues/1355
1861 $locks = self::getLocksOnContacts([$mainId, $otherId]);
e4e1db4b 1862 if (!CRM_Dedupe_Merger::skipMerge($mainId, $otherId, $migrationInfo, $mode, $conflicts)) {
1863 CRM_Dedupe_Merger::moveAllBelongings($mainId, $otherId, $migrationInfo, $checkPermissions);
2c16165a 1864 $resultStats['merged'][] = [
e4e1db4b 1865 'main_id' => $mainId,
1866 'other_id' => $otherId,
2c16165a 1867 ];
e4e1db4b 1868 }
1869 else {
2c16165a 1870 $resultStats['skipped'][] = [
e4e1db4b 1871 'main_id' => $mainId,
1872 'other_id' => $otherId,
2c16165a 1873 ];
e4e1db4b 1874 }
1875
1876 // store any conflicts
1877 if (!empty($conflicts)) {
ffa59d18 1878 CRM_Core_BAO_PrevNextCache::markConflict($mainId, $otherId, $cacheKeyString, $conflicts, $mode);
e4e1db4b 1879 }
1880 else {
c131d228 1881 CRM_Core_BAO_PrevNextCache::deletePair($mainId, $otherId, $cacheKeyString);
e4e1db4b 1882 }
c9f05fb3 1883 self::releaseLocks($locks);
b068bfde 1884 return $resultStats;
e4e1db4b 1885 }
1886
54ed517e 1887 /**
1888 * Replace the pseudo QFKey with zero if it is present.
1889 *
1890 * @todo - on the slim chance this is still relevant it should be moved to the form layer.
1891 *
1892 * Details about this bug are somewhat obscured by the move from svn but perhaps JIRA
1893 * can still help.
1894 *
1895 * @param array $migrationInfo
1896 */
1897 protected static function swapOutFieldsAffectedByQFZeroBug(&$migrationInfo) {
1898 $qfZeroBug = 'e8cddb72-a257-11dc-b9cc-0016d3330ee9';
1899 foreach ($migrationInfo as $key => &$value) {
723c5a81 1900 if ($value === $qfZeroBug) {
54ed517e 1901 $value = '0';
1902 }
1903 }
1904 }
1905
830ee2be 1906 /**
1907 * Honestly - what DOES this do - hopefully some refactoring will reveal it's purpose.
1908 *
1600a9c0 1909 * Update this function formats fields in preparation for them to be submitted to the
1910 * 'ProfileContactCreate action. This is a lot of code to do this & for
1911 * - for some fields it fails - e.g Country - per testMergeCustomFields.
1912 *
1913 * Goal is to move all custom field handling into 'move' functions on the various BAO
1914 * with an underlying DAO function. For custom fields it has been started on the BAO.
1915 *
723c5a81 1916 * @param int $mainId
1917 * @param string $key
1918 * @param array $cFields
1919 * @param array $submitted
1920 * @param mixed $value
830ee2be 1921 *
1922 * @return array
9a248526 1923 * @throws \CRM_Core_Exception
830ee2be 1924 */
1600a9c0 1925 protected static function processCustomFields($mainId, $key, $cFields, $submitted, $value) {
723c5a81 1926 if (substr($key, 0, 7) === 'custom_') {
830ee2be 1927 $fid = (int) substr($key, 7);
1928 if (empty($cFields[$fid])) {
1600a9c0 1929 return [$cFields, $submitted];
830ee2be 1930 }
1931 $htmlType = $cFields[$fid]['attributes']['html_type'];
7f348139 1932 $isSerialized = CRM_Core_BAO_CustomField::isSerialized($cFields[$fid]['attributes']);
830ee2be 1933
7f348139
CW
1934 if ($htmlType === 'File') {
1935 // Handled in CustomField->move(). Tested in testMergeCustomFields.
1936 unset($submitted["custom_$fid"]);
1937 }
1938 elseif (!$isSerialized && ($htmlType === 'Select Country' || $htmlType === 'Select State/Province')) {
1939 // @todo Test in testMergeCustomFields disabled as this does not work, Handle in CustomField->move().
1940 $submitted[$key] = CRM_Core_BAO_CustomField::displayValue($value, $fid);
1941 }
1942 elseif ($htmlType === 'Select Date') {
1943 if ($cFields[$fid]['attributes']['is_view']) {
1944 $submitted[$key] = date('YmdHis', strtotime($submitted[$key]));
1945 }
1946 }
1947 elseif ($isSerialized) {
1948 // Merge values from both contacts for multivalue fields, CRM-4385
1949 // get the existing custom values from db.
1950 $customParams = ['entityID' => $mainId, $key => TRUE];
1951 $customfieldValues = CRM_Core_BAO_CustomValueTable::getValues($customParams);
1952 if (!empty($customfieldValues[$key])) {
1953 $existingValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, $customfieldValues[$key]);
1954 if (is_array($existingValue) && !empty($existingValue)) {
1955 $mergeValue = $submittedCustomFields = [];
723c5a81 1956 if ($value === 'null') {
7f348139
CW
1957 // CRM-19074 if someone has deliberately chosen to overwrite with 'null', respect it.
1958 $submitted[$key] = $value;
1959 }
1960 else {
1961 if ($value) {
1962 $submittedCustomFields = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
830ee2be 1963 }
830ee2be 1964
7f348139
CW
1965 // CRM-19653: overwrite or add the existing custom field value with dupicate contact's
1966 // custom field value stored at $submittedCustomValue.
1967 foreach ($submittedCustomFields as $k => $v) {
1968 if ($v != '' && !in_array($v, $mergeValue)) {
1969 $mergeValue[] = $v;
830ee2be 1970 }
7f348139 1971 }
830ee2be 1972
7f348139
CW
1973 //keep state and country as array format.
1974 //for checkbox and m-select format w/ VALUE_SEPARATOR
1975 if (in_array($htmlType, ['CheckBox', 'Select'])) {
1976 $submitted[$key] = CRM_Utils_Array::implodePadded($mergeValue);
1977 }
1978 else {
1979 $submitted[$key] = $mergeValue;
830ee2be 1980 }
1981 }
1982 }
7f348139
CW
1983 }
1984 elseif (in_array($htmlType, ['Select Country', 'Select State/Province'])) {
1985 //we require submitted values should be in array format
1986 if ($value) {
1987 $mergeValueArray = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
1988 //hack to remove null values from array.
1989 $mergeValue = [];
1990 foreach ($mergeValueArray as $k => $v) {
1991 if ($v != '') {
1992 $mergeValue[] = $v;
830ee2be 1993 }
830ee2be 1994 }
7f348139 1995 $submitted[$key] = $mergeValue;
830ee2be 1996 }
7f348139 1997 }
830ee2be 1998 }
1999 }
1600a9c0 2000 return [$cFields, $submitted];
830ee2be 2001 }
2002
f941d73a 2003 /**
2004 * Get metadata for the custom fields for the merge.
2005 *
2006 * @param string $contactType
2007 *
2008 * @return array
9a248526 2009 * @throws \CRM_Core_Exception
f941d73a 2010 */
2011 protected static function getCustomFieldMetadata($contactType) {
2012 $treeCache = [];
2013 if (!array_key_exists($contactType, $treeCache)) {
2014 $treeCache[$contactType] = CRM_Core_BAO_CustomGroup::getTree(
2015 $contactType,
2016 NULL,
2017 NULL,
2018 -1,
2019 [],
2020 NULL,
2021 TRUE,
2022 NULL,
2023 FALSE,
2024 FALSE
2025 );
2026 }
2027
2028 $cFields = [];
2029 foreach ($treeCache[$contactType] as $key => $group) {
2030 if (!isset($group['fields'])) {
2031 continue;
2032 }
2033 foreach ($group['fields'] as $fid => $field) {
2034 $cFields[$fid]['attributes'] = $field;
2035 }
2036 }
2037 return $cFields;
2038 }
2039
a2abc387 2040 /**
2041 * Get conflicts for proposed merge pair.
2042 *
2043 * @param array $migrationInfo
2044 * This is primarily to inform hooks. The can also modify it which feels
2045 * pretty fragile to do it here - but it is historical.
2046 * @param int $mainId
2047 * Main contact with whom merge has to happen.
2048 * @param int $otherId
2049 * Duplicate contact which would be deleted after merge operation.
2050 * @param string $mode
2051 * Helps decide how to behave when there are conflicts.
2052 * - A 'safe' value skips the merge if there are any un-resolved conflicts.
2053 * - Does a force merge otherwise (aggressive mode).
2054 *
2055 * @return array
aefc291e 2056 *
2057 * @throws \CRM_Core_Exception
2058 * @throws \CiviCRM_API3_Exception
a2abc387 2059 */
2060 public static function getConflicts(&$migrationInfo, $mainId, $otherId, $mode) {
2061 $conflicts = [];
aefc291e 2062 // Generate var $migrationInfo. The variable structure is exactly same as
2063 // $formValues submitted during a UI merge for a pair of contacts.
2064 $rowsElementsAndInfo = CRM_Dedupe_Merger::getRowsElementsAndInfo($mainId, $otherId, FALSE);
2065 // add additional details that we might need to resolve conflicts
2066 $migrationInfo = $rowsElementsAndInfo['migration_info'];
2067 $migrationInfo['main_details'] = &$rowsElementsAndInfo['main_details'];
2068 $migrationInfo['other_details'] = &$rowsElementsAndInfo['other_details'];
2069 $migrationInfo['rows'] = &$rowsElementsAndInfo['rows'];
2070 // go ahead with merge if there is no conflict
a2abc387 2071 $originalMigrationInfo = $migrationInfo;
2072 foreach ($migrationInfo as $key => $val) {
2073 if ($val === "null") {
2074 // Rule: Never overwrite with an empty value (in any mode)
2075 unset($migrationInfo[$key]);
2076 continue;
2077 }
2078 elseif ((in_array(substr($key, 5), CRM_Dedupe_Merger::getContactFields()) or
723c5a81 2079 substr($key, 0, 12) === 'move_custom_'
a2abc387 2080 ) and $val != NULL
2081 ) {
2082 // Rule: If both main-contact, and other-contact have a field with a
2083 // different value, then let $mode decide if to merge it or not
2084 if (
2085 (!empty($migrationInfo['rows'][$key]['main'])
2086 // For custom fields a 0 (e.g in an int field) could be a true conflict. This
2087 // is probably true for other fields too - e.g. 'do_not_email' but
2088 // leaving that investigation as a @todo - until tests can be written.
2089 // Note the handling of this has test coverage - although the data-typing
2090 // of '0' feels flakey we have insurance.
723c5a81 2091 || ($migrationInfo['rows'][$key]['main'] === '0' && substr($key, 0, 12) === 'move_custom_')
a2abc387 2092 )
2093 && $migrationInfo['rows'][$key]['main'] != $migrationInfo['rows'][$key]['other']
2094 ) {
2095
2096 // note it down & lets wait for response from the hook.
2097 // For no response $mode will decide if to skip this merge
2098 $conflicts[$key] = NULL;
2099 }
2100 }
723c5a81 2101 elseif (substr($key, 0, 14) === 'move_location_' and $val != NULL) {
a2abc387 2102 $locField = explode('_', $key);
2103 $fieldName = $locField[2];
2104 $fieldCount = $locField[3];
2105
2106 // Rule: Catch address conflicts (same address type on both contacts)
2107 if (
2108 isset($migrationInfo['main_details']['location_blocks'][$fieldName]) &&
2109 !empty($migrationInfo['main_details']['location_blocks'][$fieldName])
2110 ) {
2111
2112 // Load the address we're inspecting from the 'other' contact
2113 $addressRecord = $migrationInfo['other_details']['location_blocks'][$fieldName][$fieldCount];
9c1bc317 2114 $addressRecordLocTypeId = $addressRecord['location_type_id'] ?? NULL;
a2abc387 2115
2116 // If it exists on the 'main' contact already, skip it. Otherwise
2117 // if the location type exists already, log a conflict.
2118 foreach ($migrationInfo['main_details']['location_blocks'][$fieldName] as $mainAddressKey => $mainAddressRecord) {
2119 if (self::locationIsSame($addressRecord, $mainAddressRecord)) {
2120 unset($migrationInfo[$key]);
2121 break;
2122 }
2123 elseif ($addressRecordLocTypeId == $mainAddressRecord['location_type_id']) {
2124 $conflicts[$key] = NULL;
2125 break;
2126 }
2127 }
2128 }
2129
2130 // For other locations, don't merge/add if the values are the same
2131 elseif (CRM_Utils_Array::value('main', $migrationInfo['rows'][$key]) == $migrationInfo['rows'][$key]['other']) {
2132 unset($migrationInfo[$key]);
2133 }
2134 }
2135 }
2136
2137 // A hook to implement other algorithms for choosing which contact to bias to when
2138 // there's a conflict (to handle "gotchas"). fields_in_conflict could be modified here
2139 // merge happens with new values filled in here. For a particular field / row not to be merged
2140 // field should be unset from fields_in_conflict.
2141 $migrationData = [
2142 'old_migration_info' => $originalMigrationInfo,
2143 'mode' => $mode,
2144 'fields_in_conflict' => $conflicts,
2145 'merge_mode' => $mode,
2146 'migration_info' => $migrationInfo,
2147 ];
2148 CRM_Utils_Hook::merge('batch', $migrationData, $mainId, $otherId);
2149 $conflicts = $migrationData['fields_in_conflict'];
2150 // allow hook to override / manipulate migrationInfo as well
2151 $migrationInfo = $migrationData['migration_info'];
ffa59d18 2152 foreach ($conflicts as $key => $val) {
403400d9 2153 // Copy over the resolved values. If we are in aggressive mode we update to null
2154 // so as not to copy over. Why it's different to safe mode is a bit murky.
2155 // Working theory is it doesn't matter what we do in safe mode here if $val is NULL.
2156 // as the merge is not gonna happen if $val == NULL
2157 $migrationInfo[$key] = $val ?? ($mode === 'safe' ? $migrationInfo[$key] : NULL);
ffa59d18 2158 }
403400d9 2159 return self::formatConflictArray($conflicts, $migrationInfo['rows'], $migrationInfo['main_details']['location_blocks'], $migrationInfo['other_details']['location_blocks'], $mainId, $otherId, $mode);
ffa59d18 2160 }
2161
2162 /**
2163 * @param array $conflicts
2164 * @param array $migrationInfo
2165 * @param $toKeepContactLocationBlocks
2166 * @param $toRemoveContactLocationBlocks
2167 * @param $toKeepID
2168 * @param $toRemoveID
403400d9 2169 * @param string $mode
ffa59d18 2170 *
2171 * @return mixed
2172 * @throws \CRM_Core_Exception
2173 */
403400d9 2174 protected static function formatConflictArray($conflicts, $migrationInfo, $toKeepContactLocationBlocks, $toRemoveContactLocationBlocks, $toKeepID, $toRemoveID, $mode) {
ffa59d18 2175 $return = [];
403400d9 2176 $resolved = [];
2177 foreach ($conflicts as $key => $val) {
2178 if ($val !== NULL) {
2179 // copy over the resolved values
2180 $resolved[$key] = $val;
2181 unset($conflicts[$key]);
2182 }
2183 elseif ($mode === 'aggressive') {
2184 unset($conflicts[$key]);
2185 if (strpos($key, 'move_location_') !== 0) {
2186 // @todo - just handling plain contact fields for now because I think I need a bigger refactor
2187 // of the below to handle locations & will do as a follow up.
2188 $resolved['contact'][substr($key, 5)] = $migrationInfo[$key]['main'];
2189 }
2190 }
2191 }
ffa59d18 2192 foreach (array_keys($conflicts) as $index) {
2193 if (substr($index, 0, 14) === 'move_location_') {
2194 $parts = explode('_', $index);
2195 $entity = $parts[2];
2196 $blockIndex = $parts[3];
2197 $locationTypeID = $toKeepContactLocationBlocks[$entity][$blockIndex]['location_type_id'];
2198 $entityConflicts = [
2199 'location_type_id' => $locationTypeID,
2200 'title' => $migrationInfo[$index]['title'],
2201 ];
2202 foreach ($toKeepContactLocationBlocks[$entity][$blockIndex] as $fieldName => $fieldValue) {
2203 if (in_array($fieldName, self::ignoredFields())) {
2204 continue;
2205 }
9c1bc317 2206 $toRemoveValue = $toRemoveContactLocationBlocks[$entity][$blockIndex][$fieldName] ?? NULL;
ffa59d18 2207 if ($fieldValue !== $toRemoveValue) {
2208 $entityConflicts[$fieldName] = [
2209 $toKeepID => $fieldValue,
2210 $toRemoveID => $toRemoveValue,
2211 ];
2212 }
2213 }
2214 $return[$entity][] = $entityConflicts;
2215 }
2216 elseif (substr($index, 0, 5) === 'move_') {
2217 $contactFieldsToCompare[] = str_replace('move_', '', $index);
2218 $return['contact'][str_replace('move_', '', $index)] = [
2219 'title' => $migrationInfo[$index]['title'],
2220 $toKeepID => $migrationInfo[$index]['main'],
2221 $toRemoveID => $migrationInfo[$index]['other'],
2222 ];
2223 }
2224 else {
2225 // Can't think of why this would be the case but perhaps it's ensuring it isn't as we
2226 // refactor this.
2227 throw new CRM_Core_Exception(ts('Unknown parameter') . $index);
2228 }
2229 }
403400d9 2230 return ['conflicts' => $return, 'resolved' => $resolved];
a2abc387 2231 }
2232
4107e11e 2233 /**
e1e24a57 2234 * Get any duplicate merge pairs that have been previously cached.
2235 *
2236 * @param int $rule_group_id
2237 * @param int $group_id
2238 * @param int $batchLimit
2239 * @param bool $isSelected
2240 * @param bool $includeConflicts
2241 * @param array $criteria
2242 * @param int $checkPermissions
997a03fe 2243 * @param int $searchLimit
4107e11e 2244 *
2245 * @return array
2246 */
997a03fe 2247 protected static function getCachedDuplicateMatches($rule_group_id, $group_id, $batchLimit, $isSelected, $includeConflicts, $criteria, $checkPermissions, $searchLimit = 0) {
4107e11e 2248 return CRM_Core_BAO_PrevNextCache::retrieve(
997a03fe 2249 self::getMergeCacheKeyString($rule_group_id, $group_id, $criteria, $checkPermissions, $searchLimit),
4107e11e 2250 self::getJoinOnDedupeTable(),
2251 self::getWhereString($isSelected),
2252 0, $batchLimit,
2253 [], '',
2254 $includeConflicts
2255 );
2256 }
2257
ffa59d18 2258 /**
2259 * @return array
2260 */
2261 protected static function ignoredFields(): array {
2262 $keysToIgnore = [
2263 'id',
2264 'is_primary',
2265 'is_billing',
2266 'manual_geo_code',
2267 'contact_id',
2268 'reset_date',
2269 'hold_date',
2270 ];
2271 return $keysToIgnore;
2272 }
2273
6704a926 2274 /**
2275 * Get the field value & label for the given field.
2276 *
2277 * @param $field
2278 * @param $contact
2279 *
2280 * @return array
2281 * @throws \Exception
2282 */
2283 private static function getFieldValueAndLabel($field, $contact): array {
2284 $fields = self::getMergeFieldsMetadata();
9c1bc317 2285 $value = $label = $contact[$field] ?? NULL;
6704a926 2286 $fieldSpec = $fields[$field];
2287 if (!empty($fieldSpec['serialize']) && is_array($value)) {
2288 // In practice this only applies to preferred_communication_method as the sub types are skipped above
2289 // and no others are serialized.
2290 $labels = [];
2291 foreach ($value as $individualValue) {
2292 $labels[] = CRM_Core_PseudoConstant::getLabel('CRM_Contact_BAO_Contact', $field, $individualValue);
2293 }
2294 $label = implode(', ', $labels);
2295 // We serialize this due to historic handling but it's likely that if we just left it as an
2296 // array all would be well & we would have less code.
2297 $value = CRM_Core_DAO::serializeField($value, $fieldSpec['serialize']);
2298 }
2299 elseif (!empty($fieldSpec['type']) && $fieldSpec['type'] == CRM_Utils_Type::T_DATE) {
2300 if ($value) {
2301 $value = str_replace('-', '', $value);
2302 $label = CRM_Utils_Date::customFormat($label);
2303 }
2304 else {
2305 $value = "null";
2306 }
2307 }
2308 elseif (!empty($fields[$field]['type']) && $fields[$field]['type'] == CRM_Utils_Type::T_BOOLEAN) {
2309 if ($label === '0') {
2310 $label = ts('[ ]');
2311 }
2312 if ($label === '1') {
2313 $label = ts('[x]');
2314 }
2315 }
2316 elseif (!empty($fieldSpec['pseudoconstant'])) {
2317 $label = CRM_Core_PseudoConstant::getLabel('CRM_Contact_BAO_Contact', $field, $value);
2318 }
2319 elseif ($field == 'current_employer_id' && !empty($value)) {
2320 $label = "$value (" . CRM_Contact_BAO_Contact::displayName($value) . ")";
2321 }
2fd72602 2322 return ['label' => $label, 'value' => $value];
6704a926 2323 }
2324
1a90ac89 2325 /**
2326 * Build up the location block for the contact in dedupe-screen display format.
2327 *
2328 * @param integer $cid
2329 * @param array $blockInfo
2330 * @param string $blockName
2331 *
2332 * @return array
2333 *
2334 * @throws \CiviCRM_API3_Exception
2335 */
2336 private static function buildLocationBlockForContact($cid, $blockInfo, $blockName): array {
2337 $searchParams = [
2338 'contact_id' => $cid,
2339 // CRM-17556 Order by field-specific criteria
2340 'options' => [
2341 'sort' => $blockInfo['sortString'],
2342 ],
2343 ];
2344 $locationBlock = [];
2345 $values = civicrm_api3($blockName, 'get', $searchParams);
2346 if ($values['count']) {
2347 $cnt = 0;
2348 foreach ($values['values'] as $value) {
2349 $locationBlock[$cnt] = $value;
2350 // Fix address display
2351 if ($blockName == 'address') {
2352 // For performance avoid geocoding while merging https://issues.civicrm.org/jira/browse/CRM-21786
2353 // we can expect existing geocode values to be retained.
2354 $value['skip_geocode'] = TRUE;
2355 CRM_Core_BAO_Address::fixAddress($value);
2356 unset($value['skip_geocode']);
2357 $locationBlock[$cnt]['display'] = CRM_Utils_Address::format($value);
2358 }
2359 // Fix email display
2360 elseif ($blockName == 'email') {
2361 $locationBlock[$cnt]['display'] = CRM_Utils_Mail::format($value);
2362 }
2363
2364 $cnt++;
2365 }
2366 }
2367 return $locationBlock;
2368 }
2369
c9f05fb3 2370 /**
2371 * Get a lock on the given contact.
2372 *
2373 * The lock is like a gentleman's agreement between php & mysql. It is reserved at the
2374 * mysql level so it works across php processes but it doesn't actually lock the database.
2375 *
2376 * Instead php can check the lock to see if it has been acquired before taking an action.
2377 *
2378 * In this case we really don't want to attempt to dedupe contacts if another process is
2379 * trying to act on the specific contact as it could result in messy deadlocks & possibly data corruption.
2380 * In most databases this would be a rare event but if multiple dedupe processes are running
2381 * at once (for example) or there is also an import process in play there is potential for them to crash.
2382 * By throwing a specific error the calling process can catch it and determine it is worth trying again later without a lot of
2383 * noise.
2384 *
2385 * As of writing no other processes DO grab contact locks but it would be reasonable to consider
2386 * grabbing them doing contact edits in general as well as imports etc.
2387 *
2388 * @param array $contacts
2389 *
2390 * @return array
2391 *
2392 * @throws \CRM_Core_Exception
2393 * @throws \CRM_Core_Exception_ResourceConflictException
2394 */
2395 protected static function getLocksOnContacts($contacts):array {
2396 $locks = [];
2397 if (!CRM_Utils_SQL::supportsMultipleLocks()) {
2398 return $locks;
2399 }
2400 foreach ($contacts as $contactID) {
2401 $lock = Civi::lockManager()->acquire("data.core.contact.{$contactID}");
2402 if ($lock->isAcquired()) {
2403 $locks[] = $lock;
2404 }
2405 else {
2406 self::releaseLocks($locks);
2407 throw new CRM_Core_Exception_ResourceConflictException(ts('Contact is in Use'), 'contact_lock');
2408 }
2409 }
2410 return $locks;
2411 }
2412
2413 /**
2414 * Release contact locks so another process can alter them if it wants.
2415 *
2416 * @param array $locks
2417 */
2418 protected static function releaseLocks(array $locks) {
2419 foreach ($locks as $lock) {
2420 /* @var Civi\Core\Lock\LockInterface $lock */
2421 $lock->release();
2422 }
2423 }
2424
18226102 2425 /**
2426 * @param $mainId
2427 * @param $otherId
2428 * @param $blockInfo
2429 * @param $blockName
2430 * @param array $locations
2431 * @param array $rows
2432 * @param array $elements
2433 * @param array $migrationInfo
2434 *
2435 * @return array
2436 * @throws \CiviCRM_API3_Exception
2437 */
2438 protected static function addLocationFieldInfo($mainId, $otherId, $blockInfo, $blockName, array $locations, array $rows, array $elements, array $migrationInfo): array {
2439 // Collect existing fields from both 'main' and 'other' contacts first
2440 // This allows us to match up location/types when building the table rows
2441 $locations['main'][$blockName] = self::buildLocationBlockForContact($mainId, $blockInfo, $blockName);
2442 $locations['other'][$blockName] = self::buildLocationBlockForContact($otherId, $blockInfo, $blockName);
2443
2444 // Now, build the table rows appropriately, based off the information on
2445 // the 'other' contact
2446 if (!empty($locations['other']) && !empty($locations['other'][$blockName])) {
2447 foreach ($locations['other'][$blockName] as $count => $value) {
2448
2449 $displayValue = $value[$blockInfo['displayField']];
2450
2451 // Add this value to the table rows
2452 $rows["move_location_{$blockName}_{$count}"]['other'] = $displayValue;
2453
2454 // CRM-17556 Only display 'main' contact value if it's the same location + type
2455 // Look it up from main values...
2456
2457 $lookupLocation = FALSE;
2458 if ($blockInfo['hasLocation']) {
2459 $lookupLocation = $value['location_type_id'];
2460 }
2461
2462 $lookupType = FALSE;
2463 if ($blockInfo['hasType']) {
9c1bc317 2464 $lookupType = $value[$blockInfo['hasType']] ?? NULL;
18226102 2465 }
2466
2467 // Hold ID of main contact's matching block
2468 $mainContactBlockId = 0;
2469
2470 if (!empty($locations['main'][$blockName])) {
2471 foreach ($locations['main'][$blockName] as $mainValueCheck) {
2472 // No location/type, or matching location and type
2473 if (
2474 (empty($lookupLocation) || $lookupLocation == $mainValueCheck['location_type_id'])
2475 && (empty($lookupType) || $lookupType == $mainValueCheck[$blockInfo['hasType']])
2476 ) {
2477 // Set this value as the default against the 'other' contact value
2478 $rows["move_location_{$blockName}_{$count}"]['main'] = $mainValueCheck[$blockInfo['displayField']];
2479 $rows["move_location_{$blockName}_{$count}"]['main_is_primary'] = $mainValueCheck['is_primary'];
2480 $rows["move_location_{$blockName}_{$count}"]['location_entity'] = $blockName;
2481 $mainContactBlockId = $mainValueCheck['id'];
2482 break;
2483 }
2484 }
2485 }
2486
2487 // Add checkbox to migrate data from 'other' to 'main'
2488 $elements[] = ['advcheckbox', "move_location_{$blockName}_{$count}"];
2489
2490 // Add checkbox to set the 'other' location as primary
2491 $elements[] = [
2492 'advcheckbox',
2493 "location_blocks[$blockName][$count][set_other_primary]",
2494 NULL,
2495 ts('Set as primary'),
2496 ];
2497
2498 // Flag up this field to skipMerge function (@todo: do we need to?)
2499 $migrationInfo["move_location_{$blockName}_{$count}"] = 1;
2500
2501 // Add a hidden field to store the ID of the target main contact block
2502 $elements[] = [
2503 'hidden',
2504 "location_blocks[$blockName][$count][mainContactBlockId]",
2505 $mainContactBlockId,
2506 ];
2507
2508 // Setup variables
2509 $thisTypeId = FALSE;
2510 $thisLocId = FALSE;
2511
2512 // Provide a select drop-down for the location's location type
2513 // eg: Home, Work...
2514
2515 if ($blockInfo['hasLocation']) {
2516
2517 // Load the location options for this entity
2518 $locationOptions = civicrm_api3($blockName, 'getoptions', ['field' => 'location_type_id']);
2519
2520 $thisLocId = $value['location_type_id'];
2521
2522 // Put this field's location type at the top of the list
2523 $tmpIdList = $locationOptions['values'];
2524 $defaultLocId = [$thisLocId => $tmpIdList[$thisLocId]];
2525 unset($tmpIdList[$thisLocId]);
2526
2527 // Add the element
2528 $elements[] = [
2529 'select',
2530 "location_blocks[$blockName][$count][locTypeId]",
2531 NULL,
2532 $defaultLocId + $tmpIdList,
2533 ];
2534
2535 // Add the relevant information to the $migrationInfo
2536 // Keep location-type-id same as that of other-contact
2537 // @todo Check this logic out
2538 $migrationInfo['location_blocks'][$blockName][$count]['locTypeId'] = $thisLocId;
2539 if ($blockName != 'address') {
2540 $elements[] = [
2541 'advcheckbox',
2542 "location_blocks[{$blockName}][$count][operation]",
2543 NULL,
2544 ts('Add new'),
2545 ];
2546 // always use add operation
2547 $migrationInfo['location_blocks'][$blockName][$count]['operation'] = 1;
2548 }
2549
2550 }
2551
2552 // Provide a select drop-down for the location's type/provider
2553 // eg websites: Facebook...
2554
2555 if ($blockInfo['hasType']) {
2556
2557 // Load the type options for this entity
2558 $typeOptions = civicrm_api3($blockName, 'getoptions', ['field' => $blockInfo['hasType']]);
2559
9c1bc317 2560 $thisTypeId = $value[$blockInfo['hasType']] ?? NULL;
18226102 2561
2562 // Put this field's location type at the top of the list
2563 $tmpIdList = $typeOptions['values'];
2564 $defaultTypeId = [$thisTypeId => CRM_Utils_Array::value($thisTypeId, $tmpIdList)];
2565 unset($tmpIdList[$thisTypeId]);
2566
2567 // Add the element
2568 $elements[] = [
2569 'select',
2570 "location_blocks[$blockName][$count][typeTypeId]",
2571 NULL,
2572 $defaultTypeId + $tmpIdList,
2573 ];
2574
2575 // Add the information to the migrationInfo
2576 $migrationInfo['location_blocks'][$blockName][$count]['typeTypeId'] = $thisTypeId;
2577
2578 }
2579
2580 // Set the label for this row
2581 $rowTitle = $blockInfo['label'] . ' ' . ($count + 1);
2582 if (!empty($thisLocId)) {
2583 $rowTitle .= ' (' . $locationOptions['values'][$thisLocId] . ')';
2584 }
2585 if (!empty($thisTypeId)) {
2586 $rowTitle .= ' (' . $typeOptions['values'][$thisTypeId] . ')';
2587 }
2588 $rows["move_location_{$blockName}_$count"]['title'] = $rowTitle;
2589
2590 } // End loop through 'other' locations of this type
2591
2592 }
2593 return [$locations, $rows, $elements, $migrationInfo];
2594 }
2595
6a488035 2596}