-
Notifications
You must be signed in to change notification settings - Fork 6
/
MergeHandler.php
1211 lines (1111 loc) · 38.1 KB
/
MergeHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
use CRM_Deduper_ExtensionUtil as E;
class CRM_Deduper_BAO_MergeHandler {
/**
* Various dedupe data as passed in from core in a mystical ugly format.
*
* @var array
*/
protected array $dedupeData = [];
/**
* Location blocks as calculated by the merge code & passed in alterLocationMergeData.
*
* @var array
*/
protected array $locationBlocks = [];
/**
* Resolutions to resolvable email conflicts.
*
* @var array
*/
protected array $locationConflictResolutions = [];
/**
* Contact ID to retain.
*
* @var int
*/
protected $mainID;
/**
* Contact ID to be merged and deleted.
*
* @var int
*/
protected $otherID;
/**
* Merge context.
*
* This comes from the core deduper and is generally form or batch.
*
* @var string
*/
protected $context;
/**
* Is the dedupe in safe mode.
*
* @var bool
*/
protected $safeMode;
/**
* @return bool
*/
public function isSafeMode(): bool {
return $this->safeMode;
}
/**
* @param bool $safeMode
*/
public function setSafeMode(bool $safeMode) {
$this->safeMode = $safeMode;
}
/**
* Details of email conflicts including:
* - fields
* - to_keep
* - to_remove
*
* @var array
*/
protected array $emailConflictDetails = [];
/**
* Details of address conflicts including:
* - fields
* - to_keep
* - to_remove
*
* @var array
*/
protected array $addressConflictDetails = [];
/**
* Details of phone conflicts including:
* - fields
* - to_keep
* - to_remove
*
* @var array
*/
protected array $phoneConflictDetails = [];
/**
* Location blocks that should be deleted on merge.
*
* @var array
*/
protected array $locationBlocksToDelete = [];
/**
* Temporary stash of settings.
*
* @var array
*/
protected array $settings = [];
/**
* Getter for dedupe Data.
*
* @return array
*/
public function getDedupeData(): array {
return $this->dedupeData;
}
/**
* Setter for dedupe Data.
*
* @param array $dedupeData
*/
public function setDedupeData(array $dedupeData) {
$this->dedupeData = $dedupeData;
}
/**
* Helper for getting settings.
*
* This doesn't do much but it saves falling into questions as to whether a property
* would be faster than the cached settings.get call.
*
* @param string $setting
*
* @return mixed
*/
public function getSetting($setting) {
if (!isset($this->settings[$setting])) {
$this->settings[$setting] = \Civi::settings()->get($setting);
}
return $this->settings[$setting];
}
/**
* Getter for main ID.
*
* @return mixed
*/
public function getMainID() {
return $this->mainID;
}
/**
* Setter for main ID.
*
* @param mixed $mainID
*/
public function setMainID($mainID) {
$this->mainID = $mainID;
}
/**
* Getter for other ID.
*
* @return mixed
*/
public function getOtherID() {
return $this->otherID;
}
/**
* Setter for other ID.
*
* @param mixed $otherID
*/
public function setOtherID($otherID) {
$this->otherID = $otherID;
}
/**
* Getter for context.
*
* @return string
*/
public function getContext() {
return $this->context;
}
/**
* Setter for context.
*
* @param mixed $context
*/
public function setContext($context) {
$this->context = $context;
}
/**
* Get the fields that make up the name of an individual.
*
* @return array
*/
public function getIndividualNameFields():array {
return ['first_name', 'last_name', 'middle_name', 'nick_name'];
}
/**
* Get the fields that make up the name of an Organization.
*
* @return array
*/
public function getOrganizationNameFields():array {
return ['organization_name'];
}
/**
* Get the fields that make up the name of a Household.
*
* @return array
*/
public function getHouseholdNameFields():array {
return ['household_name'];
}
/**
* Get the fields that make up the name of a Contact.
*
* @return array
*/
public function getNameFields():array {
return array_merge($this->getIndividualNameFields(), $this->getOrganizationNameFields(), $this->getHouseholdNameFields());
}
/**
* Get the fields that make up the name of an individual.
*
* @param bool $isForContactToBeKept
* Is the value for the contact to be retained.
*
* @return array
*/
public function getIndividualNameFieldValues(bool $isForContactToBeKept):array {
$return = [];
foreach ($this->getIndividualNameFields() as $fieldName) {
$return[$fieldName] = $this->getValueForField($fieldName, $isForContactToBeKept);
}
return $return;
}
/**
* Get the address blocks for the contact.
*
* @param bool $isForContactToBeKept
*
* @return array
*/
public function getAddresses(bool $isForContactToBeKept):array {
if ($isForContactToBeKept) {
return $this->dedupeData['migration_info']['main_details']['location_blocks']['address'];
}
return $this->dedupeData['migration_info']['other_details']['location_blocks']['address'];
}
/**
* Get the location blocks for the contact for the given entity.
*
* @param string[address|phone|email] $entity
* @param bool $isForContactToBeKept
*
* @return array
*/
public function getLocationEntities($entity, $isForContactToBeKept):array {
if ($isForContactToBeKept) {
return $this->dedupeData['migration_info']['main_details']['location_blocks'][$entity];
}
return $this->dedupeData['migration_info']['other_details']['location_blocks'][$entity];
}
/**
* Get the indexed address block for the contact.
*
* @param bool $isForContactToBeKept
*
* @param int $blockNumber
*
* @return array
*/
public function getAddressBlock(bool $isForContactToBeKept, int $blockNumber):array {
if ($isForContactToBeKept) {
$otherContactAddress = $this->getAddresses(FALSE)[$blockNumber];
foreach ($this->getAddresses(TRUE) as $address) {
if (((int) $address['location_type_id']) === ((int) $otherContactAddress['location_type_id'])) {
return $address;
}
}
}
else {
return $this->getAddresses(FALSE)[$blockNumber];
}
}
/**
* Get the fields that make up the name of a contact.
*
* @param bool $isForContactToBeKept
* Is the value for the contact to be retained.
*
* @return array
*/
public function getNameFieldValues(bool $isForContactToBeKept):array {
$return = [];
foreach ($this->getNameFields() as $fieldName) {
$return[$fieldName] = $this->getValueForField($fieldName, $isForContactToBeKept);
}
return $return;
}
/**
* Get the value for the given field.
*
* @param string $fieldName
* @param bool $isForContactToBeKept
* Is the value for the contact to be retained.
*
* @return mixed
*/
public function getValueForField(string $fieldName, bool $isForContactToBeKept) {
if (strpos($fieldName, 'custom_') !== 0) {
$contactDetail = $isForContactToBeKept ? $this->dedupeData['migration_info']['main_details'] : $this->dedupeData['migration_info']['other_details'];
$value = $contactDetail[$fieldName] ?? NULL;
}
// You are now entering hell. The information you want is buried... somewhere.
elseif (!$isForContactToBeKept) {
// This is what would be 'just used' if we unset the conflict & leave 'move_custom_x' in the array
// so if should be safe-ish.
$value = $this->dedupeData['migration_info']['move_' . $fieldName];
}
else {
// Honestly let's try passing back this formatted value .... because it IS deformatted at the other end.
// We relying on unit tests & magic here.
$value = $this->dedupeData['migration_info']['rows']['move_' . $fieldName]['main'];
}
if (is_string($value)) {
return trim($value);
}
return $value;
}
/**
* Is there a conflict in a field used to name an individual.
*
* @return bool
*/
public function hasIndividualNameFieldConflict():bool {
foreach ($this->getIndividualNameFields() as $nameField) {
if ($this->isFieldInConflict($nameField)) {
return TRUE;
}
}
return FALSE;
}
/**
* Is there a conflict in a field used to name an Organization.
*
* @return bool
*/
public function hasOrganizationNameFieldConflict():bool {
return $this->isFieldInConflict('organization_name');
}
/**
* Is there a conflict in a field used to name a household.
*
* @return bool
*/
public function hasHouseholdNameFieldConflict():bool {
return $this->isFieldInConflict('household_name');
}
/**
* Is there a conflict in a name field.
*
* @return bool
*/
public function hasNameFieldConflict(): bool {
return $this->hasOrganizationNameFieldConflict() || $this->hasIndividualNameFieldConflict() || $this->hasHouseholdNameFieldConflict();
}
/**
* @return array
*/
public function getLocationBlocks(): array {
$blocks = $this->locationBlocks;
foreach ($blocks as $entity => $entityBlocks) {
if (isset($this->locationBlocksToDelete[$entity])) {
foreach ($this->locationBlocksToDelete[$entity] as $id) {
if (isset($blocks[$entity]['update'][$id])) {
$blocks[$entity]['delete'][$id] = $blocks[$entity]['update'][$id];
unset($blocks[$entity]['update'][$id]);
}
}
}
}
return $blocks;
}
/**
* @param array $locationBlocks
*/
public function setLocationBlocks(array $locationBlocks) {
$this->locationBlocks = $locationBlocks;
}
/**
* @return array
*/
public function getLocationBlocksToDelete(): array {
return $this->locationBlocksToDelete;
}
/**
* @param array $locationBlocksToDelete
*/
public function setLocationBlocksToDelete(array $locationBlocksToDelete) {
$this->locationBlocksToDelete = $locationBlocksToDelete;
}
/**
* CRM_Deduper_BAO_MergeHandler constructor.
*
* @param array $dedupeData
* Various dedupe data as passed in from core in a mystical ugly format.
* @param int $mainID
* Contact ID to retain
* @param $otherID
* Contact ID to be merged and deleted.
* @param string $context
* Merge context passed in from core -usually form or batch.
* @param bool $isSafeMode
*/
public function __construct(array $dedupeData, int $mainID, int $otherID, string $context, bool $isSafeMode) {
$this->setDedupeData($dedupeData);
$this->setMainID($mainID);
$this->setOtherID($otherID);
$this->setContext($context);
$this->setSafeMode($isSafeMode);
}
/**
* Resolve merge.
*
* @throws \CRM_Core_Exception
*/
public function resolve() {
// @todo we'll build out how we manage resolvers later.
// Ideally we will try to make it align as much as we can
// with https://github.com/systopia/de.systopia.xdedupe/tree/master/CRM/Xdedupe/Resolver
// There is a fundamental difference in that his resolvers run BEFORE a merge not in the hook
// so they do updates prior to a merge attempt. Ours are running as a merge hook and alter
// already-determined conflicts.
$resolver = new CRM_Deduper_BAO_Resolver_SkippedFieldsResolver($this);
$resolver->resolveConflicts();
$resolver = new CRM_Deduper_BAO_Resolver_GreetingResolver($this);
$resolver->resolveConflicts();
$resolver = new CRM_Deduper_BAO_Resolver_BooleanYesResolver($this);
$resolver->resolveConflicts();
$resolver = new CRM_Deduper_BAO_Resolver_EquivalentAddressResolver($this);
$resolver->resolveConflicts();
$resolver = new CRM_Deduper_BAO_Resolver_UninformativeCharactersResolver($this);
$resolver->resolveConflicts();
$resolver = new CRM_Deduper_BAO_Resolver_CasingResolver($this);
$resolver->resolveConflicts();
$resolver = new CRM_Deduper_BAO_Resolver_DiacriticResolver($this);
$resolver->resolveConflicts();
$resolver = new CRM_Deduper_BAO_Resolver_SillyNameResolver($this);
$resolver->resolveConflicts();
$resolver = new CRM_Deduper_BAO_Resolver_EquivalentNameResolver($this);
$resolver->resolveConflicts();
$resolver = new CRM_Deduper_BAO_Resolver_MisplacedNameResolver($this);
$resolver->resolveConflicts();
$resolver = new CRM_Deduper_BAO_Resolver_InitialResolver($this);
$resolver->resolveConflicts();
$resolver = new CRM_Deduper_BAO_Resolver_SubsetNameResolver($this);
$resolver->resolveConflicts();
$resolver = new CRM_Deduper_BAO_Resolver_PreferredContactLocationResolver($this);
$resolver->resolveConflicts();
// Let's do this one last - that way if someone wants to try to resolve names first they
// can & then fall back on 'just use the value from the preferred contact.
// @todo - should we make the resolvers sortable / re-order-able?
$resolver = new CRM_Deduper_BAO_Resolver_PreferredContactFieldResolver($this);
$resolver->resolveConflicts();
}
/**
* Resolve locations.
*
* The hook to resolve locations takes place later on in the process.
*/
public function resolveLocations() {
// tbc
}
/**
* Get fields in conflict.
*
* @return array of keys of conflicted fields.
*/
public function getFieldsInConflict():array {
$fields = [];
foreach (array_keys($this->dedupeData['fields_in_conflict']) as $key) {
$fields[] = str_replace('move_', '', $key);
}
return $fields;
}
/**
* Is there a conflict on the specified field.
*
* @param string $fieldName
*
* @return bool
*/
public function isFieldInConflict($fieldName):bool {
$conflictFields = $this->getFieldsInConflict();
return in_array($fieldName, $conflictFields, TRUE);
}
/**
* Resolve conflict on field using the specified value.
*
* @param string $fieldName
* @param mixed $value
*/
public function setResolvedValue($fieldName, $value) {
$moveField = 'move_' . $fieldName;
if ($this->isSafeMode()) {
unset($this->dedupeData['fields_in_conflict'][$moveField]);
}
else {
$this->dedupeData['fields_in_conflict'][$moveField] = $value;
}
$this->setValue($fieldName, $value);
}
/**
* Resolve conflict on field using the specified value.
*
* @param string $fieldName
* @param string $entity
* @param int $block
* @param string $value
*/
public function setResolvedLocationValue(string $fieldName, string $entity, int $block, string $value) {
$key = $entity . 'ConflictDetails';
unset($this->$key[$block]['fields'][$fieldName]);
$this->locationConflictResolutions[$entity][$block][$fieldName] = $value;
if (empty($this->$key[$block]['fields']) || array_keys($this->$key[$block]['fields']) === ['display']) {
$this->resolveConflictsOnLocationBlock($entity, $block);
}
}
/**
* Update an resolved address value, resolving the entire address if it is no longer in conflict.
*
* @param string $fieldName
* @param string $location
* @param int $block
* @param string $value
*/
public function setResolvedAddressValue(string $fieldName, string $location, int $block, string $value) {
$this->locationConflictResolutions[$location][$block][$fieldName] = $value;
$mainBlock = &$this->dedupeData['migration_info']['main_details']['location_blocks']['address'][$block];
$otherBlock = &$this->dedupeData['migration_info']['other_details']['location_blocks']['address'][$block];
unset($this->addressConflictDetails[$block]['fields'][$fieldName]);
if (!empty($this->addressConflictDetails[$block]['fields']['display'])) {
$mainDisplay = CRM_Utils_Address::format(array_merge($mainBlock, [$fieldName => $value]));
$otherDisplay = CRM_Utils_Address::format(array_merge($otherBlock, [$fieldName => $value]));
if ($mainDisplay === $otherDisplay) {
unset($this->addressConflictDetails[$block]['fields']['display']);
}
}
if (empty($this->addressConflictDetails[$block]['fields'])) {
$this->resolveConflictsOnLocationBlock($location, $block);
}
}
/**
* Assign location to a new available location and block so it is retained.
*
* @param string $locationEntity
* @param int $block
* @param bool $isContactToKeep
* Does the location being rehomed belong to the contact to keep.
* @param bool|null $isPrimary
* If not null the primary will be forced to this.
*/
public function relocateLocation(string $locationEntity, int $block, $isContactToKeep = FALSE, $isPrimary = NULL): void {
$locationTypeID = $this->getNextAvailableLocationType($locationEntity);
if ($isContactToKeep) {
// The CiviCRM form has no way to do this - we are kind of tricking it into thinking it is dealing with another
// address on the contact to be changed.
$nextBlock = $this->getNextAvailableLocationBlock($locationEntity);
// We add this block from the 'to keep contact' to the 'to remove contact' so it
// will 'copied across' and updated with our new location_type_id & is_primary.
$blockToKeep = $this->getLocationBlock($locationEntity, $block, TRUE);
$blockToKeep['location_type_id'] = $locationTypeID;
$this->dedupeData['migration_info']['other_details']['location_blocks'][$locationEntity][$nextBlock] = $blockToKeep;
$this->dedupeData['migration_info']['move_location_' . $locationEntity . '_' . $nextBlock] = TRUE;
$this->dedupeData['migration_info']['location_blocks'][$locationEntity][$nextBlock] = [
'locTypeId' => $locationTypeID,
'operation' => 1,
'set_other_primary' => FALSE,
'is_relocated' => TRUE,
];
// We later check this to see whether we should overwrite it in setLocationAddressFromOtherContactToOverwriteMainContact.
$this->dedupeData['migration_info']['location_blocks'][$locationEntity][$block]['is_relocated'] = TRUE;
}
else {
// This version is simple - we are just mimicing the forms instructions.
$this->dedupeData['migration_info']['location_blocks'][$locationEntity][$block] = [
'operation' => 1,
'set_other_primary' => $isPrimary,
'is_relocated' => TRUE,
'locTypeId' => $locationTypeID,
];
}
}
/**
* Set primary location to that of the contact to be deleted
* @param string $locationEntity
* @param int $block
*/
public function setPrimaryLocationToDeleteContact($locationEntity, $block) {
$blockOnContactToKeep = $this->getLocationBlock($locationEntity, $block, TRUE);
$blockToBePrimary = $this->getLocationBlock($locationEntity, $block, FALSE);
if (!empty($blockOnContactToKeep) && $blockOnContactToKeep['is_primary'] && $this->isBlockEquivalent($locationEntity, $blockOnContactToKeep, $blockToBePrimary)
&& $blockOnContactToKeep['location_type_id'] === $blockToBePrimary['location_type_id']
) {
// No action required - it already is the primary.
return;
}
$this->dedupeData['migration_info']['location_blocks'][$locationEntity][$block]['set_other_primary'] = 1;
$this->dedupeData['migration_info']['location_blocks'][$locationEntity][$block]['operation'] = 2;
$this->dedupeData['migration_info']['move_location_' . $locationEntity . '_' . $block] = TRUE;
foreach ($this->getLocationEntities($locationEntity, TRUE) as $blockNumber => $toKeepBlock) {
if ($this->isBlockEquivalent($locationEntity, $toKeepBlock, $blockToBePrimary)) {
$this->dedupeData['migration_info']['location_blocks'][$locationEntity][$block]['mainContactBlockId'] = $toKeepBlock['id'];
}
}
}
/**
* Is the second block functionally the same as the second.
*
* For example if they both have the same phone number they are functionally
* the same information.
*
* @param string[address|phone|email] $locationEntity
* @param array $entity1
* @param array $entity2
*
* @return bool
*/
public function isBlockEquivalent($locationEntity, $entity1, $entity2) {
return $locationEntity === 'phone' ? ($entity1['phone_numeric'] === $entity2['phone_numeric']) : ($entity1['display'] === $entity2['display']);
}
/**
* Does this block hold unique information ot otherwise replicated in other blocks.
*
* @param string[address|phone|email] $locationEntity
* @param array $entityToConsiderRehoming
* @param int $blockNumber
*
* @return bool
*/
public function isBlockUnique(string $locationEntity, array $entityToConsiderRehoming, int $blockNumber): bool {
foreach ($this->getAllLocationBlocks($locationEntity) as $existingEntity) {
if ($existingEntity['id'] !== $entityToConsiderRehoming['id'] && $this->isBlockEquivalent($locationEntity, $existingEntity, $entityToConsiderRehoming)) {
return FALSE;
}
}
return TRUE;
}
/**
* Get all blocks for the given location, from both contacts.
*
* @param string $locationEntity
*
* @return array
*/
public function getAllLocationBlocks($locationEntity): array {
$blocks = [];
foreach ($this->dedupeData['migration_info']['main_details']['location_blocks'][$locationEntity] as $block => $detail) {
$detail['block'] = $block;
$blocks[] = $detail;
}
foreach ($this->dedupeData['migration_info']['other_details']['location_blocks'][$locationEntity] as $block => $detail) {
$detail['block'] = $block;
$blocks[] = $detail;
}
return $blocks;
}
public function getNextAvailableLocationBlock($locationEntity) {
$blocksInUse = [];
foreach (array_merge(
array_keys($this->dedupeData['migration_info']['main_details']['location_blocks'][$locationEntity]),
array_keys($this->dedupeData['migration_info']['other_details']['location_blocks'][$locationEntity])
) as $block) {
$blocksInUse[$block] = $block;
}
return array_pop($blocksInUse) + 1;
}
/**
* Get the specified block.
*
* @param string $location
* @param int $block
* @param bool $isForContactToBeKept
*
* @return array
*/
public function getLocationBlock(string $location, int $block, bool $isForContactToBeKept):array {
if ($isForContactToBeKept) {
// Search for the relevant location type block on the main contact.
$locationTypeID = (int) $this->dedupeData['migration_info']['other_details']['location_blocks'][$location][$block]['location_type_id'];
foreach ($this->dedupeData['migration_info']['main_details']['location_blocks'][$location] as $mainContactLocation) {
if ($locationTypeID === ((int) $mainContactLocation['location_type_id'])) {
return $mainContactLocation;
}
}
}
else {
return $this->dedupeData['migration_info']['other_details']['location_blocks'][$location][$block] ?? [];
}
return [];
}
/**
* Get merge instructions.
*
* If we had a form the values on the form would dictate this but we
* mimic those in this class to achieve the desired result.
*
* @param string $locationEntity
* @param int $block
*
* @return mixed
*/
public function getMergeInstructionForBlock(string $locationEntity, int $block) {
return $this->dedupeData['migration_info']['location_blocks'][$locationEntity][$block];
}
/**
* Has the block been marked for relocation.
*
* @param string $locationEntity
* @param int $block
*
* @return false|mixed
*/
public function isRelocated(string $locationEntity, int $block) {
return $this->getMergeInstructionForBlock($locationEntity, $block)['is_relocated'] ?? FALSE;
}
/**
* Get the specified value from the specified block.
*
* @param string $location
* @param int $block
* @param bool $isForContactToBeKept
* @param string $field
*
* @return mixed
*/
public function getLocationBlockValue(string $location, int $block, bool $isForContactToBeKept, string $field) {
return $this->getLocationBlock($location, $block, $isForContactToBeKept)[$field] ?? NULL;
}
/**
* Get conflicts for the email address of the given block.
*
* @param int $emailBlockNumber
*
* @return array
* List of fields with conflicts against the email in the provided block number for the contact to delete..
*/
public function getEmailConflicts(int $emailBlockNumber):array {
return $this->getEmailConflictDetails($emailBlockNumber)['fields'];
}
/**
* @param int $emailBlockNumber
*
* @return array
*/
public function getEmailConflictDetails(int $emailBlockNumber): array {
if (!isset($this->emailConflictDetails[$emailBlockNumber])) {
$otherContactEmail = $this->getBlockForEntity('email', $emailBlockNumber, FALSE);
$mainContactEmail = $this->getBlockForEntity('email', $emailBlockNumber, TRUE);
$this->emailConflictDetails[$emailBlockNumber] = [];
// As defined in CRM_Dedupe_Merger::ignoredFields + display which is for the form layer.
$keysToIgnore = [
'id',
'is_primary',
'is_billing',
'manual_geo_code',
'contact_id',
'reset_date',
'hold_date',
'display',
];
foreach ($otherContactEmail as $field => $value) {
if (
isset($mainContactEmail[$field])
&& $mainContactEmail[$field] !== $value
&& !in_array($field, $keysToIgnore, TRUE)) {
$this->emailConflictDetails[$emailBlockNumber]['fields'][$field] = $value;
$this->emailConflictDetails[$emailBlockNumber]['to_keep'] = $mainContactEmail;
$this->emailConflictDetails[$emailBlockNumber]['to_remove'] = $otherContactEmail;
}
}
}
return $this->emailConflictDetails[$emailBlockNumber];
}
/**
* @param int $blockNumber
*
* @return array
*/
public function getPhoneConflictDetails(int $blockNumber): array {
if (!isset($this->phoneConflictDetails[$blockNumber])) {
$mainContactEntity = $this->getBlockForEntity('phone', $blockNumber, TRUE);
$otherContactEntity = $this->getBlockForEntity('phone', $blockNumber, FALSE);
$this->phoneConflictDetails[$blockNumber] = [];
// As defined in CRM_Dedupe_Merger::ignoredFields + display which is for the form layer.
$keysToIgnore = [
'id',
'is_primary',
'is_billing',
'contact_id',
'display',
];
foreach ($otherContactEntity as $field => $value) {
if (
isset($mainContactEntity[$field])
&& $mainContactEntity[$field] !== $value
&& !in_array($field, $keysToIgnore, TRUE)) {
$this->phoneConflictDetails[$blockNumber]['fields'][$field] = $value;
$this->phoneConflictDetails[$blockNumber]['to_keep'] = $mainContactEntity;
$this->phoneConflictDetails[$blockNumber]['to_remove'] = $otherContactEntity;
}
}
}
return $this->phoneConflictDetails[$blockNumber];
}
/**
* Get conflicts on all address blocks.
*
* @return array
*/
public function getAllAddressConflicts(): array {
$conflicts = [];
foreach ($this->getFieldsInConflict() as $conflictedField) {
if (strpos($conflictedField, 'location_address') === 0) {
$blockNumber = intval(str_replace('location_address_', '', $conflictedField));
$conflicts[$blockNumber] = $this->getAddressConflicts($blockNumber);
}
}
return $conflicts;
}
/**
* Get conflicts on all address blocks.
*
* @param string $entity
*
* @return array
*/
public function getAllConflictsForEntity(string $entity): array {
$conflicts = [];
foreach ($this->getFieldsInConflict() as $conflictedField) {
if (strpos($conflictedField, 'location_' . $entity) === 0) {
$blockNumber = (int) (str_replace('location_' . $entity . '_', '', $conflictedField));
if ($entity === 'email') {
$blockConflicts = $this->getEmailConflictDetails($blockNumber);
}
if ($entity === 'address') {
$blockConflicts = $this->getAddressConflictDetails($blockNumber);
}
if ($entity === 'phone') {
$blockConflicts = $this->getPhoneConflictDetails($blockNumber);
}
if (!empty($blockConflicts)) {
$conflicts[$blockNumber] = $blockConflicts;
}
}
}
return $conflicts;
}
/**
* Get conflicts for the address of the given block.
*
* @param int $blockNumber
*
* @return array
* Conflicts in addresses.
*/
public function getAddressConflicts(int $blockNumber):array {
return $this->getAddressConflictDetails($blockNumber)['fields'] ?? [];
}
/**
* @param int $blockNumber
*
* @return array
*/
public function getAddressConflictDetails(int $blockNumber): array {
if (!isset($this->addressConflictDetails[$blockNumber])) {
$mainContactAddress = $this->getBlockForEntity('address', $blockNumber, TRUE);
$otherContactAddress = $this->getBlockForEntity('address', $blockNumber, FALSE);
$this->addressConflictDetails[$blockNumber] = [];
// As defined in CRM_Dedupe_Merger::ignoredFields + display which is for the form layer.
$keysToIgnore = [
'id',
'is_primary',
'is_billing',
'manual_geo_code',
'contact_id',
];
foreach ($otherContactAddress as $field => $value) {
if (
isset($mainContactAddress[$field])
&& $mainContactAddress[$field] !== $value
&& !in_array($field, $keysToIgnore, TRUE)) {
$this->addressConflictDetails[$blockNumber]['fields'][$field] = $value;
$this->addressConflictDetails[$blockNumber]['to_keep'] = $mainContactAddress;
$this->addressConflictDetails[$blockNumber]['to_remove'] = $otherContactAddress;
}
}
}
return $this->addressConflictDetails[$blockNumber];
}
/**
* Set the location address from the other contact as the one to keep.
*
* This mimics the 'magic values' that are set on the form when the user chooses to overwrite the main
* contact's location block with the data from the other contact.
*
* @param string $location
* @param string $block
*/
public function setLocationAddressFromOtherContactToOverwriteMainContact($location, $block) {
$this->dedupeData['migration_info']['location_blocks'][$location][$block]['operation'] = 2;
$this->dedupeData['migration_info']['location_blocks'][$location][$block]['mainContactBlockId'] = $this->getLocationBlock($location, $block, TRUE)['id'];
}
/**
* Is the second block functionally the same as the second.
*
* For example if they both have the same phone number they are functionally
* the same information.
*
* @param string[address|phone|email] $locationEntity
* @param int $block
*/
public function setDoNotMoveBlock(string $locationEntity, int $block) {
unset($this->dedupeData['migration_info']['move_location_' . $locationEntity . '_' . $block]);