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